Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | #ifndef CONFIG_MANAGER_H |
| 17 | #define CONFIG_MANAGER_H |
| 18 | |
| 19 | #include <vector> |
| 20 | #include <string> |
| 21 | #include <unordered_map> |
| 22 | #include <unordered_set> |
| 23 | |
| 24 | #include <tinyxml2.h> |
| 25 | |
| 26 | #include <system/camera_metadata.h> |
| 27 | #include <log/log.h> |
| 28 | #include <android/hardware/automotive/evs/1.1/types.h> |
| 29 | |
| 30 | #include "ConfigManagerUtil.h" |
| 31 | |
| 32 | using namespace std; |
| 33 | using namespace tinyxml2; |
| 34 | |
| 35 | using ::android::hardware::hidl_vec; |
| 36 | using ::android::hardware::camera::device::V3_2::Stream; |
| 37 | using ::android::hardware::automotive::evs::V1_1::CameraParam; |
| 38 | |
| 39 | /* |
| 40 | * Plese note that this is different from what is defined in |
| 41 | * libhardware/modules/camera/3_4/metadata/types.h; this has one additional |
| 42 | * field to store a framerate. |
| 43 | */ |
| 44 | const size_t kStreamCfgSz = 6; |
| 45 | typedef std::array<int32_t, kStreamCfgSz> RawStreamConfiguration; |
| 46 | |
| 47 | class ConfigManager { |
| 48 | public: |
| 49 | static std::unique_ptr<ConfigManager> Create(const char *path = ""); |
| 50 | ConfigManager(const ConfigManager&) = delete; |
| 51 | ConfigManager& operator=(const ConfigManager&) = delete; |
| 52 | |
| 53 | virtual ~ConfigManager(); |
| 54 | |
| 55 | /* Camera device's capabilities and metadata */ |
| 56 | class CameraInfo { |
| 57 | public: |
| 58 | CameraInfo() : |
| 59 | characteristics(nullptr) { |
| 60 | /* Nothing to do */ |
| 61 | } |
| 62 | |
| 63 | virtual ~CameraInfo() { |
| 64 | free_camera_metadata(characteristics); |
| 65 | } |
| 66 | |
| 67 | /* Allocate memory for camera_metadata_t */ |
| 68 | bool allocate(size_t entry_cap, size_t data_cap) { |
| 69 | if (characteristics != nullptr) { |
| 70 | ALOGE("Camera metadata is already allocated"); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | characteristics = allocate_camera_metadata(entry_cap, data_cap); |
| 75 | return characteristics != nullptr; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * List of supported controls that the master client can program. |
| 80 | * Paraemters are stored with its valid range |
| 81 | */ |
| 82 | unordered_map<CameraParam, |
| 83 | tuple<int32_t, int32_t, int32_t>> controls; |
| 84 | |
| 85 | /* List of supported frame rates */ |
| 86 | unordered_set<int32_t> frameRates; |
| 87 | |
| 88 | /* |
| 89 | * List of supported output stream configurations; each array stores |
| 90 | * format, width, height, and direction values in the order. |
| 91 | */ |
| 92 | unordered_map<int32_t, RawStreamConfiguration> streamConfigurations; |
| 93 | |
| 94 | /* |
| 95 | * Internal storage for camera metadata. Each entry holds a pointer to |
| 96 | * data and number of elements |
| 97 | */ |
| 98 | unordered_map<camera_metadata_tag_t, |
| 99 | pair<unique_ptr<void *>, size_t>> cameraMetadata; |
| 100 | |
| 101 | /* Camera module characteristics */ |
| 102 | camera_metadata_t *characteristics; |
| 103 | }; |
| 104 | |
| 105 | class CameraGroup { |
| 106 | public: |
| 107 | CameraGroup() {} |
| 108 | |
| 109 | /* ID of member camera devices */ |
| 110 | unordered_set<string> devices; |
| 111 | |
| 112 | /* The capture operation of member camera devices are synchronized */ |
| 113 | bool synchronized = false; |
| 114 | |
| 115 | /* |
| 116 | * List of stream configurations that are supposed by all camera devices |
| 117 | * in this group. |
| 118 | */ |
| 119 | unordered_map<int32_t, RawStreamConfiguration> streamConfigurations; |
| 120 | }; |
| 121 | |
| 122 | class SystemInfo { |
| 123 | public: |
| 124 | /* number of available cameras */ |
| 125 | int32_t numCameras = 0; |
| 126 | }; |
| 127 | |
| 128 | class DisplayInfo { |
| 129 | public: |
| 130 | /* |
| 131 | * List of supported input stream configurations; each array stores |
| 132 | * format, width, height, and direction values in the order. |
| 133 | */ |
| 134 | unordered_map<int32_t, RawStreamConfiguration> streamConfigurations; |
| 135 | }; |
| 136 | |
| 137 | /* |
| 138 | * Return system information |
| 139 | * |
| 140 | * @return SystemInfo |
| 141 | * Constant reference of SystemInfo. |
| 142 | */ |
| 143 | const SystemInfo &getSystemInfo() { |
| 144 | return mSystemInfo; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Return a list of cameras |
| 149 | * |
| 150 | * This function assumes that it is not being called frequently. |
| 151 | * |
| 152 | * @return vector<string> |
| 153 | * A vector that contains unique camera device identifiers. |
| 154 | */ |
| 155 | vector<string> getCameraList() { |
| 156 | vector<string> aList; |
| 157 | for (auto &v : mCameraInfo) { |
| 158 | aList.emplace_back(v.first); |
| 159 | } |
| 160 | |
| 161 | return aList; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * Return a list of cameras |
| 167 | * |
| 168 | * @return CameraGroup |
| 169 | * A pointer to a camera group identified by a given id. |
| 170 | */ |
| 171 | unique_ptr<CameraGroup>& getCameraGroup(const string& gid) { |
| 172 | return mCameraGroups[gid]; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | * Return a camera metadata |
| 178 | * |
| 179 | * @param cameraId |
| 180 | * Unique camera node identifier in string |
| 181 | * |
| 182 | * @return unique_ptr<CameraInfo> |
| 183 | * A pointer to CameraInfo that is associated with a given camera |
| 184 | * ID. This returns a null pointer if this does not recognize a |
| 185 | * given camera identifier. |
| 186 | */ |
| 187 | unique_ptr<CameraInfo>& getCameraInfo(const string cameraId) noexcept { |
| 188 | return mCameraInfo[cameraId]; |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | /* Constructors */ |
| 193 | ConfigManager(const char *xmlPath) : |
| 194 | mConfigFilePath(xmlPath) { |
| 195 | } |
| 196 | |
| 197 | /* System configuration */ |
| 198 | SystemInfo mSystemInfo; |
| 199 | |
| 200 | /* Internal data structure for camera device information */ |
| 201 | unordered_map<string, unique_ptr<CameraInfo>> mCameraInfo; |
| 202 | |
| 203 | /* Internal data structure for camera device information */ |
| 204 | unordered_map<string, unique_ptr<DisplayInfo>> mDisplayInfo; |
| 205 | |
| 206 | /* Camera groups are stored in <groud id, CameraGroup> hash map */ |
| 207 | unordered_map<string, unique_ptr<CameraGroup>> mCameraGroups; |
| 208 | |
| 209 | /* |
| 210 | * Camera positions are stored in <position, camera id set> hash map. |
| 211 | * The position must be one of front, rear, left, and right. |
| 212 | */ |
| 213 | unordered_map<string, unordered_set<string>> mCameraPosition; |
| 214 | |
| 215 | /* A path to XML configuration file */ |
| 216 | const char *mConfigFilePath; |
| 217 | |
| 218 | /* |
| 219 | * Parse a given EVS configuration file and store the information |
| 220 | * internally. |
| 221 | * |
| 222 | * @return bool |
| 223 | * True if it completes parsing a file successfully. |
| 224 | */ |
| 225 | bool readConfigDataFromXML() noexcept; |
| 226 | |
| 227 | /* |
| 228 | * read the information of the vehicle |
| 229 | * |
| 230 | * @param aSysElem |
| 231 | * A pointer to "system" XML element. |
| 232 | */ |
| 233 | void readSystemInfo(const XMLElement * const aSysElem); |
| 234 | |
| 235 | /* |
| 236 | * read the information of camera devices |
| 237 | * |
| 238 | * @param aCameraElem |
| 239 | * A pointer to "camera" XML element that may contain multiple |
| 240 | * "device" elements. |
| 241 | */ |
| 242 | void readCameraInfo(const XMLElement * const aCameraElem); |
| 243 | |
| 244 | /* |
| 245 | * read display device information |
| 246 | * |
| 247 | * @param aDisplayElem |
| 248 | * A pointer to "display" XML element that may contain multiple |
| 249 | * "device" elements. |
| 250 | */ |
| 251 | void readDisplayInfo(const XMLElement * const aDisplayElem); |
| 252 | |
| 253 | /* |
| 254 | * read camera device information |
| 255 | * |
| 256 | * @param aDeviceElem |
| 257 | * A pointer to "device" XML element that contains camera module |
| 258 | * capability info and its characteristics. |
| 259 | * |
| 260 | * @return unique_ptr<CameraInfo> |
| 261 | * A pointer to CameraInfo class that contains camera module |
| 262 | * capability and characteristics. Please note that this transfers |
| 263 | * the ownership of created CameraInfo to the caller. |
| 264 | */ |
| 265 | unique_ptr<CameraInfo> readCameraDeviceInfo(const XMLElement *aDeviceElem); |
| 266 | |
| 267 | /* |
| 268 | * read camera metadata |
| 269 | * |
| 270 | * @param aCapElem |
| 271 | * A pointer to "cap" XML element. |
| 272 | * @param aCamera |
| 273 | * A pointer to CameraInfo that is being filled by this method. |
| 274 | * @param dataSize |
| 275 | * Required size of memory to store camera metadata found in this |
| 276 | * method. This is calculated in this method and returned to the |
| 277 | * caller for camera_metadata allocation. |
| 278 | * |
| 279 | * @return size_t |
| 280 | * Number of camera metadata entries |
| 281 | */ |
| 282 | size_t readCameraCapabilities(const XMLElement * const aCapElem, |
| 283 | unique_ptr<CameraInfo> &aCamera, |
| 284 | size_t &dataSize); |
| 285 | |
| 286 | /* |
| 287 | * read camera metadata |
| 288 | * |
| 289 | * @param aParamElem |
| 290 | * A pointer to "characteristics" XML element. |
| 291 | * @param aCamera |
| 292 | * A pointer to CameraInfo that is being filled by this method. |
| 293 | * @param dataSize |
| 294 | * Required size of memory to store camera metadata found in this |
| 295 | * method. |
| 296 | * |
| 297 | * @return size_t |
| 298 | * Number of camera metadata entries |
| 299 | */ |
| 300 | size_t readCameraMetadata(const XMLElement * const aParamElem, |
| 301 | unique_ptr<CameraInfo> &aCamera, |
| 302 | size_t &dataSize); |
| 303 | |
| 304 | /* |
| 305 | * construct camera_metadata_t from camera capabilities and metadata |
| 306 | * |
| 307 | * @param aCamera |
| 308 | * A pointer to CameraInfo that is being filled by this method. |
| 309 | * @param totalEntries |
| 310 | * Number of camera metadata entries to be added. |
| 311 | * @param totalDataSize |
| 312 | * Sum of sizes of camera metadata entries to be added. |
| 313 | * |
| 314 | * @return bool |
| 315 | * False if either it fails to allocate memory for camera metadata |
| 316 | * or its size is not large enough to add all found camera metadata |
| 317 | * entries. |
| 318 | */ |
| 319 | bool constructCameraMetadata(unique_ptr<CameraInfo> &aCamera, |
| 320 | const size_t totalEntries, |
| 321 | const size_t totalDataSize); |
| 322 | |
| 323 | /* |
| 324 | * parse a comma-separated list of camera devices and add them to |
| 325 | * CameraGroup. |
| 326 | * |
| 327 | * @param devices |
| 328 | * A comma-separated list of camera device identifiers. |
| 329 | * @param aGroup |
| 330 | * Camera group which cameras will be added to. |
| 331 | */ |
| 332 | void addCameraDevices(const char *devices, |
| 333 | unique_ptr<CameraGroup> &aGroup); |
| 334 | }; |
| 335 | #endif // CONFIG_MANAGER_H |
| 336 | |