• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "AudioUsageStrategyParser"
17 #endif
18 
19 #include "audio_usage_strategy_parser.h"
20 #include "media_monitor_manager.h"
21 #include "audio_errors.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
LoadConfiguration()25 bool AudioUsageStrategyParser::LoadConfiguration()
26 {
27     curNode_ = AudioXmlNode::Create();
28     int32_t ret = curNode_->Config(DEVICE_CONFIG_FILE, nullptr, 0);
29     if (ret != SUCCESS) {
30         std::shared_ptr<Media::MediaMonitor::EventBean> bean = std::make_shared<Media::MediaMonitor::EventBean>(
31             Media::MediaMonitor::AUDIO, Media::MediaMonitor::LOAD_CONFIG_ERROR,
32             Media::MediaMonitor::FAULT_EVENT);
33         bean->Add("CATEGORY", Media::MediaMonitor::AUDIO_USAGE_STRATEGY);
34         Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean);
35         return false;
36     }
37     if (!ParseInternal(curNode_->GetCopyNode())) {
38         return false;
39     }
40     return true;
41 }
42 
Destroy()43 void AudioUsageStrategyParser::Destroy()
44 {
45     curNode_->FreeDoc();
46 }
47 
ParseInternal(std::shared_ptr<AudioXmlNode> curNode)48 bool AudioUsageStrategyParser::ParseInternal(std::shared_ptr<AudioXmlNode> curNode)
49 {
50     for (; curNode->IsNodeValid(); curNode->MoveToNext()) {
51         if (curNode->CompareName("adapter")) {
52             std::string pValueStr;
53             curNode->GetProp("name", pValueStr);
54             if (pValueStr == "streamUsage") {
55                 ParserStreamUsageList(curNode->GetChildrenNode());
56             } else if (pValueStr == "sourceType") {
57                 ParserSourceTypeList(curNode->GetChildrenNode());
58             }
59         } else {
60             ParseInternal(curNode->GetChildrenNode());
61         }
62     }
63     return true;
64 }
65 
ParserStreamUsageList(std::shared_ptr<AudioXmlNode> curNode)66 void AudioUsageStrategyParser::ParserStreamUsageList(std::shared_ptr<AudioXmlNode> curNode)
67 {
68     while (curNode->IsNodeValid()) {
69         if (curNode->CompareName("strategy")) {
70             std::string strategyName;
71             std::string streamUsages;
72             curNode->GetProp("name", strategyName);
73             curNode->GetProp("streamUsage", streamUsages);
74             ParserStreamUsageInfo(strategyName, streamUsages);
75         }
76         curNode->MoveToNext();
77     }
78 }
79 
ParserSourceTypeList(std::shared_ptr<AudioXmlNode> curNode)80 void AudioUsageStrategyParser::ParserSourceTypeList(std::shared_ptr<AudioXmlNode> curNode)
81 {
82     while (curNode->IsNodeValid()) {
83         if (curNode->CompareName("strategy")) {
84             std::string strategyName;
85             std::string sourceTypes;
86             curNode->GetProp("name", strategyName);
87             curNode->GetProp("sourceType", sourceTypes);
88             ParserSourceTypeInfo(strategyName, sourceTypes);
89         }
90         curNode->MoveToNext();
91     }
92 }
93 
split(const std::string & line,const std::string & sep)94 std::vector<std::string> AudioUsageStrategyParser::split(const std::string &line, const std::string &sep)
95 {
96     std::vector<std::string> buf;
97     size_t temp = 0;
98     std::string::size_type pos = 0;
99     while (true) {
100         pos = line.find(sep, temp);
101         if (pos == std::string::npos) {
102             break;
103         }
104         buf.push_back(line.substr(temp, pos-temp));
105         temp = pos + sep.length();
106     }
107     buf.push_back(line.substr(temp, line.length()));
108     return buf;
109 }
110 
ParserStreamUsage(const std::vector<std::string> & buf,const std::string & routerName)111 void AudioUsageStrategyParser::ParserStreamUsage(const std::vector<std::string> &buf,
112     const std::string &routerName)
113 {
114     StreamUsage usage;
115     for (auto &name : buf) {
116         auto pos = streamUsageMap.find(name);
117         if (pos != streamUsageMap.end()) {
118             usage = pos->second;
119         }
120         renderConfigMap_[usage] = routerName;
121     }
122 }
123 
ParserStreamUsageInfo(const std::string & strategyName,const std::string & streamUsage)124 void AudioUsageStrategyParser::ParserStreamUsageInfo(const std::string &strategyName,
125     const std::string &streamUsage)
126 {
127     std::vector<std::string> buf = split(streamUsage, ",");
128     if (strategyName == "MEDIA_RENDER") {
129         ParserStreamUsage(buf, "MediaRenderRouters");
130     } else if (strategyName == "CALL_RENDER") {
131         ParserStreamUsage(buf, "CallRenderRouters");
132     } else if (strategyName == "RING_RENDER") {
133         ParserStreamUsage(buf, "RingRenderRouters");
134     } else if (strategyName == "TONE_RENDER") {
135         ParserStreamUsage(buf, "ToneRenderRouters");
136     }
137 }
138 
ParserSourceTypes(const std::vector<std::string> & buf,const std::string & sourceTypes)139 void AudioUsageStrategyParser::ParserSourceTypes(const std::vector<std::string> &buf,
140     const std::string &sourceTypes)
141 {
142     SourceType sourceType;
143     for (auto &name : buf) {
144         auto pos = sourceTypeMap.find(name);
145         if (pos != sourceTypeMap.end()) {
146             sourceType = pos->second;
147         }
148         capturerConfigMap_[sourceType] = sourceTypes;
149     }
150 }
151 
ParserSourceTypeInfo(const std::string & strategyName,const std::string & sourceTypes)152 void AudioUsageStrategyParser::ParserSourceTypeInfo(const std::string &strategyName, const std::string &sourceTypes)
153 {
154     std::vector<std::string> buf = split(sourceTypes, ",");
155     if (strategyName == "RECORD_CAPTURE") {
156         ParserSourceTypes(buf, "RecordCaptureRouters");
157     } else if (strategyName == "CALL_CAPTURE") {
158         ParserSourceTypes(buf, "CallCaptureRouters");
159     } else if (strategyName == "VOICE_MESSAGE_CAPTURE") {
160         ParserSourceTypes(buf, "VoiceMessages");
161     }
162 }
163 } // namespace AudioStandard
164 } // namespace OHOS
165