blob: 0a1536010ba82520ad19450dd1abf91aabbdcc64 [file] [log] [blame]
Changyeon Joc6fa0ab2019-10-12 05:25:44 -07001/*
2 * Copyright (C) 2019 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 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -080016#ifndef ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_CONFIGMANAGER_H
17#define ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_CONFIGMANAGER_H
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070018
19#include "ConfigManagerUtil.h"
20
Changyeon Jo33ba66b2022-01-16 16:33:52 -080021#include <android/hardware/automotive/evs/1.1/types.h>
22#include <log/log.h>
23#include <system/camera_metadata.h>
24#include <tinyxml2.h>
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070025
Changyeon Jo33ba66b2022-01-16 16:33:52 -080026#include <string>
27#include <unordered_map>
28#include <unordered_set>
29#include <vector>
30
31namespace android::hardware::automotive::evs::V1_1::implementation {
32
33using hardware::hidl_vec;
34using hardware::automotive::evs::V1_1::CameraParam;
35using hardware::camera::device::V3_2::Stream;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070036
37/*
38 * Plese note that this is different from what is defined in
39 * libhardware/modules/camera/3_4/metadata/types.h; this has one additional
40 * field to store a framerate.
41 */
42const size_t kStreamCfgSz = 6;
43typedef std::array<int32_t, kStreamCfgSz> RawStreamConfiguration;
44
45class ConfigManager {
Changyeon Jo33ba66b2022-01-16 16:33:52 -080046 public:
47 static std::unique_ptr<ConfigManager> Create(const char* path = "");
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070048 ConfigManager(const ConfigManager&) = delete;
49 ConfigManager& operator=(const ConfigManager&) = delete;
50
51 virtual ~ConfigManager();
52
53 /* Camera device's capabilities and metadata */
54 class CameraInfo {
Changyeon Jo33ba66b2022-01-16 16:33:52 -080055 public:
56 CameraInfo() : characteristics(nullptr) { /* Nothing to do */
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070057 }
58
Changyeon Jo33ba66b2022-01-16 16:33:52 -080059 virtual ~CameraInfo() { free_camera_metadata(characteristics); }
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070060
61 /* Allocate memory for camera_metadata_t */
62 bool allocate(size_t entry_cap, size_t data_cap) {
63 if (characteristics != nullptr) {
64 ALOGE("Camera metadata is already allocated");
65 return false;
66 }
67
68 characteristics = allocate_camera_metadata(entry_cap, data_cap);
69 return characteristics != nullptr;
70 }
71
72 /*
Changyeon Jocb877f92020-08-06 13:14:43 -070073 * List of supported controls that the primary client can program.
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070074 * Paraemters are stored with its valid range
75 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -080076 std::unordered_map<CameraParam, std::tuple<int32_t, int32_t, int32_t>> controls;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070077
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070078 /*
79 * List of supported output stream configurations; each array stores
80 * format, width, height, and direction values in the order.
81 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -080082 std::unordered_map<int32_t, RawStreamConfiguration> streamConfigurations;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070083
84 /*
85 * Internal storage for camera metadata. Each entry holds a pointer to
86 * data and number of elements
87 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -080088 std::unordered_map<camera_metadata_tag_t, std::pair<std::unique_ptr<void*>, size_t>>
89 cameraMetadata;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070090
91 /* Camera module characteristics */
Changyeon Jo33ba66b2022-01-16 16:33:52 -080092 camera_metadata_t* characteristics;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070093 };
94
Changyeon Jo56c9b372019-10-09 14:04:31 -070095 class CameraGroupInfo : public CameraInfo {
Changyeon Jo33ba66b2022-01-16 16:33:52 -080096 public:
Changyeon Jo56c9b372019-10-09 14:04:31 -070097 CameraGroupInfo() {}
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070098
99 /* ID of member camera devices */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800100 std::unordered_set<std::string> devices;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700101
102 /* The capture operation of member camera devices are synchronized */
103 bool synchronized = false;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700104 };
105
106 class SystemInfo {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800107 public:
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700108 /* number of available cameras */
109 int32_t numCameras = 0;
110 };
111
112 class DisplayInfo {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800113 public:
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700114 /*
115 * List of supported input stream configurations; each array stores
116 * format, width, height, and direction values in the order.
117 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800118 std::unordered_map<int32_t, RawStreamConfiguration> streamConfigurations;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700119 };
120
121 /*
122 * Return system information
123 *
124 * @return SystemInfo
125 * Constant reference of SystemInfo.
126 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800127 const SystemInfo& getSystemInfo() { return mSystemInfo; }
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700128
129 /*
130 * Return a list of cameras
131 *
132 * This function assumes that it is not being called frequently.
133 *
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800134 * @return std::vector<std::string>
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700135 * A vector that contains unique camera device identifiers.
136 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800137 std::vector<std::string> getCameraList() {
138 std::vector<std::string> aList;
139 for (auto& v : mCameraInfo) {
140 aList.push_back(v.first);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700141 }
142
143 return aList;
144 }
145
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700146 /*
147 * Return a list of cameras
148 *
Changyeon Jo56c9b372019-10-09 14:04:31 -0700149 * @return CameraGroupInfo
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700150 * A pointer to a camera group identified by a given id.
151 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800152 std::unique_ptr<CameraGroupInfo>& getCameraGroupInfo(const std::string& gid) {
Changyeon Jo56c9b372019-10-09 14:04:31 -0700153 return mCameraGroupInfos[gid];
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700154 }
155
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700156 /*
157 * Return a camera metadata
158 *
159 * @param cameraId
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800160 * Unique camera node identifier in std::string
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700161 *
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800162 * @return std::unique_ptr<CameraInfo>
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700163 * A pointer to CameraInfo that is associated with a given camera
164 * ID. This returns a null pointer if this does not recognize a
165 * given camera identifier.
166 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800167 std::unique_ptr<CameraInfo>& getCameraInfo(const std::string cameraId) noexcept {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700168 return mCameraInfo[cameraId];
169 }
170
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800171 private:
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700172 /* Constructors */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800173 ConfigManager(const char* xmlPath) : mConfigFilePath(xmlPath) {}
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700174
175 /* System configuration */
176 SystemInfo mSystemInfo;
177
178 /* Internal data structure for camera device information */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800179 std::unordered_map<std::string, std::unique_ptr<CameraInfo>> mCameraInfo;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700180
181 /* Internal data structure for camera device information */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800182 std::unordered_map<std::string, std::unique_ptr<DisplayInfo>> mDisplayInfo;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700183
Changyeon Jo56c9b372019-10-09 14:04:31 -0700184 /* Camera groups are stored in <groud id, CameraGroupInfo> hash map */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800185 std::unordered_map<std::string, std::unique_ptr<CameraGroupInfo>> mCameraGroupInfos;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700186
187 /*
188 * Camera positions are stored in <position, camera id set> hash map.
189 * The position must be one of front, rear, left, and right.
190 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800191 std::unordered_map<std::string, std::unordered_set<std::string>> mCameraPosition;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700192
193 /* A path to XML configuration file */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800194 const char* mConfigFilePath;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700195
196 /*
197 * Parse a given EVS configuration file and store the information
198 * internally.
199 *
200 * @return bool
201 * True if it completes parsing a file successfully.
202 */
203 bool readConfigDataFromXML() noexcept;
204
205 /*
206 * read the information of the vehicle
207 *
208 * @param aSysElem
209 * A pointer to "system" XML element.
210 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800211 void readSystemInfo(const tinyxml2::XMLElement* const aSysElem);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700212
213 /*
214 * read the information of camera devices
215 *
216 * @param aCameraElem
217 * A pointer to "camera" XML element that may contain multiple
218 * "device" elements.
219 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800220 void readCameraInfo(const tinyxml2::XMLElement* const aCameraElem);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700221
222 /*
223 * read display device information
224 *
225 * @param aDisplayElem
226 * A pointer to "display" XML element that may contain multiple
227 * "device" elements.
228 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800229 void readDisplayInfo(const tinyxml2::XMLElement* const aDisplayElem);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700230
231 /*
232 * read camera device information
233 *
Changyeon Jo56c9b372019-10-09 14:04:31 -0700234 * @param aCamera
235 * A pointer to CameraInfo that will be completed by this
236 * method.
237 * aDeviceElem
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700238 * A pointer to "device" XML element that contains camera module
239 * capability info and its characteristics.
240 *
Changyeon Jo56c9b372019-10-09 14:04:31 -0700241 * @return bool
242 * Return false upon any failure in reading and processing camera
243 * device information.
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700244 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800245 bool readCameraDeviceInfo(CameraInfo* aCamera, const tinyxml2::XMLElement* aDeviceElem);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700246
247 /*
248 * read camera metadata
249 *
250 * @param aCapElem
251 * A pointer to "cap" XML element.
252 * @param aCamera
253 * A pointer to CameraInfo that is being filled by this method.
254 * @param dataSize
255 * Required size of memory to store camera metadata found in this
256 * method. This is calculated in this method and returned to the
257 * caller for camera_metadata allocation.
258 *
259 * @return size_t
260 * Number of camera metadata entries
261 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800262 size_t readCameraCapabilities(const tinyxml2::XMLElement* const aCapElem, CameraInfo* aCamera,
263 size_t& dataSize);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700264
265 /*
266 * read camera metadata
267 *
268 * @param aParamElem
269 * A pointer to "characteristics" XML element.
270 * @param aCamera
271 * A pointer to CameraInfo that is being filled by this method.
272 * @param dataSize
273 * Required size of memory to store camera metadata found in this
274 * method.
275 *
276 * @return size_t
277 * Number of camera metadata entries
278 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800279 size_t readCameraMetadata(const tinyxml2::XMLElement* const aParamElem, CameraInfo* aCamera,
280 size_t& dataSize);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700281
282 /*
283 * construct camera_metadata_t from camera capabilities and metadata
284 *
285 * @param aCamera
286 * A pointer to CameraInfo that is being filled by this method.
287 * @param totalEntries
288 * Number of camera metadata entries to be added.
289 * @param totalDataSize
290 * Sum of sizes of camera metadata entries to be added.
291 *
292 * @return bool
293 * False if either it fails to allocate memory for camera metadata
294 * or its size is not large enough to add all found camera metadata
295 * entries.
296 */
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800297 bool constructCameraMetadata(CameraInfo* aCamera, const size_t totalEntries,
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700298 const size_t totalDataSize);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700299};
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700300
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800301} // namespace android::hardware::automotive::evs::V1_1::implementation
302#endif // ANDROID_HARDWARE_AUTOMOTIVE_EVS_V1_1_CONFIGMANAGER_H