• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LOG_TAG
16 #define LOG_TAG "AudioDefinitionAdapterInfo"
17 #endif
18 
19 #include "audio_definition_adapter_info.h"
20 #include "audio_device_type.h"
21 #include "audio_effect.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
GetInstance()25 AudioPolicyConfigData& AudioPolicyConfigData::GetInstance()
26 {
27     static AudioPolicyConfigData instance;
28     return instance;
29 }
30 
SetDeviceInfoMap(std::list<std::shared_ptr<AdapterDeviceInfo>> & deviceInfos,std::unordered_map<std::string,std::shared_ptr<AdapterDeviceInfo>> & tmpDeviceInfoMap_)31 void AudioPolicyConfigData::SetDeviceInfoMap(std::list<std::shared_ptr<AdapterDeviceInfo>> &deviceInfos,
32     std::unordered_map<std::string, std::shared_ptr<AdapterDeviceInfo>> &tmpDeviceInfoMap_)
33 {
34     if (deviceInfos.size() <= 0) {
35         AUDIO_ERR_LOG("SetDeviceInfoMap failed, deviceInfos is empty");
36     }
37 
38     for (auto &deviceInfo : deviceInfos) {
39         std::pair<DeviceType, DeviceRole> deviceMapKey = std::make_pair(deviceInfo->type_, deviceInfo->role_);
40         auto deviceInfoIt = deviceInfoMap.find(deviceMapKey);
41         if (deviceInfoIt != deviceInfoMap.end()) {
42             deviceInfoIt->second.insert(deviceInfo);
43         } else {
44             std::set<std::shared_ptr<AdapterDeviceInfo>> deviceSet;
45             deviceSet.insert(deviceInfo);
46             deviceInfoMap.insert({deviceMapKey, deviceSet});
47         }
48         tmpDeviceInfoMap_.insert({deviceInfo->name_, deviceInfo});
49     }
50 }
51 
SetSupportDeviceAndPipeMap(std::shared_ptr<AdapterPipeInfo> & pipeInfo_,std::unordered_map<std::string,std::shared_ptr<AdapterDeviceInfo>> & tmpDeviceInfoMap_)52 void AudioPolicyConfigData::SetSupportDeviceAndPipeMap(std::shared_ptr<AdapterPipeInfo> &pipeInfo_,
53     std::unordered_map<std::string, std::shared_ptr<AdapterDeviceInfo>> &tmpDeviceInfoMap_)
54 {
55     if (pipeInfo_->streamPropInfos_.size() == 0) {
56         AUDIO_INFO_LOG("dynamic setting");
57         for (auto deviceName : pipeInfo_->supportDevices_) {
58             auto uniqueDeviceIt = tmpDeviceInfoMap_.find(deviceName);
59             CHECK_AND_CONTINUE_LOG(uniqueDeviceIt != tmpDeviceInfoMap_.end(),
60                 "pipe needed device:%{public}s not exists.", deviceName.c_str());
61             uniqueDeviceIt->second->supportPipeMap_.insert({pipeInfo_->supportFlags_, pipeInfo_});
62         }
63         return;
64     }
65     for (auto &streamPropInfo : pipeInfo_->streamPropInfos_) {
66         for (auto deviceName : streamPropInfo->supportDevices_) {
67             auto uniqueDeviceIt = tmpDeviceInfoMap_.find(deviceName);
68             if (uniqueDeviceIt == tmpDeviceInfoMap_.end()) {
69                 AUDIO_WARNING_LOG("streamProp needed device:%{public}s not exists.", deviceName.c_str());
70                 continue;
71             }
72             streamPropInfo->supportDeviceMap_.insert({uniqueDeviceIt->second->type_, uniqueDeviceIt->second});
73             uniqueDeviceIt->second->supportPipeMap_.insert({pipeInfo_->supportFlags_, pipeInfo_});
74         }
75     }
76 }
77 
Reorganize()78 void AudioPolicyConfigData::Reorganize()
79 {
80     std::unordered_map<std::string, std::shared_ptr<AdapterDeviceInfo>> tmpDeviceInfoMap;
81 
82     for (auto &pair : adapterInfoMap) {
83         SetDeviceInfoMap(pair.second->deviceInfos, tmpDeviceInfoMap);
84     }
85 
86     for (auto &pair : adapterInfoMap) {
87         if (pair.second->pipeInfos.size() <= 0) {
88             AUDIO_ERR_LOG("Set Pipes failed, pipeInfos is empty");
89         }
90         for (auto &pipeInfo_ : pair.second->pipeInfos) {
91             SetSupportDeviceAndPipeMap(pipeInfo_, tmpDeviceInfoMap);
92         }
93     }
94 
95     SelfCheck();
96 }
97 
SelfCheck()98 void AudioPolicyConfigData::SelfCheck()
99 {
100     CHECK_AND_RETURN_LOG(adapterInfoMap.size() != 0, "SelfCheck Failled! Config No Adapter!");
101     CHECK_AND_RETURN_LOG(deviceInfoMap.size() != 0, "SelfCheck Failled! Config No Device!");
102 
103     for (auto &pair : adapterInfoMap) {
104         pair.second->SelfCheck();
105     }
106 }
107 
SetVersion(const std::string & version)108 void AudioPolicyConfigData::SetVersion(const std::string &version)
109 {
110     if (!version.empty()) {
111         version_ = version;
112     } else {
113         AUDIO_ERR_LOG("Set version failed, data is empty");
114     }
115 }
116 
GetVersion()117 std::string AudioPolicyConfigData::GetVersion()
118 {
119     return version_;
120 }
121 
GetAdapterDeviceInfo(DeviceType type_,DeviceRole role_,const std::string & networkId_,uint32_t flags,int32_t a2dpOffloadFlag)122 std::shared_ptr<AdapterDeviceInfo> AudioPolicyConfigData::GetAdapterDeviceInfo(
123     DeviceType type_, DeviceRole role_, const std::string &networkId_, uint32_t flags, int32_t a2dpOffloadFlag)
124 {
125     // use primary to select device when in remote cast;
126     DeviceType tempType = (type_ == DEVICE_TYPE_REMOTE_CAST ? DEVICE_TYPE_SPEAKER : type_);
127     std::pair<DeviceType, DeviceRole> deviceMapKey = std::make_pair(tempType, role_);
128     auto deviceSetIt = deviceInfoMap.find(deviceMapKey);
129     if (deviceSetIt == deviceInfoMap.end()) {
130         AUDIO_ERR_LOG("Device Not Configured!");
131         return nullptr;
132     }
133 
134     if (deviceSetIt->second.empty()) {
135         AUDIO_ERR_LOG("Device Set Is Empty!");
136         return nullptr;
137     }
138 
139     if (deviceSetIt->second.size() == 1) {
140         return *(deviceSetIt->second.begin());
141     }
142 
143     std::string targetAdapterName = "";
144     if (networkId_ != LOCAL_NETWORK_ID) {
145         targetAdapterName = "remote";
146     } else {
147         if (type_ == DEVICE_TYPE_BLUETOOTH_A2DP && a2dpOffloadFlag != A2DP_OFFLOAD) {
148             targetAdapterName = "a2dp";
149         } else {
150             targetAdapterName = "primary";
151         }
152     }
153 
154     for (auto &deviceInfo : deviceSetIt->second) {
155         std::shared_ptr<PolicyAdapterInfo> adapterInfoPtr = deviceInfo->adapterInfo_.lock();
156         if (adapterInfoPtr == nullptr) {
157             AUDIO_ERR_LOG("AdapterInfo is nullptr!");
158             continue;
159         }
160         if (adapterInfoPtr->adapterName == targetAdapterName) {
161             return deviceInfo;
162         }
163     }
164     AUDIO_ERR_LOG("Can not match any Device!");
165     return nullptr;
166 }
167 
UpdateDynamicStreamProps(const std::string adapterName,const std::string & pipeName,const std::list<std::shared_ptr<PipeStreamPropInfo>> & streamProps)168 void AudioPolicyConfigData::UpdateDynamicStreamProps(const std::string adapterName, const std::string &pipeName,
169     const std::list<std::shared_ptr<PipeStreamPropInfo>> &streamProps)
170 {
171     CHECK_AND_RETURN_LOG(!streamProps.empty(), "streamProps is empty");
172     AudioAdapterType adapterType = PolicyAdapterInfo::GetAdapterType(adapterName);
173     CHECK_AND_RETURN_LOG(adapterInfoMap.count(adapterType) != 0, "adapter not exist");
174     std::shared_ptr<PolicyAdapterInfo> adapterInfo = adapterInfoMap[adapterType];
175     CHECK_AND_RETURN_LOG(adapterInfo != nullptr, "adapterInfo is nullptr");
176     std::shared_ptr<AdapterPipeInfo> pipeInfo = adapterInfo->GetPipeInfoByName(pipeName);
177     CHECK_AND_RETURN_LOG(pipeInfo != nullptr, "pipeInfo is nullptr");
178 
179     std::unordered_map<std::string, std::shared_ptr<AdapterDeviceInfo>> tmpDeviceInfoMap;
180     for (auto &deviceInfo : adapterInfo->deviceInfos) {
181         tmpDeviceInfoMap.insert({deviceInfo->name_, deviceInfo});
182     }
183     for (auto &streamProp : streamProps) {
184         CHECK_AND_RETURN_LOG(streamProp != nullptr, "streamProp is nullptr");
185         streamProp->pipeInfo_ = pipeInfo;
186         for (auto deviceName : streamProp->supportDevices_) {
187             auto uniqueDeviceIt = tmpDeviceInfoMap.find(deviceName);
188             CHECK_AND_CONTINUE_LOG(uniqueDeviceIt != tmpDeviceInfoMap.end(),
189                 "streamProp needed device %{public}s not exist", deviceName.c_str());
190             streamProp->supportDeviceMap_.insert({uniqueDeviceIt->second->type_, uniqueDeviceIt->second});
191         }
192     }
193     pipeInfo->UpdateDynamicStreamProps(streamProps);
194 }
195 
ClearDynamicStreamProps(const std::string adapterName,const std::string & pipeName)196 void AudioPolicyConfigData::ClearDynamicStreamProps(const std::string adapterName, const std::string &pipeName)
197 {
198     AudioAdapterType adapterType = PolicyAdapterInfo::GetAdapterType(adapterName);
199     CHECK_AND_RETURN_LOG(adapterInfoMap.count(adapterType) != 0, "adapter not exist");
200     std::shared_ptr<PolicyAdapterInfo> adapterInfo = adapterInfoMap[adapterType];
201     CHECK_AND_RETURN_LOG(adapterInfo != nullptr, "adapterInfo is nullptr");
202     std::shared_ptr<AdapterPipeInfo> pipeInfo = adapterInfo->GetPipeInfoByName(pipeName);
203     CHECK_AND_RETURN_LOG(pipeInfo != nullptr, "pipeInfo is nullptr");
204     pipeInfo->ClearDynamicStreamProps();
205 }
206 
GetConfigStreamPropsSize(const std::string adapterName,const std::string & pipeName) const207 uint32_t AudioPolicyConfigData::GetConfigStreamPropsSize(const std::string adapterName,
208     const std::string &pipeName) const
209 {
210     AudioAdapterType adapterType = PolicyAdapterInfo::GetAdapterType(adapterName);
211     auto it = adapterInfoMap.find(adapterType);
212     CHECK_AND_RETURN_RET_LOG(it != adapterInfoMap.end(), 0, "adapter not exist");
213     std::shared_ptr<PolicyAdapterInfo> adapterInfo = it->second;
214     CHECK_AND_RETURN_RET_LOG(adapterInfo != nullptr, 0, "adapterInfo is nullptr");
215     std::shared_ptr<AdapterPipeInfo> pipeInfo = adapterInfo->GetPipeInfoByName(pipeName);
216     CHECK_AND_RETURN_RET_LOG(pipeInfo != nullptr, 0, "pipeInfo is nullptr");
217     return pipeInfo->streamPropInfos_.size();
218 }
219 
GetDynamicStreamPropsSize(const std::string adapterName,const std::string & pipeName) const220 uint32_t AudioPolicyConfigData::GetDynamicStreamPropsSize(const std::string adapterName,
221     const std::string &pipeName) const
222 {
223     AudioAdapterType adapterType = PolicyAdapterInfo::GetAdapterType(adapterName);
224     auto it = adapterInfoMap.find(adapterType);
225     CHECK_AND_RETURN_RET_LOG(it != adapterInfoMap.end(), 0, "adapter not exist");
226     std::shared_ptr<PolicyAdapterInfo> adapterInfo = it->second;
227     CHECK_AND_RETURN_RET_LOG(adapterInfo != nullptr, 0, "adapterInfo is nullptr");
228     std::shared_ptr<AdapterPipeInfo> pipeInfo = adapterInfo->GetPipeInfoByName(pipeName);
229     CHECK_AND_RETURN_RET_LOG(pipeInfo != nullptr, 0, "pipeInfo is nullptr");
230     return pipeInfo->dynamicStreamPropInfos_.size();
231 }
232 
PolicyAdapterInfo()233 PolicyAdapterInfo::PolicyAdapterInfo()
234 {
235     AUDIO_INFO_LOG("in");
236 }
~PolicyAdapterInfo()237 PolicyAdapterInfo::~PolicyAdapterInfo()
238 {
239     AUDIO_INFO_LOG("in");
240 }
241 
GetTypeEnum()242 AudioAdapterType PolicyAdapterInfo::GetTypeEnum()
243 {
244     return GetAdapterType(adapterName);
245 }
246 
GetAdapterType(const std::string & adapterName)247 AudioAdapterType PolicyAdapterInfo::GetAdapterType(const std::string &adapterName)
248 {
249     if (adapterName == ADAPTER_TYPE_PRIMARY) {
250         return AudioAdapterType::TYPE_PRIMARY;
251     } else if (adapterName == ADAPTER_TYPE_A2DP) {
252         return AudioAdapterType::TYPE_A2DP;
253     } else if (adapterName == ADAPTER_TYPE_HEARING_AID) {
254         return AudioAdapterType::TYPE_HEARING_AID;
255     } else if (adapterName == ADAPTER_TYPE_REMOTE) {
256         return AudioAdapterType::TYPE_REMOTE_AUDIO;
257     } else if (adapterName == ADAPTER_TYPE_FILE) {
258         return AudioAdapterType::TYPE_FILE_IO;
259     } else if (adapterName == ADAPTER_TYPE_USB) {
260         return AudioAdapterType::TYPE_USB;
261     } else if (adapterName == ADAPTER_TYPE_DP) {
262         return AudioAdapterType::TYPE_DP;
263     } else if (adapterName == ADAPTER_TYPE_ACCESSORY) {
264         return AudioAdapterType::TYPE_ACCESSORY;
265     } else if (adapterName == ADAPTER_TYPE_SLE) {
266         return AudioAdapterType::TYPE_SLE;
267     } else {
268         return AudioAdapterType::TYPE_INVALID;
269     }
270 }
271 
GetPipeInfoByName(const std::string & pipeName)272 std::shared_ptr<AdapterPipeInfo> PolicyAdapterInfo::GetPipeInfoByName(const std::string &pipeName)
273 {
274     for (auto &pipeInfo : pipeInfos) {
275         if (pipeInfo == nullptr) {
276             AUDIO_ERR_LOG("pipeInfo is null!");
277             continue;
278         }
279         if (pipeInfo->name_ == pipeName) {
280             return pipeInfo;
281         }
282     }
283     AUDIO_ERR_LOG("Can not match pipe:%{public}s!", pipeName.c_str());
284     return nullptr;
285 }
286 
GetDeviceInfoByType(DeviceType deviceType,DeviceRole role)287 std::shared_ptr<AdapterDeviceInfo> PolicyAdapterInfo::GetDeviceInfoByType(DeviceType deviceType, DeviceRole role)
288 {
289     for (auto &deviceInfo : deviceInfos) {
290         if (deviceInfo->type_ == deviceType && deviceInfo->role_ == role) {
291             return deviceInfo;
292         }
293     }
294     return nullptr;
295 }
296 
SelfCheck()297 void PolicyAdapterInfo::SelfCheck()
298 {
299     CHECK_AND_RETURN_LOG(pipeInfos.size() != 0, "SelfCheck Failled! Adapter:%{public}s No Pipe!",
300         adapterName.c_str());
301     CHECK_AND_RETURN_LOG(deviceInfos.size() != 0, "SelfCheck Failled! Adapter:%{public}s No Device!",
302         adapterName.c_str());
303 
304     for (auto &pipeInfo : pipeInfos) {
305         pipeInfo->SelfCheck();
306     }
307     for (auto &deviceInfo : deviceInfos) {
308         deviceInfo->SelfCheck();
309     }
310 }
311 
SelfCheck()312 void AdapterDeviceInfo::SelfCheck()
313 {
314     std::shared_ptr<PolicyAdapterInfo> adapterInfoPtr = adapterInfo_.lock();
315     CHECK_AND_RETURN_LOG(adapterInfoPtr != nullptr, "SelfCheck Failled! Device:%{public}s adapterInfo is null!",
316         name_.c_str());
317     CHECK_AND_RETURN_LOG(supportPipeMap_.size() != 0, "SelfCheck Failled! Device:%{public}s Not Support Any Pipe!",
318         name_.c_str());
319 
320     for (auto &pipeName : supportPipes_) {
321         bool flag = false;
322         for (auto &pair : supportPipeMap_) {
323             if (pair.second->name_ == pipeName) {
324                 flag = true;
325             }
326         }
327         if (!flag) {
328             AUDIO_ERR_LOG("SelfCheck Failled! Device:%{public}s Not Support Pipe:%{public}s!",
329                 name_.c_str(), pipeName.c_str());
330         }
331     }
332 }
333 
SelfCheck()334 void AdapterPipeInfo::SelfCheck()
335 {
336     std::shared_ptr<PolicyAdapterInfo> adapterInfoPtr = adapterInfo_.lock();
337     CHECK_AND_RETURN_LOG(adapterInfoPtr != nullptr, "SelfCheck Failled! Pipe:%{public}s adapterInfo is null!",
338         name_.c_str());
339     CHECK_AND_RETURN_LOG(streamPropInfos_.size() != 0, "SelfCheck Failled! Pipe:%{public}s No streamProp!",
340         name_.c_str());
341     CHECK_AND_RETURN_LOG(attributeInfos_.size() != 0, "SelfCheck Failled! Pipe:%{public}s No Attribute!",
342         name_.c_str());
343 
344     for (auto &streamPropInfo : streamPropInfos_) {
345         streamPropInfo->SelfCheck();
346     }
347 }
348 
UpdateDynamicStreamProps(const std::list<std::shared_ptr<PipeStreamPropInfo>> & streamProps)349 void AdapterPipeInfo::UpdateDynamicStreamProps(const std::list<std::shared_ptr<PipeStreamPropInfo>> &streamProps)
350 {
351     CHECK_AND_RETURN_LOG(streamProps.size() != 0, "streamProps is empty");
352     std::lock_guard<std::mutex> lock(dynamicMtx_);
353     dynamicStreamPropInfos_ = streamProps;
354 }
355 
ClearDynamicStreamProps()356 void AdapterPipeInfo::ClearDynamicStreamProps()
357 {
358     std::lock_guard<std::mutex> lock(dynamicMtx_);
359     dynamicStreamPropInfos_.clear();
360 }
361 
SelfCheck()362 void PipeStreamPropInfo::SelfCheck()
363 {
364     std::shared_ptr<AdapterPipeInfo> pipeInfoPtr = pipeInfo_.lock();
365     CHECK_AND_RETURN_LOG(pipeInfoPtr != nullptr, "SelfCheck Failled! pipeinfo is null!");
366     CHECK_AND_RETURN_LOG(supportDeviceMap_.size() != 0, "SelfCheck Failled! Pipe:%{public}s Not support Any Device!",
367         pipeInfoPtr->name_.c_str());
368 
369     for (auto &deviceName : supportDevices_) {
370         bool flag = false;
371         for (auto &pair : supportDeviceMap_) {
372             if (pair.second->name_ == deviceName) {
373                 flag = true;
374             }
375         }
376         if (!flag) {
377             AUDIO_ERR_LOG("SelfCheck Failled! Pipe:%{public}s Not Support Device:%{public}s!",
378                 pipeInfoPtr->name_.c_str(), deviceName.c_str());
379         }
380     }
381 }
382 
383 }
384 }
385