1 /*
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
17 #include <optional>
18 #include <string>
19 #define LOG_TAG "AHAL_EffectConfig"
20 #include <android-base/logging.h>
21 #include <media/AidlConversionCppNdk.h>
22 #include <system/audio.h>
23 #include <system/audio_aidl_utils.h>
24 #include <system/audio_effects/audio_effects_conf.h>
25 #include <system/audio_effects/effect_uuid.h>
26
27 #include "effectFactory-impl/EffectConfig.h"
28
29 #ifdef __ANDROID_APEX__
30 #include <android/apexsupport.h>
31 #endif
32
33 using aidl::android::media::audio::common::AudioDevice;
34 using aidl::android::media::audio::common::AudioDeviceAddress;
35 using aidl::android::media::audio::common::AudioDeviceDescription;
36 using aidl::android::media::audio::common::AudioDeviceType;
37 using aidl::android::media::audio::common::AudioSource;
38 using aidl::android::media::audio::common::AudioStreamType;
39 using aidl::android::media::audio::common::AudioUuid;
40
41 namespace aidl::android::hardware::audio::effect {
42
EffectConfig(const std::string & file)43 EffectConfig::EffectConfig(const std::string& file) {
44 tinyxml2::XMLDocument doc;
45 doc.LoadFile(file.c_str());
46 // 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")) {
73 // AudioSource
74 registerFailure(parseProcessing(Processing::Type::source, xmlStream));
75 }
76 }
77
78 // Parse post processing chains
79 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
80 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
81 // AudioStreamType
82 registerFailure(parseProcessing(Processing::Type::streamType, xmlStream));
83 }
84 }
85
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 }
93 }
94 LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
95 << " element(s)";
96 }
97
getChildren(const tinyxml2::XMLNode & node,const char * childTag)98 std::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
resolveLibrary(const std::string & path,std::string * resolvedPath)108 bool EffectConfig::resolveLibrary(const std::string& path, std::string* resolvedPath) {
109 if constexpr (__ANDROID_VENDOR_API__ >= 202404) {
110 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
127 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
parseLibrary(const tinyxml2::XMLElement & xml)137 bool 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
143 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;
150 return true;
151 }
152
parseEffect(const tinyxml2::XMLElement & xml)153 bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
154 struct EffectLibraries effectLibraries;
155 std::vector<Library> libraries;
156 std::string name = xml.Attribute("name");
157 RETURN_VALUE_IF(name == "", false, "effectsNoName");
158
159 LOG(VERBOSE) << __func__ << dump(xml);
160 struct Library library;
161 if (std::strcmp(xml.Name(), "effectProxy") == 0) {
162 // proxy lib and uuid
163 RETURN_VALUE_IF(!parseLibrary(xml, library, true), false, "parseProxyLibFailed");
164 effectLibraries.proxyLibrary = library;
165 // proxy effect libs and UUID
166 auto xmlProxyLib = xml.FirstChildElement();
167 RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
168 while (xmlProxyLib) {
169 struct Library tempLibrary;
170 RETURN_VALUE_IF(!parseLibrary(*xmlProxyLib, tempLibrary), false,
171 "parseEffectLibFailed");
172 libraries.push_back(std::move(tempLibrary));
173 xmlProxyLib = xmlProxyLib->NextSiblingElement();
174 }
175 } else {
176 // expect only one library if not proxy
177 RETURN_VALUE_IF(!parseLibrary(xml, library), false, "parseEffectLibFailed");
178 libraries.push_back(std::move(library));
179 }
180
181 effectLibraries.libraries = std::move(libraries);
182 mEffectsMap[name] = std::move(effectLibraries);
183 return true;
184 }
185
parseLibrary(const tinyxml2::XMLElement & xml,struct Library & library,bool isProxy)186 bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml, struct Library& library,
187 bool isProxy) {
188 // 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");
192 library.name = name;
193 }
194
195 const char* uuidStr = xml.Attribute("uuid");
196 RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
197 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");
202
203 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 : "");
208 return true;
209 }
210
stringToProcessingType(Processing::Type::Tag typeTag,const std::string & type,const std::string & address)211 std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag,
212 const std::string& type,
213 const std::string& address) {
214 // 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 = {
235 {MIC_SRC_TAG, AudioSource::MIC},
236 {VOICE_UL_SRC_TAG, AudioSource::VOICE_UPLINK},
237 {VOICE_DL_SRC_TAG, AudioSource::VOICE_DOWNLINK},
238 {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
239 {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}};
245
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 }
256 } 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();
269 }
270
271 return std::nullopt;
272 }
273
parseProcessing(Processing::Type::Tag typeTag,const tinyxml2::XMLElement & xml)274 bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) {
275 LOG(VERBOSE) << __func__ << dump(xml);
276 const char* typeStr = xml.Attribute("type");
277 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 : "");
281 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]);
292 }
293 return true;
294 }
295
296 const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>&
getProcessingMap() const297 EffectConfig::getProcessingMap() const {
298 return mProcessingMap;
299 }
300
findUuid(const std::pair<std::string,struct EffectLibraries> & effectElem,AudioUuid * uuid)301 bool EffectConfig::findUuid(const std::pair<std::string, struct EffectLibraries>& effectElem,
302 AudioUuid* uuid) {
303 // 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) \
312 V("extensioneffect", Extension) \
313 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
329 const std::string xmlEffectName = effectElem.first;
330 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 }
338
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 }
346 return false;
347 }
348
dump(const tinyxml2::XMLElement & element,tinyxml2::XMLPrinter && printer) const349 const 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
356