• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <system/audio_aidl_utils.h>
22 #include <system/audio_effects/audio_effects_conf.h>
23 #include <system/audio_effects/effect_uuid.h>
24 
25 #include "effectFactory-impl/EffectConfig.h"
26 
27 using aidl::android::media::audio::common::AudioSource;
28 using aidl::android::media::audio::common::AudioStreamType;
29 using aidl::android::media::audio::common::AudioUuid;
30 
31 namespace aidl::android::hardware::audio::effect {
32 
EffectConfig(const std::string & file)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")) {
64                 // AudioSource
65                 registerFailure(parseProcessing(Processing::Type::source, xmlStream));
66             }
67         }
68 
69         // Parse post processing chains
70         for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
71             for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
72                 // AudioStreamType
73                 registerFailure(parseProcessing(Processing::Type::streamType, xmlStream));
74             }
75         }
76     }
77     LOG(DEBUG) << __func__ << " successfully parsed " << file << ", skipping " << mSkippedElements
78                << " element(s)";
79 }
80 
getChildren(const tinyxml2::XMLNode & node,const char * childTag)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 
resolveLibrary(const std::string & path,std::string * resolvedPath)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 
parseLibrary(const tinyxml2::XMLElement & xml)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 
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;
115     return true;
116 }
117 
parseEffect(const tinyxml2::XMLElement & xml)118 bool EffectConfig::parseEffect(const tinyxml2::XMLElement& xml) {
119     struct EffectLibraries effectLibraries;
120     std::vector<Library> libraries;
121     std::string name = xml.Attribute("name");
122     RETURN_VALUE_IF(name == "", false, "effectsNoName");
123 
124     LOG(DEBUG) << __func__ << dump(xml);
125     struct Library library;
126     if (std::strcmp(xml.Name(), "effectProxy") == 0) {
127         // proxy lib and uuid
128         RETURN_VALUE_IF(!parseLibrary(xml, library, true), false, "parseProxyLibFailed");
129         effectLibraries.proxyLibrary = library;
130         // proxy effect libs and UUID
131         auto xmlProxyLib = xml.FirstChildElement();
132         RETURN_VALUE_IF(!xmlProxyLib, false, "noLibForProxy");
133         while (xmlProxyLib) {
134             struct Library tempLibrary;
135             RETURN_VALUE_IF(!parseLibrary(*xmlProxyLib, tempLibrary), false,
136                             "parseEffectLibFailed");
137             libraries.push_back(std::move(tempLibrary));
138             xmlProxyLib = xmlProxyLib->NextSiblingElement();
139         }
140     } else {
141         // expect only one library if not proxy
142         RETURN_VALUE_IF(!parseLibrary(xml, library), false, "parseEffectLibFailed");
143         libraries.push_back(std::move(library));
144     }
145 
146     effectLibraries.libraries = std::move(libraries);
147     mEffectsMap[name] = std::move(effectLibraries);
148     return true;
149 }
150 
parseLibrary(const tinyxml2::XMLElement & xml,struct Library & library,bool isProxy)151 bool EffectConfig::parseLibrary(const tinyxml2::XMLElement& xml, struct Library& library,
152                                 bool isProxy) {
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");
157         library.name = name;
158     }
159 
160     const char* uuidStr = xml.Attribute("uuid");
161     RETURN_VALUE_IF(!uuidStr, false, "noUuidAttribute");
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");
167 
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                            : "");
173     return true;
174 }
175 
stringToProcessingType(Processing::Type::Tag typeTag,const std::string & type)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 = {
199             {MIC_SRC_TAG, AudioSource::VOICE_CALL},
200             {VOICE_UL_SRC_TAG, AudioSource::VOICE_CALL},
201             {VOICE_DL_SRC_TAG, AudioSource::VOICE_CALL},
202             {VOICE_CALL_SRC_TAG, AudioSource::VOICE_CALL},
203             {CAMCORDER_SRC_TAG, AudioSource::VOICE_CALL},
204             {VOICE_REC_SRC_TAG, AudioSource::VOICE_CALL},
205             {VOICE_COMM_SRC_TAG, AudioSource::VOICE_CALL},
206             {REMOTE_SUBMIX_SRC_TAG, AudioSource::VOICE_CALL},
207             {UNPROCESSED_SRC_TAG, AudioSource::VOICE_CALL},
208             {VOICE_PERFORMANCE_SRC_TAG, AudioSource::VOICE_CALL}};
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 
parseProcessing(Processing::Type::Tag typeTag,const tinyxml2::XMLElement & xml)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>>&
getProcessingMap() const246 EffectConfig::getProcessingMap() const {
247     return mProcessingMap;
248 }
249 
findUuid(const std::pair<std::string,struct EffectLibraries> & effectElem,AudioUuid * uuid)250 bool EffectConfig::findUuid(const std::pair<std::string, struct EffectLibraries>& effectElem,
251                             AudioUuid* uuid) {
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)                              \
261     V("extensioneffect", Extension)                        \
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 
278     const std::string xmlEffectName = effectElem.first;
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     }
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     }
295     return false;
296 }
297 
dump(const tinyxml2::XMLElement & element,tinyxml2::XMLPrinter && printer) const298 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
305