blob: e1427ec3d3233b1d8e7e5652fdac0a19d0a26fc7 [file] [log] [blame]
Shunkai Yao52abf0a2022-11-08 02:44:03 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "AHAL_EffectConfig"
18#include <android-base/logging.h>
19
20#include "effectFactory-impl/EffectConfig.h"
21
22using aidl::android::media::audio::common::AudioUuid;
23
24namespace aidl::android::hardware::audio::effect {
25
26EffectConfig::EffectConfig(const std::string& file) {
27 tinyxml2::XMLDocument doc;
28 doc.LoadFile(file.c_str());
29 LOG(DEBUG) << __func__ << " loading " << file;
30 // parse the xml file into maps
31 if (doc.Error()) {
32 LOG(ERROR) << __func__ << " tinyxml2 failed to load " << file
33 << " error: " << doc.ErrorStr();
34 return;
35 }
36
37 auto registerFailure = [&](bool result) { mSkippedElements += result ? 0 : 1; };
38
39 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
40 // Parse library
41 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
42 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
43 registerFailure(parseLibrary(xmlLibrary));
44 }
45 }
46
47 // Parse effects
48 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
49 for (auto& xmlEffect : getChildren(xmlEffects)) {
50 registerFailure(parseEffect(xmlEffect));
51 }
52 }
53
54 // Parse pre processing chains
55 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
56 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
57 registerFailure(parseStream(xmlStream));
58 }
59 }
60
61 // Parse post processing chains
62 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
63 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
64 registerFailure(parseStream(xmlStream));
65 }
66 }
67 }
68 LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
69 << " element(s)";
70}
71
72std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> EffectConfig::getChildren(
73 const tinyxml2::XMLNode& node, const char* childTag) {
74 std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> children;
75 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
76 child = child->NextSiblingElement(childTag)) {
77 children.emplace_back(*child);
78 }
79 return children;
80}
81
82bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
83 const char* name = xml.Attribute("name");
84 RETURN_VALUE_IF(!name, false, "noNameAttribute");
85 const char* path = xml.Attribute("path");
86 RETURN_VALUE_IF(!path, false, "noPathAttribute");
87
88 mLibraryMap[name] = path;
89 LOG(DEBUG) << __func__ << " " << name << " : " << path;
90 return true;
91}
92
93bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
94 struct EffectLibraries effectLibraries;
95 std::vector<LibraryUuid> libraryUuids;
96 std::string name = xml.Attribute("name");
97 RETURN_VALUE_IF(name == "", false, "effectsNoName");
98
99 LOG(DEBUG) << __func__ << dump(xml);
100 struct LibraryUuid libraryUuid;
101 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
102 // proxy lib and uuid
103 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid, true), false, "parseProxyLibFailed");
104 effectLibraries.proxyLibrary = libraryUuid;
105 // proxy effect libs and UUID
106 auto xmlProxyLib = xml.FirstChildElement();
107 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
108 while (xmlProxyLib) {
109 struct LibraryUuid tempLibraryUuid;
110 RETURN_VALUE_IF(!parseLibraryUuid(*xmlProxyLib, tempLibraryUuid), false,
111 "parseEffectLibFailed");
112 libraryUuids.push_back(std::move(tempLibraryUuid));
113 xmlProxyLib = xmlProxyLib->NextSiblingElement();
114 }
115 } else {
116 // expect only one library if not proxy
117 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid), false, "parseEffectLibFailed");
118 libraryUuids.push_back(std::move(libraryUuid));
119 }
120
121 effectLibraries.libraries = std::move(libraryUuids);
122 mEffectsMap[name] = std::move(effectLibraries);
123 return true;
124}
125
126bool EffectConfig::parseStream(const tinyxml2::XMLElement& xml) {
127 LOG(DEBUG) << __func__ << dump(xml);
128 const char* type = xml.Attribute("type");
129 RETURN_VALUE_IF(!type, false, "noTypeInProcess");
130 RETURN_VALUE_IF(0 != mProcessingMap.count(type), false, "duplicateType");
131
132 for (auto& apply : getChildren(xml, "apply")) {
133 const char* name = apply.get().Attribute("effect");
134 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
135 mProcessingMap[type].push_back(name);
136 LOG(DEBUG) << __func__ << " " << type << " : " << name;
137 }
138 return true;
139}
140
141bool EffectConfig::parseLibraryUuid(const tinyxml2::XMLElement& xml,
142 struct LibraryUuid& libraryUuid, bool isProxy) {
143 // Retrieve library name only if not effectProxy element
144 if (!isProxy) {
145 const char* name = xml.Attribute("library");
146 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
147 libraryUuid.name = name;
148 }
149
150 const char* uuid = xml.Attribute("uuid");
151 RETURN_VALUE_IF(!uuid, false, "noUuidAttribute");
152 RETURN_VALUE_IF(!stringToUuid(uuid, &libraryUuid.uuid), false, "invalidUuidAttribute");
153
154 LOG(DEBUG) << __func__ << (isProxy ? " proxy " : libraryUuid.name) << " : "
155 << libraryUuid.uuid.toString();
156 return true;
157}
158
159const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
160 tinyxml2::XMLPrinter&& printer) const {
161 element.Accept(&printer);
162 return printer.CStr();
163}
164
165} // namespace aidl::android::hardware::audio::effect