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