Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 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 | |
Shunkai Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 17 | #include <optional> |
| 18 | #include <string> |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 19 | #define LOG_TAG "AHAL_EffectConfig" |
| 20 | #include <android-base/logging.h> |
Shunkai Yao | beef909 | 2023-06-29 18:18:20 +0000 | [diff] [blame] | 21 | #include <system/audio_aidl_utils.h> |
Shunkai Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 22 | #include <system/audio_effects/audio_effects_conf.h> |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 23 | #include <system/audio_effects/effect_uuid.h> |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 24 | |
| 25 | #include "effectFactory-impl/EffectConfig.h" |
| 26 | |
Shunkai Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 27 | using aidl::android::media::audio::common::AudioSource; |
| 28 | using aidl::android::media::audio::common::AudioStreamType; |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 29 | using aidl::android::media::audio::common::AudioUuid; |
| 30 | |
| 31 | namespace aidl::android::hardware::audio::effect { |
| 32 | |
| 33 | EffectConfig::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 Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 64 | // AudioSource |
| 65 | registerFailure(parseProcessing(Processing::Type::source, xmlStream)); |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | // Parse post processing chains |
| 70 | for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) { |
| 71 | for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) { |
Shunkai Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 72 | // AudioStreamType |
| 73 | registerFailure(parseProcessing(Processing::Type::streamType, xmlStream)); |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements |
| 78 | << " element(s)"; |
| 79 | } |
| 80 | |
| 81 | std::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 Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 91 | bool 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 Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 102 | bool 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 Yao | 52ba4dc | 2023-02-01 21:57:20 +0000 | [diff] [blame] | 108 | 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 Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) { |
| 119 | struct EffectLibraries effectLibraries; |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 120 | std::vector<Library> libraries; |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 121 | std::string name = xml.Attribute("name"); |
| 122 | RETURN_VALUE_IF(name == "", false, "effectsNoName"); |
| 123 | |
| 124 | LOG(DEBUG) << __func__ << dump(xml); |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 125 | struct Library library; |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 126 | if (std::strcmp(xml.Name(), "effectProxy") == 0) { |
| 127 | // proxy lib and uuid |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 128 | RETURN_VALUE_IF(!parseLibrary(xml, library, true), false, "parseProxyLibFailed"); |
| 129 | effectLibraries.proxyLibrary = library; |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 130 | // proxy effect libs and UUID |
| 131 | auto xmlProxyLib = xml.FirstChildElement(); |
| 132 | RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy"); |
| 133 | while (xmlProxyLib) { |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 134 | struct Library tempLibrary; |
| 135 | RETURN_VALUE_IF(!parseLibrary(*xmlProxyLib, tempLibrary), false, |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 136 | "parseEffectLibFailed"); |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 137 | libraries.push_back(std::move(tempLibrary)); |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 138 | xmlProxyLib = xmlProxyLib->NextSiblingElement(); |
| 139 | } |
| 140 | } else { |
| 141 | // expect only one library if not proxy |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 142 | RETURN_VALUE_IF(!parseLibrary(xml, library), false, "parseEffectLibFailed"); |
| 143 | libraries.push_back(std::move(library)); |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 146 | effectLibraries.libraries = std::move(libraries); |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 147 | mEffectsMap[name] = std::move(effectLibraries); |
| 148 | return true; |
| 149 | } |
| 150 | |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 151 | bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml, struct Library& library, |
| 152 | bool isProxy) { |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 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"); |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 157 | library.name = name; |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 160 | const char* uuidStr = xml.Attribute("uuid"); |
| 161 | RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute"); |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 162 | library.uuid = stringToUuid(uuidStr); |
| 163 | if (const char* typeUuidStr = xml.Attribute("type")) { |
| 164 | library.type = stringToUuid(typeUuidStr); |
| 165 | } |
| 166 | RETURN_VALUE_IF((library.uuid == getEffectUuidZero()), false, "invalidUuidAttribute"); |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 167 | |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 168 | LOG(DEBUG) << __func__ << (isProxy ? " proxy " : library.name) << " : uuid " |
| 169 | << ::android::audio::utils::toString(library.uuid) |
| 170 | << (library.type.has_value() |
| 171 | ? ::android::audio::utils::toString(library.type.value()) |
| 172 | : ""); |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
Shunkai Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 176 | std::optional<Processing::Type> EffectConfig::stringToProcessingType(Processing::Type::Tag typeTag, |
| 177 | const std::string& type) { |
| 178 | // see list of audio stream types in audio_stream_type_t: |
| 179 | // system/media/audio/include/system/audio_effects/audio_effects_conf.h |
| 180 | // AUDIO_STREAM_DEFAULT_TAG is not listed here because according to SYS_RESERVED_DEFAULT in |
| 181 | // AudioStreamType.aidl: "Value reserved for system use only. HALs must never return this value |
| 182 | // to the system or accept it from the system". |
| 183 | static const std::map<const std::string, AudioStreamType> sAudioStreamTypeTable = { |
| 184 | {AUDIO_STREAM_VOICE_CALL_TAG, AudioStreamType::VOICE_CALL}, |
| 185 | {AUDIO_STREAM_SYSTEM_TAG, AudioStreamType::SYSTEM}, |
| 186 | {AUDIO_STREAM_RING_TAG, AudioStreamType::RING}, |
| 187 | {AUDIO_STREAM_MUSIC_TAG, AudioStreamType::MUSIC}, |
| 188 | {AUDIO_STREAM_ALARM_TAG, AudioStreamType::ALARM}, |
| 189 | {AUDIO_STREAM_NOTIFICATION_TAG, AudioStreamType::NOTIFICATION}, |
| 190 | {AUDIO_STREAM_BLUETOOTH_SCO_TAG, AudioStreamType::BLUETOOTH_SCO}, |
| 191 | {AUDIO_STREAM_ENFORCED_AUDIBLE_TAG, AudioStreamType::ENFORCED_AUDIBLE}, |
| 192 | {AUDIO_STREAM_DTMF_TAG, AudioStreamType::DTMF}, |
| 193 | {AUDIO_STREAM_TTS_TAG, AudioStreamType::TTS}, |
| 194 | {AUDIO_STREAM_ASSISTANT_TAG, AudioStreamType::ASSISTANT}}; |
| 195 | |
| 196 | // see list of audio sources in audio_source_t: |
| 197 | // system/media/audio/include/system/audio_effects/audio_effects_conf.h |
| 198 | static const std::map<const std::string, AudioSource> sAudioSourceTable = { |
Shunkai Yao | c105694 | 2023-08-23 16:53:57 +0000 | [diff] [blame] | 199 | {MIC_SRC_TAG, AudioSource::MIC}, |
| 200 | {VOICE_UL_SRC_TAG, AudioSource::VOICE_UPLINK}, |
| 201 | {VOICE_DL_SRC_TAG, AudioSource::VOICE_DOWNLINK}, |
Shunkai Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 202 | {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL}, |
Shunkai Yao | c105694 | 2023-08-23 16:53:57 +0000 | [diff] [blame] | 203 | {CAMCORDER_SRC_TAG, AudioSource::CAMCORDER}, |
| 204 | {VOICE_REC_SRC_TAG, AudioSource::VOICE_RECOGNITION}, |
| 205 | {VOICE_COMM_SRC_TAG, AudioSource::VOICE_COMMUNICATION}, |
| 206 | {REMOTE_SUBMIX_SRC_TAG, AudioSource::REMOTE_SUBMIX}, |
| 207 | {UNPROCESSED_SRC_TAG, AudioSource::UNPROCESSED}, |
| 208 | {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_PERFORMANCE}}; |
Shunkai Yao | 5824efb | 2023-05-08 21:16:13 +0000 | [diff] [blame] | 209 | |
| 210 | if (typeTag == Processing::Type::streamType) { |
| 211 | auto typeIter = sAudioStreamTypeTable.find(type); |
| 212 | if (typeIter != sAudioStreamTypeTable.end()) { |
| 213 | return typeIter->second; |
| 214 | } |
| 215 | } else if (typeTag == Processing::Type::source) { |
| 216 | auto typeIter = sAudioSourceTable.find(type); |
| 217 | if (typeIter != sAudioSourceTable.end()) { |
| 218 | return typeIter->second; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return std::nullopt; |
| 223 | } |
| 224 | |
| 225 | bool EffectConfig::parseProcessing(Processing::Type::Tag typeTag, const tinyxml2::XMLElement& xml) { |
| 226 | LOG(DEBUG) << __func__ << dump(xml); |
| 227 | const char* typeStr = xml.Attribute("type"); |
| 228 | auto aidlType = stringToProcessingType(typeTag, typeStr); |
| 229 | RETURN_VALUE_IF(!aidlType.has_value(), false, "illegalStreamType"); |
| 230 | RETURN_VALUE_IF(0 != mProcessingMap.count(aidlType.value()), false, "duplicateStreamType"); |
| 231 | |
| 232 | for (auto& apply : getChildren(xml, "apply")) { |
| 233 | const char* name = apply.get().Attribute("effect"); |
| 234 | if (mEffectsMap.find(name) == mEffectsMap.end()) { |
| 235 | LOG(ERROR) << __func__ << " effect " << name << " doesn't exist, skipping"; |
| 236 | continue; |
| 237 | } |
| 238 | RETURN_VALUE_IF(!name, false, "noEffectAttribute"); |
| 239 | mProcessingMap[aidlType.value()].emplace_back(mEffectsMap[name]); |
| 240 | LOG(WARNING) << __func__ << " " << typeStr << " : " << name; |
| 241 | } |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | const std::map<Processing::Type, std::vector<EffectConfig::EffectLibraries>>& |
| 246 | EffectConfig::getProcessingMap() const { |
| 247 | return mProcessingMap; |
| 248 | } |
| 249 | |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 250 | bool EffectConfig::findUuid(const std::pair<std::string, struct EffectLibraries>& effectElem, |
| 251 | AudioUuid* uuid) { |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 252 | // Difference from EFFECT_TYPE_LIST_DEF, there could be multiple name mapping to same Effect Type |
| 253 | #define EFFECT_XML_TYPE_LIST_DEF(V) \ |
| 254 | V("acoustic_echo_canceler", AcousticEchoCanceler) \ |
| 255 | V("automatic_gain_control_v1", AutomaticGainControlV1) \ |
| 256 | V("automatic_gain_control_v2", AutomaticGainControlV2) \ |
| 257 | V("bassboost", BassBoost) \ |
| 258 | V("downmix", Downmix) \ |
| 259 | V("dynamics_processing", DynamicsProcessing) \ |
| 260 | V("equalizer", Equalizer) \ |
Shunkai Yao | beef909 | 2023-06-29 18:18:20 +0000 | [diff] [blame] | 261 | V("extensioneffect", Extension) \ |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 262 | V("haptic_generator", HapticGenerator) \ |
| 263 | V("loudness_enhancer", LoudnessEnhancer) \ |
| 264 | V("env_reverb", EnvReverb) \ |
| 265 | V("reverb_env_aux", EnvReverb) \ |
| 266 | V("reverb_env_ins", EnvReverb) \ |
| 267 | V("preset_reverb", PresetReverb) \ |
| 268 | V("reverb_pre_aux", PresetReverb) \ |
| 269 | V("reverb_pre_ins", PresetReverb) \ |
| 270 | V("noise_suppression", NoiseSuppression) \ |
| 271 | V("spatializer", Spatializer) \ |
| 272 | V("virtualizer", Virtualizer) \ |
| 273 | V("visualizer", Visualizer) \ |
| 274 | V("volume", Volume) |
| 275 | |
| 276 | #define GENERATE_MAP_ENTRY_V(s, symbol) {s, &getEffectTypeUuid##symbol}, |
| 277 | |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 278 | const std::string xmlEffectName = effectElem.first; |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 279 | typedef const AudioUuid& (*UuidGetter)(void); |
| 280 | static const std::map<std::string, UuidGetter> uuidMap{ |
| 281 | // std::make_pair("s", &getEffectTypeUuidExtension)}; |
| 282 | {EFFECT_XML_TYPE_LIST_DEF(GENERATE_MAP_ENTRY_V)}}; |
| 283 | if (auto it = uuidMap.find(xmlEffectName); it != uuidMap.end()) { |
| 284 | *uuid = (*it->second)(); |
| 285 | return true; |
| 286 | } |
Shunkai Yao | 80e5850 | 2023-07-14 22:10:46 +0000 | [diff] [blame] | 287 | |
| 288 | const auto& libs = effectElem.second.libraries; |
| 289 | for (const auto& lib : libs) { |
| 290 | if (lib.type.has_value()) { |
| 291 | *uuid = lib.type.value(); |
| 292 | return true; |
| 293 | } |
| 294 | } |
Shunkai Yao | f8be1ac | 2023-03-06 18:41:27 +0000 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | |
Shunkai Yao | 52abf0a | 2022-11-08 02:44:03 +0000 | [diff] [blame] | 298 | const char* EffectConfig::dump(const tinyxml2::XMLElement& element, |
| 299 | tinyxml2::XMLPrinter&& printer) const { |
| 300 | element.Accept(&printer); |
| 301 | return printer.CStr(); |
| 302 | } |
| 303 | |
| 304 | } // namespace aidl::android::hardware::audio::effect |