• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 AVMETADATA_MOCK_H
17 #define AVMETADATA_MOCK_H
18 
19 #include "securec.h"
20 #include "avmetadatahelper_server.h"
21 #include "unittest_log.h"
22 
23 namespace OHOS {
24 namespace Media {
25 namespace AVMetadataTestParam {
26 inline constexpr int32_t PARA_MAX_LEN = 256;
27 #define AVMETA_KEY_TO_STRING_MAP_ITEM(key) { key, #key }
28 static const std::unordered_map<int32_t, std::string_view> AVMETA_KEY_TO_STRING_MAP = {
29     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_ALBUM),
30     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_ALBUM_ARTIST),
31     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_DATE_TIME),
32     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_ARTIST),
33     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_AUTHOR),
34     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_COMPOSER),
35     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_DURATION),
36     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_GENRE),
37     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_HAS_AUDIO),
38     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_HAS_VIDEO),
39     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_MIME_TYPE),
40     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_NUM_TRACKS),
41     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_SAMPLE_RATE),
42     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_TITLE),
43     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_VIDEO_HEIGHT),
44     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_VIDEO_WIDTH),
45     AVMETA_KEY_TO_STRING_MAP_ITEM(AV_KEY_VIDEO_IS_HDR_VIVID),
46 };
47 
48 static std::unordered_map<int32_t, std::string> EXPECT_META = {
49     {AV_KEY_ALBUM, "media"},
50     {AV_KEY_ALBUM_ARTIST, "media_test"},
51     {AV_KEY_ARTIST, "元数据测试"},
52     {AV_KEY_AUTHOR, ""},
53     {AV_KEY_COMPOSER, "测试"},
54     {AV_KEY_DURATION, "10030"},
55     {AV_KEY_GENRE, "Lyrical"},
56     {AV_KEY_HAS_AUDIO, "yes"},
57     {AV_KEY_HAS_VIDEO, "yes"},
58     {AV_KEY_MIME_TYPE, "video/mp4"},
59     {AV_KEY_NUM_TRACKS, "2"},
60     {AV_KEY_SAMPLE_RATE, "44100"},
61     {AV_KEY_TITLE, "test"},
62     {AV_KEY_VIDEO_HEIGHT, "480"},
63     {AV_KEY_VIDEO_WIDTH, "720"},
64     {AV_KEY_DATE_TIME, "2022"},
65 };
66 } // namespace AVMetadataTestParam
67 
68 static const int RGB888_PIXEL_BYTES = 3;
69 static const int RGB565_PIXEL_BYTES = 2;
70 static const unsigned short RGB565_MASK_RED = 0x001F;
71 static const unsigned short RGB565_MASK_GREEN = 0x07E0;
72 static const unsigned short RGB565_MASK_BLUE = 0xF800;
73 static const unsigned char SHIFT_2_BIT = 2;
74 static const unsigned char SHIFT_3_BIT = 3;
75 static const unsigned char SHIFT_5_BIT = 5;
76 static const unsigned char SHIFT_11_BIT = 11;
77 static const unsigned char R_INDEX = 2;
78 static const unsigned char G_INDEX = 1;
79 static const unsigned char B_INDEX = 0;
80 static const int32_t dstWidthMin = 32;
81 static const int32_t dstHeightMin = 32;
82 static const int32_t dstWidthMax = 7680;
83 static const int32_t dstHeightMax = 4320;
84 
85 class AVMetadataMock : public NoCopyable {
86 public:
87     std::shared_ptr<OHOS::Media::AVMetadataHelper> avMetadataHelper_ = nullptr;
88     AVMetadataMock();
89     ~AVMetadataMock();
90     DISALLOW_COPY_AND_MOVE(AVMetadataMock);
91     bool CreateAVMetadataHelper();
92     int32_t SetSource(const std::string &uri, int32_t usage);
93     int32_t SetSource(const std::string &path, int64_t offset, int64_t size, int32_t usage);
94     void PrintMetadata();
95     std::string ResolveMetadata(int32_t key);
96     std::unordered_map<int32_t, std::string> ResolveMetadata();
97     std::shared_ptr<PixelMap> FetchFrameAtTime(int64_t timeUs, int32_t option, PixelMapParams param);
98     std::shared_ptr<PixelMap> FetchFrameYuv(int64_t timeUs, int32_t option, PixelMapParams param);
99     std::shared_ptr<AVSharedMemory> FetchArtPicture();
100     void Release();
101     void FrameToFile(std::shared_ptr<PixelMap> frame, const char *fileName, int64_t timeUs, int32_t queryOption);
102     void SurfaceToFile(std::shared_ptr<AVSharedMemory> frame, const char *fileName);
103     void FrameToJpeg(std::shared_ptr<PixelMap> frame, const char *fileName, int64_t timeUs, int32_t queryOption);
104     int32_t GetTimeByFrameIndex(uint32_t index, uint64_t &time);
105     int32_t GetFrameIndexByTime(uint64_t time, uint32_t &index);
106 private:
107     int32_t RGB565ToRGB888(const unsigned short *rgb565Buf, int rgb565Size, unsigned char *rgb888Buf, int rgb888Size);
108 };
109 class AVMetadataTestBase {
110 public:
GetInstance()111     static AVMetadataTestBase &GetInstance()
112     {
113         static AVMetadataTestBase config;
114         return config;
115     }
GetMountPath()116     std::string GetMountPath() const
117     {
118         return mountPath_;
119     }
SetMountPath(std::string mountPath)120     void SetMountPath(std::string mountPath)
121     {
122         mountPath_ = mountPath;
123     }
124     bool StrToInt64(const std::string &str, int64_t &value);
125     std::string GetPrettyDuration(int64_t duration);
126     bool CompareMetadata(int32_t key, const std::string &result, const std::string &expected);
127     bool CompareMetadata(const std::unordered_map<int32_t, std::string> &result,
128                          const std::unordered_map<int32_t, std::string> &expected);
129 private:
130     AVMetadataTestBase();
131     ~AVMetadataTestBase();
132     std::string mountPath_ = "file:///data/test/";
133 };
134 }
135 }
136 #endif
137