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 AVMETA_DATA_COLLECTOR_H 17 #define AVMETA_DATA_COLLECTOR_H 18 19 #include <condition_variable> 20 #include <mutex> 21 #include <nocopyable.h> 22 #include <set> 23 #include <unordered_map> 24 25 #include "media_demuxer.h" 26 #include "meta/meta.h" 27 #include "meta/meta_key.h" 28 29 namespace OHOS { 30 namespace Media { 31 struct Metadata { 32 Metadata() = default; 33 ~Metadata() = default; 34 SetMetaMetadata35 void SetMeta(int32_t key, const std::string &value) 36 { 37 tbl_[key] = value; 38 } 39 TryGetMetaMetadata40 bool TryGetMeta(int32_t key, std::string &value) const 41 { 42 auto it = tbl_.find(key); 43 if (it == tbl_.end()) { 44 return false; 45 } 46 value = it->second; 47 return true; 48 } 49 HasMetaMetadata50 bool HasMeta(int32_t key) const 51 { 52 return tbl_.count(key) != 0; 53 } 54 GetMetaMetadata55 std::string GetMeta(int32_t key) const 56 { 57 if (tbl_.count(key) != 0) { 58 return tbl_.at(key); 59 } 60 return ""; 61 } 62 63 std::unordered_map<int32_t, std::string> tbl_; 64 }; 65 66 class AVMetaDataCollector : public NoCopyable { 67 public: 68 explicit AVMetaDataCollector(std::shared_ptr<MediaDemuxer> &mediaDemuxer); 69 ~AVMetaDataCollector(); 70 71 std::unordered_map<int32_t, std::string> ExtractMetadata(); 72 std::shared_ptr<Meta> GetAVMetadata(); 73 std::string ExtractMetadata(int32_t key); 74 std::shared_ptr<AVSharedMemory> GetArtPicture(); 75 int32_t GetTimeByFrameIndex(uint32_t index, uint64_t &timeUs); 76 int32_t GetFrameIndexByTime(uint64_t timeUs, uint32_t &index); 77 void Reset(); 78 void Destroy(); 79 void GetAudioTrackInfo(const std::shared_ptr<Meta> &trackInfo, const std::string& mime, size_t index); 80 void GetVideoTrackInfo(const std::shared_ptr<Meta> &trackInfo, const std::string& mime, size_t index); 81 void GetSubtitleTrackInfo(const std::shared_ptr<Meta> &trackInfo, const std::string& mime, size_t index); 82 void GetOtherTrackInfo(const std::shared_ptr<Meta> &trackInfo, size_t index); 83 private: 84 std::shared_ptr<MediaDemuxer> mediaDemuxer_; 85 std::unordered_map<int32_t, std::string> collectedMeta_ = {}; 86 std::shared_ptr<AVSharedMemory> collectedArtPicture_; 87 std::shared_ptr<Meta> customInfo_; 88 std::shared_ptr<Meta> collectedAVMetaData_; 89 uint32_t videoTrackId_ = 0; 90 std::atomic<bool> hasVideo_ = false; 91 std::vector<Format> trackInfoVec_; 92 93 std::unordered_map<int32_t, std::string> GetMetadata( 94 const std::shared_ptr<Meta> &globalInfo, const std::vector<std::shared_ptr<Meta>> &trackInfos); 95 void ConvertToAVMeta(const std::shared_ptr<Meta> &innerMeta, Metadata &avmeta) const; 96 void FormatAVMeta(Metadata &avmeta, int32_t imageTrackCount, const std::shared_ptr<Meta> &globalInfo); 97 void FormatMimeType(Metadata &avmeta, const std::shared_ptr<Meta> &globalInfo); 98 void FormatDateTime(Metadata &avmeta, const std::shared_ptr<Meta> &globalInfo); 99 void FormatVideoRotateOrientation(Metadata &avmeta); 100 bool IsAllDigits(const std::string& str); 101 void SetEmptyStringIfNoData(Metadata &avmeta, int32_t avKey) const; 102 bool SetStringByValueType(const std::shared_ptr<Meta> &innerMeta, 103 Metadata &avmeta, int32_t avKey, std::string innerKey) const; 104 Status GetVideoTrackId(uint32_t &trackId); 105 int32_t GetSarVideoWidth(std::shared_ptr<Meta> trackInfo) const; 106 int32_t GetSarVideoHeight(std::shared_ptr<Meta> trackInfo) const; 107 bool IsVideoMime(const std::string& mime) const; 108 bool IsAudioMime(const std::string& mime) const; 109 bool IsSubtitleMime(const std::string& mime) const; 110 void InitTracksInfoVector(const std::shared_ptr<Meta> &meta, size_t index); 111 }; 112 } // namespace Media 113 } // namespace OHOS 114 #endif // AVMETA_DATA_COLLECTOR_H