blob: fa12056f557dfe5e52c460782f756eaea734e97d [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>
François Gaffieed095e62024-05-30 13:50:50 +020021#include <media/AidlConversionCppNdk.h>
22#include <system/audio.h>
Shunkai Yaobeef9092023-06-29 18:18:20 +000023#include <system/audio_aidl_utils.h>
Shunkai Yao5824efb2023-05-08 21:16:13 +000024#include <system/audio_effects/audio_effects_conf.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000025#include <system/audio_effects/effect_uuid.h>
Shunkai Yao52abf0a2022-11-08 02:44:03 +000026
27#include "effectFactory-impl/EffectConfig.h"
28
Deyao Ren08746262023-12-07 20:08:17 +000029#ifdef __ANDROID_APEX__
30#include <android/apexsupport.h>
31#endif
32
François Gaffieed095e62024-05-30 13:50:50 +020033using aidl::android::media::audio::common::AudioDevice;
34using aidl::android::media::audio::common::AudioDeviceAddress;
35using aidl::android::media::audio::common::AudioDeviceDescription;
36using aidl::android::media::audio::common::AudioDeviceType;
Shunkai Yao5824efb2023-05-08 21:16:13 +000037using aidl::android::media::audio::common::AudioSource;
38using aidl::android::media::audio::common::AudioStreamType;
Shunkai Yao52abf0a2022-11-08 02:44:03 +000039using aidl::android::media::audio::common::AudioUuid;
40
41namespace aidl::android::hardware::audio::effect {
42
43EffectConfig::EffectConfig(const std::string& file) {
44 tinyxml2::XMLDocument doc;
45 doc.LoadFile(file.c_str());
Shunkai Yao52abf0a2022-11-08 02:44:03 +000046 // parse the xml file into maps
47 if (doc.Error()) {
48 LOG(ERROR) << __func__ << " tinyxml2 failed to load " << file
49 << " error: " << doc.ErrorStr();
50 return;
51 }
52
53 auto registerFailure = [&](bool result) { mSkippedElements += result ? 0 : 1; };
54
55 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
56 // Parse library
57 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
58 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
59 registerFailure(parseLibrary(xmlLibrary));
60 }
61 }
62
63 // Parse effects
64 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
65 for (auto& xmlEffect : getChildren(xmlEffects)) {
66 registerFailure(parseEffect(xmlEffect));
67 }
68 }
69
70 // Parse pre processing chains
71 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
72 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000073 // AudioSource
74 registerFailure(parseProcessing(Processing::Type::source, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000075 }
76 }
77
78 // Parse post processing chains
79 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
80 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
Shunkai Yao5824efb2023-05-08 21:16:13 +000081 // AudioStreamType
82 registerFailure(parseProcessing(Processing::Type::streamType, xmlStream));
Shunkai Yao52abf0a2022-11-08 02:44:03 +000083 }
84 }
François Gaffieed095e62024-05-30 13:50:50 +020085
86 // Parse device effect chains
87 for (auto& xmlDeviceEffects : getChildren(xmlConfig, "deviceEffects")) {
88 for (auto& xmlDevice : getChildren(xmlDeviceEffects, "device")) {
89 // AudioDevice
90 registerFailure(parseProcessing(Processing::Type::device, xmlDevice));
91 }
92 }
Shunkai Yao52abf0a2022-11-08 02:44:03 +000093 }
94 LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
95 << " element(s)";
96}
97
98std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> EffectConfig::getChildren(
99 const tinyxml2::XMLNode& node, const char* childTag) {
100 std::vector<std::reference_wrapper<const tinyxml2::XMLElement>> children;
101 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
102 child = child->NextSiblingElement(childTag)) {
103 children.emplace_back(*child);
104 }
105 return children;
106}
107
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000108bool EffectConfig::resolveLibrary(const std::string& path, std::string* resolvedPath) {
Shunkai Yao6190a962024-12-11 19:19:39 +0000109#ifdef __ANDROID_APEX__
Jooyung Han23e5bf22024-02-01 12:49:35 +0900110 if constexpr (__ANDROID_VENDOR_API__ >= 202404) {
Deyao Ren08746262023-12-07 20:08:17 +0000111 AApexInfo *apexInfo;
112 if (AApexInfo_create(&apexInfo) == AAPEXINFO_OK) {
113 std::string apexName(AApexInfo_getName(apexInfo));
114 AApexInfo_destroy(apexInfo);
115 std::string candidatePath("/apex/");
116 candidatePath.append(apexName).append(kEffectLibApexPath).append(path);
117 LOG(DEBUG) << __func__ << " effect lib path " << candidatePath;
118 if (access(candidatePath.c_str(), R_OK) == 0) {
119 *resolvedPath = std::move(candidatePath);
120 return true;
121 }
122 }
123 } else {
124 LOG(DEBUG) << __func__ << " libapexsupport is not supported";
125 }
Shunkai Yao6190a962024-12-11 19:19:39 +0000126#endif
Deyao Ren08746262023-12-07 20:08:17 +0000127
128 // If audio effects libs are not in vendor apex, locate them in kEffectLibPath
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000129 for (auto* libraryDirectory : kEffectLibPath) {
130 std::string candidatePath = std::string(libraryDirectory) + '/' + path;
131 if (access(candidatePath.c_str(), R_OK) == 0) {
132 *resolvedPath = std::move(candidatePath);
133 return true;
134 }
135 }
136 return false;
137}
138
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000139bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
140 const char* name = xml.Attribute("name");
141 RETURN_VALUE_IF(!name, false, "noNameAttribute");
142 const char* path = xml.Attribute("path");
143 RETURN_VALUE_IF(!path, false, "noPathAttribute");
144
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000145 std::string resolvedPath;
146 if (!resolveLibrary(path, &resolvedPath)) {
147 LOG(ERROR) << __func__ << " can't find " << path;
148 return false;
149 }
150 mLibraryMap[name] = resolvedPath;
151 LOG(DEBUG) << __func__ << " " << name << " : " << resolvedPath;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000152 return true;
153}
154
155bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
156 struct EffectLibraries effectLibraries;
Shunkai Yao80e58502023-07-14 22:10:46 +0000157 std::vector<Library> libraries;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000158 std::string name = xml.Attribute("name");
159 RETURN_VALUE_IF(name == "", false, "effectsNoName");
160
Shunkai Yaobb35eff2024-03-14 22:04:02 +0000161 LOG(VERBOSE) << __func__ << dump(xml);
Shunkai Yao80e58502023-07-14 22:10:46 +0000162 struct Library library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000163 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
164 // proxy lib and uuid
Shunkai Yao80e58502023-07-14 22:10:46 +0000165 RETURN_VALUE_IF(!parseLibrary(xml, library, true), false, "parseProxyLibFailed");
166 effectLibraries.proxyLibrary = library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000167 // proxy effect libs and UUID
168 auto xmlProxyLib = xml.FirstChildElement();
169 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
170 while (xmlProxyLib) {
Shunkai Yao80e58502023-07-14 22:10:46 +0000171 struct Library tempLibrary;
172 RETURN_VALUE_IF(!parseLibrary(*xmlProxyLib, tempLibrary), false,
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000173 "parseEffectLibFailed");
Shunkai Yao80e58502023-07-14 22:10:46 +0000174 libraries.push_back(std::move(tempLibrary));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000175 xmlProxyLib = xmlProxyLib->NextSiblingElement();
176 }
177 } else {
178 // expect only one library if not proxy
Shunkai Yao80e58502023-07-14 22:10:46 +0000179 RETURN_VALUE_IF(!parseLibrary(xml, library), false, "parseEffectLibFailed");
180 libraries.push_back(std::move(library));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000181 }
182
Shunkai Yao80e58502023-07-14 22:10:46 +0000183 effectLibraries.libraries = std::move(libraries);
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000184 mEffectsMap[name] = std::move(effectLibraries);
185 return true;
186}
187
Shunkai Yao80e58502023-07-14 22:10:46 +0000188bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml, struct Library& library,
189 bool isProxy) {
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000190 // Retrieve library name only if not effectProxy element
191 if (!isProxy) {
192 const char* name = xml.Attribute("library");
193 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000194 library.name = name;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000195 }
196
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000197 const char* uuidStr = xml.Attribute("uuid");
198 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000199 library.uuid = stringToUuid(uuidStr);
200 if (const char* typeUuidStr = xml.Attribute("type")) {
201 library.type = stringToUuid(typeUuidStr);
202 }
203 RETURN_VALUE_IF((library.uuid == getEffectUuidZero()), false, "invalidUuidAttribute");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000204
Shunkai Yaobb35eff2024-03-14 22:04:02 +0000205 LOG(VERBOSE) << __func__ << (isProxy ? " proxy " : library.name) << " : uuid "
206 << ::android::audio::utils::toString(library.uuid)
207 << (library.type.has_value()
208 ? ::android::audio::utils::toString(library.type.value())
209 : "");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000210 return true;
211}
212
Shunkai Yao5824efb2023-05-08 21:16:13 +0000213std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag,
François Gaffieed095e62024-05-30 13:50:50 +0200214 const std::string& type,
215 const std::string& address) {
Shunkai Yao5824efb2023-05-08 21:16:13 +0000216 // see list of audio stream types in audio_stream_type_t:
217 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
218 // AUDIO_STREAM_DEFAULT_TAG is not listed here because according to SYS_RESERVED_DEFAULT in
219 // AudioStreamType.aidl: "Value reserved for system use only. HALs must never return this value
220 // to the system or accept it from the system".
221 static const std::map<const std::string, AudioStreamType> sAudioStreamTypeTable = {
222 {AUDIO_STREAM_VOICE_CALL_TAG, AudioStreamType::VOICE_CALL},
223 {AUDIO_STREAM_SYSTEM_TAG, AudioStreamType::SYSTEM},
224 {AUDIO_STREAM_RING_TAG, AudioStreamType::RING},
225 {AUDIO_STREAM_MUSIC_TAG, AudioStreamType::MUSIC},
226 {AUDIO_STREAM_ALARM_TAG, AudioStreamType::ALARM},
227 {AUDIO_STREAM_NOTIFICATION_TAG, AudioStreamType::NOTIFICATION},
228 {AUDIO_STREAM_BLUETOOTH_SCO_TAG, AudioStreamType::BLUETOOTH_SCO},
229 {AUDIO_STREAM_ENFORCED_AUDIBLE_TAG, AudioStreamType::ENFORCED_AUDIBLE},
230 {AUDIO_STREAM_DTMF_TAG, AudioStreamType::DTMF},
231 {AUDIO_STREAM_TTS_TAG, AudioStreamType::TTS},
232 {AUDIO_STREAM_ASSISTANT_TAG, AudioStreamType::ASSISTANT}};
233
234 // see list of audio sources in audio_source_t:
235 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
236 static const std::map<const std::string, AudioSource> sAudioSourceTable = {
Shunkai Yaoc1056942023-08-23 16:53:57 +0000237 {MIC_SRC_TAG, AudioSource::MIC},
238 {VOICE_UL_SRC_TAG, AudioSource::VOICE_UPLINK},
239 {VOICE_DL_SRC_TAG, AudioSource::VOICE_DOWNLINK},
Shunkai Yao5824efb2023-05-08 21:16:13 +0000240 {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
Shunkai Yaoc1056942023-08-23 16:53:57 +0000241 {CAMCORDER_SRC_TAG, AudioSource::CAMCORDER},
242 {VOICE_REC_SRC_TAG, AudioSource::VOICE_RECOGNITION},
243 {VOICE_COMM_SRC_TAG, AudioSource::VOICE_COMMUNICATION},
244 {REMOTE_SUBMIX_SRC_TAG, AudioSource::REMOTE_SUBMIX},
245 {UNPROCESSED_SRC_TAG, AudioSource::UNPROCESSED},
246 {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_PERFORMANCE}};
Shunkai Yao5824efb2023-05-08 21:16:13 +0000247
248 if (typeTag == Processing::Type::streamType) {
249 auto typeIter = sAudioStreamTypeTable.find(type);
250 if (typeIter != sAudioStreamTypeTable.end()) {
251 return typeIter->second;
252 }
253 } else if (typeTag == Processing::Type::source) {
254 auto typeIter = sAudioSourceTable.find(type);
255 if (typeIter != sAudioSourceTable.end()) {
256 return typeIter->second;
257 }
François Gaffieed095e62024-05-30 13:50:50 +0200258 } else if (typeTag == Processing::Type::device) {
259 audio_devices_t deviceType;
260 if (!audio_device_from_string(type.c_str(), &deviceType)) {
261 LOG(ERROR) << __func__ << "DeviceEffect: invalid type " << type;
262 return std::nullopt;
263 }
264 auto ret = ::aidl::android::legacy2aidl_audio_device_AudioDevice(deviceType, address);
265 if (!ret.ok()) {
266 LOG(ERROR) << __func__ << "DeviceEffect: Failed to get AudioDevice from type "
267 << deviceType << ", address " << address;
268 return std::nullopt;
269 }
270 return ret.value();
Shunkai Yao5824efb2023-05-08 21:16:13 +0000271 }
272
273 return std::nullopt;
274}
275
276bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) {
Shunkai Yaobb35eff2024-03-14 22:04:02 +0000277 LOG(VERBOSE) << __func__ << dump(xml);
Shunkai Yao5824efb2023-05-08 21:16:13 +0000278 const char* typeStr = xml.Attribute("type");
François Gaffieed095e62024-05-30 13:50:50 +0200279 const char* addressStr = xml.Attribute("address");
280 // For device effect, device address is optional, match will be done for the given device type
281 // with empty address.
282 auto aidlType = stringToProcessingType(typeTag, typeStr, addressStr ? addressStr : "");
Shunkai Yao5824efb2023-05-08 21:16:13 +0000283 RETURN_VALUE_IF(!aidlType.has_value(), false, "illegalStreamType");
284 RETURN_VALUE_IF(0 != mProcessingMap.count(aidlType.value()), false, "duplicateStreamType");
285
286 for (auto& apply : getChildren(xml, "apply")) {
287 const char* name = apply.get().Attribute("effect");
288 if (mEffectsMap.find(name) == mEffectsMap.end()) {
289 LOG(ERROR) << __func__ << " effect " << name << " doesn't exist, skipping";
290 continue;
291 }
292 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
293 mProcessingMap[aidlType.value()].emplace_back(mEffectsMap[name]);
Shunkai Yao5824efb2023-05-08 21:16:13 +0000294 }
295 return true;
296}
297
298const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>&
299EffectConfig::getProcessingMap() const {
300 return mProcessingMap;
301}
302
Shunkai Yao80e58502023-07-14 22:10:46 +0000303bool EffectConfig::findUuid(const std::pair<std::string, struct EffectLibraries>& effectElem,
304 AudioUuid* uuid) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000305// Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type
306#define EFFECT_XML_TYPE_LIST_DEF(V) \
307 V("acoustic_echo_canceler", AcousticEchoCanceler) \
308 V("automatic_gain_control_v1", AutomaticGainControlV1) \
309 V("automatic_gain_control_v2", AutomaticGainControlV2) \
310 V("bassboost", BassBoost) \
311 V("downmix", Downmix) \
312 V("dynamics_processing", DynamicsProcessing) \
313 V("equalizer", Equalizer) \
Shunkai Yaobeef9092023-06-29 18:18:20 +0000314 V("extensioneffect", Extension) \
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000315 V("haptic_generator", HapticGenerator) \
316 V("loudness_enhancer", LoudnessEnhancer) \
317 V("env_reverb", EnvReverb) \
318 V("reverb_env_aux", EnvReverb) \
319 V("reverb_env_ins", EnvReverb) \
320 V("preset_reverb", PresetReverb) \
321 V("reverb_pre_aux", PresetReverb) \
322 V("reverb_pre_ins", PresetReverb) \
323 V("noise_suppression", NoiseSuppression) \
324 V("spatializer", Spatializer) \
325 V("virtualizer", Virtualizer) \
326 V("visualizer", Visualizer) \
327 V("volume", Volume)
328
329#define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol},
330
Shunkai Yao80e58502023-07-14 22:10:46 +0000331 const std::string xmlEffectName = effectElem.first;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000332 typedef const AudioUuid& (*UuidGetter)(void);
333 static const std::map<std::string, UuidGetter> uuidMap{
334 // std::make_pair("s", &getEffectTypeUuidExtension)};
335 {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}};
336 if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) {
337 *uuid = (*it->second)();
338 return true;
339 }
Shunkai Yao80e58502023-07-14 22:10:46 +0000340
341 const auto& libs = effectElem.second.libraries;
342 for (const auto& lib : libs) {
343 if (lib.type.has_value()) {
344 *uuid = lib.type.value();
345 return true;
346 }
347 }
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000348 return false;
349}
350
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000351const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
352 tinyxml2::XMLPrinter&& printer) const {
353 element.Accept(&printer);
354 return printer.CStr();
355}
356
357} // namespace aidl::android::hardware::audio::effect