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 
18 #ifndef ANDROID_MEDIA_EFFECTSCONFIG_H
19 #define ANDROID_MEDIA_EFFECTSCONFIG_H
20 
21 /** @file Parses audio effects configuration file to C and C++ structure.
22  * @see audio_effects_conf_V2_0.xsd for documentation on each structure
23  */
24 
25 #include <system/audio_effect.h>
26 
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 namespace android {
33 namespace effectsConfig {
34 
35 /** Default path of effect configuration file. Relative to DEFAULT_LOCATIONS. */
36 constexpr const char* DEFAULT_NAME = "audio_effects.xml";
37 
38 /** Directories where the effect libraries will be search for. */
39 constexpr const char* LD_EFFECT_LIBRARY_PATH[] =
40 #ifdef __LP64__
41         {"/odm/lib64/soundfx", "/vendor/lib64/soundfx", "/system/lib64/soundfx"};
42 #else
43         {"/odm/lib/soundfx", "/vendor/lib/soundfx", "/system/lib/soundfx"};
44 #endif
45 
46 struct Library {
47     std::string name;
48     std::string path;
49 };
50 using Libraries = std::vector<Library>;
51 
52 struct EffectImpl {
53     Library* library; //< Only valid as long as the associated library vector is unmodified
54     effect_uuid_t uuid;
55 };
56 
57 struct Effect : public EffectImpl {
58     std::string name;
59     bool isProxy;
60     EffectImpl libSw; //< Only valid if isProxy
61     EffectImpl libHw; //< Only valid if isProxy
62 };
63 
64 using Effects = std::vector<Effect>;
65 
66 template <class Type>
67 struct Stream {
68     Type type;
69     std::vector<std::reference_wrapper<Effect>> effects;
70 };
71 using OutputStream = Stream<audio_stream_type_t>;
72 using InputStream = Stream<audio_source_t>;
73 
74 struct DeviceEffects : Stream<audio_devices_t> {
75     std::string address;
76 };
77 
78 /** Parsed configuration.
79  * Intended to be a transient structure only used for deserialization.
80  * Note: Everything is copied in the configuration from the xml dom.
81  *       If copies needed to be avoided due to performance issue,
82  *       consider keeping a private handle on the xml dom and replace all strings by dom pointers.
83  *       Or even better, use SAX parsing to avoid the allocations all together.
84  */
85 struct Config {
86     float version;
87     Libraries libraries;
88     Effects effects;
89     std::vector<OutputStream> postprocess;
90     std::vector<InputStream> preprocess;
91     std::vector<DeviceEffects> deviceprocess;
92 };
93 
94 /** Result of `parse(const char*)` */
95 struct ParsingResult {
96     /** Parsed config, nullptr if the xml lib could not load the file */
97     std::unique_ptr<Config> parsedConfig;
98     size_t nbSkippedElement; //< Number of skipped invalid library, effect or processing chain
99     const std::string configPath; //< Path to the loaded configuration
100 };
101 
102 /** Parses the provided effect configuration.
103  * Parsing do not stop of first invalid element, but continues to the next.
104  * @param[in] path of the configuration file do load
105  *                 if nullptr, look for DEFAULT_NAME in DEFAULT_LOCATIONS.
106  * @see ParsingResult::nbSkippedElement
107  */
108 ParsingResult parse(const char* path = nullptr);
109 
110 } // namespace effectsConfig
111 } // namespace android
112 #endif  // ANDROID_MEDIA_EFFECTSCONFIG_H
113