1 /*
2  * Copyright 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 #ifndef MEDIA_STAGEFRIGHT_XMLPARSER_H_
18 #define MEDIA_STAGEFRIGHT_XMLPARSER_H_
19 
20 #include <sys/types.h>
21 #include <utils/Errors.h>
22 
23 #include <map>
24 #include <mutex>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 struct XML_ParserStruct; // from expat library
30 
31 namespace android {
32 
33 class MediaCodecsXmlParser {
34 public:
35 
36     // Treblized media codec list will be located in /product/etc, /odm/etc or
37     // /vendor/etc.
getDefaultSearchDirs()38     static std::vector<std::string> getDefaultSearchDirs() {
39         return { "/product/etc",
40                  "/odm/etc",
41                  "/vendor/etc",
42                  "/system/etc" };
43     }
44 
45     static std::vector<std::string> getDefaultXmlNames();
46 
47     static constexpr char const* defaultProfilingResultsXmlPath =
48             "/data/misc/media/media_codecs_profiling_results.xml";
49 
50     MediaCodecsXmlParser();
51     ~MediaCodecsXmlParser();
52 
53     typedef std::pair<std::string, std::string> Attribute;
54     typedef std::map<std::string, std::string> AttributeMap;
55 
56     typedef std::pair<std::string, AttributeMap> Type;
57     typedef std::map<std::string, AttributeMap> TypeMap;
58 
59     typedef std::set<std::string> StringSet;
60 
61     /**
62      * Properties of a codec (node)
63      */
64     struct CodecProperties {
65         bool isEncoder;    ///< Whether this codec is an encoder or a decoder
66         size_t order;      ///< Order of appearance in the file (starting from 0)
67         StringSet quirkSet; ///< Set of quirks requested by this codec
68         StringSet domainSet; ///< Set of domains this codec is in
69         StringSet variantSet; ///< Set of variants this codec is enabled on
70         TypeMap typeMap;   ///< Map of types supported by this codec
71         std::vector<std::string> aliases; ///< Name aliases for this codec
72         std::string rank;  ///< Rank of this codec. This is a numeric string.
73     };
74 
75     typedef std::pair<std::string, CodecProperties> Codec;
76     typedef std::map<std::string, CodecProperties> CodecMap;
77 
78     /**
79      * Properties of a node (for IOmxStore)
80      */
81     struct NodeInfo {
82         std::string name;
83         std::vector<Attribute> attributeList;
84         // note: aliases are not exposed here as they are not part of the role map
85     };
86 
87     /**
88      * Properties of a role (for IOmxStore)
89      */
90     struct RoleProperties {
91         std::string type;
92         bool isEncoder;
93         std::multimap<size_t, NodeInfo> nodeList;
94     };
95 
96     typedef std::pair<std::string, RoleProperties> Role;
97     typedef std::map<std::string, RoleProperties> RoleMap;
98 
99     /**
100      * Return a map for attributes that are service-specific.
101      */
102     const AttributeMap& getServiceAttributeMap() const;
103 
104     /**
105      * Return a map for codecs and their properties.
106      */
107     const CodecMap& getCodecMap() const;
108 
109     /**
110      * Return a map for roles and their properties.
111      * This map is generated from the CodecMap.
112      */
113     const RoleMap& getRoleMap() const;
114 
115     /**
116      * Return a common prefix of all node names.
117      *
118      * The prefix is not provided in the xml, so it has to be computed by taking
119      * the longest common prefix of all node names.
120      */
121     const char* getCommonPrefix() const;
122 
123     status_t getParsingStatus() const;
124 
125     /**
126      * Parse top level XML files from a group of search directories.
127      *
128      * @param xmlFiles ordered list of XML file names (no paths)
129      * @param searchDirs ordered list of paths to consider
130      *
131      * @return parsing status
132      */
133     status_t parseXmlFilesInSearchDirs(
134             const std::vector<std::string> &xmlFiles = getDefaultXmlNames(),
135             const std::vector<std::string> &searchDirs = getDefaultSearchDirs());
136 
137 
138     /**
139      * Parse a top level XML file.
140      *
141      * @param path XML file path
142      *
143      * @return parsing status
144      */
145     status_t parseXmlPath(const std::string &path);
146 
147 private:
148     struct Impl;
149     std::shared_ptr<Impl> mImpl;
150 
151     MediaCodecsXmlParser(const MediaCodecsXmlParser&) = delete;
152     MediaCodecsXmlParser& operator=(const MediaCodecsXmlParser&) = delete;
153 };
154 
155 } // namespace android
156 
157 #endif // MEDIA_STAGEFRIGHT_XMLPARSER_H_
158