blob: 71d111bd062eda14f10a23e5557b23c48c3fc62f [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 Yao5824efb2023-05-08 21:16:13 +000021#include <system/audio_effects/audio_effects_conf.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000022#include <system/audio_effects/effect_uuid.h>
Shunkai Yao52abf0a2022-11-08 02:44:03 +000023
24#include "effectFactory-impl/EffectConfig.h"
25
Shunkai Yao5824efb2023-05-08 21:16:13 +000026using aidl::android::media::audio::common::AudioSource;
27using aidl::android::media::audio::common::AudioStreamType;
Shunkai Yao52abf0a2022-11-08 02:44:03 +000028using aidl::android::media::audio::common::AudioUuid;
29
30namespace aidl::android::hardware::audio::effect {
31
32EffectConfig::EffectConfig(const std::string& file) {
33 tinyxml2::XMLDocument doc;
34 doc.LoadFile(file.c_str());
35 LOG(DEBUG) << __func__ << " loading " << file;
36 // parse the xml file into maps
37 if (doc.Error()) {
38 LOG(ERROR) << __func__ << " tinyxml2 failed to load " << file
39 << " error: " << doc.ErrorStr();
40 return;
41 }
42
43 auto registerFailure = [&](bool result) { mSkippedElements += result ? 0 : 1; };
44
45 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
46 // Parse library
47 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
48 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
49 registerFailure(parseLibrary(xmlLibrary));
50 }
51 }
52
53 // Parse effects
54 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
55 for (auto& xmlEffect : getChildren(xmlEffects)) {
56 registerFailure(parseEffect(xmlEffect));
57 }
58 }
59
60 // Parse pre processing chains
61 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
62 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000063 // AudioSource
64 registerFailure(parseProcessing(Processing::Type::source, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000065 }
66 }
67
68 // Parse post processing chains
69 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
70 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000071 // AudioStreamType
72 registerFailure(parseProcessing(Processing::Type::streamType, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000073 }
74 }
75 }
76 LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
77 << " element(s)";
78}
79
80std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> EffectConfig::getChildren(
81 const tinyxml2::XMLNode& node, const char* childTag) {
82 std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> children;
83 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
84 child = child->NextSiblingElement(childTag)) {
85 children.emplace_back(*child);
86 }
87 return children;
88}
89
Shunkai Yao52ba4dc2023-02-01 21:57:20 +000090bool EffectConfig::resolveLibrary(const std::string& path, std::string* resolvedPath) {
91 for (auto* libraryDirectory : kEffectLibPath) {
92 std::string candidatePath = std::string(libraryDirectory) + '/' + path;
93 if (access(candidatePath.c_str(), R_OK) == 0) {
94 *resolvedPath = std::move(candidatePath);
95 return true;
96 }
97 }
98 return false;
99}
100
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000101bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
102 const char* name = xml.Attribute("name");
103 RETURN_VALUE_IF(!name, false, "noNameAttribute");
104 const char* path = xml.Attribute("path");
105 RETURN_VALUE_IF(!path, false, "noPathAttribute");
106
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000107 std::string resolvedPath;
108 if (!resolveLibrary(path, &resolvedPath)) {
109 LOG(ERROR) << __func__ << " can't find " << path;
110 return false;
111 }
112 mLibraryMap[name] = resolvedPath;
113 LOG(DEBUG) << __func__ << " " << name << " : " << resolvedPath;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000114 return true;
115}
116
117bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
118 struct EffectLibraries effectLibraries;
119 std::vector<LibraryUuid> libraryUuids;
120 std::string name = xml.Attribute("name");
121 RETURN_VALUE_IF(name == "", false, "effectsNoName");
122
123 LOG(DEBUG) << __func__ << dump(xml);
124 struct LibraryUuid libraryUuid;
125 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
126 // proxy lib and uuid
127 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid, true), false, "parseProxyLibFailed");
128 effectLibraries.proxyLibrary = libraryUuid;
129 // proxy effect libs and UUID
130 auto xmlProxyLib = xml.FirstChildElement();
131 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
132 while (xmlProxyLib) {
133 struct LibraryUuid tempLibraryUuid;
134 RETURN_VALUE_IF(!parseLibraryUuid(*xmlProxyLib, tempLibraryUuid), false,
135 "parseEffectLibFailed");
136 libraryUuids.push_back(std::move(tempLibraryUuid));
137 xmlProxyLib = xmlProxyLib->NextSiblingElement();
138 }
139 } else {
140 // expect only one library if not proxy
141 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid), false, "parseEffectLibFailed");
142 libraryUuids.push_back(std::move(libraryUuid));
143 }
144
145 effectLibraries.libraries = std::move(libraryUuids);
146 mEffectsMap[name] = std::move(effectLibraries);
147 return true;
148}
149
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000150bool EffectConfig::parseLibraryUuid(const tinyxml2::XMLElement& xml,
151 struct LibraryUuid& libraryUuid, bool isProxy) {
152 // Retrieve library name only if not effectProxy element
153 if (!isProxy) {
154 const char* name = xml.Attribute("library");
155 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
156 libraryUuid.name = name;
157 }
158
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000159 const char* uuidStr = xml.Attribute("uuid");
160 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
161 libraryUuid.uuid = stringToUuid(uuidStr);
162 RETURN_VALUE_IF((libraryUuid.uuid == getEffectUuidZero()), false, "invalidUuidAttribute");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000163
164 LOG(DEBUG) << __func__ << (isProxy ? " proxy " : libraryUuid.name) << " : "
165 << libraryUuid.uuid.toString();
166 return true;
167}
168
Shunkai Yao5824efb2023-05-08 21:16:13 +0000169std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag,
170 const std::string& type) {
171 // see list of audio stream types in audio_stream_type_t:
172 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
173 // AUDIO_STREAM_DEFAULT_TAG is not listed here because according to SYS_RESERVED_DEFAULT in
174 // AudioStreamType.aidl: "Value reserved for system use only. HALs must never return this value
175 // to the system or accept it from the system".
176 static const std::map<const std::string, AudioStreamType> sAudioStreamTypeTable = {
177 {AUDIO_STREAM_VOICE_CALL_TAG, AudioStreamType::VOICE_CALL},
178 {AUDIO_STREAM_SYSTEM_TAG, AudioStreamType::SYSTEM},
179 {AUDIO_STREAM_RING_TAG, AudioStreamType::RING},
180 {AUDIO_STREAM_MUSIC_TAG, AudioStreamType::MUSIC},
181 {AUDIO_STREAM_ALARM_TAG, AudioStreamType::ALARM},
182 {AUDIO_STREAM_NOTIFICATION_TAG, AudioStreamType::NOTIFICATION},
183 {AUDIO_STREAM_BLUETOOTH_SCO_TAG, AudioStreamType::BLUETOOTH_SCO},
184 {AUDIO_STREAM_ENFORCED_AUDIBLE_TAG, AudioStreamType::ENFORCED_AUDIBLE},
185 {AUDIO_STREAM_DTMF_TAG, AudioStreamType::DTMF},
186 {AUDIO_STREAM_TTS_TAG, AudioStreamType::TTS},
187 {AUDIO_STREAM_ASSISTANT_TAG, AudioStreamType::ASSISTANT}};
188
189 // see list of audio sources in audio_source_t:
190 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
191 static const std::map<const std::string, AudioSource> sAudioSourceTable = {
192 {MIC_SRC_TAG, AudioSource::VOICE_CALL},
193 {VOICE_UL_SRC_TAG, AudioSource::VOICE_CALL},
194 {VOICE_DL_SRC_TAG, AudioSource::VOICE_CALL},
195 {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
196 {CAMCORDER_SRC_TAG, AudioSource::VOICE_CALL},
197 {VOICE_REC_SRC_TAG, AudioSource::VOICE_CALL},
198 {VOICE_COMM_SRC_TAG, AudioSource::VOICE_CALL},
199 {REMOTE_SUBMIX_SRC_TAG, AudioSource::VOICE_CALL},
200 {UNPROCESSED_SRC_TAG, AudioSource::VOICE_CALL},
201 {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_CALL}};
202
203 if (typeTag == Processing::Type::streamType) {
204 auto typeIter = sAudioStreamTypeTable.find(type);
205 if (typeIter != sAudioStreamTypeTable.end()) {
206 return typeIter->second;
207 }
208 } else if (typeTag == Processing::Type::source) {
209 auto typeIter = sAudioSourceTable.find(type);
210 if (typeIter != sAudioSourceTable.end()) {
211 return typeIter->second;
212 }
213 }
214
215 return std::nullopt;
216}
217
218bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) {
219 LOG(DEBUG) << __func__ << dump(xml);
220 const char* typeStr = xml.Attribute("type");
221 auto aidlType = stringToProcessingType(typeTag, typeStr);
222 RETURN_VALUE_IF(!aidlType.has_value(), false, "illegalStreamType");
223 RETURN_VALUE_IF(0 != mProcessingMap.count(aidlType.value()), false, "duplicateStreamType");
224
225 for (auto& apply : getChildren(xml, "apply")) {
226 const char* name = apply.get().Attribute("effect");
227 if (mEffectsMap.find(name) == mEffectsMap.end()) {
228 LOG(ERROR) << __func__ << " effect " << name << " doesn't exist, skipping";
229 continue;
230 }
231 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
232 mProcessingMap[aidlType.value()].emplace_back(mEffectsMap[name]);
233 LOG(WARNING) << __func__ << " " << typeStr << " : " << name;
234 }
235 return true;
236}
237
238const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>&
239EffectConfig::getProcessingMap() const {
240 return mProcessingMap;
241}
242
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000243bool EffectConfig::findUuid(const std::string& xmlEffectName, AudioUuid* uuid) {
244// Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type
245#define EFFECT_XML_TYPE_LIST_DEF(V) \
246 V("acoustic_echo_canceler", AcousticEchoCanceler) \
247 V("automatic_gain_control_v1", AutomaticGainControlV1) \
248 V("automatic_gain_control_v2", AutomaticGainControlV2) \
249 V("bassboost", BassBoost) \
250 V("downmix", Downmix) \
251 V("dynamics_processing", DynamicsProcessing) \
252 V("equalizer", Equalizer) \
253 V("haptic_generator", HapticGenerator) \
254 V("loudness_enhancer", LoudnessEnhancer) \
255 V("env_reverb", EnvReverb) \
256 V("reverb_env_aux", EnvReverb) \
257 V("reverb_env_ins", EnvReverb) \
258 V("preset_reverb", PresetReverb) \
259 V("reverb_pre_aux", PresetReverb) \
260 V("reverb_pre_ins", PresetReverb) \
261 V("noise_suppression", NoiseSuppression) \
262 V("spatializer", Spatializer) \
263 V("virtualizer", Virtualizer) \
264 V("visualizer", Visualizer) \
265 V("volume", Volume)
266
267#define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol},
268
269 typedef const AudioUuid& (*UuidGetter)(void);
270 static const std::map<std::string, UuidGetter> uuidMap{
271 // std::make_pair("s", &getEffectTypeUuidExtension)};
272 {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}};
273 if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) {
274 *uuid = (*it->second)();
275 return true;
276 }
277 return false;
278}
279
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000280const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
281 tinyxml2::XMLPrinter&& printer) const {
282 element.Accept(&printer);
283 return printer.CStr();
284}
285
286} // namespace aidl::android::hardware::audio::effect