blob: 5a83fef99aaf6666f0f9fb068974a2c19a30d2f2 [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>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000019#include <system/audio_effects/effect_uuid.h>
Shunkai Yao52abf0a2022-11-08 02:44:03 +000020
21#include "effectFactory-impl/EffectConfig.h"
22
23using aidl::android::media::audio::common::AudioUuid;
24
25namespace aidl::android::hardware::audio::effect {
26
27EffectConfig::EffectConfig(const std::string& file) {
28 tinyxml2::XMLDocument doc;
29 doc.LoadFile(file.c_str());
30 LOG(DEBUG) << __func__ << " loading " << file;
31 // parse the xml file into maps
32 if (doc.Error()) {
33 LOG(ERROR) << __func__ << " tinyxml2 failed to load " << file
34 << " error: " << doc.ErrorStr();
35 return;
36 }
37
38 auto registerFailure = [&](bool result) { mSkippedElements += result ? 0 : 1; };
39
40 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
41 // Parse library
42 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
43 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
44 registerFailure(parseLibrary(xmlLibrary));
45 }
46 }
47
48 // Parse effects
49 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
50 for (auto& xmlEffect : getChildren(xmlEffects)) {
51 registerFailure(parseEffect(xmlEffect));
52 }
53 }
54
55 // Parse pre processing chains
56 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
57 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
58 registerFailure(parseStream(xmlStream));
59 }
60 }
61
62 // Parse post processing chains
63 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
64 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
65 registerFailure(parseStream(xmlStream));
66 }
67 }
68 }
69 LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
70 << " element(s)";
71}
72
73std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> EffectConfig::getChildren(
74 const tinyxml2::XMLNode& node, const char* childTag) {
75 std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> children;
76 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
77 child = child->NextSiblingElement(childTag)) {
78 children.emplace_back(*child);
79 }
80 return children;
81}
82
Shunkai Yao52ba4dc2023-02-01 21:57:20 +000083bool EffectConfig::resolveLibrary(const std::string& path, std::string* resolvedPath) {
84 for (auto* libraryDirectory : kEffectLibPath) {
85 std::string candidatePath = std::string(libraryDirectory) + '/' + path;
86 if (access(candidatePath.c_str(), R_OK) == 0) {
87 *resolvedPath = std::move(candidatePath);
88 return true;
89 }
90 }
91 return false;
92}
93
Shunkai Yao52abf0a2022-11-08 02:44:03 +000094bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
95 const char* name = xml.Attribute("name");
96 RETURN_VALUE_IF(!name, false, "noNameAttribute");
97 const char* path = xml.Attribute("path");
98 RETURN_VALUE_IF(!path, false, "noPathAttribute");
99
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000100 std::string resolvedPath;
101 if (!resolveLibrary(path, &resolvedPath)) {
102 LOG(ERROR) << __func__ << " can't find " << path;
103 return false;
104 }
105 mLibraryMap[name] = resolvedPath;
106 LOG(DEBUG) << __func__ << " " << name << " : " << resolvedPath;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000107 return true;
108}
109
110bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
111 struct EffectLibraries effectLibraries;
112 std::vector<LibraryUuid> libraryUuids;
113 std::string name = xml.Attribute("name");
114 RETURN_VALUE_IF(name == "", false, "effectsNoName");
115
116 LOG(DEBUG) << __func__ << dump(xml);
117 struct LibraryUuid libraryUuid;
118 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
119 // proxy lib and uuid
120 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid, true), false, "parseProxyLibFailed");
121 effectLibraries.proxyLibrary = libraryUuid;
122 // proxy effect libs and UUID
123 auto xmlProxyLib = xml.FirstChildElement();
124 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
125 while (xmlProxyLib) {
126 struct LibraryUuid tempLibraryUuid;
127 RETURN_VALUE_IF(!parseLibraryUuid(*xmlProxyLib, tempLibraryUuid), false,
128 "parseEffectLibFailed");
129 libraryUuids.push_back(std::move(tempLibraryUuid));
130 xmlProxyLib = xmlProxyLib->NextSiblingElement();
131 }
132 } else {
133 // expect only one library if not proxy
134 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid), false, "parseEffectLibFailed");
135 libraryUuids.push_back(std::move(libraryUuid));
136 }
137
138 effectLibraries.libraries = std::move(libraryUuids);
139 mEffectsMap[name] = std::move(effectLibraries);
140 return true;
141}
142
143bool EffectConfig::parseStream(const tinyxml2::XMLElement& xml) {
144 LOG(DEBUG) << __func__ << dump(xml);
145 const char* type = xml.Attribute("type");
146 RETURN_VALUE_IF(!type, false, "noTypeInProcess");
147 RETURN_VALUE_IF(0 != mProcessingMap.count(type), false, "duplicateType");
148
149 for (auto& apply : getChildren(xml, "apply")) {
150 const char* name = apply.get().Attribute("effect");
151 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
152 mProcessingMap[type].push_back(name);
153 LOG(DEBUG) << __func__ << " " << type << " : " << name;
154 }
155 return true;
156}
157
158bool EffectConfig::parseLibraryUuid(const tinyxml2::XMLElement& xml,
159 struct LibraryUuid& libraryUuid, bool isProxy) {
160 // Retrieve library name only if not effectProxy element
161 if (!isProxy) {
162 const char* name = xml.Attribute("library");
163 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
164 libraryUuid.name = name;
165 }
166
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000167 const char* uuidStr = xml.Attribute("uuid");
168 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
169 libraryUuid.uuid = stringToUuid(uuidStr);
170 RETURN_VALUE_IF((libraryUuid.uuid == getEffectUuidZero()), false, "invalidUuidAttribute");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000171
172 LOG(DEBUG) << __func__ << (isProxy ? " proxy " : libraryUuid.name) << " : "
173 << libraryUuid.uuid.toString();
174 return true;
175}
176
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000177bool EffectConfig::findUuid(const std::string& xmlEffectName, AudioUuid* uuid) {
178// Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type
179#define EFFECT_XML_TYPE_LIST_DEF(V) \
180 V("acoustic_echo_canceler", AcousticEchoCanceler) \
181 V("automatic_gain_control_v1", AutomaticGainControlV1) \
182 V("automatic_gain_control_v2", AutomaticGainControlV2) \
183 V("bassboost", BassBoost) \
184 V("downmix", Downmix) \
185 V("dynamics_processing", DynamicsProcessing) \
186 V("equalizer", Equalizer) \
187 V("haptic_generator", HapticGenerator) \
188 V("loudness_enhancer", LoudnessEnhancer) \
189 V("env_reverb", EnvReverb) \
190 V("reverb_env_aux", EnvReverb) \
191 V("reverb_env_ins", EnvReverb) \
192 V("preset_reverb", PresetReverb) \
193 V("reverb_pre_aux", PresetReverb) \
194 V("reverb_pre_ins", PresetReverb) \
195 V("noise_suppression", NoiseSuppression) \
196 V("spatializer", Spatializer) \
197 V("virtualizer", Virtualizer) \
198 V("visualizer", Visualizer) \
199 V("volume", Volume)
200
201#define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol},
202
203 typedef const AudioUuid& (*UuidGetter)(void);
204 static const std::map<std::string, UuidGetter> uuidMap{
205 // std::make_pair("s", &getEffectTypeUuidExtension)};
206 {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}};
207 if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) {
208 *uuid = (*it->second)();
209 return true;
210 }
211 return false;
212}
213
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000214const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
215 tinyxml2::XMLPrinter&& printer) const {
216 element.Accept(&printer);
217 return printer.CStr();
218}
219
220} // namespace aidl::android::hardware::audio::effect