blob: 85fbf1139014d59b531f9512a833d591d43b8d45 [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>
François Gaffiec1b0fe42020-01-07 09:12:36 +010029#include <media/TypeConverter.h>
Kevin Rocard42aa39a2017-06-09 19:22:43 -070030
31using namespace tinyxml2;
32
33namespace android {
34namespace effectsConfig {
35
36/** All functions except `parse(const char*)` are static. */
37namespace {
38
39/** @return all `node`s children that are elements and match the tag if provided. */
40std::vector<std::reference_wrapper<const XMLElement>> getChildren(const XMLNode& node,
41 const char* childTag = nullptr) {
42 std::vector<std::reference_wrapper<const XMLElement>> children;
43 for (auto* child = node.FirstChildElement(childTag); child != nullptr;
44 child = child->NextSiblingElement(childTag)) {
45 children.emplace_back(*child);
46 }
47 return children;
48}
49
50/** @return xml dump of the provided element.
51 * By not providing a printer, it is implicitly created in the caller context.
52 * In such case the return pointer has the same lifetime as the expression containing dump().
53 */
54const char* dump(const XMLElement& element, XMLPrinter&& printer = {}) {
55 element.Accept(&printer);
56 return printer.CStr();
57}
58
59
60bool stringToUuid(const char *str, effect_uuid_t *uuid)
61{
62 uint32_t tmp[10];
63
64 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
65 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
66 return false;
67 }
68 uuid->timeLow = (uint32_t)tmp[0];
69 uuid->timeMid = (uint16_t)tmp[1];
70 uuid->timeHiAndVersion = (uint16_t)tmp[2];
71 uuid->clockSeq = (uint16_t)tmp[3];
72 uuid->node[0] = (uint8_t)tmp[4];
73 uuid->node[1] = (uint8_t)tmp[5];
74 uuid->node[2] = (uint8_t)tmp[6];
75 uuid->node[3] = (uint8_t)tmp[7];
76 uuid->node[4] = (uint8_t)tmp[8];
77 uuid->node[5] = (uint8_t)tmp[9];
78
79 return true;
80}
81
82/** Map the enum and string representation of a string type.
83 * Intended to be specialized for each enum to deserialize.
84 * The general template is disabled.
85 */
86template <class Enum>
87constexpr std::enable_if<false, Enum> STREAM_NAME_MAP;
88
89/** All output stream types which support effects.
Kevin Rocard8cb2eab2017-08-29 17:12:30 -070090 * This need to be kept in sync with the xsd streamOutputType.
Kevin Rocard42aa39a2017-06-09 19:22:43 -070091 */
92template <>
93constexpr std::pair<audio_stream_type_t, const char*> STREAM_NAME_MAP<audio_stream_type_t>[] = {
94 {AUDIO_STREAM_VOICE_CALL, "voice_call"},
95 {AUDIO_STREAM_SYSTEM, "system"},
96 {AUDIO_STREAM_RING, "ring"},
97 {AUDIO_STREAM_MUSIC, "music"},
98 {AUDIO_STREAM_ALARM, "alarm"},
99 {AUDIO_STREAM_NOTIFICATION, "notification"},
100 {AUDIO_STREAM_BLUETOOTH_SCO, "bluetooth_sco"},
101 {AUDIO_STREAM_ENFORCED_AUDIBLE, "enforced_audible"},
102 {AUDIO_STREAM_DTMF, "dtmf"},
103 {AUDIO_STREAM_TTS, "tts"},
Baekgyeong Kim3a26bb22019-10-30 20:29:41 +0900104 {AUDIO_STREAM_ASSISTANT, "assistant"},
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700105};
106
107/** All input stream types which support effects.
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700108 * This need to be kept in sync with the xsd streamOutputType.
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700109 */
110template <>
111constexpr std::pair<audio_source_t, const char*> STREAM_NAME_MAP<audio_source_t>[] = {
112 {AUDIO_SOURCE_MIC, "mic"},
113 {AUDIO_SOURCE_VOICE_UPLINK, "voice_uplink"},
114 {AUDIO_SOURCE_VOICE_DOWNLINK, "voice_downlink"},
115 {AUDIO_SOURCE_VOICE_CALL, "voice_call"},
116 {AUDIO_SOURCE_CAMCORDER, "camcorder"},
117 {AUDIO_SOURCE_VOICE_RECOGNITION, "voice_recognition"},
118 {AUDIO_SOURCE_VOICE_COMMUNICATION, "voice_communication"},
119 {AUDIO_SOURCE_UNPROCESSED, "unprocessed"},
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800120 {AUDIO_SOURCE_VOICE_PERFORMANCE, "voice_performance"},
Francois Gaffieefa2b5c2019-03-07 10:11:43 +0100121 {AUDIO_SOURCE_ECHO_REFERENCE, "echo_reference"},
122 {AUDIO_SOURCE_FM_TUNER, "fm_tuner"},
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700123};
124
125/** Find the stream type enum corresponding to the stream type name or return false */
126template <class Type>
127bool stringToStreamType(const char *streamName, Type* type)
128{
129 for (auto& streamNamePair : STREAM_NAME_MAP<Type>) {
130 if (strcmp(streamNamePair.second, streamName) == 0) {
131 *type = streamNamePair.first;
132 return true;
133 }
134 }
135 return false;
136}
137
François Gaffiec1b0fe42020-01-07 09:12:36 +0100138template <>
139bool stringToStreamType(const char *streamName, audio_devices_t* type) {
140 return deviceFromString(streamName, *type);
141}
142
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700143/** Parse a library xml note and push the result in libraries or return false on failure. */
144bool parseLibrary(const XMLElement& xmlLibrary, Libraries* libraries) {
145 const char* name = xmlLibrary.Attribute("name");
146 const char* path = xmlLibrary.Attribute("path");
147 if (name == nullptr || path == nullptr) {
148 ALOGE("library must have a name and a path: %s", dump(xmlLibrary));
149 return false;
150 }
151 libraries->push_back({name, path});
152 return true;
153}
154
155/** Find an element in a collection by its name.
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700156 * @return nullptr if not found, the element address if found.
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700157 */
158template <class T>
159T* findByName(const char* name, std::vector<T>& collection) {
160 auto it = find_if(begin(collection), end(collection),
161 [name] (auto& item) { return item.name == name; });
162 return it != end(collection) ? &*it : nullptr;
163}
164
165/** Parse an effect from an xml element describing it.
166 * @return true and pushes the effect in effects on success,
167 * false on failure. */
168bool parseEffect(const XMLElement& xmlEffect, Libraries& libraries, Effects* effects) {
169 Effect effect{};
170
171 const char* name = xmlEffect.Attribute("name");
172 if (name == nullptr) {
173 ALOGE("%s must have a name: %s", xmlEffect.Value(), dump(xmlEffect));
174 return false;
175 }
176 effect.name = name;
177
178 // Function to parse effect.library and effect.uuid from xml
179 auto parseImpl = [&libraries](const XMLElement& xmlImpl, EffectImpl& effect) {
180 // Retrieve library name and uuid from xml
181 const char* libraryName = xmlImpl.Attribute("library");
182 const char* uuid = xmlImpl.Attribute("uuid");
183 if (libraryName == nullptr || uuid == nullptr) {
184 ALOGE("effect must have a library name and a uuid: %s", dump(xmlImpl));
185 return false;
186 }
187
188 // Convert library name to a pointer to the previously loaded library
189 auto* library = findByName(libraryName, libraries);
190 if (library == nullptr) {
191 ALOGE("Could not find library referenced in: %s", dump(xmlImpl));
192 return false;
193 }
194 effect.library = library;
195
196 if (!stringToUuid(uuid, &effect.uuid)) {
197 ALOGE("Invalid uuid in: %s", dump(xmlImpl));
198 return false;
199 }
200 return true;
201 };
202
203 if (!parseImpl(xmlEffect, effect)) {
204 return false;
205 }
206
207 // Handle proxy effects
208 effect.isProxy = false;
209 if (std::strcmp(xmlEffect.Name(), "effectProxy") == 0) {
210 effect.isProxy = true;
211
212 // Function to parse libhw and libsw
213 auto parseProxy = [&xmlEffect, &parseImpl](const char* tag, EffectImpl& proxyLib) {
214 auto* xmlProxyLib = xmlEffect.FirstChildElement(tag);
215 if (xmlProxyLib == nullptr) {
Kevin Rocard82e14cd2018-03-30 10:09:42 -0700216 ALOGE("effectProxy must contain a <%s>: %s", tag, dump(xmlEffect));
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700217 return false;
218 }
219 return parseImpl(*xmlProxyLib, proxyLib);
220 };
221 if (!parseProxy("libhw", effect.libHw) || !parseProxy("libsw", effect.libSw)) {
222 return false;
223 }
224 }
225
226 effects->push_back(std::move(effect));
227 return true;
228}
229
François Gaffiec1b0fe42020-01-07 09:12:36 +0100230/** Parse an <Output|Input>stream or a device from an xml element describing it.
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700231 * @return true and pushes the stream in streams on success,
232 * false on failure. */
233template <class Stream>
234bool parseStream(const XMLElement& xmlStream, Effects& effects, std::vector<Stream>* streams) {
235 const char* streamType = xmlStream.Attribute("type");
236 if (streamType == nullptr) {
237 ALOGE("stream must have a type: %s", dump(xmlStream));
238 return false;
239 }
240 Stream stream;
241 if (!stringToStreamType(streamType, &stream.type)) {
François Gaffiec1b0fe42020-01-07 09:12:36 +0100242 ALOGE("Invalid <stream|device> type %s: %s", streamType, dump(xmlStream));
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700243 return false;
244 }
245
246 for (auto& xmlApply : getChildren(xmlStream, "apply")) {
247 const char* effectName = xmlApply.get().Attribute("effect");
248 if (effectName == nullptr) {
François Gaffiec1b0fe42020-01-07 09:12:36 +0100249 ALOGE("<stream|device>/apply must have reference an effect: %s", dump(xmlApply));
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700250 return false;
251 }
252 auto* effect = findByName(effectName, effects);
253 if (effect == nullptr) {
254 ALOGE("Could not find effect referenced in: %s", dump(xmlApply));
255 return false;
256 }
257 stream.effects.emplace_back(*effect);
258 }
259 streams->push_back(std::move(stream));
260 return true;
261}
262
François Gaffiec1b0fe42020-01-07 09:12:36 +0100263bool parseDeviceEffects(
264 const XMLElement& xmlDevice, Effects& effects, std::vector<DeviceEffects>* deviceEffects) {
265
266 const char* address = xmlDevice.Attribute("address");
267 if (address == nullptr) {
268 ALOGE("device must have an address: %s", dump(xmlDevice));
269 return false;
270 }
271 if (!parseStream(xmlDevice, effects, deviceEffects)) {
272 return false;
273 }
274 deviceEffects->back().address = address;
275 return true;
276}
277
Kevin Rocardb18bd862018-07-12 17:14:27 -0700278/** Internal version of the public parse(const char* path) where path always exist. */
279ParsingResult parseWithPath(std::string&& path) {
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700280 XMLDocument doc;
Kevin Rocardb18bd862018-07-12 17:14:27 -0700281 doc.LoadFile(path.c_str());
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700282 if (doc.Error()) {
Kevin Rocardb18bd862018-07-12 17:14:27 -0700283 ALOGE("Failed to parse %s: Tinyxml2 error (%d): %s", path.c_str(),
Narayan Kamathcf7c2432017-12-22 11:19:14 +0000284 doc.ErrorID(), doc.ErrorStr());
Kevin Rocardb18bd862018-07-12 17:14:27 -0700285 return {nullptr, 0, std::move(path)};
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700286 }
287
288 auto config = std::make_unique<Config>();
289 size_t nbSkippedElements = 0;
290 auto registerFailure = [&nbSkippedElements](bool result) {
291 nbSkippedElements += result ? 0 : 1;
292 };
293 for (auto& xmlConfig : getChildren(doc, "audio_effects_conf")) {
294
295 // Parse library
296 for (auto& xmlLibraries : getChildren(xmlConfig, "libraries")) {
297 for (auto& xmlLibrary : getChildren(xmlLibraries, "library")) {
298 registerFailure(parseLibrary(xmlLibrary, &config->libraries));
299 }
300 }
301
302 // Parse effects
303 for (auto& xmlEffects : getChildren(xmlConfig, "effects")) {
304 for (auto& xmlEffect : getChildren(xmlEffects)) {
305 registerFailure(parseEffect(xmlEffect, config->libraries, &config->effects));
306 }
307 }
308
309 // Parse pre processing chains
310 for (auto& xmlPreprocess : getChildren(xmlConfig, "preprocess")) {
311 for (auto& xmlStream : getChildren(xmlPreprocess, "stream")) {
312 registerFailure(parseStream(xmlStream, config->effects, &config->preprocess));
313 }
314 }
315
316 // Parse post processing chains
317 for (auto& xmlPostprocess : getChildren(xmlConfig, "postprocess")) {
318 for (auto& xmlStream : getChildren(xmlPostprocess, "stream")) {
319 registerFailure(parseStream(xmlStream, config->effects, &config->postprocess));
320 }
321 }
François Gaffiec1b0fe42020-01-07 09:12:36 +0100322
323 // Parse device effect chains
324 for (auto& xmlDeviceEffects : getChildren(xmlConfig, "deviceEffects")) {
325 for (auto& xmlDevice : getChildren(xmlDeviceEffects, "devicePort")) {
326 registerFailure(
327 parseDeviceEffects(xmlDevice, config->effects, &config->deviceprocess));
328 }
329 }
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700330 }
Kevin Rocardb18bd862018-07-12 17:14:27 -0700331 return {std::move(config), nbSkippedElements, std::move(path)};
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700332}
333
334}; // namespace
335
336ParsingResult parse(const char* path) {
337 if (path != nullptr) {
338 return parseWithPath(path);
339 }
340
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800341 for (const std::string& location : DEFAULT_LOCATIONS) {
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700342 std::string defaultPath = location + '/' + DEFAULT_NAME;
343 if (access(defaultPath.c_str(), R_OK) != 0) {
344 continue;
345 }
Kevin Rocardb18bd862018-07-12 17:14:27 -0700346 auto result = parseWithPath(std::move(defaultPath));
Kevin Rocard8cb2eab2017-08-29 17:12:30 -0700347 if (result.parsedConfig != nullptr) {
348 return result;
349 }
350 }
351
352 ALOGE("Could not parse effect configuration in any of the default locations.");
Kevin Rocardb18bd862018-07-12 17:14:27 -0700353 return {nullptr, 0, ""};
Kevin Rocard42aa39a2017-06-09 19:22:43 -0700354}
355
356} // namespace effectsConfig
357} // namespace android