blob: f3f674f6efa5caf0a190a97b4880498b42981968 [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;
120 std::vector<LibraryUuid> libraryUuids;
121 std::string name = xml.Attribute("name");
122 RETURN_VALUE_IF(name == "", false, "effectsNoName");
123
124 LOG(DEBUG) << __func__ << dump(xml);
125 struct LibraryUuid libraryUuid;
126 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
127 // proxy lib and uuid
128 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid, true), false, "parseProxyLibFailed");
129 effectLibraries.proxyLibrary = libraryUuid;
130 // proxy effect libs and UUID
131 auto xmlProxyLib = xml.FirstChildElement();
132 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
133 while (xmlProxyLib) {
134 struct LibraryUuid tempLibraryUuid;
135 RETURN_VALUE_IF(!parseLibraryUuid(*xmlProxyLib, tempLibraryUuid), false,
136 "parseEffectLibFailed");
137 libraryUuids.push_back(std::move(tempLibraryUuid));
138 xmlProxyLib = xmlProxyLib->NextSiblingElement();
139 }
140 } else {
141 // expect only one library if not proxy
142 RETURN_VALUE_IF(!parseLibraryUuid(xml, libraryUuid), false, "parseEffectLibFailed");
143 libraryUuids.push_back(std::move(libraryUuid));
144 }
145
146 effectLibraries.libraries = std::move(libraryUuids);
147 mEffectsMap[name] = std::move(effectLibraries);
148 return true;
149}
150
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000151bool EffectConfig::parseLibraryUuid(const tinyxml2::XMLElement& xml,
152 struct LibraryUuid& libraryUuid, bool isProxy) {
153 // 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");
157 libraryUuid.name = name;
158 }
159
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000160 const char* uuidStr = xml.Attribute("uuid");
161 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
162 libraryUuid.uuid = stringToUuid(uuidStr);
163 RETURN_VALUE_IF((libraryUuid.uuid == getEffectUuidZero()), false, "invalidUuidAttribute");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000164
165 LOG(DEBUG) << __func__ << (isProxy ? " proxy " : libraryUuid.name) << " : "
Shunkai Yaobeef9092023-06-29 18:18:20 +0000166 << ::android::audio::utils::toString(libraryUuid.uuid);
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000167 return true;
168}
169
Shunkai Yao5824efb2023-05-08 21:16:13 +0000170std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag,
171 const std::string& type) {
172 // see list of audio stream types in audio_stream_type_t:
173 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
174 // AUDIO_STREAM_DEFAULT_TAG is not listed here because according to SYS_RESERVED_DEFAULT in
175 // AudioStreamType.aidl: "Value reserved for system use only. HALs must never return this value
176 // to the system or accept it from the system".
177 static const std::map<const std::string, AudioStreamType> sAudioStreamTypeTable = {
178 {AUDIO_STREAM_VOICE_CALL_TAG, AudioStreamType::VOICE_CALL},
179 {AUDIO_STREAM_SYSTEM_TAG, AudioStreamType::SYSTEM},
180 {AUDIO_STREAM_RING_TAG, AudioStreamType::RING},
181 {AUDIO_STREAM_MUSIC_TAG, AudioStreamType::MUSIC},
182 {AUDIO_STREAM_ALARM_TAG, AudioStreamType::ALARM},
183 {AUDIO_STREAM_NOTIFICATION_TAG, AudioStreamType::NOTIFICATION},
184 {AUDIO_STREAM_BLUETOOTH_SCO_TAG, AudioStreamType::BLUETOOTH_SCO},
185 {AUDIO_STREAM_ENFORCED_AUDIBLE_TAG, AudioStreamType::ENFORCED_AUDIBLE},
186 {AUDIO_STREAM_DTMF_TAG, AudioStreamType::DTMF},
187 {AUDIO_STREAM_TTS_TAG, AudioStreamType::TTS},
188 {AUDIO_STREAM_ASSISTANT_TAG, AudioStreamType::ASSISTANT}};
189
190 // see list of audio sources in audio_source_t:
191 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
192 static const std::map<const std::string, AudioSource> sAudioSourceTable = {
193 {MIC_SRC_TAG, AudioSource::VOICE_CALL},
194 {VOICE_UL_SRC_TAG, AudioSource::VOICE_CALL},
195 {VOICE_DL_SRC_TAG, AudioSource::VOICE_CALL},
196 {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
197 {CAMCORDER_SRC_TAG, AudioSource::VOICE_CALL},
198 {VOICE_REC_SRC_TAG, AudioSource::VOICE_CALL},
199 {VOICE_COMM_SRC_TAG, AudioSource::VOICE_CALL},
200 {REMOTE_SUBMIX_SRC_TAG, AudioSource::VOICE_CALL},
201 {UNPROCESSED_SRC_TAG, AudioSource::VOICE_CALL},
202 {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_CALL}};
203
204 if (typeTag == Processing::Type::streamType) {
205 auto typeIter = sAudioStreamTypeTable.find(type);
206 if (typeIter != sAudioStreamTypeTable.end()) {
207 return typeIter->second;
208 }
209 } else if (typeTag == Processing::Type::source) {
210 auto typeIter = sAudioSourceTable.find(type);
211 if (typeIter != sAudioSourceTable.end()) {
212 return typeIter->second;
213 }
214 }
215
216 return std::nullopt;
217}
218
219bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) {
220 LOG(DEBUG) << __func__ << dump(xml);
221 const char* typeStr = xml.Attribute("type");
222 auto aidlType = stringToProcessingType(typeTag, typeStr);
223 RETURN_VALUE_IF(!aidlType.has_value(), false, "illegalStreamType");
224 RETURN_VALUE_IF(0 != mProcessingMap.count(aidlType.value()), false, "duplicateStreamType");
225
226 for (auto& apply : getChildren(xml, "apply")) {
227 const char* name = apply.get().Attribute("effect");
228 if (mEffectsMap.find(name) == mEffectsMap.end()) {
229 LOG(ERROR) << __func__ << " effect " << name << " doesn't exist, skipping";
230 continue;
231 }
232 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
233 mProcessingMap[aidlType.value()].emplace_back(mEffectsMap[name]);
234 LOG(WARNING) << __func__ << " " << typeStr << " : " << name;
235 }
236 return true;
237}
238
239const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>&
240EffectConfig::getProcessingMap() const {
241 return mProcessingMap;
242}
243
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000244bool EffectConfig::findUuid(const std::string& xmlEffectName, AudioUuid* uuid) {
245// Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type
246#define EFFECT_XML_TYPE_LIST_DEF(V) \
247 V("acoustic_echo_canceler", AcousticEchoCanceler) \
248 V("automatic_gain_control_v1", AutomaticGainControlV1) \
249 V("automatic_gain_control_v2", AutomaticGainControlV2) \
250 V("bassboost", BassBoost) \
251 V("downmix", Downmix) \
252 V("dynamics_processing", DynamicsProcessing) \
253 V("equalizer", Equalizer) \
Shunkai Yaobeef9092023-06-29 18:18:20 +0000254 V("extensioneffect", Extension) \
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000255 V("haptic_generator", HapticGenerator) \
256 V("loudness_enhancer", LoudnessEnhancer) \
257 V("env_reverb", EnvReverb) \
258 V("reverb_env_aux", EnvReverb) \
259 V("reverb_env_ins", EnvReverb) \
260 V("preset_reverb", PresetReverb) \
261 V("reverb_pre_aux", PresetReverb) \
262 V("reverb_pre_ins", PresetReverb) \
263 V("noise_suppression", NoiseSuppression) \
264 V("spatializer", Spatializer) \
265 V("virtualizer", Virtualizer) \
266 V("visualizer", Visualizer) \
267 V("volume", Volume)
268
269#define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol},
270
271 typedef const AudioUuid& (*UuidGetter)(void);
272 static const std::map<std::string, UuidGetter> uuidMap{
273 // std::make_pair("s", &getEffectTypeUuidExtension)};
274 {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}};
275 if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) {
276 *uuid = (*it->second)();
277 return true;
278 }
279 return false;
280}
281
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000282const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
283 tinyxml2::XMLPrinter&& printer) const {
284 element.Accept(&printer);
285 return printer.CStr();
286}
287
288} // namespace aidl::android::hardware::audio::effect