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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioAffinityParser"
17 #endif
18
19 #include "audio_affinity_parser.h"
20 #include "audio_errors.h"
21 #include "media_monitor_manager.h"
22
23 namespace OHOS {
24 namespace AudioStandard {
25
26 static std::map<std::string, DeviceType> deviceTypeMap_ = {
27 {"DEVICE_TYPE_EARPIECE", DEVICE_TYPE_EARPIECE},
28 {"DEVICE_TYPE_SPEAKER", DEVICE_TYPE_SPEAKER},
29 {"DEVICE_TYPE_WIRED_HEADSET", DEVICE_TYPE_WIRED_HEADSET},
30 {"DEVICE_TYPE_WIRED_HEADPHONES", DEVICE_TYPE_WIRED_HEADPHONES},
31 {"DEVICE_TYPE_BLUETOOTH_SCO", DEVICE_TYPE_BLUETOOTH_SCO},
32 {"DEVICE_TYPE_BLUETOOTH_A2DP", DEVICE_TYPE_BLUETOOTH_A2DP},
33 {"DEVICE_TYPE_BLUETOOTH_A2DP_IN", DEVICE_TYPE_BLUETOOTH_A2DP_IN},
34 {"DEVICE_TYPE_USB_HEADSET", DEVICE_TYPE_USB_HEADSET},
35 {"DEVICE_TYPE_USB_ARM_HEADSET", DEVICE_TYPE_USB_ARM_HEADSET},
36 {"DEVICE_TYPE_DP", DEVICE_TYPE_DP},
37 {"DEVICE_TYPE_REMOTE_CAST", DEVICE_TYPE_REMOTE_CAST},
38 {"DEVICE_TYPE_MIC", DEVICE_TYPE_MIC},
39 {"DEVICE_TYPE_HDMI", DEVICE_TYPE_HDMI},
40 };
41
LoadConfiguration()42 bool AudioAffinityParser::LoadConfiguration()
43 {
44 bool ret = curNode_->Config(AFFINITY_CONFIG_FILE, nullptr, 0);
45 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "AudioAffinityParser xmlReadFile failed");
46
47 curNode_->MoveToChildren();
48 CHECK_AND_RETURN_RET_LOG(curNode_->IsNodeValid(), false, "AudioAffinityParser Missing node");
49 if (!ParseInternal(curNode_->GetCopyNode())) {
50 return false;
51 }
52 CHECK_AND_RETURN_RET_LOG(audioAffinityManager_ != nullptr, false, "audioAffinityManager_ is null");
53 audioAffinityManager_->OnXmlParsingCompleted(affinityDeviceInfoArray_);
54 return true;
55 }
56
Destroy()57 void AudioAffinityParser::Destroy()
58 {
59 curNode_->FreeDoc();
60 }
61
ParseInternal(std::shared_ptr<AudioXmlNode> curNode)62 bool AudioAffinityParser::ParseInternal(std::shared_ptr<AudioXmlNode> curNode)
63 {
64 while (curNode->IsNodeValid()) {
65 if (curNode->CompareName("OutputDevices")) {
66 ParserAffinityGroups(curNode->GetCopyNode(), OUTPUT_DEVICES_FLAG);
67 } else if (curNode->CompareName("InputDevices")) {
68 ParserAffinityGroups(curNode->GetCopyNode(), INPUT_DEVICES_FLAG);
69 }
70 curNode->MoveToNext();
71 }
72 return true;
73 }
74
ParserAffinityGroups(std::shared_ptr<AudioXmlNode> curNode,const DeviceFlag & deviceFlag)75 void AudioAffinityParser::ParserAffinityGroups(std::shared_ptr<AudioXmlNode> curNode, const DeviceFlag& deviceFlag)
76 {
77 curNode->MoveToChildren();
78 CHECK_AND_RETURN_LOG(curNode->IsNodeValid(), "audioAffinityParser Missing node groups");
79
80 while (curNode->IsNodeValid()) {
81 if (curNode->CompareName("AffinityGroups")) {
82 ParserAffinityGroupAttribute(curNode->GetCopyNode(), deviceFlag);
83 }
84 curNode->MoveToNext();
85 }
86 }
87
ParserAffinityGroupAttribute(std::shared_ptr<AudioXmlNode> curNode,const DeviceFlag & deviceFlag)88 void AudioAffinityParser::ParserAffinityGroupAttribute(std::shared_ptr<AudioXmlNode> curNode,
89 const DeviceFlag& deviceFlag)
90 {
91 curNode->MoveToChildren();
92 CHECK_AND_RETURN_LOG(curNode->IsNodeValid(), "audioAffinityParser Missing node attr");
93
94 AffinityDeviceInfo deviceInfo = {};
95 deviceInfo.deviceFlag = deviceFlag;
96 while (curNode->IsNodeValid()) {
97 if (curNode->CompareName("AffinityGroup")) {
98 std::string attrPrimary;
99 if (curNode->GetProp("isPrimary", attrPrimary) == SUCCESS) {
100 deviceInfo.isPrimary = (attrPrimary == "True" ? true : false);
101 }
102 std::string attrGroupName;
103 if (curNode->GetProp("name", attrGroupName) == SUCCESS) {
104 deviceInfo.groupName = attrGroupName;
105 }
106 ParserAffinityGroupDeviceInfos(curNode->GetCopyNode(), deviceInfo);
107 }
108 curNode->MoveToNext();
109 }
110 }
111
ParserAffinityGroupDeviceInfos(std::shared_ptr<AudioXmlNode> curNode,AffinityDeviceInfo & deviceInfo)112 void AudioAffinityParser::ParserAffinityGroupDeviceInfos(std::shared_ptr<AudioXmlNode> curNode,
113 AffinityDeviceInfo& deviceInfo)
114 {
115 curNode->MoveToChildren();
116 CHECK_AND_RETURN_LOG(curNode->IsNodeValid(), "audioAffinityParser Missing node device");
117
118 while (curNode->IsNodeValid()) {
119 if (curNode->CompareName("Affinity")) {
120 curNode->GetProp("networkId", deviceInfo.networkID);
121
122 std::string deviceType;
123 curNode->GetProp("deviceType", deviceType);
124 std::map<std::string, DeviceType>::iterator item = deviceTypeMap_.find(deviceType);
125 deviceInfo.deviceType = (item != deviceTypeMap_.end() ? item->second : DEVICE_TYPE_INVALID);
126
127 std::string supportedConcurrency;
128 curNode->GetProp("supportedConcurrency", supportedConcurrency);
129 deviceInfo.SupportedConcurrency = (supportedConcurrency == "True") ? true : false;
130
131 affinityDeviceInfoArray_.push_back(deviceInfo);
132 }
133 curNode->MoveToNext();
134 }
135 }
136
137 } // namespace AudioStandard
138 } // namespace OHOS
139