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