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 #include <iostream>
17 #include <cstdio>
18 #include <atomic>
19 #include <fstream>
20 #include <thread>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 
25 #include "gtest/gtest.h"
26 #include "native_avcodec_videodecoder.h"
27 #include "native_avcodec_base.h"
28 #include "videodec_sample.h"
29 #include "videodec_api11_sample.h"
30 using namespace std;
31 using namespace OHOS;
32 using namespace OHOS::Media;
33 using namespace testing::ext;
34 
35 namespace OHOS {
36 namespace Media {
37 class SwdecReliNdkTest : public testing::Test {
38 public:
39     static void SetUpTestCase();    // 第一个测试用例执行前
40     static void TearDownTestCase(); // 最后一个测试用例执行后
41     void SetUp() override;          // 每个测试用例执行前
42     void TearDown() override;       // 每个测试用例执行后
43     void InputFunc();
44     void OutputFunc();
45     void Release();
46     int32_t Stop();
47 
48 protected:
49     bool createCodecSuccess_ = false;
50     OH_AVCodec *vdec_;
51     const char *INP_DIR_720_30 = "/data/test/media/1280x720_30_10M.h264";
52     const char *INP_DIR_1080_30 = "/data/test/media/1920_1080_10_30Mb.h264";
53     const char *INP_DIR_720_30_ARRAY[16] = {
54         "/data/test/media/1280x720_30_10M.h264",    "/data/test/media/1280x720_30_10M_1.h264",
55         "/data/test/media/1280x720_30_10M_2.h264",  "/data/test/media/1280x720_30_10M_3.h264",
56         "/data/test/media/1280x720_30_10M_8.h264",  "/data/test/media/1280x720_30_10M_12.h264",
57         "/data/test/media/1280x720_30_10M_4.h264",  "/data/test/media/1280x720_30_10M_9.h264",
58         "/data/test/media/1280x720_30_10M_13.h264", "/data/test/media/1280x720_30_10M_5.h264",
59         "/data/test/media/1280x720_30_10M_10.h264", "/data/test/media/1280x720_30_10M_14.h264",
60         "/data/test/media/1280x720_30_10M_6.h264",  "/data/test/media/1280x720_30_10M_11.h264",
61         "/data/test/media/1280x720_30_10M_15.h264", "/data/test/media/1280x720_30_10M_7.h264"};
62 };
63 } // namespace Media
64 } // namespace OHOS
65 
SetUpTestCase()66 void SwdecReliNdkTest::SetUpTestCase() {}
TearDownTestCase()67 void SwdecReliNdkTest::TearDownTestCase() {}
SetUp()68 void SwdecReliNdkTest::SetUp() {}
TearDown()69 void SwdecReliNdkTest::TearDown() {}
70 
71 namespace {
72 constexpr uint32_t MAX_THREAD = 16;
73 /**
74  * @tc.number    : VIDEO_SWDEC_RELI_0200
75  * @tc.name      : confige-start-flush-start-reset 1000 times
76  * @tc.desc      : reliable test
77  */
78 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_RELI_0200, TestSize.Level4)
79 {
80     vdec_ = OH_VideoDecoder_CreateByName("OH.Media.Codec.Decoder.Video.AVC");
81     for (int i = 0; i < 1000; i++) {
82         ASSERT_NE(nullptr, vdec_);
83         OH_AVFormat *format = OH_AVFormat_Create();
84         ASSERT_NE(nullptr, format);
85         string widthStr = "width";
86         string heightStr = "height";
87         string frameRateStr = "frame_rate";
88         (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), 1920);
89         (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), 1080);
90         (void)OH_AVFormat_SetIntValue(format, frameRateStr.c_str(), 30);
91         ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Configure(vdec_, format));
92         OH_AVFormat_Destroy(format);
93         ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Start(vdec_));
94         ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Flush(vdec_));
95         ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Start(vdec_));
96         ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Reset(vdec_));
97     }
98     OH_VideoDecoder_Stop(vdec_);
99     OH_VideoDecoder_Destroy(vdec_);
100 }
101 
102 /**
103  * @tc.number    : VIDEO_SWDEC_RELI_0400
104  * @tc.name      : SetParameter 1000 times
105  * @tc.desc      : reliable test
106  */
107 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_RELI_0400, TestSize.Level4)
108 {
109     vdec_ = OH_VideoDecoder_CreateByName("OH.Media.Codec.Decoder.Video.AVC");
110     ASSERT_NE(nullptr, vdec_);
111     OH_AVFormat *format = OH_AVFormat_Create();
112     ASSERT_NE(nullptr, format);
113     string widthStr = "width";
114     string heightStr = "height";
115     string frameRateStr = "frame_rate";
116     int64_t widht = 1920;
117     (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), widht);
118     (void)OH_AVFormat_SetIntValue(format, heightStr.c_str(), 1080);
119     (void)OH_AVFormat_SetIntValue(format, frameRateStr.c_str(), 30);
120     ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Configure(vdec_, format));
121     ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Start(vdec_));
122     for (int i = 0; i < 1000; i++) {
123         widht++;
124         (void)OH_AVFormat_SetIntValue(format, widthStr.c_str(), widht);
125         ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_SetParameter(vdec_, format));
126     }
127     OH_AVFormat_Destroy(format);
128     OH_VideoDecoder_Destroy(vdec_);
129 }
130 
131 /**
132  * @tc.number    : VIDEO_SWDEC_RELI_WHILE_0100
133  * @tc.name      : 16 instances
134  * @tc.desc      : reliable test
135  */
136 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_RELI_WHILE_0100, TestSize.Level3)
137 {
138     for (int i = 0; i < 16; i++) {
139         VDecNdkSample *vDecSample = new VDecNdkSample();
140         vDecSample->SURFACE_OUTPUT = false;
141         vDecSample->INP_DIR = INP_DIR_720_30_ARRAY[i];
142         vDecSample->DEFAULT_WIDTH = 1280;
143         vDecSample->DEFAULT_HEIGHT = 720;
144         vDecSample->DEFAULT_FRAME_RATE = 30;
145         vDecSample->sleepOnFPS = true;
146         vDecSample->repeatRun = true;
147         ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC"));
148         ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
149         ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
150         ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
151         if (i == 15) {
152             vDecSample->WaitForEOS();
153             ASSERT_EQ(AV_ERR_OK, vDecSample->errCount);
154         }
155     }
156 }
157 
158 /**
159  * @tc.number    : VIDEO_SWDEC_RELI_WHILE_0200
160  * @tc.name      : 16 instances while true
161  * @tc.desc      : reliable test
162  */
163 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_RELI_WHILE_0200, TestSize.Level3)
164 {
165     VDecNdkSample *vDecList[16] = {};
166     while (true) {
167         for (int i = 0; i < 16; i++) {
168             VDecNdkSample *vDecSample = new VDecNdkSample();
169             vDecList[i] = vDecSample;
170             vDecSample->SURFACE_OUTPUT = false;
171             vDecSample->INP_DIR = INP_DIR_720_30_ARRAY[i];
172             vDecSample->DEFAULT_WIDTH = 1280;
173             vDecSample->DEFAULT_HEIGHT = 720;
174             vDecSample->DEFAULT_FRAME_RATE = 30;
175             vDecSample->sleepOnFPS = true;
176             ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC"));
177             ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
178             ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
179             ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
180         }
181         for (int i = 0; i < 16; i++) {
182             vDecList[i]->WaitForEOS();
183             ASSERT_EQ(AV_ERR_OK, vDecList[i]->errCount);
184             delete vDecList[i];
185             vDecList[i] = nullptr;
186         }
187         usleep(5000000);
188     }
189 }
190 
191 /**
192  * @tc.number    : VIDEO_SWDEC_RELI_WHILE_0300
193  * @tc.name      : long decode
194  * @tc.desc      : reliable test
195  */
196 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_RELI_WHILE_0300, TestSize.Level3)
197 {
198     shared_ptr<VDecNdkSample> vDecSample = make_shared<VDecNdkSample>();
199     vDecSample->SURFACE_OUTPUT = false;
200     vDecSample->INP_DIR = INP_DIR_720_30;
201     vDecSample->DEFAULT_WIDTH = 1280;
202     vDecSample->DEFAULT_HEIGHT = 720;
203     vDecSample->DEFAULT_FRAME_RATE = 30;
204     vDecSample->repeatRun = true;
205     ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC"));
206     ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
207     ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
208     ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
209     vDecSample->WaitForEOS();
210     ASSERT_EQ(AV_ERR_OK, vDecSample->errCount);
211 }
212 
213 /**
214  * @tc.number    : VIDEO_SWDEC_RELI_WHILE_0300
215  * @tc.name      : confige-start-flush-start-reset while true
216  * @tc.desc      : reli test
217  */
218 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_RELI_WHILE_0400, TestSize.Level3)
219 {
220     while (true) {
221         auto vDecSample = make_shared<VDecNdkSample>();
222         vDecSample->SURFACE_OUTPUT = false;
223         vDecSample->INP_DIR = INP_DIR_720_30;
224         vDecSample->DEFAULT_WIDTH = 1280;
225         vDecSample->DEFAULT_HEIGHT = 720;
226         vDecSample->DEFAULT_FRAME_RATE = 30;
227         vDecSample->sleepOnFPS = true;
228         ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC"));
229         ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
230         ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
231         ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
232         vDecSample->WaitForEOS();
233         ASSERT_EQ(AV_ERR_OK, vDecSample->errCount);
234     }
235 }
236 
237 /**
238  * @tc.number    : VIDEO_SWDEC_MULTIINSTANCE_0100
239  * @tc.name      : create 16 decoder (320*240)
240  * @tc.desc      : reliable test
241  */
242 
243 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_MULTIINSTANCE_0100, TestSize.Level3)
244 {
245     vector<shared_ptr<VDecNdkSample>> decVec;
246     for (int i = 0; i < MAX_THREAD; i++) {
247         auto vDecSample = make_shared<VDecNdkSample>();
248         decVec.push_back(vDecSample);
249         vDecSample->INP_DIR = INP_DIR_1080_30;
250         vDecSample->DEFAULT_WIDTH = 1920;
251         vDecSample->DEFAULT_HEIGHT = 1080;
252         vDecSample->DEFAULT_FRAME_RATE = 30;
253         vDecSample->SURFACE_OUTPUT = false;
254         cout << i << " ";
255         ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC"));
256         ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
257         ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
258         ASSERT_EQ(AV_ERR_OK, vDecSample->Start());
259     }
260 }
261 
262 /*
263  * @tc.number    : VIDEO_SWDEC_MULTIINSTANCE_0100
264  * @tc.name      : create 17 decoder
265  * @tc.desc      : reliable test
266  */
267 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_MULTIINSTANCE_0200, TestSize.Level3)
268 {
269     vector<shared_ptr<VDecNdkSample>> decVec;
270     for (int i = 0; i < MAX_THREAD; i++) {
271         auto vDecSample = make_shared<VDecNdkSample>();
272         decVec.push_back(vDecSample);
273         vDecSample->INP_DIR = INP_DIR_1080_30;
274         vDecSample->DEFAULT_WIDTH = 1920;
275         vDecSample->DEFAULT_HEIGHT = 1080;
276         vDecSample->DEFAULT_FRAME_RATE = 30;
277         vDecSample->SURFACE_OUTPUT = false;
278         ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC"));
279         ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
280         ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
281         ASSERT_EQ(AV_ERR_OK, vDecSample->Start());
282     }
283     auto vDecSampleExtra = make_shared<VDecNdkSample>();
284     vDecSampleExtra->INP_DIR = INP_DIR_1080_30;
285     vDecSampleExtra->DEFAULT_WIDTH = 1920;
286     vDecSampleExtra->DEFAULT_HEIGHT = 1080;
287     vDecSampleExtra->DEFAULT_FRAME_RATE = 30;
288     vDecSampleExtra->SURFACE_OUTPUT = false;
289     ASSERT_EQ(AV_ERR_UNKNOWN, vDecSampleExtra->CreateVideoDecoder("OH.Media.Codec.Decoder.Video.AVC"));
290 }
291 
292 /**
293  * @tc.number    : VIDEO_SWDEC_RELI_ATTIME_0010
294  * @tc.name      : test h264 asyn decode surface,use at time
295  * @tc.desc      : perf test
296  */
297 HWTEST_F(SwdecReliNdkTest, VIDEO_SWDEC_RELI_ATTIME_0010, TestSize.Level3)
298 {
299     while (true) {
300         auto vDecSample = make_shared<VDecAPI11Sample>();
301         const char *INP_DIR_720_30 = "/data/test/media/1280_720_30_10Mb.h264";
302         vDecSample->INP_DIR = INP_DIR_720_30;
303         vDecSample->DEFAULT_WIDTH = 1280;
304         vDecSample->DEFAULT_HEIGHT = 720;
305         vDecSample->DEFAULT_FRAME_RATE = 30;
306         vDecSample->SURFACE_OUTPUT = true;
307         vDecSample->rsAtTime = true;
308         ASSERT_EQ(AV_ERR_OK, vDecSample->RunVideoDec_Surface("OH.Media.Codec.Decoder.Video.AVC"));
309         vDecSample->WaitForEOS();
310         ASSERT_EQ(0, vDecSample->errCount);
311     }
312 }
313 } // namespace