blob: 218249da0433652596845f39feccc5fbded72b13 [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"},
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700120};
121
122/** Find the stream type enum corresponding to the stream type name or return false */
123template <class Type>
124bool stringToStreamType(const char *streamName, Type* type)
125{
126 for (auto& streamNamePair : STREAM_NAME_MAP<Type>) {
127 if (strcmp(streamNamePair.second, streamName) == 0) {
128 *type = streamNamePair.first;
129 return true;
130 }
131 }
132 return false;
133}
134
135/** Parse a library xml note and push the result in libraries or return false on failure. */
136bool parseLibrary(const XMLElement& xmlLibrary, Libraries* libraries) {
137 const char* name = xmlLibrary.Attribute("name");
138 const char* path = xmlLibrary.Attribute("path");
139 if (name == nullptr || path == nullptr) {
140 ALOGE("library must have a name and a path: %s", dump(xmlLibrary));
141 return false;
142 }
143 libraries->push_back({name, path});
144 return true;
145}
146
147/** Find an element in a collection by its name.
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700148 * @return nullptr if not found, the element address if found.
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700149 */
150template <class T>
151T* findByName(const char* name, std::vector<T>& collection) {
152 auto it = find_if(begin(collection), end(collection),
153 [name] (auto& item) { return item.name == name; });
154 return it != end(collection) ? &*it : nullptr;
155}
156
157/** Parse an effect from an xml element describing it.
158 * @return true and pushes the effect in effects on success,
159 * false on failure. */
160bool parseEffect(const XMLElement& xmlEffect, Libraries& libraries, Effects* effects) {
161 Effect effect{};
162
163 const char* name = xmlEffect.Attribute("name");
164 if (name == nullptr) {
165 ALOGE("%s must have a name: %s", xmlEffect.Value(), dump(xmlEffect));
166 return false;
167 }
168 effect.name = name;
169
170 // Function to parse effect.library and effect.uuid from xml
171 auto parseImpl = [&libraries](const XMLElement& xmlImpl, EffectImpl& effect) {
172 // Retrieve library name and uuid from xml
173 const char* libraryName = xmlImpl.Attribute("library");
174 const char* uuid = xmlImpl.Attribute("uuid");
175 if (libraryName == nullptr || uuid == nullptr) {
176 ALOGE("effect must have a library name and a uuid: %s", dump(xmlImpl));
177 return false;
178 }
179
180 // Convert library name to a pointer to the previously loaded library
181 auto* library = findByName(libraryName, libraries);
182 if (library == nullptr) {
183 ALOGE("Could not find library referenced in: %s", dump(xmlImpl));
184 return false;
185 }
186 effect.library = library;
187
188 if (!stringToUuid(uuid, &effect.uuid)) {
189 ALOGE("Invalid uuid in: %s", dump(xmlImpl));
190 return false;
191 }
192 return true;
193 };
194
195 if (!parseImpl(xmlEffect, effect)) {
196 return false;
197 }
198
199 // Handle proxy effects
200 effect.isProxy = false;
201 if (std::strcmp(xmlEffect.Name(), "effectProxy") == 0) {
202 effect.isProxy = true;
203
204 // Function to parse libhw and libsw
205 auto parseProxy = [&xmlEffect, &parseImpl](const char* tag, EffectImpl& proxyLib) {
206 auto* xmlProxyLib = xmlEffect.FirstChildElement(tag);
207 if (xmlProxyLib == nullptr) {
Kevin Rocard82e14cd2018-03-30 10:09:42 -0700208 ALOGE("effectProxy must contain a <%s>: %s", tag, dump(xmlEffect));
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700209 return false;
210 }
211 return parseImpl(*xmlProxyLib, proxyLib);
212 };
213 if (!parseProxy("libhw", effect.libHw) || !parseProxy("libsw", effect.libSw)) {
214 return false;
215 }
216 }
217
218 effects->push_back(std::move(effect));
219 return true;
220}
221
222/** Parse an stream from an xml element describing it.
223 * @return true and pushes the stream in streams on success,
224 * false on failure. */
225template <class Stream>
226bool parseStream(const XMLElement& xmlStream, Effects& effects, std::vector<Stream>* streams) {
227 const char* streamType = xmlStream.Attribute("type");
228 if (streamType == nullptr) {
229 ALOGE("stream must have a type: %s", dump(xmlStream));
230 return false;
231 }
232 Stream stream;
233 if (!stringToStreamType(streamType, &stream.type)) {
234 ALOGE("Invalid stream type %s: %s", streamType, dump(xmlStream));
235 return false;
236 }
237
238 for (auto& xmlApply : getChildren(xmlStream, "apply")) {
239 const char* effectName = xmlApply.get().Attribute("effect");
240 if (effectName == nullptr) {
241 ALOGE("stream/apply must have reference an effect: %s", dump(xmlApply));
242 return false;
243 }
244 auto* effect = findByName(effectName, effects);
245 if (effect == nullptr) {
246 ALOGE("Could not find effect referenced in: %s", dump(xmlApply));
247 return false;
248 }
249 stream.effects.emplace_back(*effect);
250 }
251 streams->push_back(std::move(stream));
252 return true;
253}
254
Kevin Rocardb18bd862018-07-12 17:14:27 -0700255/** Internal version of the public parse(const char* path) where path always exist. */
256ParsingResult parseWithPath(std::string&& path) {
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700257 XMLDocument doc;
Kevin Rocardb18bd862018-07-12 17:14:27 -0700258 doc.LoadFile(path.c_str());
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700259 if (doc.Error()) {
Kevin Rocardb18bd862018-07-12 17:14:27 -0700260 ALOGE("Failed to parse %s: Tinyxml2 error (%d): %s", path.c_str(),
Narayan Kamathcf7c2432017-12-22 11:19:14 +0000261 doc.ErrorID(), doc.ErrorStr());
Kevin Rocardb18bd862018-07-12 17:14:27 -0700262 return {nullptr, 0, std::move(path)};
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700263 }
264
265 auto config = std::make_unique<Config>();
266 size_t nbSkippedElements = 0;
267 auto registerFailure = [&nbSkippedElements](bool result) {
268 nbSkippedElements += result ? 0 : 1;
269 };
270 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
271
272 // Parse library
273 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
274 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
275 registerFailure(parseLibrary(xmlLibrary, &config->libraries));
276 }
277 }
278
279 // Parse effects
280 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
281 for (auto& xmlEffect : getChildren(xmlEffects)) {
282 registerFailure(parseEffect(xmlEffect, config->libraries, &config->effects));
283 }
284 }
285
286 // Parse pre processing chains
287 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
288 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
289 registerFailure(parseStream(xmlStream, config->effects, &config->preprocess));
290 }
291 }
292
293 // Parse post processing chains
294 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
295 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
296 registerFailure(parseStream(xmlStream, config->effects, &config->postprocess));
297 }
298 }
299 }
Kevin Rocardb18bd862018-07-12 17:14:27 -0700300 return {std::move(config), nbSkippedElements, std::move(path)};
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700301}
302
303}; // namespace
304
305ParsingResult parse(const char* path) {
306 if (path != nullptr) {
307 return parseWithPath(path);
308 }
309
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800310 for (const std::string& location : DEFAULT_LOCATIONS) {
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700311 std::string defaultPath = location + '/' + DEFAULT_NAME;
312 if (access(defaultPath.c_str(), R_OK) != 0) {
313 continue;
314 }
Kevin Rocardb18bd862018-07-12 17:14:27 -0700315 auto result = parseWithPath(std::move(defaultPath));
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700316 if (result.parsedConfig != nullptr) {
317 return result;
318 }
319 }
320
321 ALOGE("Could not parse effect configuration in any of the default locations.");
Kevin Rocardb18bd862018-07-12 17:14:27 -0700322 return {nullptr, 0, ""};
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700323}
324
325} // namespace effectsConfig
326} // namespace android