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