blob: 1cc48978d3f54654de3f8ea3c13f876e0d98db7e [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
Deyao Ren08746262023-12-07 20:08:17 +000027#ifdef __ANDROID_APEX__
28#include <android/apexsupport.h>
29#endif
30
Shunkai Yao5824efb2023-05-08 21:16:13 +000031using aidl::android::media::audio::common::AudioSource;
32using aidl::android::media::audio::common::AudioStreamType;
Shunkai Yao52abf0a2022-11-08 02:44:03 +000033using aidl::android::media::audio::common::AudioUuid;
34
35namespace aidl::android::hardware::audio::effect {
36
37EffectConfig::EffectConfig(const std::string& file) {
38 tinyxml2::XMLDocument doc;
39 doc.LoadFile(file.c_str());
40 LOG(DEBUG) << __func__ << " loading " << file;
41 // parse the xml file into maps
42 if (doc.Error()) {
43 LOG(ERROR) << __func__ << " tinyxml2 failed to load " << file
44 << " error: " << doc.ErrorStr();
45 return;
46 }
47
48 auto registerFailure = [&](bool result) { mSkippedElements += result ? 0 : 1; };
49
50 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
51 // Parse library
52 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
53 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
54 registerFailure(parseLibrary(xmlLibrary));
55 }
56 }
57
58 // Parse effects
59 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
60 for (auto& xmlEffect : getChildren(xmlEffects)) {
61 registerFailure(parseEffect(xmlEffect));
62 }
63 }
64
65 // Parse pre processing chains
66 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
67 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000068 // AudioSource
69 registerFailure(parseProcessing(Processing::Type::source, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000070 }
71 }
72
73 // Parse post processing chains
74 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
75 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000076 // AudioStreamType
77 registerFailure(parseProcessing(Processing::Type::streamType, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000078 }
79 }
80 }
81 LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
82 << " element(s)";
83}
84
85std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> EffectConfig::getChildren(
86 const tinyxml2::XMLNode& node, const char* childTag) {
87 std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> children;
88 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
89 child = child->NextSiblingElement(childTag)) {
90 children.emplace_back(*child);
91 }
92 return children;
93}
94
Shunkai Yao52ba4dc2023-02-01 21:57:20 +000095bool EffectConfig::resolveLibrary(const std::string& path, std::string* resolvedPath) {
Deyao Ren08746262023-12-07 20:08:17 +000096 if (__builtin_available(android AAPEXSUPPORT_API, *)) {
97 AApexInfo *apexInfo;
98 if (AApexInfo_create(&apexInfo) == AAPEXINFO_OK) {
99 std::string apexName(AApexInfo_getName(apexInfo));
100 AApexInfo_destroy(apexInfo);
101 std::string candidatePath("/apex/");
102 candidatePath.append(apexName).append(kEffectLibApexPath).append(path);
103 LOG(DEBUG) << __func__ << " effect lib path " << candidatePath;
104 if (access(candidatePath.c_str(), R_OK) == 0) {
105 *resolvedPath = std::move(candidatePath);
106 return true;
107 }
108 }
109 } else {
110 LOG(DEBUG) << __func__ << " libapexsupport is not supported";
111 }
112
113 // If audio effects libs are not in vendor apex, locate them in kEffectLibPath
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000114 for (auto* libraryDirectory : kEffectLibPath) {
115 std::string candidatePath = std::string(libraryDirectory) + '/' + path;
116 if (access(candidatePath.c_str(), R_OK) == 0) {
117 *resolvedPath = std::move(candidatePath);
118 return true;
119 }
120 }
121 return false;
122}
123
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000124bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
125 const char* name = xml.Attribute("name");
126 RETURN_VALUE_IF(!name, false, "noNameAttribute");
127 const char* path = xml.Attribute("path");
128 RETURN_VALUE_IF(!path, false, "noPathAttribute");
129
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000130 std::string resolvedPath;
131 if (!resolveLibrary(path, &resolvedPath)) {
132 LOG(ERROR) << __func__ << " can't find " << path;
133 return false;
134 }
135 mLibraryMap[name] = resolvedPath;
136 LOG(DEBUG) << __func__ << " " << name << " : " << resolvedPath;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000137 return true;
138}
139
140bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
141 struct EffectLibraries effectLibraries;
Shunkai Yao80e58502023-07-14 22:10:46 +0000142 std::vector<Library> libraries;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000143 std::string name = xml.Attribute("name");
144 RETURN_VALUE_IF(name == "", false, "effectsNoName");
145
146 LOG(DEBUG) << __func__ << dump(xml);
Shunkai Yao80e58502023-07-14 22:10:46 +0000147 struct Library library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000148 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
149 // proxy lib and uuid
Shunkai Yao80e58502023-07-14 22:10:46 +0000150 RETURN_VALUE_IF(!parseLibrary(xml, library, true), false, "parseProxyLibFailed");
151 effectLibraries.proxyLibrary = library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000152 // proxy effect libs and UUID
153 auto xmlProxyLib = xml.FirstChildElement();
154 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
155 while (xmlProxyLib) {
Shunkai Yao80e58502023-07-14 22:10:46 +0000156 struct Library tempLibrary;
157 RETURN_VALUE_IF(!parseLibrary(*xmlProxyLib, tempLibrary), false,
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000158 "parseEffectLibFailed");
Shunkai Yao80e58502023-07-14 22:10:46 +0000159 libraries.push_back(std::move(tempLibrary));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000160 xmlProxyLib = xmlProxyLib->NextSiblingElement();
161 }
162 } else {
163 // expect only one library if not proxy
Shunkai Yao80e58502023-07-14 22:10:46 +0000164 RETURN_VALUE_IF(!parseLibrary(xml, library), false, "parseEffectLibFailed");
165 libraries.push_back(std::move(library));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000166 }
167
Shunkai Yao80e58502023-07-14 22:10:46 +0000168 effectLibraries.libraries = std::move(libraries);
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000169 mEffectsMap[name] = std::move(effectLibraries);
170 return true;
171}
172
Shunkai Yao80e58502023-07-14 22:10:46 +0000173bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml, struct Library& library,
174 bool isProxy) {
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000175 // Retrieve library name only if not effectProxy element
176 if (!isProxy) {
177 const char* name = xml.Attribute("library");
178 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000179 library.name = name;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000180 }
181
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000182 const char* uuidStr = xml.Attribute("uuid");
183 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000184 library.uuid = stringToUuid(uuidStr);
185 if (const char* typeUuidStr = xml.Attribute("type")) {
186 library.type = stringToUuid(typeUuidStr);
187 }
188 RETURN_VALUE_IF((library.uuid == getEffectUuidZero()), false, "invalidUuidAttribute");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000189
Shunkai Yao80e58502023-07-14 22:10:46 +0000190 LOG(DEBUG) << __func__ << (isProxy ? " proxy " : library.name) << " : uuid "
191 << ::android::audio::utils::toString(library.uuid)
192 << (library.type.has_value()
193 ? ::android::audio::utils::toString(library.type.value())
194 : "");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000195 return true;
196}
197
Shunkai Yao5824efb2023-05-08 21:16:13 +0000198std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag,
199 const std::string& type) {
200 // see list of audio stream types in audio_stream_type_t:
201 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
202 // AUDIO_STREAM_DEFAULT_TAG is not listed here because according to SYS_RESERVED_DEFAULT in
203 // AudioStreamType.aidl: "Value reserved for system use only. HALs must never return this value
204 // to the system or accept it from the system".
205 static const std::map<const std::string, AudioStreamType> sAudioStreamTypeTable = {
206 {AUDIO_STREAM_VOICE_CALL_TAG, AudioStreamType::VOICE_CALL},
207 {AUDIO_STREAM_SYSTEM_TAG, AudioStreamType::SYSTEM},
208 {AUDIO_STREAM_RING_TAG, AudioStreamType::RING},
209 {AUDIO_STREAM_MUSIC_TAG, AudioStreamType::MUSIC},
210 {AUDIO_STREAM_ALARM_TAG, AudioStreamType::ALARM},
211 {AUDIO_STREAM_NOTIFICATION_TAG, AudioStreamType::NOTIFICATION},
212 {AUDIO_STREAM_BLUETOOTH_SCO_TAG, AudioStreamType::BLUETOOTH_SCO},
213 {AUDIO_STREAM_ENFORCED_AUDIBLE_TAG, AudioStreamType::ENFORCED_AUDIBLE},
214 {AUDIO_STREAM_DTMF_TAG, AudioStreamType::DTMF},
215 {AUDIO_STREAM_TTS_TAG, AudioStreamType::TTS},
216 {AUDIO_STREAM_ASSISTANT_TAG, AudioStreamType::ASSISTANT}};
217
218 // see list of audio sources in audio_source_t:
219 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
220 static const std::map<const std::string, AudioSource> sAudioSourceTable = {
Shunkai Yaoc1056942023-08-23 16:53:57 +0000221 {MIC_SRC_TAG, AudioSource::MIC},
222 {VOICE_UL_SRC_TAG, AudioSource::VOICE_UPLINK},
223 {VOICE_DL_SRC_TAG, AudioSource::VOICE_DOWNLINK},
Shunkai Yao5824efb2023-05-08 21:16:13 +0000224 {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
Shunkai Yaoc1056942023-08-23 16:53:57 +0000225 {CAMCORDER_SRC_TAG, AudioSource::CAMCORDER},
226 {VOICE_REC_SRC_TAG, AudioSource::VOICE_RECOGNITION},
227 {VOICE_COMM_SRC_TAG, AudioSource::VOICE_COMMUNICATION},
228 {REMOTE_SUBMIX_SRC_TAG, AudioSource::REMOTE_SUBMIX},
229 {UNPROCESSED_SRC_TAG, AudioSource::UNPROCESSED},
230 {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_PERFORMANCE}};
Shunkai Yao5824efb2023-05-08 21:16:13 +0000231
232 if (typeTag == Processing::Type::streamType) {
233 auto typeIter = sAudioStreamTypeTable.find(type);
234 if (typeIter != sAudioStreamTypeTable.end()) {
235 return typeIter->second;
236 }
237 } else if (typeTag == Processing::Type::source) {
238 auto typeIter = sAudioSourceTable.find(type);
239 if (typeIter != sAudioSourceTable.end()) {
240 return typeIter->second;
241 }
242 }
243
244 return std::nullopt;
245}
246
247bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) {
248 LOG(DEBUG) << __func__ << dump(xml);
249 const char* typeStr = xml.Attribute("type");
250 auto aidlType = stringToProcessingType(typeTag, typeStr);
251 RETURN_VALUE_IF(!aidlType.has_value(), false, "illegalStreamType");
252 RETURN_VALUE_IF(0 != mProcessingMap.count(aidlType.value()), false, "duplicateStreamType");
253
254 for (auto& apply : getChildren(xml, "apply")) {
255 const char* name = apply.get().Attribute("effect");
256 if (mEffectsMap.find(name) == mEffectsMap.end()) {
257 LOG(ERROR) << __func__ << " effect " << name << " doesn't exist, skipping";
258 continue;
259 }
260 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
261 mProcessingMap[aidlType.value()].emplace_back(mEffectsMap[name]);
262 LOG(WARNING) << __func__ << " " << typeStr << " : " << name;
263 }
264 return true;
265}
266
267const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>&
268EffectConfig::getProcessingMap() const {
269 return mProcessingMap;
270}
271
Shunkai Yao80e58502023-07-14 22:10:46 +0000272bool EffectConfig::findUuid(const std::pair<std::string, struct EffectLibraries>& effectElem,
273 AudioUuid* uuid) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000274// Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type
275#define EFFECT_XML_TYPE_LIST_DEF(V) \
276 V("acoustic_echo_canceler", AcousticEchoCanceler) \
277 V("automatic_gain_control_v1", AutomaticGainControlV1) \
278 V("automatic_gain_control_v2", AutomaticGainControlV2) \
279 V("bassboost", BassBoost) \
280 V("downmix", Downmix) \
281 V("dynamics_processing", DynamicsProcessing) \
282 V("equalizer", Equalizer) \
Shunkai Yaobeef9092023-06-29 18:18:20 +0000283 V("extensioneffect", Extension) \
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000284 V("haptic_generator", HapticGenerator) \
285 V("loudness_enhancer", LoudnessEnhancer) \
286 V("env_reverb", EnvReverb) \
287 V("reverb_env_aux", EnvReverb) \
288 V("reverb_env_ins", EnvReverb) \
289 V("preset_reverb", PresetReverb) \
290 V("reverb_pre_aux", PresetReverb) \
291 V("reverb_pre_ins", PresetReverb) \
292 V("noise_suppression", NoiseSuppression) \
293 V("spatializer", Spatializer) \
294 V("virtualizer", Virtualizer) \
295 V("visualizer", Visualizer) \
296 V("volume", Volume)
297
298#define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol},
299
Shunkai Yao80e58502023-07-14 22:10:46 +0000300 const std::string xmlEffectName = effectElem.first;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000301 typedef const AudioUuid& (*UuidGetter)(void);
302 static const std::map<std::string, UuidGetter> uuidMap{
303 // std::make_pair("s", &getEffectTypeUuidExtension)};
304 {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}};
305 if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) {
306 *uuid = (*it->second)();
307 return true;
308 }
Shunkai Yao80e58502023-07-14 22:10:46 +0000309
310 const auto& libs = effectElem.second.libraries;
311 for (const auto& lib : libs) {
312 if (lib.type.has_value()) {
313 *uuid = lib.type.value();
314 return true;
315 }
316 }
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000317 return false;
318}
319
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000320const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
321 tinyxml2::XMLPrinter&& printer) const {
322 element.Accept(&printer);
323 return printer.CStr();
324}
325
326} // namespace aidl::android::hardware::audio::effect