blob: 9c335babac4803acb374b719d522a1a3802e0f44 [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) {
Jooyung Han23e5bf22024-02-01 12:49:35 +0900109 if constexpr (__ANDROID_VENDOR_API__ >= 202404) {
Deyao Ren08746262023-12-07 20:08:17 +0000110 AApexInfo *apexInfo;
111 if (AApexInfo_create(&apexInfo) == AAPEXINFO_OK) {
112 std::string apexName(AApexInfo_getName(apexInfo));
113 AApexInfo_destroy(apexInfo);
114 std::string candidatePath("/apex/");
115 candidatePath.append(apexName).append(kEffectLibApexPath).append(path);
116 LOG(DEBUG) << __func__ << " effect lib path " << candidatePath;
117 if (access(candidatePath.c_str(), R_OK) == 0) {
118 *resolvedPath = std::move(candidatePath);
119 return true;
120 }
121 }
122 } else {
123 LOG(DEBUG) << __func__ << " libapexsupport is not supported";
124 }
125
126 // If audio effects libs are not in vendor apex, locate them in kEffectLibPath
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000127 for (auto* libraryDirectory : kEffectLibPath) {
128 std::string candidatePath = std::string(libraryDirectory) + '/' + path;
129 if (access(candidatePath.c_str(), R_OK) == 0) {
130 *resolvedPath = std::move(candidatePath);
131 return true;
132 }
133 }
134 return false;
135}
136
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000137bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml) {
138 const char* name = xml.Attribute("name");
139 RETURN_VALUE_IF(!name, false, "noNameAttribute");
140 const char* path = xml.Attribute("path");
141 RETURN_VALUE_IF(!path, false, "noPathAttribute");
142
Shunkai Yao52ba4dc2023-02-01 21:57:20 +0000143 std::string resolvedPath;
144 if (!resolveLibrary(path, &resolvedPath)) {
145 LOG(ERROR) << __func__ << " can't find " << path;
146 return false;
147 }
148 mLibraryMap[name] = resolvedPath;
149 LOG(DEBUG) << __func__ << " " << name << " : " << resolvedPath;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000150 return true;
151}
152
153bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
154 struct EffectLibraries effectLibraries;
Shunkai Yao80e58502023-07-14 22:10:46 +0000155 std::vector<Library> libraries;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000156 std::string name = xml.Attribute("name");
157 RETURN_VALUE_IF(name == "", false, "effectsNoName");
158
Shunkai Yaobb35eff2024-03-14 22:04:02 +0000159 LOG(VERBOSE) << __func__ << dump(xml);
Shunkai Yao80e58502023-07-14 22:10:46 +0000160 struct Library library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000161 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
162 // proxy lib and uuid
Shunkai Yao80e58502023-07-14 22:10:46 +0000163 RETURN_VALUE_IF(!parseLibrary(xml, library, true), false, "parseProxyLibFailed");
164 effectLibraries.proxyLibrary = library;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000165 // proxy effect libs and UUID
166 auto xmlProxyLib = xml.FirstChildElement();
167 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
168 while (xmlProxyLib) {
Shunkai Yao80e58502023-07-14 22:10:46 +0000169 struct Library tempLibrary;
170 RETURN_VALUE_IF(!parseLibrary(*xmlProxyLib, tempLibrary), false,
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000171 "parseEffectLibFailed");
Shunkai Yao80e58502023-07-14 22:10:46 +0000172 libraries.push_back(std::move(tempLibrary));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000173 xmlProxyLib = xmlProxyLib->NextSiblingElement();
174 }
175 } else {
176 // expect only one library if not proxy
Shunkai Yao80e58502023-07-14 22:10:46 +0000177 RETURN_VALUE_IF(!parseLibrary(xml, library), false, "parseEffectLibFailed");
178 libraries.push_back(std::move(library));
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000179 }
180
Shunkai Yao80e58502023-07-14 22:10:46 +0000181 effectLibraries.libraries = std::move(libraries);
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000182 mEffectsMap[name] = std::move(effectLibraries);
183 return true;
184}
185
Shunkai Yao80e58502023-07-14 22:10:46 +0000186bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml, struct Library& library,
187 bool isProxy) {
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000188 // Retrieve library name only if not effectProxy element
189 if (!isProxy) {
190 const char* name = xml.Attribute("library");
191 RETURN_VALUE_IF(!name, false, "noLibraryAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000192 library.name = name;
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000193 }
194
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000195 const char* uuidStr = xml.Attribute("uuid");
196 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
Shunkai Yao80e58502023-07-14 22:10:46 +0000197 library.uuid = stringToUuid(uuidStr);
198 if (const char* typeUuidStr = xml.Attribute("type")) {
199 library.type = stringToUuid(typeUuidStr);
200 }
201 RETURN_VALUE_IF((library.uuid == getEffectUuidZero()), false, "invalidUuidAttribute");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000202
Shunkai Yaobb35eff2024-03-14 22:04:02 +0000203 LOG(VERBOSE) << __func__ << (isProxy ? " proxy " : library.name) << " : uuid "
204 << ::android::audio::utils::toString(library.uuid)
205 << (library.type.has_value()
206 ? ::android::audio::utils::toString(library.type.value())
207 : "");
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000208 return true;
209}
210
Shunkai Yao5824efb2023-05-08 21:16:13 +0000211std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag,
François Gaffieed095e62024-05-30 13:50:50 +0200212 const std::string& type,
213 const std::string& address) {
Shunkai Yao5824efb2023-05-08 21:16:13 +0000214 // see list of audio stream types in audio_stream_type_t:
215 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
216 // AUDIO_STREAM_DEFAULT_TAG is not listed here because according to SYS_RESERVED_DEFAULT in
217 // AudioStreamType.aidl: "Value reserved for system use only. HALs must never return this value
218 // to the system or accept it from the system".
219 static const std::map<const std::string, AudioStreamType> sAudioStreamTypeTable = {
220 {AUDIO_STREAM_VOICE_CALL_TAG, AudioStreamType::VOICE_CALL},
221 {AUDIO_STREAM_SYSTEM_TAG, AudioStreamType::SYSTEM},
222 {AUDIO_STREAM_RING_TAG, AudioStreamType::RING},
223 {AUDIO_STREAM_MUSIC_TAG, AudioStreamType::MUSIC},
224 {AUDIO_STREAM_ALARM_TAG, AudioStreamType::ALARM},
225 {AUDIO_STREAM_NOTIFICATION_TAG, AudioStreamType::NOTIFICATION},
226 {AUDIO_STREAM_BLUETOOTH_SCO_TAG, AudioStreamType::BLUETOOTH_SCO},
227 {AUDIO_STREAM_ENFORCED_AUDIBLE_TAG, AudioStreamType::ENFORCED_AUDIBLE},
228 {AUDIO_STREAM_DTMF_TAG, AudioStreamType::DTMF},
229 {AUDIO_STREAM_TTS_TAG, AudioStreamType::TTS},
230 {AUDIO_STREAM_ASSISTANT_TAG, AudioStreamType::ASSISTANT}};
231
232 // see list of audio sources in audio_source_t:
233 // system/media/audio/include/system/audio_effects/audio_effects_conf.h
234 static const std::map<const std::string, AudioSource> sAudioSourceTable = {
Shunkai Yaoc1056942023-08-23 16:53:57 +0000235 {MIC_SRC_TAG, AudioSource::MIC},
236 {VOICE_UL_SRC_TAG, AudioSource::VOICE_UPLINK},
237 {VOICE_DL_SRC_TAG, AudioSource::VOICE_DOWNLINK},
Shunkai Yao5824efb2023-05-08 21:16:13 +0000238 {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
Shunkai Yaoc1056942023-08-23 16:53:57 +0000239 {CAMCORDER_SRC_TAG, AudioSource::CAMCORDER},
240 {VOICE_REC_SRC_TAG, AudioSource::VOICE_RECOGNITION},
241 {VOICE_COMM_SRC_TAG, AudioSource::VOICE_COMMUNICATION},
242 {REMOTE_SUBMIX_SRC_TAG, AudioSource::REMOTE_SUBMIX},
243 {UNPROCESSED_SRC_TAG, AudioSource::UNPROCESSED},
244 {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_PERFORMANCE}};
Shunkai Yao5824efb2023-05-08 21:16:13 +0000245
246 if (typeTag == Processing::Type::streamType) {
247 auto typeIter = sAudioStreamTypeTable.find(type);
248 if (typeIter != sAudioStreamTypeTable.end()) {
249 return typeIter->second;
250 }
251 } else if (typeTag == Processing::Type::source) {
252 auto typeIter = sAudioSourceTable.find(type);
253 if (typeIter != sAudioSourceTable.end()) {
254 return typeIter->second;
255 }
François Gaffieed095e62024-05-30 13:50:50 +0200256 } else if (typeTag == Processing::Type::device) {
257 audio_devices_t deviceType;
258 if (!audio_device_from_string(type.c_str(), &deviceType)) {
259 LOG(ERROR) << __func__ << "DeviceEffect: invalid type " << type;
260 return std::nullopt;
261 }
262 auto ret = ::aidl::android::legacy2aidl_audio_device_AudioDevice(deviceType, address);
263 if (!ret.ok()) {
264 LOG(ERROR) << __func__ << "DeviceEffect: Failed to get AudioDevice from type "
265 << deviceType << ", address " << address;
266 return std::nullopt;
267 }
268 return ret.value();
Shunkai Yao5824efb2023-05-08 21:16:13 +0000269 }
270
271 return std::nullopt;
272}
273
274bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) {
Shunkai Yaobb35eff2024-03-14 22:04:02 +0000275 LOG(VERBOSE) << __func__ << dump(xml);
Shunkai Yao5824efb2023-05-08 21:16:13 +0000276 const char* typeStr = xml.Attribute("type");
François Gaffieed095e62024-05-30 13:50:50 +0200277 const char* addressStr = xml.Attribute("address");
278 // For device effect, device address is optional, match will be done for the given device type
279 // with empty address.
280 auto aidlType = stringToProcessingType(typeTag, typeStr, addressStr ? addressStr : "");
Shunkai Yao5824efb2023-05-08 21:16:13 +0000281 RETURN_VALUE_IF(!aidlType.has_value(), false, "illegalStreamType");
282 RETURN_VALUE_IF(0 != mProcessingMap.count(aidlType.value()), false, "duplicateStreamType");
283
284 for (auto& apply : getChildren(xml, "apply")) {
285 const char* name = apply.get().Attribute("effect");
286 if (mEffectsMap.find(name) == mEffectsMap.end()) {
287 LOG(ERROR) << __func__ << " effect " << name << " doesn't exist, skipping";
288 continue;
289 }
290 RETURN_VALUE_IF(!name, false, "noEffectAttribute");
291 mProcessingMap[aidlType.value()].emplace_back(mEffectsMap[name]);
Shunkai Yao5824efb2023-05-08 21:16:13 +0000292 }
293 return true;
294}
295
296const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>&
297EffectConfig::getProcessingMap() const {
298 return mProcessingMap;
299}
300
Shunkai Yao80e58502023-07-14 22:10:46 +0000301bool EffectConfig::findUuid(const std::pair<std::string, struct EffectLibraries>& effectElem,
302 AudioUuid* uuid) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000303// Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type
304#define EFFECT_XML_TYPE_LIST_DEF(V) \
305 V("acoustic_echo_canceler", AcousticEchoCanceler) \
306 V("automatic_gain_control_v1", AutomaticGainControlV1) \
307 V("automatic_gain_control_v2", AutomaticGainControlV2) \
308 V("bassboost", BassBoost) \
309 V("downmix", Downmix) \
310 V("dynamics_processing", DynamicsProcessing) \
311 V("equalizer", Equalizer) \
Shunkai Yaobeef9092023-06-29 18:18:20 +0000312 V("extensioneffect", Extension) \
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000313 V("haptic_generator", HapticGenerator) \
314 V("loudness_enhancer", LoudnessEnhancer) \
315 V("env_reverb", EnvReverb) \
316 V("reverb_env_aux", EnvReverb) \
317 V("reverb_env_ins", EnvReverb) \
318 V("preset_reverb", PresetReverb) \
319 V("reverb_pre_aux", PresetReverb) \
320 V("reverb_pre_ins", PresetReverb) \
321 V("noise_suppression", NoiseSuppression) \
322 V("spatializer", Spatializer) \
323 V("virtualizer", Virtualizer) \
324 V("visualizer", Visualizer) \
325 V("volume", Volume)
326
327#define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol},
328
Shunkai Yao80e58502023-07-14 22:10:46 +0000329 const std::string xmlEffectName = effectElem.first;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000330 typedef const AudioUuid& (*UuidGetter)(void);
331 static const std::map<std::string, UuidGetter> uuidMap{
332 // std::make_pair("s", &getEffectTypeUuidExtension)};
333 {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}};
334 if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) {
335 *uuid = (*it->second)();
336 return true;
337 }
Shunkai Yao80e58502023-07-14 22:10:46 +0000338
339 const auto& libs = effectElem.second.libraries;
340 for (const auto& lib : libs) {
341 if (lib.type.has_value()) {
342 *uuid = lib.type.value();
343 return true;
344 }
345 }
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000346 return false;
347}
348
Shunkai Yao52abf0a2022-11-08 02:44:03 +0000349const char* EffectConfig::dump(const tinyxml2::XMLElement& element,
350 tinyxml2::XMLPrinter&& printer) const {
351 element.Accept(&printer);
352 return printer.CStr();
353}
354
355} // namespace aidl::android::hardware::audio::effect