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 #include "gtest/gtest.h"
17
18 #include "native_avcodec_base.h"
19 #include "native_avdemuxer.h"
20 #include "native_avformat.h"
21 #include "native_avsource.h"
22
23 #include <iostream>
24 #include <cstdio>
25 #include <string>
26 #include <fcntl.h>
27 namespace OHOS {
28 namespace Media {
29 class DemuxerPerfNdkTest : public testing::Test {
30 public:
31 // SetUpTestCase: Called before all test cases
32 static void SetUpTestCase(void);
33 // TearDownTestCase: Called after all test case
34 static void TearDownTestCase(void);
35 // SetUp: Called before each test cases
36 void SetUp(void);
37 // TearDown: Called after each test cases
38 void TearDown(void);
39 };
40
41 static OH_AVMemory *memory = nullptr;
42 static int32_t g_width = 3840;
43 static int32_t g_height = 2160;
44
SetUpTestCase()45 void DemuxerPerfNdkTest::SetUpTestCase()
46 {
47 OH_AVMemory_Create(g_width * g_height);
48 }
TearDownTestCase()49 void DemuxerPerfNdkTest::TearDownTestCase()
50 {
51 if (memory != nullptr) {
52 OH_AVMemory_Destroy(memory);
53 }
54 }
SetUp()55 void DemuxerPerfNdkTest::SetUp() {}
TearDown()56 void DemuxerPerfNdkTest::TearDown() {}
57 } // namespace Media
58 } // namespace OHOS
59
60 using namespace std;
61 using namespace OHOS;
62 using namespace OHOS::Media;
63 using namespace testing::ext;
64
65 namespace {
66 /**
67 * @tc.number : DEMUXER_MEMORY_0100
68 * @tc.name : demux memory 1280x720 30fps 10M
69 * @tc.desc : performance test
70 */
71 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_MEMORY_0100, TestSize.Level3)
72 {
73 const char *file = "/data/test/media/1280x720_30.mp4";
74 OH_AVErrCode ret = AV_ERR_OK;
75 uint32_t trackIndex = 0;
76 OH_AVCodecBufferAttr attr;
77 int fd = open(file, O_RDONLY);
78 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
79 ASSERT_NE(source, nullptr);
80 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
81 ASSERT_NE(demuxer, nullptr);
82 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
83 ASSERT_EQ(ret, AV_ERR_OK);
84 while ((attr.flags & AVCODEC_BUFFER_FLAGS_EOS) != 0) {
85 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
86 ASSERT_EQ(ret, AV_ERR_OK);
87 }
88 OH_AVSource_Destroy(source);
89 close(fd);
90 }
91
92 /**
93 * @tc.number : DEMUXER_MEMORY_0200
94 * @tc.name : demux memory 1920x1080 30fps 20M
95 * @tc.desc : performance test
96 */
97 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_MEMORY_0200, TestSize.Level3)
98 {
99 const char *file = "/data/test/media/1920x1080_30.mp4";
100 OH_AVErrCode ret = AV_ERR_OK;
101 OH_AVCodecBufferAttr attr;
102 int fd = open(file, O_RDONLY);
103 uint32_t trackIndex = 0;
104 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
105 ASSERT_NE(source, nullptr);
106 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
107 ASSERT_NE(demuxer, nullptr);
108 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
109 ASSERT_EQ(ret, AV_ERR_OK);
110 while ((attr.flags & AVCODEC_BUFFER_FLAGS_EOS) != 0) {
111 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
112 ASSERT_EQ(ret, AV_ERR_OK);
113 }
114 OH_AVSource_Destroy(source);
115 close(fd);
116 }
117
118 /**
119 * @tc.number : DEMUXER_MEMORY_0300
120 * @tc.name : demux memory 1280x720 30fps 50M
121 * @tc.desc : performance test
122 */
123 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_MEMORY_0300, TestSize.Level3)
124 {
125 const char *file = "/data/test/media/3840x2160_30.mp4";
126 OH_AVErrCode ret = AV_ERR_OK;
127 OH_AVCodecBufferAttr attr;
128 uint32_t trackIndex = 0;
129 int fd = open(file, O_RDONLY);
130 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
131 ASSERT_NE(source, nullptr);
132 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
133 ASSERT_NE(demuxer, nullptr);
134 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
135 ASSERT_EQ(ret, AV_ERR_OK);
136 while ((attr.flags & AVCODEC_BUFFER_FLAGS_EOS) != 0) {
137 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
138 ASSERT_EQ(ret, AV_ERR_OK);
139 }
140 OH_AVSource_Destroy(source);
141 close(fd);
142 }
143
144 /**
145 * @tc.number : DEMUXER_PERFORMANCE_0100
146 * @tc.name : demux memory 1280x720 30fps 10M
147 * @tc.desc : performance test
148 */
149 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0100, TestSize.Level3)
150 {
151 const char *file = "/data/test/media/1280x720_30.mp4";
152 OH_AVErrCode ret = AV_ERR_OK;
153 OH_AVCodecBufferAttr attr;
154 uint32_t trackIndex = 0;
155 int fd = open(file, O_RDONLY);
156 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
157 ASSERT_NE(source, nullptr);
158 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
159 ASSERT_NE(demuxer, nullptr);
160 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
161 ASSERT_EQ(ret, AV_ERR_OK);
162 srand(time(nullptr));
163 for (int i = 0; i < 20; i++) {
164 int pos_to = rand() % (30 * 3600);
165 int64_t toMs = pos_to * 33333;
166 ret = OH_AVDemuxer_SeekToTime(demuxer, toMs, SEEK_MODE_NEXT_SYNC);
167 ASSERT_EQ(ret, AV_ERR_OK);
168 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
169 ASSERT_EQ(ret, AV_ERR_OK);
170 }
171 OH_AVSource_Destroy(source);
172 close(fd);
173 }
174
175 /**
176 * @tc.number : DEMUXER_PERFORMANCE_0200
177 * @tc.name : demux memory 1280x720 60fps 10M
178 * @tc.desc : performance test
179 */
180 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0200, TestSize.Level3)
181 {
182 const char *file = "/data/test/media/1280x720_60.mp4";
183 OH_AVErrCode ret = AV_ERR_OK;
184 OH_AVCodecBufferAttr attr;
185 int fd = open(file, O_RDONLY);
186 uint32_t trackIndex = 0;
187 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
188 ASSERT_NE(source, nullptr);
189 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
190 ASSERT_NE(demuxer, nullptr);
191 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
192 ASSERT_EQ(ret, AV_ERR_OK);
193 srand(time(nullptr));
194 for (int i = 0; i < 20; i++) {
195 int pos_to = rand() % (60 * 3600);
196 int64_t toMs = pos_to * 16666;
197 ret = OH_AVDemuxer_SeekToTime(demuxer, toMs, SEEK_MODE_NEXT_SYNC);
198 ASSERT_EQ(ret, AV_ERR_OK);
199 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
200 ASSERT_EQ(ret, AV_ERR_OK);
201 }
202 OH_AVSource_Destroy(source);
203 close(fd);
204 }
205
206 /**
207 * @tc.number : DEMUXER_PERFORMANCE_0300
208 * @tc.name : demux memory 1920x1080 30fps 20M
209 * @tc.desc : performance test
210 */
211 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0300, TestSize.Level3)
212 {
213 const char *file = "/data/test/media/1920x1080_30.mp4";
214 OH_AVErrCode ret = AV_ERR_OK;
215 OH_AVCodecBufferAttr attr;
216 uint32_t trackIndex = 0;
217 int fd = open(file, O_RDONLY);
218 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
219 ASSERT_NE(source, nullptr);
220 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
221 ASSERT_NE(demuxer, nullptr);
222 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
223 ASSERT_EQ(ret, AV_ERR_OK);
224 srand(time(nullptr));
225 for (int i = 0; i < 20; i++) {
226 int pos_to = rand() % (30 * 3600);
227 int64_t toMs = pos_to * 33333;
228 ret = OH_AVDemuxer_SeekToTime(demuxer, toMs, SEEK_MODE_NEXT_SYNC);
229 ASSERT_EQ(ret, AV_ERR_OK);
230 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
231 ASSERT_EQ(ret, AV_ERR_OK);
232 }
233 OH_AVSource_Destroy(source);
234 close(fd);
235 }
236
237 /**
238 * @tc.number : DEMUXER_PERFORMANCE_0400
239 * @tc.name : demux memory 1920x1080 60fps 20M
240 * @tc.desc : performance test
241 */
242 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0400, TestSize.Level3)
243 {
244 const char *file = "/data/test/media/1920x1080_60.mp4";
245 OH_AVErrCode ret = AV_ERR_OK;
246 OH_AVCodecBufferAttr attr;
247 uint32_t trackIndex = 0;
248 int fd = open(file, O_RDONLY);
249 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
250 ASSERT_NE(source, nullptr);
251 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
252 ASSERT_NE(demuxer, nullptr);
253 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
254 ASSERT_EQ(ret, AV_ERR_OK);
255 srand(time(nullptr));
256 for (int i = 0; i < 20; i++) {
257 int pos_to = rand() % (60 * 3600);
258 int64_t toMs = pos_to * 16666;
259 ret = OH_AVDemuxer_SeekToTime(demuxer, toMs, SEEK_MODE_NEXT_SYNC);
260 ASSERT_EQ(ret, AV_ERR_OK);
261 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
262 ASSERT_EQ(ret, AV_ERR_OK);
263 }
264 OH_AVSource_Destroy(source);
265 close(fd);
266 }
267
268 /**
269 * @tc.number : DEMUXER_PERFORMANCE_0500
270 * @tc.name : demux memory 3840x2160 30fps 50M
271 * @tc.desc : performance test
272 */
273 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0500, TestSize.Level3)
274 {
275 const char *file = "/data/test/media/3840x2160_30.mp4";
276 OH_AVErrCode ret = AV_ERR_OK;
277 OH_AVCodecBufferAttr attr;
278 uint32_t trackIndex = 0;
279 int fd = open(file, O_RDONLY);
280 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
281 ASSERT_NE(source, nullptr);
282 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
283 ASSERT_NE(demuxer, nullptr);
284 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
285 ASSERT_EQ(ret, AV_ERR_OK);
286 srand(time(nullptr));
287 for (int i = 0; i < 20; i++) {
288 int pos_to = rand() % (30 * 3600);
289 int64_t toMs = pos_to * 33333;
290 ret = OH_AVDemuxer_SeekToTime(demuxer, toMs, SEEK_MODE_NEXT_SYNC);
291 ASSERT_EQ(ret, AV_ERR_OK);
292 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
293 ASSERT_EQ(ret, AV_ERR_OK);
294 }
295 OH_AVSource_Destroy(source);
296 close(fd);
297 }
298
299 /**
300 * @tc.number : DEMUXER_PERFORMANCE_0600
301 * @tc.name : demux memory 3840x2160 60fps 50M
302 * @tc.desc : performance test
303 */
304 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0600, TestSize.Level3)
305 {
306 const char *file = "/data/test/media/3840x2160_60.mp4";
307 OH_AVErrCode ret = AV_ERR_OK;
308 OH_AVCodecBufferAttr attr;
309 uint32_t trackIndex = 0;
310 int fd = open(file, O_RDONLY);
311 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
312 ASSERT_NE(source, nullptr);
313 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
314 ASSERT_NE(demuxer, nullptr);
315 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
316 ASSERT_EQ(ret, AV_ERR_OK);
317 srand(time(nullptr));
318 for (int i = 0; i < 20; i++) {
319 int pos_to = rand() % (60 * 3600);
320 int64_t toMs = pos_to * 16666;
321 ret = OH_AVDemuxer_SeekToTime(demuxer, toMs, SEEK_MODE_NEXT_SYNC);
322 ASSERT_EQ(ret, AV_ERR_OK);
323 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
324 ASSERT_EQ(ret, AV_ERR_OK);
325 }
326 OH_AVSource_Destroy(source);
327 close(fd);
328 }
329
330 /**
331 * @tc.number : DEMUXER_PERFORMANCE_0700
332 * @tc.name : test CopyNextSample time 1280x720 30fps 10M
333 * @tc.desc : performance test
334 */
335 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0700, TestSize.Level3)
336 {
337 const char *file = "/data/test/media/1280x720_30_10M.mp4";
338 OH_AVErrCode ret = AV_ERR_OK;
339 OH_AVCodecBufferAttr attr;
340 uint32_t trackIndex = 0;
341 int fd = open(file, O_RDONLY);
342 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
343 ASSERT_NE(source, nullptr);
344 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
345 ASSERT_NE(demuxer, nullptr);
346 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
347 ASSERT_EQ(ret, AV_ERR_OK);
348 while ((attr.flags & AVCODEC_BUFFER_FLAGS_EOS) != 0) {
349 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
350 ASSERT_EQ(ret, AV_ERR_OK);
351 }
352 OH_AVSource_Destroy(source);
353 close(fd);
354 }
355
356 /**
357 * @tc.number : DEMUXER_PERFORMANCE_0800
358 * @tc.name : test CopyNextSample time 1920x1080 30fps 20M
359 * @tc.desc : performance test
360 */
361 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0800, TestSize.Level3)
362 {
363 const char *file = "/data/test/media/1920x1080_30.mp4";
364 OH_AVErrCode ret = AV_ERR_OK;
365 OH_AVCodecBufferAttr attr;
366 uint32_t trackIndex = 0;
367 int fd = open(file, O_RDONLY);
368 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
369 ASSERT_NE(source, nullptr);
370 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
371 ASSERT_NE(demuxer, nullptr);
372 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
373 ASSERT_EQ(ret, AV_ERR_OK);
374 while ((attr.flags & AVCODEC_BUFFER_FLAGS_EOS) != 0) {
375 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
376 ASSERT_EQ(ret, AV_ERR_OK);
377 }
378 OH_AVSource_Destroy(source);
379 close(fd);
380 }
381
382 /**
383 * @tc.number : DEMUXER_PERFORMANCE_0900
384 * @tc.name : test CopyNextSample time 3840x2160 30fps 50M
385 * @tc.desc : performance test
386 */
387 HWTEST_F(DemuxerPerfNdkTest, DEMUXER_PERFORMANCE_0900, TestSize.Level3)
388 {
389 const char *file = "/data/test/media/3840x2160_30.mp4";
390 OH_AVErrCode ret = AV_ERR_OK;
391 OH_AVCodecBufferAttr attr;
392 uint32_t trackIndex = 0;
393 int fd = open(file, O_RDONLY);
394 OH_AVSource *source = OH_AVSource_CreateWithFD(fd, 0, 0);
395 ASSERT_NE(source, nullptr);
396 OH_AVDemuxer *demuxer = OH_AVDemuxer_CreateWithSource(source);
397 ASSERT_NE(demuxer, nullptr);
398 ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
399 ASSERT_EQ(ret, AV_ERR_OK);
400 while ((attr.flags & AVCODEC_BUFFER_FLAGS_EOS) != 0) {
401 ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
402 ASSERT_EQ(ret, AV_ERR_OK);
403 }
404 OH_AVSource_Destroy(source);
405 close(fd);
406 }
407 }