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