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 "AudioStrategyRouterParser"
17 #endif
18
19 #include "audio_strategy_router_parser.h"
20 #include "user_select_router.h"
21 #include "app_select_router.h"
22 #include "privacy_priority_router.h"
23 #include "public_priority_router.h"
24 #include "stream_filter_router.h"
25 #include "cockpit_phone_router.h"
26 #include "pair_device_router.h"
27 #include "default_router.h"
28
29 #include "media_monitor_manager.h"
30
31 namespace OHOS {
32 namespace AudioStandard {
LoadConfiguration()33 bool AudioStrategyRouterParser::LoadConfiguration()
34 {
35 curNode_ = AudioXmlNode::Create();
36 int32_t ret = curNode_->Config(DEVICE_CONFIG_FILE, nullptr, 0);
37 if (ret != SUCCESS) {
38 AUDIO_ERR_LOG("Not found audio_strategy_router.xml!");
39 std::shared_ptr<Media::MediaMonitor::EventBean> bean = std::make_shared<Media::MediaMonitor::EventBean>(
40 Media::MediaMonitor::AUDIO, Media::MediaMonitor::LOAD_CONFIG_ERROR, Media::MediaMonitor::FAULT_EVENT);
41 bean->Add("CATEGORY", Media::MediaMonitor::AUDIO_STRATEGY_ROUTER);
42 Media::MediaMonitor::MediaMonitorManager::GetInstance().WriteLogMsg(bean);
43 return false;
44 }
45 bool result = ParseInternal(curNode_->GetCopyNode());
46 CHECK_AND_RETURN_RET_LOG(result, false, "Audio strategy router xml parse failed.");
47 return true;
48 }
49
Destroy()50 void AudioStrategyRouterParser::Destroy()
51 {
52 curNode_->FreeDoc();
53 }
54
ParseInternal(std::shared_ptr<AudioXmlNode> curNode)55 bool AudioStrategyRouterParser::ParseInternal(std::shared_ptr<AudioXmlNode> curNode)
56 {
57 for (; curNode->IsNodeValid(); curNode->MoveToNext()) {
58 if (!curNode->IsElementNode()) {
59 continue;
60 }
61 if (curNode->CompareName("strategy")) {
62 ParserStrategyInfo(curNode->GetCopyNode());
63 } else {
64 ParseInternal(curNode->GetChildrenNode());
65 }
66 }
67 return true;
68 }
69
ParserStrategyInfo(std::shared_ptr<AudioXmlNode> curNode)70 void AudioStrategyRouterParser::ParserStrategyInfo(std::shared_ptr<AudioXmlNode> curNode)
71 {
72 string name;
73 string routers;
74 curNode->GetProp("name", name);
75 curNode->GetProp("routers", routers);
76
77 if (name == "MEDIA_RENDER") {
78 AddRouters(mediaRenderRouters_, routers);
79 } else if (name == "CALL_RENDER") {
80 AddRouters(callRenderRouters_, routers);
81 } else if (name == "RING_RENDER") {
82 AddRouters(ringRenderRouters_, routers);
83 } else if (name == "TONE_RENDER") {
84 AddRouters(toneRenderRouters_, routers);
85 } else if (name == "RECORD_CAPTURE") {
86 AddRouters(recordCaptureRouters_, routers);
87 } else if (name == "CALL_CAPTURE") {
88 AddRouters(callCaptureRouters_, routers);
89 } else if (name == "VOICE_MESSAGE_CAPTURE") {
90 AddRouters(voiceMessageRouters_, routers);
91 }
92 }
93
split(const std::string & line,const std::string & sep)94 std::vector<std::string> AudioStrategyRouterParser::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
AddRouters(std::vector<std::unique_ptr<RouterBase>> & routers,string & routeName)111 void AudioStrategyRouterParser::AddRouters(std::vector<std::unique_ptr<RouterBase>> &routers, string &routeName)
112 {
113 vector<string> buf = split(routeName, ",");
114 for (const auto &name : buf) {
115 if (name == "AppSelectRouter") {
116 routers.push_back(make_unique<AppSelectRouter>());
117 } else if (name == "UserSelectRouter") {
118 routers.push_back(make_unique<UserSelectRouter>());
119 } else if (name == "PrivacyPriorityRouter") {
120 routers.push_back(make_unique<PrivacyPriorityRouter>());
121 } else if (name == "PublicPriorityRouter") {
122 routers.push_back(make_unique<PublicPriorityRouter>());
123 } else if (name == "StreamFilterRouter") {
124 routers.push_back(make_unique<StreamFilterRouter>());
125 } else if (name == "DefaultRouter") {
126 routers.push_back(make_unique<DefaultRouter>());
127 } else if (name == "CockpitPhoneRouter") {
128 routers.push_back(make_unique<CockpitPhoneRouter>());
129 } else if (name == "PairDeviceRouter") {
130 routers.push_back(make_unique<PairDeviceRouter>());
131 }
132 }
133 }
134 } // namespace AudioStandard
135 } // namespace OHOS
136