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 #include "gtest/gtest.h"
25 #include "native_avcodec_videodecoder.h"
26 #include "native_avcodec_base.h"
27 #include "videodec_ndk_sample.h"
28 #include "native_avcapability.h"
29
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 HwdecReliNdkTest : 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 *inpDir720 = "/data/test/media/1280x720_30_10M.h264";
52 const char *inpDir720Array[16] = {
53 "/data/test/media//1280_720_30_10M.h264", "/data/test/media//1280_720_30_10M_1.h264",
54 "/data/test/media//1280_720_30_10M_2.h264", "/data/test/media//1280_720_30_10M_3.h264",
55 "/data/test/media//1280_720_30_10M_4.h264", "/data/test/media//1280_720_30_10M_5.h264",
56 "/data/test/media//1280_720_30_10M_6.h264", "/data/test/media//1280_720_30_10M_7.h264",
57 "/data/test/media//1280_720_30_10M_8.h264", "/data/test/media//1280_720_30_10M_9.h264",
58 "/data/test/media//1280_720_30_10M_10.h264", "/data/test/media//1280_720_30_10M_11.h264",
59 "/data/test/media//1280_720_30_10M_12.h264", "/data/test/media//1280_720_30_10M_13.h264",
60 "/data/test/media//1280_720_30_10M_14.h264", "/data/test/media//1280_720_30_10M_15.h264"};
61 };
62 } // namespace Media
63 } // namespace OHOS
64
65 static string g_codecName;
66 static string g_codecMime = "video/avc";
67 static OH_AVCapability *cap = nullptr;
SetUpTestCase()68 void HwdecReliNdkTest::SetUpTestCase()
69 {
70 cap = OH_AVCodec_GetCapabilityByCategory(g_codecMime.c_str(), false, HARDWARE);
71 g_codecName = OH_AVCapability_GetName(cap);
72 cout << "codecname: " << g_codecName << endl;
73 }
74
TearDownTestCase()75 void HwdecReliNdkTest::TearDownTestCase() {}
76
SetUp()77 void HwdecReliNdkTest::SetUp() {}
78
TearDown()79 void HwdecReliNdkTest::TearDown() {}
80
81 namespace {
82 /**
83 * @tc.number : VIDEO_HWDEC_STABILITY_0200
84 * @tc.name : confige-start-flush-start-reset 1000 times
85 * @tc.desc : reli test
86 */
87 HWTEST_F(HwdecReliNdkTest, VIDEO_HWDEC_STABILITY_0200, TestSize.Level4)
88 {
89 vdec_ = OH_VideoDecoder_CreateByName(g_codecName.c_str());
90 for (int i = 0; i < 1000; i++) {
91 ASSERT_NE(nullptr, vdec_);
92 OH_AVFormat *format = OH_AVFormat_Create();
93 ASSERT_NE(nullptr, format);
94 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, 1920);
95 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, 1080);
96 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
97 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_FRAME_RATE, 30);
98 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Configure(vdec_, format));
99 OH_AVFormat_Destroy(format);
100 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Start(vdec_));
101 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Flush(vdec_));
102 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Start(vdec_));
103 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Reset(vdec_));
104 }
105 OH_VideoDecoder_Stop(vdec_);
106 OH_VideoDecoder_Destroy(vdec_);
107 }
108
109 /**
110 * @tc.number : VIDEO_HWDEC_STABILITY_0400
111 * @tc.name : SetParameter 1000 times
112 * @tc.desc : reli test
113 */
114 HWTEST_F(HwdecReliNdkTest, VIDEO_HWDEC_STABILITY_0400, TestSize.Level4)
115 {
116 vdec_ = OH_VideoDecoder_CreateByName(g_codecName.c_str());
117 ASSERT_NE(nullptr, vdec_);
118 OH_AVFormat *format = OH_AVFormat_Create();
119 ASSERT_NE(nullptr, format);
120 int64_t widht = 1920;
121 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, widht);
122 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, 1080);
123 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, AV_PIXEL_FORMAT_NV12);
124 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_FRAME_RATE, 30);
125 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Configure(vdec_, format));
126 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_Start(vdec_));
127 for (int i = 0; i < 1000; i++) {
128 widht++;
129 (void)OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, widht);
130 ASSERT_EQ(AV_ERR_OK, OH_VideoDecoder_SetParameter(vdec_, format));
131 }
132 OH_AVFormat_Destroy(format);
133 OH_VideoDecoder_Stop(vdec_);
134 OH_VideoDecoder_Destroy(vdec_);
135 }
136
137 /**
138 * @tc.number : VIDEO_HWDEC_PERFORMANCE_WHILE_0100
139 * @tc.name :
140 * @tc.desc : perf test
141 */
142 HWTEST_F(HwdecReliNdkTest, VIDEO_HWDEC_PERFORMANCE_WHILE_0100, TestSize.Level3)
143 {
144 while (true) {
145 shared_ptr<VDecNdkSample> vDecSample = make_shared<VDecNdkSample>();
146 vDecSample->SF_OUTPUT = false;
147 vDecSample->INP_DIR = inpDir720;
148 vDecSample->DEFAULT_WIDTH = 1280;
149 vDecSample->DEFAULT_HEIGHT = 720;
150 vDecSample->DEFAULT_FRAME_RATE = 30;
151 vDecSample->sleepOnFPS = true;
152 ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder(g_codecName.c_str()));
153 ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
154 ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
155 ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
156 vDecSample->WaitForEOS();
157 ASSERT_EQ(AV_ERR_OK, vDecSample->errCount);
158 }
159 }
160
161 /**
162 * @tc.number : VIDEO_HWDEC_PERFORMANCE_WHILE_0200
163 * @tc.name :
164 * @tc.desc : perf test
165 */
166 HWTEST_F(HwdecReliNdkTest, VIDEO_HWDEC_PERFORMANCE_WHILE_0200, TestSize.Level3)
167 {
168 for (int i = 0; i < 16; i++) {
169 VDecNdkSample *vDecSample = new VDecNdkSample();
170 vDecSample->SF_OUTPUT = false;
171 vDecSample->INP_DIR = inpDir720Array[i];
172 vDecSample->DEFAULT_WIDTH = 1280;
173 vDecSample->DEFAULT_HEIGHT = 720;
174 vDecSample->DEFAULT_FRAME_RATE = 30;
175 vDecSample->sleepOnFPS = true;
176 vDecSample->repeatRun = true;
177 ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder(g_codecName.c_str()));
178 ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
179 ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
180 ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
181 if (i == 15) {
182 vDecSample->WaitForEOS();
183 ASSERT_EQ(AV_ERR_OK, vDecSample->errCount);
184 }
185 }
186 }
187
188 /**
189 * @tc.number : VIDEO_HWDEC_PERFORMANCE_WHILE_0300
190 * @tc.name :
191 * @tc.desc : perf test
192 */
193 HWTEST_F(HwdecReliNdkTest, VIDEO_HWDEC_PERFORMANCE_WHILE_0300, TestSize.Level3)
194 {
195 shared_ptr<VDecNdkSample> vDecSample = make_shared<VDecNdkSample>();
196 vDecSample->SF_OUTPUT = false;
197 vDecSample->INP_DIR = inpDir720;
198 vDecSample->DEFAULT_WIDTH = 1280;
199 vDecSample->DEFAULT_HEIGHT = 720;
200 vDecSample->DEFAULT_FRAME_RATE = 30;
201 vDecSample->sleepOnFPS = true;
202 vDecSample->repeatRun = true;
203 ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder(g_codecName.c_str()));
204 ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
205 ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
206 ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
207 vDecSample->WaitForEOS();
208 ASSERT_EQ(AV_ERR_OK, vDecSample->errCount);
209 }
210
211 /**
212 * @tc.number : VIDEO_HWDEC_PERFORMANCE_WHILE_0400
213 * @tc.name :
214 * @tc.desc : perf test
215 */
216 HWTEST_F(HwdecReliNdkTest, VIDEO_HWDEC_PERFORMANCE_WHILE_0400, TestSize.Level3)
217 {
218 while (true) {
219 vector<shared_ptr<VDecNdkSample>> decVec;
220 for (int i = 0; i < 16; i++) {
221 auto vDecSample = make_shared<VDecNdkSample>();
222 decVec.push_back(vDecSample);
223 vDecSample->SF_OUTPUT = false;
224 vDecSample->INP_DIR = inpDir720Array[i];
225 vDecSample->DEFAULT_WIDTH = 1280;
226 vDecSample->DEFAULT_HEIGHT = 720;
227 vDecSample->DEFAULT_FRAME_RATE = 30;
228 vDecSample->sleepOnFPS = true;
229 vDecSample->repeatRun = true;
230 ASSERT_EQ(AV_ERR_OK, vDecSample->CreateVideoDecoder(g_codecName.c_str()));
231 ASSERT_EQ(AV_ERR_OK, vDecSample->ConfigureVideoDecoder());
232 ASSERT_EQ(AV_ERR_OK, vDecSample->SetVideoDecoderCallback());
233 ASSERT_EQ(AV_ERR_OK, vDecSample->StartVideoDecoder());
234 if (i == 15) {
235 vDecSample->WaitForEOS();
236 ASSERT_EQ(AV_ERR_OK, vDecSample->errCount);
237 }
238 }
239 }
240 }
241 } // namespace