1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FORMAT_H 17 #define FORMAT_H 18 19 #include <string> 20 #include <map> 21 #include <vector> 22 23 namespace OHOS { 24 namespace MediaAVCodec { 25 enum FormatDataType : uint32_t { 26 /* None */ 27 FORMAT_TYPE_NONE, 28 /* Int32 */ 29 FORMAT_TYPE_INT32, 30 /* Int64 */ 31 FORMAT_TYPE_INT64, 32 /* Float */ 33 FORMAT_TYPE_FLOAT, 34 /* Double */ 35 FORMAT_TYPE_DOUBLE, 36 /* String */ 37 FORMAT_TYPE_STRING, 38 /* Addr */ 39 FORMAT_TYPE_ADDR, 40 }; 41 42 struct FormatData { 43 FormatDataType type = FORMAT_TYPE_NONE; 44 union Val { 45 int32_t int32Val; 46 int64_t int64Val; 47 float floatVal; 48 double doubleVal; 49 } val = {0}; 50 std::string stringVal = ""; 51 uint8_t *addr = nullptr; 52 size_t size = 0; 53 }; 54 55 class __attribute__((visibility("default"))) Format { 56 public: 57 Format() = default; 58 ~Format(); 59 60 Format(const Format &rhs); 61 Format(Format &&rhs) noexcept; 62 Format &operator=(const Format &rhs); 63 Format &operator=(Format &&rhs) noexcept; 64 65 /** 66 * @brief Sets metadata of the integer type. 67 * 68 * @param key Indicates the metadata key. 69 * @param value Indicates the metadata value, which is a 32-bit integer. 70 * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise. 71 * @since 10 72 * @version 1.0 73 */ 74 bool PutIntValue(const std::string_view &key, int32_t value); 75 76 /** 77 * @brief Sets metadata of the long integer type. 78 * 79 * @param key Indicates the metadata key. 80 * @param value Indicates the metadata value, which is a 64-bit integer. 81 * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise. 82 * @since 10 83 * @version 1.0 84 */ 85 bool PutLongValue(const std::string_view &key, int64_t value); 86 87 /** 88 * @brief Sets metadata of the single-precision floating-point type. 89 * 90 * @param key Indicates the metadata key. 91 * @param value Indicates the metadata value, which is a single-precision floating-point number. 92 * @return Returns <b>true</b> if the metadata is successfully set; returns <b>false</b> otherwise. 93 * @since 10 94 * @version 1.0 95 */ 96 bool PutFloatValue(const std::string_view &key, float value); 97 98 /** 99 * @brief Sets metadata of the double-precision floating-point type. 100 * 101 * @param key Indicates the metadata key. 102 * @param value Indicates the metadata value, which is a double-precision floating-point number. 103 * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise. 104 * @since 10 105 * @version 1.0 106 */ 107 bool PutDoubleValue(const std::string_view &key, double value); 108 109 /** 110 * @brief Sets metadata of the string type. 111 * 112 * @param key Indicates the metadata key. 113 * @param value Indicates the metadata value, which is a string. 114 * @return Returns <b>true</b> if the metadata is successfully set; returns <b>false</b> otherwise. 115 * @since 10 116 * @version 1.0 117 */ 118 bool PutStringValue(const std::string_view &key, const std::string_view &value); 119 120 /** 121 * @brief Sets metadata of the string type. 122 * 123 * @param key Indicates the metadata key. 124 * @param addr Indicates the metadata addr, which is a uint8_t *. 125 * @param size Indicates the metadata addr size, which is a size_t. 126 * @return Returns <b>true</b> if the metadata is successfully set; returns <b>false</b> otherwise. 127 * @since 10 128 * @version 1.0 129 */ 130 bool PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size); 131 132 /** 133 * @brief Obtains the metadata value of the integer type. 134 * 135 * @param key Indicates the metadata key. 136 * @param value Indicates the metadata value to obtain, which is a 32-bit integer. 137 * @return Returns <b>true</b> if the integer is successfully obtained; returns <b>false</b> otherwise. 138 * @since 10 139 * @version 1.0 140 */ 141 bool GetIntValue(const std::string_view &key, int32_t &value) const; 142 143 /** 144 * @brief Obtains the metadata value of the long integer type. 145 * 146 * @param key Indicates the metadata key. 147 * @param value Indicates the metadata value to obtain, which is a 64-bit long integer. 148 * @return Returns <b>true</b> if the integer is successfully obtained; returns <b>false</b> otherwise. 149 * @since 10 150 * @version 1.0 151 */ 152 bool GetLongValue(const std::string_view &key, int64_t &value) const; 153 154 /** 155 * @brief Obtains the metadata value of the single-precision floating-point type. 156 * 157 * @param key Indicates the metadata key. 158 * @param value Indicates the metadata value to obtain, which is a single-precision floating-point number. 159 * @return Returns <b>true</b> if the single-precision number is successfully obtained; returns 160 * <b>false</b> otherwise. 161 * @since 10 162 * @version 1.0 163 */ 164 bool GetFloatValue(const std::string_view &key, float &value) const; 165 166 /** 167 * @brief Obtains the metadata value of the double-precision floating-point type. 168 * 169 * @param key Indicates the metadata key. 170 * @param value Indicates the metadata value to obtain, which is a double-precision floating-point number. 171 * @return Returns <b>true</b> if the double-precision number is successfully obtained; returns 172 * <b>false</b> otherwise. 173 * @since 10 174 * @version 1.0 175 */ 176 bool GetDoubleValue(const std::string_view &key, double &value) const; 177 178 /** 179 * @brief Obtains the metadata value of the string type. 180 * 181 * @param key Indicates the metadata key. 182 * @param value Indicates the metadata value to obtain, which is a string. 183 * @return Returns <b>true</b> if the string is successfully obtained; returns <b>false</b> otherwise. 184 * @since 10 185 * @version 1.0 186 */ 187 bool GetStringValue(const std::string_view &key, std::string &value) const; 188 189 /** 190 * @brief Obtains the metadata value of the string type. 191 * 192 * @param key Indicates the metadata key. 193 * @param addr Indicates the metadata addr to obtain, which is a uint8_t **. 194 * @param size Indicates the metadata addr size to obtain, which is a size_t. 195 * @return Returns <b>true</b> if the string is successfully obtained; returns <b>false</b> otherwise. 196 * @since 10 197 * @version 1.0 198 */ 199 bool GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size) const; 200 201 /** 202 * @brief Query whether the key exists in this Format. 203 * 204 * @param key Indicates the metadata key. 205 * @return true 206 * @return false 207 */ 208 bool ContainKey(const std::string_view &key) const; 209 210 /** 211 * @brief Get the value type for the key if the key exists in this Format. 212 * 213 * @param key Indicates the metadata key. 214 * @return FormatDataType. If the key does not exists, return FORMAT_TYPE_NONE. 215 */ 216 FormatDataType GetValueType(const std::string_view &key) const; 217 218 /** 219 * @brief Remove the key from the Format 220 * 221 * @param keys the key will be removed. 222 */ 223 void RemoveKey(const std::string_view &key); 224 225 /** 226 * @brief A trick to enable the comparision between the std::string and std::string_view for 227 * std::map, the trick called Transparent Comparator. 228 * 229 */ 230 using FormatDataMap = std::map<std::string, FormatData, std::less<>>; 231 232 /** 233 * @brief Obtains the metadata map. 234 * 235 * @return Returns the map object. 236 * @since 10 237 * @version 1.0 238 */ 239 const FormatDataMap &GetFormatMap() const; 240 241 /** 242 * @brief Convert the metadata map to string. 243 * 244 * @return Returns a converted string. 245 * @since 10 246 * @version 1.0 247 */ 248 std::string Stringify() const; 249 250 private: 251 FormatDataMap formatMap_; 252 }; 253 } // namespace MediaAVCodec 254 } // namespace OHOS 255 #endif // FORMAT_H 256