Kevin Rocard | 42aa39a | 2017-06-09 19:22:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #define LOG_TAG "EffectsConfig" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cstdint> |
| 21 | #include <functional> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <tinyxml2.h> |
| 25 | #include <log/log.h> |
| 26 | |
| 27 | #include <media/EffectsConfig.h> |
| 28 | |
| 29 | using namespace tinyxml2; |
| 30 | |
| 31 | namespace android { |
| 32 | namespace effectsConfig { |
| 33 | |
| 34 | /** All functions except `parse(const char*)` are static. */ |
| 35 | namespace { |
| 36 | |
| 37 | /** @return all `node`s children that are elements and match the tag if provided. */ |
| 38 | std::vector<std::reference_wrapper<const XMLElement>> getChildren(const XMLNode& node, |
| 39 | const char* childTag = nullptr) { |
| 40 | std::vector<std::reference_wrapper<const XMLElement>> children; |
| 41 | for (auto* child = node.FirstChildElement(childTag); child != nullptr; |
| 42 | child = child->NextSiblingElement(childTag)) { |
| 43 | children.emplace_back(*child); |
| 44 | } |
| 45 | return children; |
| 46 | } |
| 47 | |
| 48 | /** @return xml dump of the provided element. |
| 49 | * By not providing a printer, it is implicitly created in the caller context. |
| 50 | * In such case the return pointer has the same lifetime as the expression containing dump(). |
| 51 | */ |
| 52 | const char* dump(const XMLElement& element, XMLPrinter&& printer = {}) { |
| 53 | element.Accept(&printer); |
| 54 | return printer.CStr(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | bool stringToUuid(const char *str, effect_uuid_t *uuid) |
| 59 | { |
| 60 | uint32_t tmp[10]; |
| 61 | |
| 62 | if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x", |
| 63 | tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) { |
| 64 | return false; |
| 65 | } |
| 66 | uuid->timeLow = (uint32_t)tmp[0]; |
| 67 | uuid->timeMid = (uint16_t)tmp[1]; |
| 68 | uuid->timeHiAndVersion = (uint16_t)tmp[2]; |
| 69 | uuid->clockSeq = (uint16_t)tmp[3]; |
| 70 | uuid->node[0] = (uint8_t)tmp[4]; |
| 71 | uuid->node[1] = (uint8_t)tmp[5]; |
| 72 | uuid->node[2] = (uint8_t)tmp[6]; |
| 73 | uuid->node[3] = (uint8_t)tmp[7]; |
| 74 | uuid->node[4] = (uint8_t)tmp[8]; |
| 75 | uuid->node[5] = (uint8_t)tmp[9]; |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | /** Map the enum and string representation of a string type. |
| 81 | * Intended to be specialized for each enum to deserialize. |
| 82 | * The general template is disabled. |
| 83 | */ |
| 84 | template <class Enum> |
| 85 | constexpr std::enable_if<false, Enum> STREAM_NAME_MAP; |
| 86 | |
| 87 | /** All output stream types which support effects. |
| 88 | * This need to be kept in sink with the xsd streamOutputType. |
| 89 | */ |
| 90 | template <> |
| 91 | constexpr std::pair<audio_stream_type_t, const char*> STREAM_NAME_MAP<audio_stream_type_t>[] = { |
| 92 | {AUDIO_STREAM_VOICE_CALL, "voice_call"}, |
| 93 | {AUDIO_STREAM_SYSTEM, "system"}, |
| 94 | {AUDIO_STREAM_RING, "ring"}, |
| 95 | {AUDIO_STREAM_MUSIC, "music"}, |
| 96 | {AUDIO_STREAM_ALARM, "alarm"}, |
| 97 | {AUDIO_STREAM_NOTIFICATION, "notification"}, |
| 98 | {AUDIO_STREAM_BLUETOOTH_SCO, "bluetooth_sco"}, |
| 99 | {AUDIO_STREAM_ENFORCED_AUDIBLE, "enforced_audible"}, |
| 100 | {AUDIO_STREAM_DTMF, "dtmf"}, |
| 101 | {AUDIO_STREAM_TTS, "tts"}, |
| 102 | }; |
| 103 | |
| 104 | /** All input stream types which support effects. |
| 105 | * This need to be kept in sink with the xsd streamOutputType. |
| 106 | */ |
| 107 | template <> |
| 108 | constexpr std::pair<audio_source_t, const char*> STREAM_NAME_MAP<audio_source_t>[] = { |
| 109 | {AUDIO_SOURCE_MIC, "mic"}, |
| 110 | {AUDIO_SOURCE_VOICE_UPLINK, "voice_uplink"}, |
| 111 | {AUDIO_SOURCE_VOICE_DOWNLINK, "voice_downlink"}, |
| 112 | {AUDIO_SOURCE_VOICE_CALL, "voice_call"}, |
| 113 | {AUDIO_SOURCE_CAMCORDER, "camcorder"}, |
| 114 | {AUDIO_SOURCE_VOICE_RECOGNITION, "voice_recognition"}, |
| 115 | {AUDIO_SOURCE_VOICE_COMMUNICATION, "voice_communication"}, |
| 116 | {AUDIO_SOURCE_UNPROCESSED, "unprocessed"}, |
| 117 | }; |
| 118 | |
| 119 | /** Find the stream type enum corresponding to the stream type name or return false */ |
| 120 | template <class Type> |
| 121 | bool stringToStreamType(const char *streamName, Type* type) |
| 122 | { |
| 123 | for (auto& streamNamePair : STREAM_NAME_MAP<Type>) { |
| 124 | if (strcmp(streamNamePair.second, streamName) == 0) { |
| 125 | *type = streamNamePair.first; |
| 126 | return true; |
| 127 | } |
| 128 | } |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /** Parse a library xml note and push the result in libraries or return false on failure. */ |
| 133 | bool parseLibrary(const XMLElement& xmlLibrary, Libraries* libraries) { |
| 134 | const char* name = xmlLibrary.Attribute("name"); |
| 135 | const char* path = xmlLibrary.Attribute("path"); |
| 136 | if (name == nullptr || path == nullptr) { |
| 137 | ALOGE("library must have a name and a path: %s", dump(xmlLibrary)); |
| 138 | return false; |
| 139 | } |
| 140 | libraries->push_back({name, path}); |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | /** Find an element in a collection by its name. |
| 145 | * @return nullptr if not found, the ellements address if found. |
| 146 | */ |
| 147 | template <class T> |
| 148 | T* findByName(const char* name, std::vector<T>& collection) { |
| 149 | auto it = find_if(begin(collection), end(collection), |
| 150 | [name] (auto& item) { return item.name == name; }); |
| 151 | return it != end(collection) ? &*it : nullptr; |
| 152 | } |
| 153 | |
| 154 | /** Parse an effect from an xml element describing it. |
| 155 | * @return true and pushes the effect in effects on success, |
| 156 | * false on failure. */ |
| 157 | bool parseEffect(const XMLElement& xmlEffect, Libraries& libraries, Effects* effects) { |
| 158 | Effect effect{}; |
| 159 | |
| 160 | const char* name = xmlEffect.Attribute("name"); |
| 161 | if (name == nullptr) { |
| 162 | ALOGE("%s must have a name: %s", xmlEffect.Value(), dump(xmlEffect)); |
| 163 | return false; |
| 164 | } |
| 165 | effect.name = name; |
| 166 | |
| 167 | // Function to parse effect.library and effect.uuid from xml |
| 168 | auto parseImpl = [&libraries](const XMLElement& xmlImpl, EffectImpl& effect) { |
| 169 | // Retrieve library name and uuid from xml |
| 170 | const char* libraryName = xmlImpl.Attribute("library"); |
| 171 | const char* uuid = xmlImpl.Attribute("uuid"); |
| 172 | if (libraryName == nullptr || uuid == nullptr) { |
| 173 | ALOGE("effect must have a library name and a uuid: %s", dump(xmlImpl)); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | // Convert library name to a pointer to the previously loaded library |
| 178 | auto* library = findByName(libraryName, libraries); |
| 179 | if (library == nullptr) { |
| 180 | ALOGE("Could not find library referenced in: %s", dump(xmlImpl)); |
| 181 | return false; |
| 182 | } |
| 183 | effect.library = library; |
| 184 | |
| 185 | if (!stringToUuid(uuid, &effect.uuid)) { |
| 186 | ALOGE("Invalid uuid in: %s", dump(xmlImpl)); |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | }; |
| 191 | |
| 192 | if (!parseImpl(xmlEffect, effect)) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | // Handle proxy effects |
| 197 | effect.isProxy = false; |
| 198 | if (std::strcmp(xmlEffect.Name(), "effectProxy") == 0) { |
| 199 | effect.isProxy = true; |
| 200 | |
| 201 | // Function to parse libhw and libsw |
| 202 | auto parseProxy = [&xmlEffect, &parseImpl](const char* tag, EffectImpl& proxyLib) { |
| 203 | auto* xmlProxyLib = xmlEffect.FirstChildElement(tag); |
| 204 | if (xmlProxyLib == nullptr) { |
| 205 | ALOGE("effectProxy must contain a <%s>: %s", tag, dump(*xmlProxyLib)); |
| 206 | return false; |
| 207 | } |
| 208 | return parseImpl(*xmlProxyLib, proxyLib); |
| 209 | }; |
| 210 | if (!parseProxy("libhw", effect.libHw) || !parseProxy("libsw", effect.libSw)) { |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | effects->push_back(std::move(effect)); |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | /** Parse an stream from an xml element describing it. |
| 220 | * @return true and pushes the stream in streams on success, |
| 221 | * false on failure. */ |
| 222 | template <class Stream> |
| 223 | bool parseStream(const XMLElement& xmlStream, Effects& effects, std::vector<Stream>* streams) { |
| 224 | const char* streamType = xmlStream.Attribute("type"); |
| 225 | if (streamType == nullptr) { |
| 226 | ALOGE("stream must have a type: %s", dump(xmlStream)); |
| 227 | return false; |
| 228 | } |
| 229 | Stream stream; |
| 230 | if (!stringToStreamType(streamType, &stream.type)) { |
| 231 | ALOGE("Invalid stream type %s: %s", streamType, dump(xmlStream)); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | for (auto& xmlApply : getChildren(xmlStream, "apply")) { |
| 236 | const char* effectName = xmlApply.get().Attribute("effect"); |
| 237 | if (effectName == nullptr) { |
| 238 | ALOGE("stream/apply must have reference an effect: %s", dump(xmlApply)); |
| 239 | return false; |
| 240 | } |
| 241 | auto* effect = findByName(effectName, effects); |
| 242 | if (effect == nullptr) { |
| 243 | ALOGE("Could not find effect referenced in: %s", dump(xmlApply)); |
| 244 | return false; |
| 245 | } |
| 246 | stream.effects.emplace_back(*effect); |
| 247 | } |
| 248 | streams->push_back(std::move(stream)); |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | }; // namespace |
| 253 | |
| 254 | ParsingResult parse(const char* path) { |
| 255 | XMLDocument doc; |
| 256 | doc.LoadFile(path); |
| 257 | if (doc.Error()) { |
| 258 | ALOGE("Failed to parse %s: Tinyxml2 error (%d): %s %s", path, |
| 259 | doc.ErrorID(), doc.GetErrorStr1(), doc.GetErrorStr2()); |
| 260 | return {nullptr, 0}; |
| 261 | } |
| 262 | |
| 263 | auto config = std::make_unique<Config>(); |
| 264 | size_t nbSkippedElements = 0; |
| 265 | auto registerFailure = [&nbSkippedElements](bool result) { |
| 266 | nbSkippedElements += result ? 0 : 1; |
| 267 | }; |
| 268 | for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) { |
| 269 | |
| 270 | // Parse library |
| 271 | for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) { |
| 272 | for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) { |
| 273 | registerFailure(parseLibrary(xmlLibrary, &config->libraries)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Parse effects |
| 278 | for (auto& xmlEffects : getChildren(xmlConfig, "effects")) { |
| 279 | for (auto& xmlEffect : getChildren(xmlEffects)) { |
| 280 | registerFailure(parseEffect(xmlEffect, config->libraries, &config->effects)); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // Parse pre processing chains |
| 285 | for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) { |
| 286 | for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) { |
| 287 | registerFailure(parseStream(xmlStream, config->effects, &config->preprocess)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Parse post processing chains |
| 292 | for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) { |
| 293 | for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) { |
| 294 | registerFailure(parseStream(xmlStream, config->effects, &config->postprocess)); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | return {std::move(config), nbSkippedElements}; |
| 299 | } |
| 300 | |
| 301 | } // namespace effectsConfig |
| 302 | } // namespace android |