blob: 730c0bf758a519f17ffdee10f4eee2fe4bd4c758 [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
Shunkai Yao5824efb2023-05-08 21:16:13 +000017#include <optional>
18#include <string>
Shunkai Yao52abf0a2022-11-08 02:44:03 +000019#define LOG_TAG "AHAL_EffectConfig"
20#include <android-base/logging.h>
Shunkai Yaobeef9092023-06-29 18:18:20 +000021#include <system/audio_aidl_utils.h>
Shunkai Yao5824efb2023-05-08 21:16:13 +000022#include <system/audio_effects/audio_effects_conf.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000023#include <system/audio_effects/effect_uuid.h>
Shunkai Yao52abf0a2022-11-08 02:44:03 +000024
25#include "effectFactory-impl/EffectConfig.h"
26
Shunkai Yao5824efb2023-05-08 21:16:13 +000027using aidl::android::media::audio::common::AudioSource;
28using aidl::android::media::audio::common::AudioStreamType;
Shunkai Yao52abf0a2022-11-08 02:44:03 +000029using aidl::android::media::audio::common::AudioUuid;
30
31namespace aidl::android::hardware::audio::effect {
32
33EffectConfig::EffectConfig(const std::string& file) {
34 tinyxml2::XMLDocument doc;
35 doc.LoadFile(file.c_str());
36 LOG(DEBUG) << __func__ << " loading " << file;
37 // parse the xml file into maps
38 if (doc.Error()) {
39 LOG(ERROR) << __func__ << " tinyxml2 failed to load " << file
40 << " error: " << doc.ErrorStr();
41 return;
42 }
43
44 auto registerFailure = [&](bool result) { mSkippedElements += result ? 0 : 1; };
45
46 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
47 // Parse library
48 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
49 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
50 registerFailure(parseLibrary(xmlLibrary));
51 }
52 }
53
54 // Parse effects
55 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
56 for (auto& xmlEffect : getChildren(xmlEffects)) {
57 registerFailure(parseEffect(xmlEffect));
58 }
59 }
60
61 // Parse pre processing chains
62 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
63 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000064 // AudioSource
65 registerFailure(parseProcessing(Processing::Type::source, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000066 }
67 }
68
69 // Parse post processing chains
70 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
71 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000072 // AudioStreamType
73 registerFailure(parseProcessing(Processing::Type::streamType, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000074 }
75 }
76 }
77 LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
78 << " element(s)";
79}
80
81std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> EffectConfig::getChildren(
82 const tinyxml2::XMLNode& node, const char* childTag) {
83 std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> children;
84 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
85 child = child->NextSiblingElement(childTag)) {
86 children.emplace_back(*child);
87 }
88 return children;
89}
90
Shunkai Yao52ba4dc2023-02-01 21:57:20 +000091bool EffectConfig::resolveLibrary(const std::string& path, std::string* resolvedPath) {
92 for (auto* libraryDirectory : kEffectLibPath) {
93 std::string candidatePath = std::string(libraryDirectory) + '/' + path;
94 if (access(candidatePath.c_str(), R_OK) == 0) {
95 *resolvedPath = std::move(candidatePath);
96 return true;
97 }
98 }
99 return false;
100}
101
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000102bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
103 const char* name = xml.Attribute("name");
104 RETURN_VALUE_IF(!name, false, "noNameAttribute");
105 const char* path = xml.Attribute("path");
106 RETURN_VALUE_IF(!path, false, "noPathAttribute");
107
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000108 std::string resolvedPath;
109 if (!resolveLibrary(path, &resolvedPath)) {
110 LOG(ERROR) << __func__ << " can't find " << path;
111 return false;
112 }
113 mLibraryMap[name] = resolvedPath;
114 LOG(DEBUG) << __func__ << " " << name << " : " << resolvedPath;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000115 return true;
116}
117
118bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
119 struct EffectLibraries effectLibraries;
Shunkai Yao80e58502023-07-14 22:10:46 +0000120 std::vector<Library> libraries;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000121 std::string name = xml.Attribute("name");
122 RETURN_VALUE_IF(name == "", false, "effectsNoName");
123
124 LOG(DEBUG) << __func__ << dump(xml);
Shunkai Yao80e58502023-07-14 22:10:46 +0000125 struct Library library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000126 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
127 // proxy lib and uuid
Shunkai Yao80e58502023-07-14 22:10:46 +0000128 RETURN_VALUE_IF(!parseLibrary(xml, library, true), false, "parseProxyLibFailed");
129 effectLibraries.proxyLibrary = library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000130 // proxy effect libs and UUID
131 auto xmlProxyLib = xml.FirstChildElement();
132 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
133 while (xmlProxyLib) {
Shunkai Yao80e58502023-07-14 22:10:46 +0000134 struct Library tempLibrary;
135 RETURN_VALUE_IF(!parseLibrary(*xmlProxyLib, tempLibrary), false,
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000136 "parseEffectLibFailed");
Shunkai Yao80e58502023-07-14 22:10:46 +0000137 libraries.push_back(std::move(tempLibrary));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000138 xmlProxyLib = xmlProxyLib->NextSiblingElement();
139 }
140 } else {
141 // expect only one library if not proxy
Shunkai Yao80e58502023-07-14 22:10:46 +0000142 RETURN_VALUE_IF(!parseLibrary(xml, library), false, "parseEffectLibFailed");
143 libraries.push_back(std::move(library));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000144 }
145
Shunkai Yao80e58502023-07-14 22:10:46 +0000146 effectLibraries.libraries = std::move(libraries);
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000147 mEffectsMap[name] = std::move(effectLibraries);
148 return true;
149}
150
Shunkai Yao80e58502023-07-14 22:10:46 +0000151bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml, struct Library& library,
152 bool isProxy) {
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000153 // Retrieve library name only if not effectProxy element
154 if (!isProxy) {
155 const char* name = xml.Attribute("library");
156 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000157 library.name = name;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000158 }
159
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000160 const char* uuidStr = xml.Attribute("uuid");
161 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000162 library.uuid = stringToUuid(uuidStr);
163 if (const char* typeUuidStr = xml.Attribute("type")) {
164 library.type = stringToUuid(typeUuidStr);
165 }
166 RETURN_VALUE_IF((library.uuid == getEffectUuidZero()), false, "invalidUuidAttribute");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000167
Shunkai Yao80e58502023-07-14 22:10:46 +0000168 LOG(DEBUG) << __func__ << (isProxy ? " proxy " : library.name) << " : uuid "
169 << ::android::audio::utils::toString(library.uuid)
170 << (library.type.has_value()
171 ? ::android::audio::utils::toString(library.type.value())
172 : "");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000173 return true;
174}
175
Shunkai Yao5824efb2023-05-08 21:16:13 +0000176std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag,
177 const std::string& type) {
178 // see list of audio stream types in audio_stream_type_t:
179 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
180 // AUDIO_STREAM_DEFAULT_TAG is not listed here because according to SYS_RESERVED_DEFAULT in
181 // AudioStreamType.aidl: "Value reserved for system use only. HALs must never return this value
182 // to the system or accept it from the system".
183 static const std::map<const std::string, AudioStreamType> sAudioStreamTypeTable = {
184 {AUDIO_STREAM_VOICE_CALL_TAG, AudioStreamType::VOICE_CALL},
185 {AUDIO_STREAM_SYSTEM_TAG, AudioStreamType::SYSTEM},
186 {AUDIO_STREAM_RING_TAG, AudioStreamType::RING},
187 {AUDIO_STREAM_MUSIC_TAG, AudioStreamType::MUSIC},
188 {AUDIO_STREAM_ALARM_TAG, AudioStreamType::ALARM},
189 {AUDIO_STREAM_NOTIFICATION_TAG, AudioStreamType::NOTIFICATION},
190 {AUDIO_STREAM_BLUETOOTH_SCO_TAG, AudioStreamType::BLUETOOTH_SCO},
191 {AUDIO_STREAM_ENFORCED_AUDIBLE_TAG, AudioStreamType::ENFORCED_AUDIBLE},
192 {AUDIO_STREAM_DTMF_TAG, AudioStreamType::DTMF},
193 {AUDIO_STREAM_TTS_TAG, AudioStreamType::TTS},
194 {AUDIO_STREAM_ASSISTANT_TAG, AudioStreamType::ASSISTANT}};
195
196 // see list of audio sources in audio_source_t:
197 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
198 static const std::map<const std::string, AudioSource> sAudioSourceTable = {
199 {MIC_SRC_TAG, AudioSource::VOICE_CALL},
200 {VOICE_UL_SRC_TAG, AudioSource::VOICE_CALL},
201 {VOICE_DL_SRC_TAG, AudioSource::VOICE_CALL},
202 {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
203 {CAMCORDER_SRC_TAG, AudioSource::VOICE_CALL},
204 {VOICE_REC_SRC_TAG, AudioSource::VOICE_CALL},
205 {VOICE_COMM_SRC_TAG, AudioSource::VOICE_CALL},
206 {REMOTE_SUBMIX_SRC_TAG, AudioSource::VOICE_CALL},
207 {UNPROCESSED_SRC_TAG, AudioSource::VOICE_CALL},
208 {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_CALL}};
209
210 if (typeTag == Processing::Type::streamType) {
211 auto typeIter = sAudioStreamTypeTable.find(type);
212 if (typeIter != sAudioStreamTypeTable.end()) {
213 return typeIter->second;
214 }
215 } else if (typeTag == Processing::Type::source) {
216 auto typeIter = sAudioSourceTable.find(type);
217 if (typeIter != sAudioSourceTable.end()) {
218 return typeIter->second;
219 }
220 }
221
222 return std::nullopt;
223}
224
225bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) {
226 LOG(DEBUG) << __func__ << dump(xml);
227 const char* typeStr = xml.Attribute("type");
228 auto aidlType = stringToProcessingType(typeTag, typeStr);
229 RETURN_VALUE_IF(!aidlType.has_value(), false, "illegalStreamType");
230 RETURN_VALUE_IF(0 != mProcessingMap.count(aidlType.value()), false, "duplicateStreamType");
231
232 for (auto& apply : getChildren(xml, "apply")) {
233 const char* name = apply.get().Attribute("effect");
234 if (mEffectsMap.find(name) == mEffectsMap.end()) {
235 LOG(ERROR) << __func__ << " effect " << name << " doesn't exist, skipping";
236 continue;
237 }
238 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
239 mProcessingMap[aidlType.value()].emplace_back(mEffectsMap[name]);
240 LOG(WARNING) << __func__ << " " << typeStr << " : " << name;
241 }
242 return true;
243}
244
245const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>&
246EffectConfig::getProcessingMap() const {
247 return mProcessingMap;
248}
249
Shunkai Yao80e58502023-07-14 22:10:46 +0000250bool EffectConfig::findUuid(const std::pair<std::string, struct EffectLibraries>& effectElem,
251 AudioUuid* uuid) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000252// Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type
253#define EFFECT_XML_TYPE_LIST_DEF(V) \
254 V("acoustic_echo_canceler", AcousticEchoCanceler) \
255 V("automatic_gain_control_v1", AutomaticGainControlV1) \
256 V("automatic_gain_control_v2", AutomaticGainControlV2) \
257 V("bassboost", BassBoost) \
258 V("downmix", Downmix) \
259 V("dynamics_processing", DynamicsProcessing) \
260 V("equalizer", Equalizer) \
Shunkai Yaobeef9092023-06-29 18:18:20 +0000261 V("extensioneffect", Extension) \
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000262 V("haptic_generator", HapticGenerator) \
263 V("loudness_enhancer", LoudnessEnhancer) \
264 V("env_reverb", EnvReverb) \
265 V("reverb_env_aux", EnvReverb) \
266 V("reverb_env_ins", EnvReverb) \
267 V("preset_reverb", PresetReverb) \
268 V("reverb_pre_aux", PresetReverb) \
269 V("reverb_pre_ins", PresetReverb) \
270 V("noise_suppression", NoiseSuppression) \
271 V("spatializer", Spatializer) \
272 V("virtualizer", Virtualizer) \
273 V("visualizer", Visualizer) \
274 V("volume", Volume)
275
276#define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol},
277
Shunkai Yao80e58502023-07-14 22:10:46 +0000278 const std::string xmlEffectName = effectElem.first;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000279 typedef const AudioUuid& (*UuidGetter)(void);
280 static const std::map<std::string, UuidGetter> uuidMap{
281 // std::make_pair("s", &getEffectTypeUuidExtension)};
282 {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}};
283 if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) {
284 *uuid = (*it->second)();
285 return true;
286 }
Shunkai Yao80e58502023-07-14 22:10:46 +0000287
288 const auto& libs = effectElem.second.libraries;
289 for (const auto& lib : libs) {
290 if (lib.type.has_value()) {
291 *uuid = lib.type.value();
292 return true;
293 }
294 }
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000295 return false;
296}
297
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000298const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
299 tinyxml2::XMLPrinter&& printer) const {
300 element.Accept(&printer);
301 return printer.CStr();
302}
303
304} // namespace aidl::android::hardware::audio::effect