blob: 90a4fbbb1fc46eccbbe51c44a6b4c663bb2cae18 [file] [log] [blame]
Kevin Rocard42aa39a2017-06-09 19:22:43 -07001/*
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>
Kevin Rocard8cb2eab2017-08-29 17:12:30 -070023#include <unistd.h>
Kevin Rocard42aa39a2017-06-09 19:22:43 -070024
25#include <tinyxml2.h>
26#include <log/log.h>
27
28#include <media/EffectsConfig.h>
29
30using namespace tinyxml2;
31
32namespace android {
33namespace effectsConfig {
34
35/** All functions except `parse(const char*)` are static. */
36namespace {
37
38/** @return all `node`s children that are elements and match the tag if provided. */
39std::vector<std::reference_wrapper<const XMLElement>> getChildren(const XMLNode& node,
40 const char* childTag = nullptr) {
41 std::vector<std::reference_wrapper<const XMLElement>> children;
42 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
43 child = child->NextSiblingElement(childTag)) {
44 children.emplace_back(*child);
45 }
46 return children;
47}
48
49/** @return xml dump of the provided element.
50 * By not providing a printer, it is implicitly created in the caller context.
51 * In such case the return pointer has the same lifetime as the expression containing dump().
52 */
53const char* dump(const XMLElement& element, XMLPrinter&& printer = {}) {
54 element.Accept(&printer);
55 return printer.CStr();
56}
57
58
59bool stringToUuid(const char *str, effect_uuid_t *uuid)
60{
61 uint32_t tmp[10];
62
63 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
64 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
65 return false;
66 }
67 uuid->timeLow = (uint32_t)tmp[0];
68 uuid->timeMid = (uint16_t)tmp[1];
69 uuid->timeHiAndVersion = (uint16_t)tmp[2];
70 uuid->clockSeq = (uint16_t)tmp[3];
71 uuid->node[0] = (uint8_t)tmp[4];
72 uuid->node[1] = (uint8_t)tmp[5];
73 uuid->node[2] = (uint8_t)tmp[6];
74 uuid->node[3] = (uint8_t)tmp[7];
75 uuid->node[4] = (uint8_t)tmp[8];
76 uuid->node[5] = (uint8_t)tmp[9];
77
78 return true;
79}
80
81/** Map the enum and string representation of a string type.
82 * Intended to be specialized for each enum to deserialize.
83 * The general template is disabled.
84 */
85template <class Enum>
86constexpr std::enable_if<false, Enum> STREAM_NAME_MAP;
87
88/** All output stream types which support effects.
Kevin Rocard8cb2eab2017-08-29 17:12:30 -070089 * This need to be kept in sync with the xsd streamOutputType.
Kevin Rocard42aa39a2017-06-09 19:22:43 -070090 */
91template <>
92constexpr std::pair<audio_stream_type_t, const char*> STREAM_NAME_MAP<audio_stream_type_t>[] = {
93 {AUDIO_STREAM_VOICE_CALL, "voice_call"},
94 {AUDIO_STREAM_SYSTEM, "system"},
95 {AUDIO_STREAM_RING, "ring"},
96 {AUDIO_STREAM_MUSIC, "music"},
97 {AUDIO_STREAM_ALARM, "alarm"},
98 {AUDIO_STREAM_NOTIFICATION, "notification"},
99 {AUDIO_STREAM_BLUETOOTH_SCO, "bluetooth_sco"},
100 {AUDIO_STREAM_ENFORCED_AUDIBLE, "enforced_audible"},
101 {AUDIO_STREAM_DTMF, "dtmf"},
102 {AUDIO_STREAM_TTS, "tts"},
Baekgyeong Kim47ea6712019-10-30 20:29:41 +0900103 {AUDIO_STREAM_ASSISTANT, "assistant"},
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700104};
105
106/** All input stream types which support effects.
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700107 * This need to be kept in sync with the xsd streamOutputType.
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700108 */
109template <>
110constexpr std::pair<audio_source_t, const char*> STREAM_NAME_MAP<audio_source_t>[] = {
111 {AUDIO_SOURCE_MIC, "mic"},
112 {AUDIO_SOURCE_VOICE_UPLINK, "voice_uplink"},
113 {AUDIO_SOURCE_VOICE_DOWNLINK, "voice_downlink"},
114 {AUDIO_SOURCE_VOICE_CALL, "voice_call"},
115 {AUDIO_SOURCE_CAMCORDER, "camcorder"},
116 {AUDIO_SOURCE_VOICE_RECOGNITION, "voice_recognition"},
117 {AUDIO_SOURCE_VOICE_COMMUNICATION, "voice_communication"},
118 {AUDIO_SOURCE_UNPROCESSED, "unprocessed"},
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800119 {AUDIO_SOURCE_VOICE_PERFORMANCE, "voice_performance"},
Francois Gaffie160863f2019-03-07 10:11:43 +0100120 {AUDIO_SOURCE_ECHO_REFERENCE, "echo_reference"},
121 {AUDIO_SOURCE_FM_TUNER, "fm_tuner"},
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700122};
123
124/** Find the stream type enum corresponding to the stream type name or return false */
125template <class Type>
126bool stringToStreamType(const char *streamName, Type* type)
127{
128 for (auto& streamNamePair : STREAM_NAME_MAP<Type>) {
129 if (strcmp(streamNamePair.second, streamName) == 0) {
130 *type = streamNamePair.first;
131 return true;
132 }
133 }
134 return false;
135}
136
137/** Parse a library xml note and push the result in libraries or return false on failure. */
138bool parseLibrary(const XMLElement& xmlLibrary, Libraries* libraries) {
139 const char* name = xmlLibrary.Attribute("name");
140 const char* path = xmlLibrary.Attribute("path");
141 if (name == nullptr || path == nullptr) {
142 ALOGE("library must have a name and a path: %s", dump(xmlLibrary));
143 return false;
144 }
145 libraries->push_back({name, path});
146 return true;
147}
148
149/** Find an element in a collection by its name.
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700150 * @return nullptr if not found, the element address if found.
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700151 */
152template <class T>
153T* findByName(const char* name, std::vector<T>& collection) {
154 auto it = find_if(begin(collection), end(collection),
155 [name] (auto& item) { return item.name == name; });
156 return it != end(collection) ? &*it : nullptr;
157}
158
159/** Parse an effect from an xml element describing it.
160 * @return true and pushes the effect in effects on success,
161 * false on failure. */
162bool parseEffect(const XMLElement& xmlEffect, Libraries& libraries, Effects* effects) {
163 Effect effect{};
164
165 const char* name = xmlEffect.Attribute("name");
166 if (name == nullptr) {
167 ALOGE("%s must have a name: %s", xmlEffect.Value(), dump(xmlEffect));
168 return false;
169 }
170 effect.name = name;
171
172 // Function to parse effect.library and effect.uuid from xml
173 auto parseImpl = [&libraries](const XMLElement& xmlImpl, EffectImpl& effect) {
174 // Retrieve library name and uuid from xml
175 const char* libraryName = xmlImpl.Attribute("library");
176 const char* uuid = xmlImpl.Attribute("uuid");
177 if (libraryName == nullptr || uuid == nullptr) {
178 ALOGE("effect must have a library name and a uuid: %s", dump(xmlImpl));
179 return false;
180 }
181
182 // Convert library name to a pointer to the previously loaded library
183 auto* library = findByName(libraryName, libraries);
184 if (library == nullptr) {
185 ALOGE("Could not find library referenced in: %s", dump(xmlImpl));
186 return false;
187 }
188 effect.library = library;
189
190 if (!stringToUuid(uuid, &effect.uuid)) {
191 ALOGE("Invalid uuid in: %s", dump(xmlImpl));
192 return false;
193 }
194 return true;
195 };
196
197 if (!parseImpl(xmlEffect, effect)) {
198 return false;
199 }
200
201 // Handle proxy effects
202 effect.isProxy = false;
203 if (std::strcmp(xmlEffect.Name(), "effectProxy") == 0) {
204 effect.isProxy = true;
205
206 // Function to parse libhw and libsw
207 auto parseProxy = [&xmlEffect, &parseImpl](const char* tag, EffectImpl& proxyLib) {
208 auto* xmlProxyLib = xmlEffect.FirstChildElement(tag);
209 if (xmlProxyLib == nullptr) {
Kevin Rocard82e14cd2018-03-30 10:09:42 -0700210 ALOGE("effectProxy must contain a <%s>: %s", tag, dump(xmlEffect));
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700211 return false;
212 }
213 return parseImpl(*xmlProxyLib, proxyLib);
214 };
215 if (!parseProxy("libhw", effect.libHw) || !parseProxy("libsw", effect.libSw)) {
216 return false;
217 }
218 }
219
220 effects->push_back(std::move(effect));
221 return true;
222}
223
224/** Parse an stream from an xml element describing it.
225 * @return true and pushes the stream in streams on success,
226 * false on failure. */
227template <class Stream>
228bool parseStream(const XMLElement& xmlStream, Effects& effects, std::vector<Stream>* streams) {
229 const char* streamType = xmlStream.Attribute("type");
230 if (streamType == nullptr) {
231 ALOGE("stream must have a type: %s", dump(xmlStream));
232 return false;
233 }
234 Stream stream;
235 if (!stringToStreamType(streamType, &stream.type)) {
236 ALOGE("Invalid stream type %s: %s", streamType, dump(xmlStream));
237 return false;
238 }
239
240 for (auto& xmlApply : getChildren(xmlStream, "apply")) {
241 const char* effectName = xmlApply.get().Attribute("effect");
242 if (effectName == nullptr) {
243 ALOGE("stream/apply must have reference an effect: %s", dump(xmlApply));
244 return false;
245 }
246 auto* effect = findByName(effectName, effects);
247 if (effect == nullptr) {
248 ALOGE("Could not find effect referenced in: %s", dump(xmlApply));
249 return false;
250 }
251 stream.effects.emplace_back(*effect);
252 }
253 streams->push_back(std::move(stream));
254 return true;
255}
256
Kevin Rocardb18bd862018-07-12 17:14:27 -0700257/** Internal version of the public parse(const char* path) where path always exist. */
258ParsingResult parseWithPath(std::string&& path) {
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700259 XMLDocument doc;
Kevin Rocardb18bd862018-07-12 17:14:27 -0700260 doc.LoadFile(path.c_str());
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700261 if (doc.Error()) {
Kevin Rocardb18bd862018-07-12 17:14:27 -0700262 ALOGE("Failed to parse %s: Tinyxml2 error (%d): %s", path.c_str(),
Narayan Kamathcf7c2432017-12-22 11:19:14 +0000263 doc.ErrorID(), doc.ErrorStr());
Kevin Rocardb18bd862018-07-12 17:14:27 -0700264 return {nullptr, 0, std::move(path)};
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700265 }
266
267 auto config = std::make_unique<Config>();
268 size_t nbSkippedElements = 0;
269 auto registerFailure = [&nbSkippedElements](bool result) {
270 nbSkippedElements += result ? 0 : 1;
271 };
272 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
273
274 // Parse library
275 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
276 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
277 registerFailure(parseLibrary(xmlLibrary, &config->libraries));
278 }
279 }
280
281 // Parse effects
282 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
283 for (auto& xmlEffect : getChildren(xmlEffects)) {
284 registerFailure(parseEffect(xmlEffect, config->libraries, &config->effects));
285 }
286 }
287
288 // Parse pre processing chains
289 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
290 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
291 registerFailure(parseStream(xmlStream, config->effects, &config->preprocess));
292 }
293 }
294
295 // Parse post processing chains
296 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
297 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
298 registerFailure(parseStream(xmlStream, config->effects, &config->postprocess));
299 }
300 }
301 }
Kevin Rocardb18bd862018-07-12 17:14:27 -0700302 return {std::move(config), nbSkippedElements, std::move(path)};
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700303}
304
305}; // namespace
306
307ParsingResult parse(const char* path) {
308 if (path != nullptr) {
309 return parseWithPath(path);
310 }
311
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800312 for (const std::string& location : DEFAULT_LOCATIONS) {
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700313 std::string defaultPath = location + '/' + DEFAULT_NAME;
314 if (access(defaultPath.c_str(), R_OK) != 0) {
315 continue;
316 }
Kevin Rocardb18bd862018-07-12 17:14:27 -0700317 auto result = parseWithPath(std::move(defaultPath));
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700318 if (result.parsedConfig != nullptr) {
319 return result;
320 }
321 }
322
323 ALOGE("Could not parse effect configuration in any of the default locations.");
Kevin Rocardb18bd862018-07-12 17:14:27 -0700324 return {nullptr, 0, ""};
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700325}
326
327} // namespace effectsConfig
328} // namespace android