1 /* 2 * Copyright (c) 2023-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 16 #ifndef AUDIO_USAGE_STRATEGY_PARSER_H 17 #define AUDIO_USAGE_STRATEGY_PARSER_H 18 19 #include <list> 20 #include <unordered_map> 21 #include <string> 22 #include <sstream> 23 24 #include "audio_policy_log.h" 25 #include "iport_observer.h" 26 #include "parser.h" 27 #include "audio_xml_parser.h" 28 29 namespace OHOS { 30 namespace AudioStandard { 31 using namespace std; 32 33 class AudioUsageStrategyParser : public Parser { 34 public: 35 static constexpr char DEVICE_CONFIG_FILE[] = "/system/etc/audio/audio_usage_strategy.xml"; 36 37 bool LoadConfiguration() final; 38 void Destroy() final; 39 AudioUsageStrategyParser()40 AudioUsageStrategyParser() 41 { 42 curNode_ = AudioXmlNode::Create(); 43 AUDIO_DEBUG_LOG("AudioUsageStrategyParser ctor"); 44 } 45 ~AudioUsageStrategyParser()46 virtual ~AudioUsageStrategyParser() 47 { 48 AUDIO_DEBUG_LOG("AudioUsageStrategyParser dtor"); 49 Destroy(); 50 curNode_ = nullptr; 51 } 52 53 std::unordered_map<StreamUsage, std::string> renderConfigMap_; 54 std::unordered_map<SourceType, std::string> capturerConfigMap_; 55 56 private: 57 bool ParseInternal(std::shared_ptr<AudioXmlNode> curNode); 58 void ParserStreamUsageList(std::shared_ptr<AudioXmlNode> curNode); 59 void ParserSourceTypeList(std::shared_ptr<AudioXmlNode> curNode); 60 void ParserStreamUsageInfo(const std::string &strategyName, const std::string &streamUsage); 61 void ParserStreamUsage(const std::vector<std::string> &buf, const std::string &routerName); 62 void ParserSourceTypeInfo(const std::string &sourceType, const std::string &nameSourceType); 63 void ParserSourceTypes(const std::vector<std::string> &buf, const std::string &sourceTypes); 64 65 std::vector<std::string> split(const std::string &line, const std::string &sep); 66 67 const unordered_map<string, StreamUsage> streamUsageMap = { 68 {"STREAM_USAGE_UNKNOWN", STREAM_USAGE_UNKNOWN}, 69 {"STREAM_USAGE_MEDIA", STREAM_USAGE_MEDIA}, 70 {"STREAM_USAGE_MUSIC", STREAM_USAGE_MUSIC}, 71 {"STREAM_USAGE_VOICE_COMMUNICATION", STREAM_USAGE_VOICE_COMMUNICATION}, 72 {"STREAM_USAGE_VOICE_ASSISTANT", STREAM_USAGE_VOICE_ASSISTANT}, 73 {"STREAM_USAGE_VOICE_CALL_ASSISTANT", STREAM_USAGE_VOICE_CALL_ASSISTANT}, 74 {"STREAM_USAGE_ALARM", STREAM_USAGE_ALARM}, 75 {"STREAM_USAGE_VOICE_MESSAGE", STREAM_USAGE_VOICE_MESSAGE}, 76 {"STREAM_USAGE_NOTIFICATION_RINGTONE", STREAM_USAGE_NOTIFICATION_RINGTONE}, 77 {"STREAM_USAGE_RINGTONE", STREAM_USAGE_RINGTONE}, 78 {"STREAM_USAGE_NOTIFICATION", STREAM_USAGE_NOTIFICATION}, 79 {"STREAM_USAGE_ACCESSIBILITY", STREAM_USAGE_ACCESSIBILITY}, 80 {"STREAM_USAGE_SYSTEM", STREAM_USAGE_SYSTEM}, 81 {"STREAM_USAGE_MOVIE", STREAM_USAGE_MOVIE}, 82 {"STREAM_USAGE_GAME", STREAM_USAGE_GAME}, 83 {"STREAM_USAGE_AUDIOBOOK", STREAM_USAGE_AUDIOBOOK}, 84 {"STREAM_USAGE_NAVIGATION", STREAM_USAGE_NAVIGATION}, 85 {"STREAM_USAGE_DTMF", STREAM_USAGE_DTMF}, 86 {"STREAM_USAGE_ENFORCED_TONE", STREAM_USAGE_ENFORCED_TONE}, 87 {"STREAM_USAGE_ULTRASONIC", STREAM_USAGE_ULTRASONIC}, 88 {"STREAM_USAGE_VIDEO_COMMUNICATION", STREAM_USAGE_VIDEO_COMMUNICATION}, 89 {"STREAM_USAGE_RANGING", STREAM_USAGE_RANGING}, 90 {"STREAM_USAGE_VOICE_MODEM_COMMUNICATION", STREAM_USAGE_VOICE_MODEM_COMMUNICATION}, 91 {"STREAM_USAGE_VOICE_RINGTONE", STREAM_USAGE_VOICE_RINGTONE}, 92 }; 93 94 const unordered_map<string, SourceType> sourceTypeMap = { 95 {"SOURCE_TYPE_MIC", SOURCE_TYPE_MIC}, 96 {"SOURCE_TYPE_CAMCORDER", SOURCE_TYPE_CAMCORDER}, 97 {"SOURCE_TYPE_VOICE_RECOGNITION", SOURCE_TYPE_VOICE_RECOGNITION}, 98 {"SOURCE_TYPE_PLAYBACK_CAPTURE", SOURCE_TYPE_PLAYBACK_CAPTURE}, 99 {"SOURCE_TYPE_WAKEUP", SOURCE_TYPE_WAKEUP}, 100 {"SOURCE_TYPE_VOICE_COMMUNICATION", SOURCE_TYPE_VOICE_COMMUNICATION}, 101 {"SOURCE_TYPE_VOICE_CALL", SOURCE_TYPE_VOICE_CALL}, 102 {"SOURCE_TYPE_ULTRASONIC", SOURCE_TYPE_ULTRASONIC}, 103 {"SOURCE_TYPE_VIRTUAL_CAPTURE", SOURCE_TYPE_VIRTUAL_CAPTURE}, 104 {"SOURCE_TYPE_VOICE_MESSAGE", SOURCE_TYPE_VOICE_MESSAGE}, 105 {"SOURCE_TYPE_VOICE_TRANSCRIPTION", SOURCE_TYPE_VOICE_TRANSCRIPTION}, 106 {"SOURCE_TYPE_UNPROCESSED", SOURCE_TYPE_UNPROCESSED}, 107 }; 108 109 std::shared_ptr<AudioXmlNode> curNode_ = nullptr; 110 }; 111 } // namespace AudioStandard 112 } // namespace OHOS 113 #endif // AUDIO_USAGE_STRATEGY_PARSER_H 114