1 /* 2 * Copyright (C) 2021 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 AVMETADATAHELPER_H 17 #define AVMETADATAHELPER_H 18 19 #include <string> 20 #include <unordered_map> 21 #include <memory> 22 #include "pixel_map.h" 23 #include "nocopyable.h" 24 #include "avsharedmemory.h" 25 26 namespace OHOS { 27 namespace Media { 28 /** 29 * @brief Enumerates avmetadata usage. 30 */ 31 enum AVMetadataUsage : int32_t { 32 /** 33 * Indicates that the avmetadahelper's instance will only be used for resolving the 34 * metadata from the given media resource. 35 */ 36 AV_META_USAGE_META_ONLY, 37 /** 38 * Indicates that the avmetadahelper's instance will be used for fetching the video frame 39 * and resolving metadata from the given media resource. 40 */ 41 AV_META_USAGE_PIXEL_MAP, 42 }; 43 44 /** 45 * @brief Enumerates avmetadata's metadata key. 46 */ 47 enum AVMetadataCode : int32_t { 48 /** 49 * The metadata key to retrieve the information about the album title 50 * of the media source. 51 */ 52 AV_KEY_ALBUM = 0, 53 /** 54 * The metadata key to retrieve the information about the performers or 55 * artist associated with the media source. 56 */ 57 AV_KEY_ALBUM_ARTIST = 1, 58 /** 59 * The metadata key to retrieve the information about the artist of 60 * the media source. 61 */ 62 AV_KEY_ARTIST = 2, 63 /** 64 * The metadata key to retrieve the information about the author of 65 * the media source. 66 */ 67 AV_KEY_AUTHOR = 3, 68 /** 69 * The metadata key to retrieve the information about the created time of 70 * the media source. 71 */ 72 AV_KEY_DATE_TIME = 4, 73 /** 74 * The metadata key to retrieve the information about the created time of 75 * the media source. This keyword is provided for the media library. 76 */ 77 AV_KEY_DATE_TIME_FORMAT = 5, 78 /** 79 * The metadata key to retrieve the information about the composer of 80 * the media source. 81 */ 82 AV_KEY_COMPOSER = 12, 83 /** 84 * The metadata key to retrieve the playback duration of the media source. 85 */ 86 AV_KEY_DURATION = 15, 87 /** 88 * The metadata key to retrieve the content type or genre of the data 89 * source. 90 */ 91 AV_KEY_GENRE = 18, 92 /** 93 * If this key exists the media contains audio content. 94 */ 95 AV_KEY_HAS_AUDIO = 19, 96 /** 97 * If this key exists the media contains video content. 98 */ 99 AV_KEY_HAS_VIDEO = 21, 100 /** 101 * The metadata key to retrieve the mime type of the media source. Some 102 * example mime types include: "video/mp4", "audio/mp4", "audio/amr-wb", 103 * etc. 104 */ 105 AV_KEY_MIME_TYPE = 29, 106 /** 107 * The metadata key to retrieve the number of tracks, such as audio, video, 108 * text, in the media source, such as a mp4 or 3gpp file. 109 */ 110 AV_KEY_NUM_TRACKS = 30, 111 /** 112 * This key retrieves the sample rate, if available. 113 */ 114 AV_KEY_SAMPLE_RATE = 31, 115 /** 116 * The metadata key to retrieve the media source title. 117 */ 118 AV_KEY_TITLE = 33, 119 /** 120 * If the media contains video, this key retrieves its height. 121 */ 122 AV_KEY_VIDEO_HEIGHT = 35, 123 /** 124 * If the media contains video, this key retrieves its width. 125 */ 126 AV_KEY_VIDEO_WIDTH = 37, 127 /** 128 * The metadata key to retrieve the information about the video 129 * orientation. 130 */ 131 AV_KEY_VIDEO_ORIENTATION = 38, 132 }; 133 134 /** 135 * @brief Enumerates avmetadata's query option. 136 */ 137 enum AVMetadataQueryOption : int32_t { 138 /** 139 * This option is used to fetch a key frame from the given media 140 * resource that is located right after or at the given time. 141 */ 142 AV_META_QUERY_NEXT_SYNC, 143 /** 144 * This option is used to fetch a key frame from the given media 145 * resource that is located right before or at the given time. 146 */ 147 AV_META_QUERY_PREVIOUS_SYNC, 148 /** 149 * This option is used to fetch a key frame from the given media 150 * resource that is located closest to or at the given time. 151 */ 152 AV_META_QUERY_CLOSEST_SYNC, 153 /** 154 * This option is used to fetch a frame (maybe not keyframe) from 155 * the given media resource that is located closest to or at the given time. 156 */ 157 AV_META_QUERY_CLOSEST, 158 }; 159 160 /** 161 * @brief Provides the definition of the returned pixelmap's configuration 162 */ 163 struct PixelMapParams { 164 /** 165 * Expected pixelmap's width, -1 means to keep consistent with the 166 * original dimensions of the given video resource. 167 */ 168 int32_t dstWidth = -1; 169 /** 170 * Expected pixelmap's width, -1 means to keep consistent with the 171 * original dimensions of the given video resource. 172 */ 173 int32_t dstHeight = -1; 174 /** 175 * Expected pixelmap's color format, see {@link PixelFormat}. Currently, 176 * RGB_565, RGB_888, RGBA_8888 are supported. 177 */ 178 PixelFormat colorFormat = PixelFormat::RGB_565; 179 }; 180 181 /** 182 * @brief Provides the interfaces to resolve metadata or fetch frame 183 * from a given media resource. 184 */ 185 class AVMetadataHelper { 186 public: 187 virtual ~AVMetadataHelper() = default; 188 189 /** 190 * Set the media source uri to resolve. Calling this method before the reset 191 * of the methods in this class. This method maybe time consuming. 192 * @param uri the URI of input media source. 193 * @param usage indicates which scene the avmedatahelper's instance will 194 * be used to, see {@link AVMetadataUsage}. If the usage need to be changed, 195 * this method must be called again. 196 * @return Returns {@link MSERR_OK} if the setting is successful; returns 197 * an error code otherwise. 198 */ 199 virtual int32_t SetSource(const std::string &uri, int32_t usage = AVMetadataUsage::AV_META_USAGE_PIXEL_MAP) = 0; 200 201 /** 202 * @brief Sets the media file descriptor source to resolve. Calling this method 203 * before the reset of the methods in this class. This method maybe time consuming. 204 * @param fd Indicates the file descriptor of media source. 205 * @param offset Indicates the offset of media source in file descriptor. 206 * @param size Indicates the size of media source. 207 * @param usage Indicates which scene the avmedatahelper's instance will 208 * be used to, see {@link AVMetadataUsage}. If the usage need to be changed, 209 * this method must be called again. 210 * @return Returns {@link MSERR_OK} if the setting is successful; returns 211 * an error code otherwise. 212 */ 213 virtual int32_t SetSource(int32_t fd, int64_t offset = 0, int64_t size = 0, 214 int32_t usage = AVMetadataUsage::AV_META_USAGE_PIXEL_MAP) = 0; 215 216 /** 217 * Retrieve the meta data associated with the specified key. This method must be 218 * called after the SetSource. 219 * @param key One of the constants listed above at the definition of {@link AVMetadataCode}. 220 * @return Returns the meta data value associate with the given key code on 221 * success; empty string on failure. 222 */ 223 virtual std::string ResolveMetadata(int32_t key) = 0; 224 225 /** 226 * Retrieve all meta data within the listed above at the definition of {@link AVMetadataCode}. 227 * This method must be called after the SetSource. 228 * @return Returns the meta data values on success; empty hash map on failure. 229 */ 230 virtual std::unordered_map<int32_t, std::string> ResolveMetadata() = 0; 231 232 /** 233 * Fetch the album art picture associated with the data source. If there are 234 * more than one pictures, the cover image will be returned preferably. 235 * @return Returns the a chunk of shared memory containing a picture, which can be 236 * null, if such a picture can not be fetched. 237 */ 238 virtual std::shared_ptr<AVSharedMemory> FetchArtPicture() = 0; 239 240 /** 241 * Fetch a representative video frame near a given timestamp by considering the given 242 * option if possible, and return a pixelmap with given parameters. This method must be 243 * called after the SetSource. 244 * @param timeUs The time position in microseconds where the frame will be fetched. 245 * When fetching the frame at the given time position, there is no guarantee that 246 * the video source has a frame located at the position. When this happens, a frame 247 * nearby will be returned. If timeUs is negative, time position and option will ignored, 248 * and any frame that the implementation considers as representative may be returned. 249 * @param option the hint about how to fetch a frame, see {@link AVMetadataQueryOption} 250 * @param param the desired configuration of returned pixelmap, see {@link PixelMapParams}. 251 * @return Returns a pixelmap containing a scaled video frame, which can be null, if such a 252 * frame cannot be fetched. 253 */ 254 virtual std::shared_ptr<PixelMap> FetchFrameAtTime(int64_t timeUs, int32_t option, const PixelMapParams ¶m) = 0; 255 256 /** 257 * Release the internel resource. After this method called, the avmetadatahelper instance 258 * can not be used again. 259 */ 260 virtual void Release() = 0; 261 }; 262 263 class __attribute__((visibility("default"))) AVMetadataHelperFactory { 264 public: 265 #ifdef UNSUPPORT_METADATA CreateAVMetadataHelper()266 static std::shared_ptr<AVMetadataHelper> CreateAVMetadataHelper() 267 { 268 return nullptr; 269 } 270 #else 271 static std::shared_ptr<AVMetadataHelper> CreateAVMetadataHelper(); 272 #endif 273 private: 274 AVMetadataHelperFactory() = default; 275 ~AVMetadataHelperFactory() = default; 276 }; 277 } // namespace Media 278 } // namespace OHOS 279 #endif // AVMETADATAHELPER_H