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 <memory> 20 #include <map> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 #include "buffer/avsharedmemory.h" 25 #include "meta/format.h" 26 #include "meta/meta.h" 27 #include "media_data_source.h" 28 #include "nocopyable.h" 29 #include "pixel_map.h" 30 31 namespace OHOS { 32 namespace Media { 33 /** 34 * @brief Enumerates avmetadata helper callback error type. 35 */ 36 enum HelperErrorType : int32_t { 37 /* State error, current operation is invalid. */ 38 INVALID_OPERATION, 39 /* Result error, current result is invalid. */ 40 INVALID_RESULT, 41 }; 42 43 /** 44 * @brief Enumerates avmetadata helper listener info type. 45 */ 46 enum HelperOnInfoType : int32_t { 47 /* Current State changed, notify NAPI with onInfo callback. */ 48 HELPER_INFO_TYPE_STATE_CHANGE, 49 }; 50 51 /** 52 * @brief Enumerates avmetadata helper states. 53 */ 54 enum HelperStates : int32_t { 55 /* error states */ 56 HELPER_STATE_ERROR = 0, 57 /* idle states */ 58 HELPER_IDLE = 1, 59 /* prepared states */ 60 HELPER_PREPARED = 2, 61 /* call done states */ 62 HELPER_CALL_DONE = 3, 63 /* released states */ 64 HELPER_RELEASED = 4, 65 }; 66 67 enum HelperState : int32_t { 68 /* error states */ 69 HELPER_ERROR = -1, 70 /* idle states */ 71 HELPER_STATE_IDLE, 72 /* RUNNABLE states */ 73 HELPER_STATE_RUNNABLE, 74 /* released states */ 75 HELPER_STATE_RELEASED, 76 }; 77 78 /** 79 * The meta data mappings from meta data enum keys to the string key. 80 */ 81 static const std::map<int32_t, const char*> g_MetadataCodeMap = { 82 {0, "album"}, 83 {1, "albumArtist"}, 84 {2, "artist"}, 85 {3, "author"}, 86 {4, "dateTime"}, 87 {5, "dateTimeFormat"}, 88 {12, "composer"}, 89 {15, "duration"}, 90 {18, "genre"}, 91 {19, "hasAudio"}, 92 {21, "hasVideo"}, 93 {29, "mimeType"}, 94 {30, "trackCount"}, 95 {31, "sampleRate"}, 96 {33, "title"}, 97 {35, "videoHeight"}, 98 {37, "videoWidth"}, 99 {38, "videoOrientation"}, 100 {39, "hdrType"}, 101 {44, "videoRotateOrientation"}, 102 }; 103 104 /** 105 * support metadata parameters 106 */ 107 static const std::vector<std::string> g_Metadata = { 108 "album", 109 "albumArtist", 110 "artist", 111 "author", 112 "dateTime", 113 "dateTimeFormat", 114 "composer", 115 "duration", 116 "genre", 117 "hasAudio", 118 "hasVideo", 119 "mimeType", 120 "trackCount", 121 "sampleRate", 122 "title", 123 "videoHeight", 124 "videoWidth", 125 "videoOrientation", 126 "hdrType", 127 "latitude", 128 "longitude", 129 "customInfo", 130 "tracks", 131 }; 132 133 enum HdrType : int32_t { 134 /** 135 * This option is used to mark none HDR type. 136 */ 137 AV_HDR_TYPE_NONE, 138 /** 139 * This option is used to mark HDR Vivid type. 140 */ 141 AV_HDR_TYPE_VIVID, 142 }; 143 144 enum Scene : int32_t { 145 /** 146 * This option is used to mark normal scene. 147 */ 148 AV_META_SCENE_NORMAL, 149 /** 150 * This option is used to mark clone scene. 151 */ 152 AV_META_SCENE_CLONE, 153 /** 154 * This option is used to mark batch handle avmeta scene. 155 */ 156 AV_META_SCENE_BATCH_HANDLE, 157 }; 158 159 /** 160 * @brief Enumerates avmetadata usage. 161 */ 162 enum AVMetadataUsage : int32_t { 163 /** 164 * Indicates that the avmetadahelper's instance will only be used for resolving the 165 * metadata from the given media resource. 166 */ 167 AV_META_USAGE_META_ONLY, 168 /** 169 * Indicates that the avmetadahelper's instance will be used for fetching the video frame 170 * and resolving metadata from the given media resource. 171 */ 172 AV_META_USAGE_PIXEL_MAP, 173 /** 174 * Only for video frame index convert, if use this usage, can only GetTimeByFrameIndex / GetFrameIndexByTime 175 */ 176 AV_META_USAGE_FRAME_INDEX_CONVERT, 177 }; 178 179 /** 180 * @brief Enumerates avmetadata caller. 181 */ 182 enum AVMetadataCaller : int32_t { 183 /** 184 * Indicates that the avmetadahelper's instance called by AVMetadataExtractor. 185 */ 186 AV_METADATA_EXTRACTOR, 187 /** 188 * Indicates that the avmetadahelper's instance called by AVImageGenerator. 189 */ 190 AV_IMAGE_GENERATOR, 191 /** 192 * Indicates that the avmetadahelper's instance called by except AVMetadataExtractor and AVImageGenerator. 193 */ 194 AV_META_DATA_DEFAULT, 195 }; 196 197 /** 198 * @brief Enumerates avmetadata's metadata key. 199 */ 200 enum AVMetadataCode : int32_t { 201 /** 202 * The metadata key to retrieve the information about the album title 203 * of the media source. 204 */ 205 AV_KEY_ALBUM = 0, 206 /** 207 * The metadata key to retrieve the information about the performers or 208 * artist associated with the media source. 209 */ 210 AV_KEY_ALBUM_ARTIST = 1, 211 /** 212 * The metadata key to retrieve the information about the artist of 213 * the media source. 214 */ 215 AV_KEY_ARTIST = 2, 216 /** 217 * The metadata key to retrieve the information about the author of 218 * the media source. 219 */ 220 AV_KEY_AUTHOR = 3, 221 /** 222 * The metadata key to retrieve the information about the created time of 223 * the media source. 224 */ 225 AV_KEY_DATE_TIME = 4, 226 /** 227 * The metadata key to retrieve the information about the created time of 228 * the media source. This keyword is provided for the media library. 229 */ 230 AV_KEY_DATE_TIME_FORMAT = 5, 231 /** 232 * The metadata key to retrieve the information about the created time of the media source, 233 * using the ISO8601 format. This keyword is provided for the media library. 234 */ 235 AV_KEY_DATE_TIME_ISO8601 = 6, 236 /** 237 * The metadata key to retrieve the information about the composer of 238 * the media source. 239 */ 240 AV_KEY_COMPOSER = 12, 241 /** 242 * The metadata key to retrieve the playback duration of the media source. 243 */ 244 AV_KEY_DURATION = 15, 245 /** 246 * The metadata key to retrieve the content type or genre of the data 247 * source. 248 */ 249 AV_KEY_GENRE = 18, 250 /** 251 * If this key exists the media contains audio content. 252 */ 253 AV_KEY_HAS_AUDIO = 19, 254 /** 255 * If this key exists the media contains video content. 256 */ 257 AV_KEY_HAS_VIDEO = 21, 258 /** 259 * The metadata key to retrieve the mime type of the media source. Some 260 * example mime types include: "video/mp4", "audio/mp4", "audio/amr-wb", 261 * etc. 262 */ 263 AV_KEY_MIME_TYPE = 29, 264 /** 265 * The metadata key to retrieve the number of tracks, such as audio, video, 266 * text, in the media source, such as a mp4 or 3gpp file. 267 */ 268 AV_KEY_NUM_TRACKS = 30, 269 /** 270 * This key retrieves the sample rate, if available. 271 */ 272 AV_KEY_SAMPLE_RATE = 31, 273 /** 274 * The metadata key to retrieve the media source title. 275 */ 276 AV_KEY_TITLE = 33, 277 /** 278 * If the media contains video, this key retrieves its height. 279 */ 280 AV_KEY_VIDEO_HEIGHT = 35, 281 /** 282 * If the media contains video, this key retrieves its width. 283 */ 284 AV_KEY_VIDEO_WIDTH = 37, 285 /** 286 * The metadata key to retrieve the information about the video 287 * rotation. 288 */ 289 AV_KEY_VIDEO_ORIENTATION = 38, 290 /** 291 * The metadata key to retrieve the information about the video 292 * is HDR or not. 293 */ 294 AV_KEY_VIDEO_IS_HDR_VIVID = 39, 295 296 /** 297 * The metadata key to retrieve the information about the location longitude 298 */ 299 AV_KEY_LOCATION_LONGITUDE = 40, 300 301 /** 302 * The metadata key to retrieve the information about the location latitude 303 */ 304 AV_KEY_LOCATION_LATITUDE = 41, 305 306 /** 307 * Custom parameter key-value map 308 */ 309 AV_KEY_CUSTOMINFO = 42, 310 311 /** 312 * Tracks info key-value map 313 */ 314 AV_KEY_TRACKS = 43, 315 /** 316 * The metadata key to retrieve the information about the video 317 * orientation. 318 */ 319 AV_KEY_VIDEO_ROTATE_ORIENTATION = 44, 320 }; 321 322 /** 323 * @brief Enumerates avmetadata's query option. 324 */ 325 enum AVMetadataQueryOption : int32_t { 326 /** 327 * This option is used to fetch a key frame from the given media 328 * resource that is located right after or at the given time. 329 */ 330 AV_META_QUERY_NEXT_SYNC, 331 /** 332 * This option is used to fetch a key frame from the given media 333 * resource that is located right before or at the given time. 334 */ 335 AV_META_QUERY_PREVIOUS_SYNC, 336 /** 337 * This option is used to fetch a key frame from the given media 338 * resource that is located closest to or at the given time. 339 */ 340 AV_META_QUERY_CLOSEST_SYNC, 341 /** 342 * This option is used to fetch a frame (maybe not keyframe) from 343 * the given media resource that is located closest to or at the given time. 344 */ 345 AV_META_QUERY_CLOSEST, 346 }; 347 348 /** 349 * @brief Enumeration for frame scaling modes. 350 * 351 * This enum defines the different ways in which a frame can be scaled. 352 */ 353 enum FrameScaleMode : int32_t { 354 /** 355 * This mode is used to shrink a frame based on user-defined width and height settings. 356 */ 357 NORMAL_RATIO, 358 /** 359 * This mode is used to scale a frame based on user-defined width and height settings, 360 * while also supporting aspect ratio scaling. 361 */ 362 ASPECT_RATIO, 363 }; 364 365 /** 366 * @brief Enumeration for video flip information. 367 * 368 * This enum defines rotation and flip information of the video. 369 */ 370 enum VideoRotateOrientationType : int32_t { 371 /** 372 * No rotation or default 373 */ 374 TOP_LEFT = 1, 375 /** 376 * Flip horizontally 377 */ 378 TOP_RIGHT, 379 /** 380 * Rotate clockwise by 180 degrees 381 */ 382 BOTTOM_RIGHT, 383 /** 384 * Flip vertically 385 */ 386 BOTTOM_LEFT, 387 /** 388 * Flip horizontally and rotate clockwise by 270 degrees 389 */ 390 LEFT_TOP, 391 /** 392 * Rotate clockwise by 90 degrees 393 */ 394 RIGHT_TOP, 395 /** 396 * Flip horizontally and rotate clockwise by 90 degrees 397 */ 398 RIGHT_BOTTOM, 399 /** 400 * Rotate clockwise by 270 degrees 401 */ 402 LEFT_BOTTOM, 403 }; 404 405 /** 406 * @brief Provides the definition of the returned pixelmap's configuration 407 */ 408 struct PixelMapParams { 409 /** 410 * Expected pixelmap's width, -1 means to keep consistent with the 411 * original dimensions of the given video resource. 412 */ 413 int32_t dstWidth = -1; 414 /** 415 * Expected pixelmap's width, -1 means to keep consistent with the 416 * original dimensions of the given video resource. 417 */ 418 int32_t dstHeight = -1; 419 /** 420 * Expected pixelmap's color format, see {@link PixelFormat}. Currently, 421 * RGB_565, RGB_888, RGBA_8888 are supported. 422 */ 423 PixelFormat colorFormat = PixelFormat::RGB_565; 424 /** 425 * Expected whether it supports flip, false is the default value, 426 * indicating that flipping is not required, true indicates that flipping is required 427 */ 428 bool isSupportFlip = false; 429 }; 430 431 /** 432 * @brief Provides the callback interfaces to notify client about errors or infos. 433 */ 434 class HelperCallback { 435 public: 436 virtual ~HelperCallback() = default; 437 /** 438 * Called when a metadata helper message is notified. 439 * 440 * @param type Indicates the information type. For details, see {@link HelperOnInfoType}. 441 * @param extra Indicates other information, for example, the current state in metadata helper server. 442 * @param infoBody According to the info type, the information carrier passed. Is an optional parameter. 443 */ 444 virtual void OnInfo(HelperOnInfoType type, int32_t extra, const Format &infoBody = {}) = 0; 445 446 /** 447 * Called when an error occurred 448 * 449 * @param errorCode Error code. 450 * @param errorMsg Error message. 451 */ 452 virtual void OnError(int32_t errorCode, const std::string &errorMsg) = 0; 453 }; 454 455 /** 456 * @brief Provides the interfaces to resolve metadata or fetch frame 457 * from a given media resource. 458 */ 459 class AVMetadataHelper { 460 public: 461 virtual ~AVMetadataHelper() = default; 462 463 /** 464 * Set the media source uri to resolve. Calling this method before the reset 465 * of the methods in this class. This method maybe time consuming. 466 * @param uri the URI of input media source. 467 * @param usage indicates which scene the avmedatahelper's instance will 468 * be used to, see {@link AVMetadataUsage}. If the usage need to be changed, 469 * this method must be called again. 470 * @return Returns {@link MSERR_OK} if the setting is successful; returns 471 * an error code otherwise. 472 */ 473 virtual int32_t SetSource(const std::string &uri, int32_t usage = AVMetadataUsage::AV_META_USAGE_PIXEL_MAP) = 0; 474 475 /** 476 * Set the caller. Calling this method before the reset 477 * of the methods in this class. This method maybe time consuming. 478 * @param caller indicates which scene the avmedatahelper's instance will 479 * be used to, see {@link AVMetadataCaller}. If the caller need to be changed, 480 * this method must be called again. 481 * @return Returns {@link MSERR_OK} if the setting is successful; returns 482 * an error code otherwise. 483 */ SetAVMetadataCaller(AVMetadataCaller caller)484 virtual int32_t SetAVMetadataCaller(AVMetadataCaller caller) 485 { 486 (void)caller; 487 return 0; 488 } 489 490 /** 491 * Set the media source online uri with header params to resolve. Calling this method before the reset 492 * of the methods in this class. This method maybe time consuming. 493 * @param uri the URI of input http/https on demand media source. 494 * @param header the request parameters of input media source. 495 * @return Returns {@link MSERR_OK} if the setting is successful; returns 496 * an error code otherwise. 497 */ SetUrlSource(const std::string & uri,const std::map<std::string,std::string> & header)498 virtual int32_t SetUrlSource(const std::string &uri, const std::map<std::string, std::string> &header) 499 { 500 (void)uri; 501 (void)header; 502 return 0; 503 } 504 505 /** 506 * @brief Sets the media file descriptor source to resolve. Calling this method 507 * before the reset of the methods in this class. This method maybe time consuming. 508 * @param fd Indicates the file descriptor of media source. 509 * @param offset Indicates the offset of media source in file descriptor. 510 * @param size Indicates the size of media source. 511 * @param usage Indicates which scene the avmedatahelper's instance will 512 * be used to, see {@link AVMetadataUsage}. If the usage need to be changed, 513 * this method must be called again. 514 * @return Returns {@link MSERR_OK} if the setting is successful; returns 515 * an error code otherwise. 516 */ 517 virtual int32_t SetSource(int32_t fd, int64_t offset = 0, int64_t size = 0, 518 int32_t usage = AVMetadataUsage::AV_META_USAGE_PIXEL_MAP) = 0; 519 520 /** 521 * @brief Sets the playback media data source for the meta data helper. 522 * 523 * @param dataSrc Indicates the media data source. in {@link media_data_source.h} 524 * @return Returns {@link MSERR_OK} if the mediadatasource is set successfully; returns an error code defined 525 * in {@link media_errors.h} otherwise. 526 */ 527 virtual int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) = 0; 528 529 /** 530 * Retrieve the meta data associated with the specified key. This method must be 531 * called after the SetSource. 532 * @param key One of the constants listed above at the definition of {@link AVMetadataCode}. 533 * @return Returns the meta data value associate with the given key code on 534 * success; empty string on failure. 535 */ 536 virtual std::string ResolveMetadata(int32_t key) = 0; 537 538 /** 539 * Retrieve all meta data within the listed above at the definition of {@link AVMetadataCode}. 540 * This method must be called after the SetSource. 541 * @return Returns the meta data values on success; empty hash map on failure. 542 */ 543 virtual std::unordered_map<int32_t, std::string> ResolveMetadata() = 0; 544 545 /** 546 * Fetch the album art picture associated with the data source. If there are 547 * more than one pictures, the cover image will be returned preferably. 548 * @return Returns the a chunk of shared memory containing a picture, which can be 549 * null, if such a picture can not be fetched. 550 */ 551 virtual std::shared_ptr<AVSharedMemory> FetchArtPicture() = 0; 552 553 /** 554 * Fetch a representative video frame near a given timestamp by considering the given 555 * option if possible, and return a pixelmap with given parameters. This method must be 556 * called after the SetSource. 557 * @param timeUs The time position in microseconds where the frame will be fetched. 558 * When fetching the frame at the given time position, there is no guarantee that 559 * the video source has a frame located at the position. When this happens, a frame 560 * nearby will be returned. If timeUs is negative, time position and option will ignored, 561 * and any frame that the implementation considers as representative may be returned. 562 * @param option the hint about how to fetch a frame, see {@link AVMetadataQueryOption} 563 * @param param the desired configuration of returned pixelmap, see {@link PixelMapParams}. 564 * @return Returns a pixelmap containing a scaled video frame, which can be null, if such a 565 * frame cannot be fetched. 566 */ 567 virtual std::shared_ptr<PixelMap> FetchFrameAtTime(int64_t timeUs, int32_t option, const PixelMapParams ¶m) = 0; 568 569 /** 570 * Fetch a representative video frame near a given timestamp by considering the given 571 * option if possible, and return a pixelmap with given parameters. This method must be 572 * called after the SetSource. 573 * @param timeUs The time position in microseconds where the frame will be fetched. 574 * When fetching the frame at the given time position, there is no guarantee that 575 * the video source has a frame located at the position. When this happens, a frame 576 * nearby will be returned. If timeUs is negative, time position and option will ignored, 577 * and any frame that the implementation considers as representative may be returned. 578 * @param option the hint about how to fetch a frame, see {@link AVMetadataQueryOption} 579 * @param param the desired configuration of returned pixelmap, see {@link PixelMapParams}. 580 * @return Returns a pixelmap containing a scaled video frame, which can be null, if such a 581 * frame cannot be fetched. 582 */ 583 virtual std::shared_ptr<PixelMap> FetchFrameYuv(int64_t timeUs, int32_t option, const PixelMapParams ¶m) = 0; 584 585 /** 586 * Fetch a representative video frame near a given timestamp by considering the given 587 * option if possible, and return a pixelmap with given parameters. This method must be 588 * called after the SetSource. Additionally, this method supports maintaining aspect ratio 589 * scaling when resizing the pixelmap. 590 * @param timeUs The time position in microseconds where the frame will be fetched. 591 * When fetching the frame at the given time position, there is no guarantee that 592 * the video source has a frame located at the position. When this happens, a frame 593 * nearby will be returned. If timeUs is negative, time position and option will ignored, 594 * and any frame that the implementation considers as representative may be returned. 595 * @param option the hint about how to fetch a frame, see {@link AVMetadataQueryOption} 596 * @param param the desired configuration of returned pixelmap, see {@link PixelMapParams}. 597 * @return Returns a pixelmap containing a scaled video frame, which can be null, if such a 598 * frame cannot be fetched. 599 */ 600 virtual std::shared_ptr<PixelMap> FetchScaledFrameYuv(int64_t timeUs, int32_t option, 601 const PixelMapParams ¶m) = 0; 602 603 /** 604 * all meta data. 605 * This method must be called after the SetSource. 606 * @return Returns the meta data values on success; nullptr on failure. 607 */ 608 virtual std::shared_ptr<Meta> GetAVMetadata() = 0; 609 610 /** 611 * Release the internel resource. After this method called, the avmetadatahelper instance 612 * can not be used again. 613 */ 614 virtual void Release() = 0; 615 616 /** 617 * @brief Method to set the meta data helper callback. 618 * 619 * @param callback object pointer. 620 * @return Returns {@link MSERR_OK} if the meta data helper callback is set; returns an error code defined 621 * in {@link media_errors.h} otherwise. 622 */ 623 virtual int32_t SetHelperCallback(const std::shared_ptr<HelperCallback> &callback) = 0; 624 625 /** 626 * Set scene, to identity the sepcial scene. 627 * 628 * @param scene indicates which scene the avmedatahelper's instance will 629 * be used to, see {@link Scene}. 630 */ 631 virtual void SetScene(Scene scene) = 0; 632 633 /** 634 * Get timestamp according to frame index. 635 * @param timeUs : Index of the frame. 636 * @returns returns time 637 */ 638 virtual int32_t GetTimeByFrameIndex(uint32_t index, uint64_t &time) = 0; 639 640 /** 641 * Get frame index according to the given timestamp. 642 * @param timeUs : Timestamp of the frame, in microseconds. 643 * @returns Returns frame 644 */ 645 virtual int32_t GetFrameIndexByTime(uint64_t time, uint32_t &index) = 0; 646 }; 647 648 class __attribute__((visibility("default"))) AVMetadataHelperFactory { 649 public: 650 #ifdef UNSUPPORT_METADATA CreateAVMetadataHelper()651 static std::shared_ptr<AVMetadataHelper> CreateAVMetadataHelper() 652 { 653 return nullptr; 654 } 655 #else 656 static std::shared_ptr<AVMetadataHelper> CreateAVMetadataHelper(); 657 #endif 658 private: 659 AVMetadataHelperFactory() = default; 660 ~AVMetadataHelperFactory() = default; 661 }; 662 } // namespace Media 663 } // namespace OHOS 664 #endif // AVMETADATAHELPER_H