blob: c030b7a88c417e2ea5ee7e0667e0212968f79d12 [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
Shunkai Yao52ba4dc2023-02-01 21:57:20 +000082bool EffectConfig::resolveLibrary(const std::string& path, std::string* resolvedPath) {
83 for (auto* libraryDirectory : kEffectLibPath) {
84 std::string candidatePath = std::string(libraryDirectory) + '/' + path;
85 if (access(candidatePath.c_str(), R_OK) == 0) {
86 *resolvedPath = std::move(candidatePath);
87 return true;
88 }
89 }
90 return false;
91}
92
Shunkai Yao52abf0a2022-11-08 02:44:03 +000093bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
94 const char* name = xml.Attribute("name");
95 RETURN_VALUE_IF(!name, false, "noNameAttribute");
96 const char* path = xml.Attribute("path");
97 RETURN_VALUE_IF(!path, false, "noPathAttribute");
98
Shunkai Yao52ba4dc2023-02-01 21:57:20 +000099 std::string resolvedPath;
100 if (!resolveLibrary(path, &resolvedPath)) {
101 LOG(ERROR) << __func__ << " can't find " << path;
102 return false;
103 }
104 mLibraryMap[name] = resolvedPath;
105 LOG(DEBUG) << __func__ << " " << name << " : " << resolvedPath;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000106 return true;
107}
108
109bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
110 struct EffectLibraries effectLibraries;
111 std::vector<LibraryUuid> libraryUuids;
112 std::string name = xml.Attribute("name");
113 RETURN_VALUE_IF(name == "", false, "effectsNoName");
114
115 LOG(DEBUG) << __func__ << dump(xml);
116 struct LibraryUuid libraryUuid;
117 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
118 // proxy lib and uuid
119 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid, true), false, "parseProxyLibFailed");
120 effectLibraries.proxyLibrary = libraryUuid;
121 // proxy effect libs and UUID
122 auto xmlProxyLib = xml.FirstChildElement();
123 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
124 while (xmlProxyLib) {
125 struct LibraryUuid tempLibraryUuid;
126 RETURN_VALUE_IF(!parseLibraryUuid(*xmlProxyLib, tempLibraryUuid), false,
127 "parseEffectLibFailed");
128 libraryUuids.push_back(std::move(tempLibraryUuid));
129 xmlProxyLib = xmlProxyLib->NextSiblingElement();
130 }
131 } else {
132 // expect only one library if not proxy
133 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid), false, "parseEffectLibFailed");
134 libraryUuids.push_back(std::move(libraryUuid));
135 }
136
137 effectLibraries.libraries = std::move(libraryUuids);
138 mEffectsMap[name] = std::move(effectLibraries);
139 return true;
140}
141
142bool EffectConfig::parseStream(const tinyxml2::XMLElement& xml) {
143 LOG(DEBUG) << __func__ << dump(xml);
144 const char* type = xml.Attribute("type");
145 RETURN_VALUE_IF(!type, false, "noTypeInProcess");
146 RETURN_VALUE_IF(0 != mProcessingMap.count(type), false, "duplicateType");
147
148 for (auto& apply : getChildren(xml, "apply")) {
149 const char* name = apply.get().Attribute("effect");
150 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
151 mProcessingMap[type].push_back(name);
152 LOG(DEBUG) << __func__ << " " << type << " : " << name;
153 }
154 return true;
155}
156
157bool EffectConfig::parseLibraryUuid(const tinyxml2::XMLElement& xml,
158 struct LibraryUuid& libraryUuid, bool isProxy) {
159 // Retrieve library name only if not effectProxy element
160 if (!isProxy) {
161 const char* name = xml.Attribute("library");
162 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
163 libraryUuid.name = name;
164 }
165
166 const char* uuid = xml.Attribute("uuid");
167 RETURN_VALUE_IF(!uuid, false, "noUuidAttribute");
168 RETURN_VALUE_IF(!stringToUuid(uuid, &libraryUuid.uuid), false, "invalidUuidAttribute");
169
170 LOG(DEBUG) << __func__ << (isProxy ? " proxy " : libraryUuid.name) << " : "
171 << libraryUuid.uuid.toString();
172 return true;
173}
174
175const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
176 tinyxml2::XMLPrinter&& printer) const {
177 element.Accept(&printer);
178 return printer.CStr();
179}
180
181} // namespace aidl::android::hardware::audio::effect