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 expect 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 #include <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 #include "avcodec_codec_name.h"
23 #include "avcodec_errors.h"
24 #include "avcodec_list.h"
25 #include "codeclist_core.h"
26 #include "meta/meta_key.h"
27 #include "codecbase.h"
28 using namespace OHOS;
29 using namespace OHOS::MediaAVCodec;
30 using namespace OHOS::Media;
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace {
35 const std::string DEFAULT_VIDEO_MIME = std::string(CodecMimeType::VIDEO_AVC);
36 const std::string DEFAULT_CODEC_NAME = "video.H.Decoder.Name.02";
37 constexpr const char CODEC_VENDOR_FLAG[] = "codec_vendor_flag";
38 constexpr const char SAMPLE_RATE[] = "samplerate";
39 class CodecListUnitTest : public testing::Test {
40 public:
41     static void SetUpTestCase(void);
42     static void TearDownTestCase(void);
43     void SetUp(void);
44     void TearDown(void);
45 
46     Format format_;
47     std::shared_ptr<CapabilityData> capabilityData_ = nullptr;
48 };
49 
50 class CodecParamCheckerTest : public testing::Test {
51 public:
SetUpTestCase(void)52     static void SetUpTestCase(void){};
TearDownTestCase(void)53     static void TearDownTestCase(void){};
SetUp(void)54     void SetUp(void){};
TearDown(void)55     void TearDown(void){};
56 };
57 
SetUpTestCase(void)58 void CodecListUnitTest::SetUpTestCase(void) {}
59 
TearDownTestCase(void)60 void CodecListUnitTest::TearDownTestCase(void) {}
61 
SetUp(void)62 void CodecListUnitTest::SetUp(void)
63 {
64     for (auto iter : HCODEC_CAPS) {
65         if (iter.codecName == DEFAULT_CODEC_NAME) {
66             capabilityData_ = std::make_shared<CapabilityData>(iter);
67         }
68     }
69     EXPECT_NE(capabilityData_, nullptr);
70 
71     format_.PutIntValue(Tag::VIDEO_WIDTH, 4096);  // 4096: valid parameter
72     format_.PutIntValue(Tag::VIDEO_HEIGHT, 4096); // 4096: valid parameter
73     format_.PutIntValue(Tag::VIDEO_PIXEL_FORMAT, 1);
74 }
75 
TearDown(void)76 void CodecListUnitTest::TearDown(void)
77 {
78     capabilityData_ = nullptr;
79     format_ = Format();
80 }
81 
82 /**
83  * @tc.name: CheckBitrate_Valid_Test_001
84  * @tc.desc: format uncontain key bitrate
85  */
86 HWTEST_F(CodecListUnitTest, CheckBitrate_Valid_Test_001, TestSize.Level1)
87 {
88     CodecListCore codecListCore;
89     bool ret = codecListCore.CheckBitrate(format_, *capabilityData_);
90     EXPECT_EQ(ret, true);
91 }
92 
93 /**
94  * @tc.name: CheckBitrate_Valid_Test_002
95  * @tc.desc: format key bitrate in range
96  */
97 HWTEST_F(CodecListUnitTest, CheckBitrate_Valid_Test_002, TestSize.Level1)
98 {
99     CodecListCore codecListCore;
100     constexpr uint64_t bitrate = 300000;
101     format_.PutLongValue(Tag::MEDIA_BITRATE, bitrate);
102     bool ret = codecListCore.CheckBitrate(format_, *capabilityData_);
103     EXPECT_EQ(ret, true);
104 }
105 
106 /**
107  * @tc.name: CheckBitrate_Invalid_Test_001
108  * @tc.desc: format key bitrate out of range
109  */
110 HWTEST_F(CodecListUnitTest, CheckBitrate_InValid_Test_001, TestSize.Level1)
111 {
112     CodecListCore codecListCore;
113     constexpr uint64_t bitrate = 300000000;
114     format_.PutLongValue(Tag::MEDIA_BITRATE, bitrate);
115     bool ret = codecListCore.CheckBitrate(format_, *capabilityData_);
116     EXPECT_EQ(ret, false);
117 }
118 
119 /**
120  * @tc.name: CheckVideoResolution_Valid_Test_001
121  * @tc.desc: format uncontain key width and height
122  */
123 HWTEST_F(CodecListUnitTest, CheckVideoResolution_Valid_Test_001, TestSize.Level1)
124 {
125     CodecListCore codecListCore;
126     Format format;
127     bool ret = codecListCore.CheckVideoResolution(format, *capabilityData_);
128     EXPECT_EQ(ret, true);
129 }
130 
131 /**
132  * @tc.name: CheckVideoResolution_Valid_Test_002
133  * @tc.desc: format key width and height in range
134  */
135 HWTEST_F(CodecListUnitTest, CheckVideoResolution_Valid_Test_002, TestSize.Level1)
136 {
137     CodecListCore codecListCore;
138     bool ret = codecListCore.CheckVideoResolution(format_, *capabilityData_);
139     EXPECT_EQ(ret, true);
140 }
141 
142 /**
143  * @tc.name: CheckVideoResolution_Invalid_Test_001
144  * @tc.desc: format key width and height out of range
145  */
146 HWTEST_F(CodecListUnitTest, CheckVideoResolution_InValid_Test_001, TestSize.Level1)
147 {
148     CodecListCore codecListCore;
149     Format format;
150     format.PutIntValue(Tag::VIDEO_WIDTH, 17);  // 17: invalid parameter
151     format.PutIntValue(Tag::VIDEO_HEIGHT, 17);  // 17: invalid parameter
152     bool ret = codecListCore.CheckVideoResolution(format, *capabilityData_);
153     EXPECT_EQ(ret, false);
154 }
155 
156 /**
157  * @tc.name: CheckVideoPixelFormat_Valid_Test_001
158  * @tc.desc: format uncontain key pixel format
159  */
160 HWTEST_F(CodecListUnitTest, CheckVideoPixelFormat_Valid_Test_001, TestSize.Level1)
161 {
162     CodecListCore codecListCore;
163     Format format;
164     bool ret = codecListCore.CheckVideoPixelFormat(format, *capabilityData_);
165     EXPECT_EQ(ret, true);
166 }
167 
168 /**
169  * @tc.name: CheckVideoPixelFormat_Valid_Test_002
170  * @tc.desc: format key pixel format in range
171  */
172 HWTEST_F(CodecListUnitTest, CheckVideoPixelFormat_Valid_Test_002, TestSize.Level1)
173 {
174     CodecListCore codecListCore;
175     bool ret = codecListCore.CheckVideoPixelFormat(format_, *capabilityData_);
176     EXPECT_EQ(ret, true);
177 }
178 
179 /**
180  * @tc.name: CheckVideoPixelFormat_Invalid_Test_001
181  * @tc.desc: format key pixel format out of range
182  */
183 HWTEST_F(CodecListUnitTest, CheckVideoPixelFormat_InValid_Test_001, TestSize.Level1)
184 {
185     CodecListCore codecListCore;
186     Format format;
187     format.PutIntValue(Tag::VIDEO_PIXEL_FORMAT, -1);  // -1: invalid parameter
188     bool ret = codecListCore.CheckVideoPixelFormat(format, *capabilityData_);
189     EXPECT_EQ(ret, false);
190 }
191 
192 /**
193  * @tc.name: CheckVideoFrameRate_Valid_Test_001
194  * @tc.desc: format uncontain key frame rate
195  */
196 HWTEST_F(CodecListUnitTest, CheckVideoFrameRate_Valid_Test_001, TestSize.Level1)
197 {
198     CodecListCore codecListCore;
199     bool ret = codecListCore.CheckVideoFrameRate(format_, *capabilityData_);
200     EXPECT_EQ(ret, true);
201 }
202 
203 /**
204  * @tc.name: CheckVideoFrameRate_Valid_Test_002
205  * @tc.desc: format key frame rate value is double
206  */
207 HWTEST_F(CodecListUnitTest, CheckVideoFrameRate_Valid_Test_002, TestSize.Level1)
208 {
209     CodecListCore codecListCore;
210     constexpr double frameRate = 60.0;
211     format_.PutDoubleValue(Tag::VIDEO_FRAME_RATE, frameRate);
212     bool ret = codecListCore.CheckVideoFrameRate(format_, *capabilityData_);
213     EXPECT_EQ(ret, true);
214 }
215 
216 /**
217  * @tc.name: CheckVideoFrameRate_Invalid_Test_001
218  * @tc.desc: format key frame rate value is double and value invalid
219  */
220 HWTEST_F(CodecListUnitTest, CheckVideoFrameRate_Invalid_Test_001, TestSize.Level1)
221 {
222     CodecListCore codecListCore;
223     constexpr double frameRate = 1000.0;
224     format_.PutDoubleValue(Tag::VIDEO_FRAME_RATE, frameRate);
225     bool ret = codecListCore.CheckVideoFrameRate(format_, *capabilityData_);
226     EXPECT_EQ(ret, false);
227 }
228 
229 /**
230  * @tc.name: CheckAudioChannel_Valid_Test_001
231  * @tc.desc: format uncontain key channel count
232  */
233 HWTEST_F(CodecListUnitTest, CheckAudioChannel_Valid_Test_001, TestSize.Level1)
234 {
235     CodecListCore codecListCore;
236     bool ret = codecListCore.CheckAudioChannel(format_, *capabilityData_);
237     EXPECT_EQ(ret, true);
238 }
239 
240 /**
241  * @tc.name: CheckAudioChannel_Valid_Test_002
242  * @tc.desc: format key channel count in range
243  */
244 HWTEST_F(CodecListUnitTest, CheckAudioChannel_Valid_Test_002, TestSize.Level1)
245 {
246     CodecListCore codecListCore;
247     constexpr uint32_t channelCount = 17;
248     format_.PutIntValue(Tag::AUDIO_CHANNEL_COUNT, channelCount);
249     bool ret = codecListCore.CheckAudioChannel(format_, *capabilityData_);
250     EXPECT_EQ(ret, true);
251 }
252 
253 /**
254  * @tc.name: CheckAudioChannel_Invalid_Test_001
255  * @tc.desc: format key channel count out of range
256  */
257 HWTEST_F(CodecListUnitTest, CheckAudioChannel_Invalid_Test_001, TestSize.Level1)
258 {
259     CodecListCore codecListCore;
260     constexpr uint32_t channelCount = 10000;
261     format_.PutIntValue(Tag::AUDIO_CHANNEL_COUNT, channelCount);
262     bool ret = codecListCore.CheckAudioChannel(format_, *capabilityData_);
263     EXPECT_EQ(ret, false);
264 }
265 
266 /**
267  * @tc.name: CheckAudioSampleRate_Valid_Test_001
268  * @tc.desc: format uncontain key samplerate
269  */
270 HWTEST_F(CodecListUnitTest, CheckAudioSampleRate_Valid_Test_001, TestSize.Level1)
271 {
272     CodecListCore codecListCore;
273     bool ret = codecListCore.CheckAudioSampleRate(format_, *capabilityData_);
274     EXPECT_EQ(ret, true);
275 }
276 
277 /**
278  * @tc.name: CheckAudioSampleRate_Valid_Test_002
279  * @tc.desc: format key samplerate in range
280  */
281 HWTEST_F(CodecListUnitTest, CheckAudioSampleRate_Valid_Test_002, TestSize.Level1)
282 {
283     CodecListCore codecListCore;
284     constexpr uint32_t samplerate = 1;
285     format_.PutIntValue(SAMPLE_RATE, samplerate);
286     bool ret = codecListCore.CheckAudioSampleRate(format_, *capabilityData_);
287     EXPECT_EQ(ret, true);
288 }
289 
290 /**
291  * @tc.name: CheckAudioSampleRate_Invalid_Test_001
292  * @tc.desc: format key samplerate out of range
293  */
294 HWTEST_F(CodecListUnitTest, CheckAudioSampleRate_Invalid_Test_001, TestSize.Level1)
295 {
296     CodecListCore codecListCore;
297     constexpr uint32_t samplerate = 10;
298     format_.PutIntValue(SAMPLE_RATE, samplerate);
299     bool ret = codecListCore.CheckAudioSampleRate(format_, *capabilityData_);
300     EXPECT_EQ(ret, false);
301 }
302 
303 /**
304  * @tc.name: IsVideoCapSupport_Valid_Test_001
305  * @tc.desc: format uncontain key
306  */
307 HWTEST_F(CodecListUnitTest, IsVideoCapSupport_Valid_Test_001, TestSize.Level1)
308 {
309     CodecListCore codecListCore;
310     Format format;
311     bool ret = codecListCore.IsVideoCapSupport(format, *capabilityData_);
312     EXPECT_EQ(ret, true);
313 }
314 
315 /**
316  * @tc.name: IsAudioCapSupport_Valid_Test_001
317  * @tc.desc: format uncontain key
318  */
319 HWTEST_F(CodecListUnitTest, IsAudioCapSupport_Valid_Test_001, TestSize.Level1)
320 {
321     CodecListCore codecListCore;
322     Format format;
323     bool ret = codecListCore.IsAudioCapSupport(format, *capabilityData_);
324     EXPECT_EQ(ret, true);
325 }
326 
327 /**
328  * @tc.name: GetCapability_Valid_Test_001
329  * @tc.desc: mime is empty
330  */
331 HWTEST_F(CodecListUnitTest, GetCapability_Valid_Test_001, TestSize.Level1)
332 {
333     CodecListCore codecListCore;
334     std::string mime = "";
335     constexpr bool isEncoder = true;
336     constexpr AVCodecCategory category = AVCodecCategory::AVCODEC_NONE;
337     bool ret = codecListCore.GetCapability(*capabilityData_, mime, isEncoder, category);
338     EXPECT_EQ(ret, true);
339 }
340 
341 /**
342  * @tc.name: FindCodec_Valid_Test_001
343  * @tc.desc: format uncontain key codec mime
344  */
345 HWTEST_F(CodecListUnitTest, FindCodec_Valid_Test_001, TestSize.Level1)
346 {
347     CodecListCore codecListCore;
348     constexpr bool isEncoder = true;
349     std::string ret = codecListCore.FindCodec(format_, isEncoder);
350     EXPECT_EQ(ret, "");
351 }
352 
353 /**
354  * @tc.name: FindCodec_Valid_Test_002
355  * @tc.desc: format key codec mime in range, contain codec vendor flag and capabilityDataArray is null
356  */
357 HWTEST_F(CodecListUnitTest, FindCodec_Valid_Test_002, TestSize.Level1)
358 {
359     CodecListCore codecListCore;
360     constexpr bool isEncoder = true;
361     constexpr int32_t vendorFlag = 1;
362     format_.PutStringValue(Tag::MIME_TYPE, CodecMimeType::VIDEO_AVC);
363     format_.PutIntValue(CODEC_VENDOR_FLAG, vendorFlag);
364     std::string ret = codecListCore.FindCodec(format_, isEncoder);
365     EXPECT_EQ(ret, "");
366 }
367 
368 /**
369  * @tc.name: FindCodec_Valid_Test_003
370  * @tc.desc: format key codec mime is audio and in mimeCapIdxMap
371  */
372 HWTEST_F(CodecListUnitTest, FindCodec_Valid_Test_003, TestSize.Level1)
373 {
374     CodecListCore codecListCore;
375     constexpr bool isEncoder = false;
376     format_.PutStringValue(Tag::MIME_TYPE, CodecMimeType::AUDIO_MPEG);
377     std::string ret = codecListCore.FindCodec(format_, isEncoder);
378     EXPECT_EQ(ret, AVCodecCodecName::AUDIO_DECODER_MP3_NAME);
379 }
380 
381 /**
382  * @tc.name: FindCodec_Valid_Test_004
383  * @tc.desc: format key codec mime is audio and not in mimeCapIdxMap
384  */
385 HWTEST_F(CodecListUnitTest, FindCodec_Valid_Test_004, TestSize.Level1)
386 {
387     CodecListCore codecListCore;
388     constexpr bool isEncoder = false;
389     constexpr int32_t vendorFlag = 1;
390     format_.PutStringValue(Tag::MIME_TYPE, CodecMimeType::AUDIO_MPEG);
391     format_.PutIntValue(CODEC_VENDOR_FLAG, vendorFlag);
392     std::string ret = codecListCore.FindCodec(format_, isEncoder);
393     EXPECT_EQ(ret, "");
394 }
395 } // namespace