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 "gtest/gtest.h"
17
18 #include "avdemuxer.h"
19 #include "avsource.h"
20 #include "meta/format.h"
21 #include "avcodec_errors.h"
22 #include "avcodec_common.h"
23 #include "buffer/avsharedmemory.h"
24 #include "buffer/avsharedmemorybase.h"
25 #include "securec.h"
26 #include "native_avcodec_base.h"
27
28 #include <iostream>
29 #include <cstdio>
30 #include <string>
31 #include <fcntl.h>
32
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::MediaAVCodec;
36 using namespace OHOS::Media;
37 using namespace testing::ext;
38
39 namespace {
40 static int32_t g_width = 3840;
41 static int32_t g_height = 2160;
42 static std::shared_ptr<AVSource> source = nullptr;
43 static std::shared_ptr<AVDemuxer> demuxer = nullptr;
44 const char *g_file1 = "/data/test/media/01_video_audio.mp4";
45 const char *g_file2 = "/data/test/media/avcc_10sec.mp4";
46 } // namespace
47
48 namespace {
49 class DemuxerInnerApiNdkTest : public testing::Test {
50 public:
51 // SetUpTestCase: Called before all test cases
52 static void SetUpTestCase(void);
53 // TearDownTestCase: Called after all test case
54 static void TearDownTestCase(void);
55 // SetUp: Called before each test cases
56 void SetUp(void);
57 // TearDown: Called after each test cases
58 void TearDown(void);
59
60 public:
61 int fd1;
62 int64_t size;
63 };
64
SetUpTestCase()65 void DemuxerInnerApiNdkTest::SetUpTestCase() {}
TearDownTestCase()66 void DemuxerInnerApiNdkTest::TearDownTestCase() {}
67
SetUp()68 void DemuxerInnerApiNdkTest::SetUp()
69 {
70 fd1 = open(g_file1, O_RDONLY);
71 struct stat fileStatus {};
72 if (stat(g_file1, &fileStatus) == 0) {
73 size = static_cast<int64_t>(fileStatus.st_size);
74 }
75
76 std::cout << fd1 << "----------" << g_file1 << "=====" << size << std::endl;
77 }
78
TearDown()79 void DemuxerInnerApiNdkTest::TearDown()
80 {
81 if (fd1 > 0) {
82 close(fd1);
83 fd1 = 0;
84 }
85
86 if (source != nullptr) {
87 source = nullptr;
88 }
89
90 if (demuxer != nullptr) {
91 demuxer = nullptr;
92 }
93 }
94 } // namespace
95
96 namespace {
97 /**
98 * @tc.number : DEMUXER_ILLEGAL_PARA_0100
99 * @tc.name : CreateWithURI para error
100 * @tc.desc : param test
101 */
102 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0100, TestSize.Level2)
103 {
104 source = AVSourceFactory::CreateWithURI("");
105 ASSERT_EQ(nullptr, source);
106 }
107
108 /**
109 * @tc.number : DEMUXER_ILLEGAL_PARA_0200
110 * @tc.name : CreateWithFD para error
111 * @tc.desc : param test
112 */
113 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0200, TestSize.Level2)
114 {
115 // fd must bigger than 2
116 source = AVSourceFactory::CreateWithFD(2, 0, 0);
117 ASSERT_EQ(nullptr, source);
118 }
119
120 /**
121 * @tc.number : DEMUXER_ILLEGAL_PARA_0300
122 * @tc.name : CreateWithFD para error
123 * @tc.desc : param test
124 */
125 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0300, TestSize.Level2)
126 {
127 // fd must bigger than 2
128 source = AVSourceFactory::CreateWithFD(3, 0, -1);
129 ASSERT_EQ(nullptr, source);
130 }
131
132 /**
133 * @tc.number : DEMUXER_ILLEGAL_PARA_0400
134 * @tc.name : CreateWithFD para error
135 * @tc.desc : param test
136 */
137 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0400, TestSize.Level2)
138 {
139 // fd must bigger than
140 source = AVSourceFactory::CreateWithFD(3, -1, 1);
141 ASSERT_EQ(nullptr, source);
142 }
143
144 /**
145 * @tc.number : DEMUXER_ILLEGAL_PARA_0500
146 * @tc.name : GetTrackFormat para error
147 * @tc.desc : param test
148 */
149 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0500, TestSize.Level2)
150 {
151 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
152 ASSERT_NE(nullptr, source);
153
154 Format format;
155 int32_t ret = source->GetTrackFormat(format, -1);
156 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
157 }
158
159 /**
160 * @tc.number : DEMUXER_ILLEGAL_PARA_0600
161 * @tc.name : CreateWithFD para error
162 * @tc.desc : param test
163 */
164 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0600, TestSize.Level2)
165 {
166 source = AVSourceFactory::CreateWithFD(fd1, 0, 0);
167 ASSERT_EQ(nullptr, source);
168 }
169
170 /**
171 * @tc.number : DEMUXER_ILLEGAL_PARA_0700
172 * @tc.name : CreateWithSource para error
173 * @tc.desc : param test
174 */
175 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0700, TestSize.Level2)
176 {
177 demuxer = AVDemuxerFactory::CreateWithSource(nullptr);
178 ASSERT_EQ(nullptr, demuxer);
179 }
180
181 /**
182 * @tc.number : DEMUXER_ILLEGAL_PARA_0800
183 * @tc.name : SelectTrackByID para error
184 * @tc.desc : param test
185 */
186 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0800, TestSize.Level2)
187 {
188 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
189 ASSERT_NE(nullptr, source);
190 demuxer = AVDemuxerFactory::CreateWithSource(source);
191 ASSERT_NE(nullptr, demuxer);
192 ASSERT_EQ(AVCS_ERR_INVALID_VAL, demuxer->SelectTrackByID(-1));
193 }
194
195 /**
196 * @tc.number : DEMUXER_ILLEGAL_PARA_0900
197 * @tc.name : UnselectTrackByID para error
198 * @tc.desc : param test
199 */
200 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_0900, TestSize.Level2)
201 {
202 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
203 ASSERT_NE(nullptr, source);
204 demuxer = AVDemuxerFactory::CreateWithSource(source);
205 ASSERT_NE(nullptr, demuxer);
206 ASSERT_EQ(AVCS_ERR_OK, demuxer->UnselectTrackByID(-1));
207 }
208
209 /**
210 * @tc.number : DEMUXER_ILLEGAL_PARA_1000
211 * @tc.name : ReadSample para error
212 * @tc.desc : param test
213 */
214 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_1000, TestSize.Level2)
215 {
216 int32_t size = g_width * g_height;
217 uint32_t trackIndex = -1;
218 uint8_t data[size];
219 std::shared_ptr<AVSharedMemoryBase> avMemBuffer = std::make_shared<AVSharedMemoryBase>
220 (size, AVSharedMemory::FLAGS_READ_WRITE, "userBuffer");
221 avMemBuffer->Init();
222 (void)memcpy_s(avMemBuffer->GetBase(), avMemBuffer->GetSize(), data, size);
223
224 AVCodecBufferInfo info;
225 uint32_t flag;
226 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
227 ASSERT_NE(nullptr, source);
228 demuxer = AVDemuxerFactory::CreateWithSource(source);
229 ASSERT_NE(nullptr, demuxer);
230 ASSERT_EQ(AVCS_ERR_INVALID_OPERATION, demuxer->ReadSample(trackIndex, avMemBuffer, info, flag));
231 }
232
233 /**
234 * @tc.number : DEMUXER_ILLEGAL_PARA_1100
235 * @tc.name : ReadSample para error
236 * @tc.desc : param test
237 */
238 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_1100, TestSize.Level2)
239 {
240 uint8_t data[100];
241 std::shared_ptr<AVSharedMemoryBase> avMemBuffer = std::make_shared<AVSharedMemoryBase>(2,
242 AVSharedMemory::FLAGS_READ_WRITE, "userBuffer");
243 avMemBuffer->Init();
244 (void)memcpy_s(avMemBuffer->GetBase(), avMemBuffer->GetSize(), data, 2);
245
246 uint32_t trackIndex = 0;
247 AVCodecBufferInfo info;
248 uint32_t flag;
249 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
250 ASSERT_NE(nullptr, source);
251 demuxer = AVDemuxerFactory::CreateWithSource(source);
252 ASSERT_NE(nullptr, demuxer);
253 ASSERT_EQ(AVCS_ERR_OK, demuxer->SelectTrackByID(0));
254 ASSERT_EQ(AVCS_ERR_NO_MEMORY, demuxer->ReadSample(trackIndex, avMemBuffer, info, flag));
255 }
256
257 /**
258 * @tc.number : DEMUXER_ILLEGAL_PARA_1200
259 * @tc.name : ReadSample para error
260 * @tc.desc : param test
261 */
262 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_1200, TestSize.Level2)
263 {
264 uint8_t data[100];
265 std::shared_ptr<AVSharedMemoryBase> avMemBuffer = std::make_shared<AVSharedMemoryBase>(2,
266 AVSharedMemory::FLAGS_READ_WRITE, "userBuffer");
267 avMemBuffer->Init();
268 (void)memcpy_s(avMemBuffer->GetBase(), avMemBuffer->GetSize(), data, 2);
269
270 uint32_t trackIndex = 0;
271 AVCodecBufferInfo info;
272 uint32_t flag;
273 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
274 ASSERT_NE(nullptr, source);
275 demuxer = AVDemuxerFactory::CreateWithSource(source);
276 ASSERT_NE(nullptr, demuxer);
277 ASSERT_EQ(AVCS_ERR_INVALID_OPERATION, demuxer->ReadSample(trackIndex, avMemBuffer, info, flag));
278 }
279
280 /**
281 * @tc.number : DEMUXER_ILLEGAL_PARA_1300
282 * @tc.name : ReadSample para error
283 * @tc.desc : param test
284 */
285 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_1300, TestSize.Level2)
286 {
287 uint32_t trackIndex = 0;
288 AVCodecBufferInfo info;
289 uint32_t flag;
290 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
291 ASSERT_NE(nullptr, source);
292 demuxer = AVDemuxerFactory::CreateWithSource(source);
293 ASSERT_NE(nullptr, demuxer);
294 ASSERT_EQ(AVCS_ERR_INVALID_VAL, demuxer->ReadSample(trackIndex, nullptr, info, flag));
295 }
296
297 /**
298 * @tc.number : DEMUXER_ILLEGAL_PARA_1400
299 * @tc.name : Memory_Create para error
300 * @tc.desc : param test
301 */
302 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_ILLEGAL_PARA_1400, TestSize.Level2)
303 {
304 int32_t size = g_width * g_height;
305 uint8_t data[size];
306 std::shared_ptr<AVSharedMemoryBase> avMemBuffer = std::make_shared<AVSharedMemoryBase>
307 (size, AVSharedMemory::FLAGS_READ_WRITE, "userBuffer");
308 avMemBuffer->Init();
309 (void)memcpy_s(avMemBuffer->GetBase(), avMemBuffer->GetSize(), data, size);
310
311 ASSERT_NE(nullptr, avMemBuffer);
312 }
313
314 /**
315 * @tc.number : DEMUXER_API_0100
316 * @tc.name : CreateWithFD Repeat Call
317 * @tc.desc : api test
318 */
319 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_API_0100, TestSize.Level2)
320 {
321 std::shared_ptr<AVSource> source1 = AVSourceFactory::CreateWithFD(fd1, 0, size);
322 ASSERT_NE(nullptr, source1);
323
324 int fd2 = open(g_file2, O_RDONLY);
325 int64_t size2 = 0;
326
327 struct stat fileStatus {};
328 if (stat(g_file2, &fileStatus) == 0) {
329 size2 = static_cast<int64_t>(fileStatus.st_size);
330 }
331 std::shared_ptr<AVSource> source2 = AVSourceFactory::CreateWithFD(fd2, 0, size2);
332 ASSERT_NE(nullptr, source2);
333 close(fd2);
334 }
335
336 /**
337 * @tc.number : DEMUXER_API_0200
338 * @tc.name : GetSourceFormat Repeat Call
339 * @tc.desc : api test
340 */
341 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_API_0200, TestSize.Level2)
342 {
343 Format format;
344 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
345 ASSERT_NE(nullptr, source);
346
347 int32_t ret = source->GetSourceFormat(format);
348 ASSERT_EQ(AVCS_ERR_OK, ret);
349 ret = source->GetSourceFormat(format);
350 ASSERT_EQ(AVCS_ERR_OK, ret);
351 }
352
353 /**
354 * @tc.number : DEMUXER_API_0300
355 * @tc.name : GetTrackFormat Repeat Call
356 * @tc.desc : api test
357 */
358 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_API_0300, TestSize.Level2)
359 {
360 Format format;
361 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
362 ASSERT_NE(nullptr, source);
363
364 int32_t ret = source->GetTrackFormat(format, 0);
365 ASSERT_EQ(AVCS_ERR_OK, ret);
366 ret = source->GetTrackFormat(format, 0);
367 ASSERT_EQ(AVCS_ERR_OK, ret);
368 }
369
370 /**
371 * @tc.number : DEMUXER_API_0400
372 * @tc.name : SelectTrackByID Repeat Call
373 * @tc.desc : api test
374 */
375 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_API_0400, TestSize.Level2)
376 {
377 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
378 ASSERT_NE(nullptr, source);
379 demuxer = AVDemuxerFactory::CreateWithSource(source);
380 ASSERT_NE(nullptr, demuxer);
381
382 int32_t ret = demuxer->SelectTrackByID(0);
383 ASSERT_EQ(AVCS_ERR_OK, ret);
384 ret = demuxer->SelectTrackByID(0);
385 ASSERT_EQ(AVCS_ERR_OK, ret);
386 }
387
388 /**
389 * @tc.number : DEMUXER_API_0500
390 * @tc.name : UnselectTrackByID Repeat Call
391 * @tc.desc : api test
392 */
393 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_API_0500, TestSize.Level2)
394 {
395 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
396 ASSERT_NE(nullptr, source);
397 demuxer = AVDemuxerFactory::CreateWithSource(source);
398 ASSERT_NE(nullptr, demuxer);
399
400 int32_t ret = demuxer->SelectTrackByID(0);
401 ASSERT_EQ(AVCS_ERR_OK, ret);
402 ret = demuxer->UnselectTrackByID(0);
403 ASSERT_EQ(AVCS_ERR_OK, ret);
404 ret = demuxer->UnselectTrackByID(0);
405 ASSERT_EQ(AVCS_ERR_OK, ret);
406 }
407
408 /**
409 * @tc.number : DEMUXER_API_0600
410 * @tc.name : ReadSample Repeat Call
411 * @tc.desc : api test
412 */
413 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_API_0600, TestSize.Level2)
414 {
415 int32_t size = g_width * g_height;
416 uint8_t data[size];
417 std::shared_ptr<AVSharedMemoryBase> avMemBuffer = std::make_shared<AVSharedMemoryBase>
418 (size, AVSharedMemory::FLAGS_READ_WRITE, "userBuffer");
419 avMemBuffer->Init();
420 (void)memcpy_s(avMemBuffer->GetBase(), avMemBuffer->GetSize(), data, size);
421
422 AVCodecBufferInfo info;
423 uint32_t flag;
424 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
425 ASSERT_NE(nullptr, source);
426 demuxer = AVDemuxerFactory::CreateWithSource(source);
427 ASSERT_NE(nullptr, demuxer);
428
429 int32_t ret = demuxer->SelectTrackByID(0);
430 ASSERT_EQ(AVCS_ERR_OK, ret);
431 ret = demuxer->ReadSample(0, avMemBuffer, info, flag);
432 ASSERT_EQ(AVCS_ERR_OK, ret);
433 ret = demuxer->ReadSample(0, avMemBuffer, info, flag);
434 ASSERT_EQ(AVCS_ERR_OK, ret);
435 }
436
437 /**
438 * @tc.number : DEMUXER_API_0700
439 * @tc.name : SeekToTime Repeat Call
440 * @tc.desc : api test
441 */
442 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_API_0700, TestSize.Level2)
443 {
444 uint32_t ms = 1000;
445 source = AVSourceFactory::CreateWithFD(fd1, 0, size);
446 ASSERT_NE(nullptr, source);
447 demuxer = AVDemuxerFactory::CreateWithSource(source);
448 ASSERT_NE(nullptr, demuxer);
449
450 int32_t ret = demuxer->SelectTrackByID(0);
451 ASSERT_EQ(AVCS_ERR_OK, ret);
452 ret = demuxer->SeekToTime(ms, SeekMode::SEEK_NEXT_SYNC);
453 ASSERT_EQ(AVCS_ERR_OK, ret);
454 ret = demuxer->SeekToTime(ms, SeekMode::SEEK_NEXT_SYNC);
455 ASSERT_EQ(AVCS_ERR_OK, ret);
456 }
457
458 /**
459 * @tc.number : DEMUXER_PTS_INDEX_INNER_API_0010
460 * @tc.name : GetFrameIndexByPresentationTimeUs with unSupported source
461 * @tc.desc : api test
462 */
463 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_PTS_INDEX_INNER_API_0010, TestSize.Level1)
464 {
465 int32_t trackType;
466 int32_t trackCount;
467 Format source_format;
468 Format track_format;
469 uint32_t frameIndex = 0;
470 const char *file = "/data/test/media/h264_aac_1280.ts";
471 int fd = open(file, O_RDONLY);
472 struct stat fileStatus {};
473 if (stat(file, &fileStatus) == 0) {
474 size = static_cast<int64_t>(fileStatus.st_size);
475 }
476 cout << file << "----------------------" << fd << "---------" << size << endl;
477 source = AVSourceFactory::CreateWithFD(fd, 0, size);
478 ASSERT_NE(source, nullptr);
479
480 demuxer = AVDemuxerFactory::CreateWithSource(source);
481 ASSERT_NE(demuxer, nullptr);
482
483 int32_t ret = source->GetSourceFormat(source_format);
484 ASSERT_EQ(AVCS_ERR_OK, ret);
485
486 ASSERT_TRUE(source_format.GetIntValue(OH_MD_KEY_TRACK_COUNT, trackCount));
487 for (int32_t index = 0; index < trackCount; index++) {
488 ret = demuxer->SelectTrackByID(index);
489 ASSERT_EQ(AVCS_ERR_OK, ret);
490 ret = source->GetTrackFormat(track_format, index);
491 ASSERT_EQ(AVCS_ERR_OK, ret);
492 track_format.GetIntValue(OH_MD_KEY_TRACK_TYPE, trackType);
493 if (trackType == OHOS::Media::MEDIA_TYPE_VID) {
494 ret = demuxer->GetIndexByRelativePresentationTimeUs(index, 100000, frameIndex);
495 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
496 }else if (trackType == OHOS::Media::MEDIA_TYPE_VID) {
497 ret = demuxer->GetIndexByRelativePresentationTimeUs(index, 1804855, frameIndex);
498 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
499 }
500 }
501 close(fd);
502 }
503
504 /**
505 * @tc.number : DEMUXER_PTS_INDEX_INNER_API_0020
506 * @tc.name : GetFrameIndexByPresentationTimeUs with non-existent trackIndex
507 * @tc.desc : api test
508 */
509 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_PTS_INDEX_INNER_API_0020, TestSize.Level1)
510 {
511 int32_t trackCount;
512 Format source_format;
513 Format track_format;
514 uint32_t frameIndex = 0;
515 const char *file = "/data/test/media/demuxer_parser_ipb_frame_avc.mp4";
516 int fd = open(file, O_RDONLY);
517 struct stat fileStatus {};
518 if (stat(file, &fileStatus) == 0) {
519 size = static_cast<int64_t>(fileStatus.st_size);
520 }
521 cout << file << "----------------------" << fd << "---------" << size << endl;
522 source = AVSourceFactory::CreateWithFD(fd, 0, size);
523 ASSERT_NE(source, nullptr);
524
525 demuxer = AVDemuxerFactory::CreateWithSource(source);
526 ASSERT_NE(demuxer, nullptr);
527
528 int32_t ret = source->GetSourceFormat(source_format);
529 ASSERT_EQ(AVCS_ERR_OK, ret);
530
531 ASSERT_TRUE(source_format.GetIntValue(OH_MD_KEY_TRACK_COUNT, trackCount));
532 ret = demuxer->GetIndexByRelativePresentationTimeUs(trackCount+1, 7733333, frameIndex);
533 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
534 close(fd);
535 }
536
537 /**
538 * @tc.number : DEMUXER_PTS_INDEX_INNER_API_0030
539 * @tc.name : GetFrameIndexByPresentationTimeUs with non-existent presentationTimeUs
540 * @tc.desc : api test
541 */
542 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_PTS_INDEX_INNER_API_0030, TestSize.Level1)
543 {
544 int32_t trackType;
545 int32_t trackCount;
546 Format source_format;
547 Format track_format;
548 uint32_t frameIndex = 0;
549 const char *file = "/data/test/media/demuxer_parser_ipb_frame_avc.mp4";
550 int fd = open(file, O_RDONLY);
551 struct stat fileStatus {};
552 if (stat(file, &fileStatus) == 0) {
553 size = static_cast<int64_t>(fileStatus.st_size);
554 }
555 cout << file << "----------------------" << fd << "---------" << size << endl;
556 source = AVSourceFactory::CreateWithFD(fd, 0, size);
557 ASSERT_NE(source, nullptr);
558
559 demuxer = AVDemuxerFactory::CreateWithSource(source);
560 ASSERT_NE(demuxer, nullptr);
561
562 int32_t ret = source->GetSourceFormat(source_format);
563 ASSERT_EQ(AVCS_ERR_OK, ret);
564
565 ASSERT_TRUE(source_format.GetIntValue(OH_MD_KEY_TRACK_COUNT, trackCount));
566 for (int32_t index = 0; index < trackCount; index++) {
567 ret = demuxer->SelectTrackByID(index);
568 ASSERT_EQ(AVCS_ERR_OK, ret);
569 ret = source->GetTrackFormat(track_format, index);
570 ASSERT_EQ(AVCS_ERR_OK, ret);
571 track_format.GetIntValue(OH_MD_KEY_TRACK_TYPE, trackType);
572 if (trackType == OHOS::Media::MEDIA_TYPE_VID) {
573 ret = demuxer->GetIndexByRelativePresentationTimeUs(index, 20000000, frameIndex);
574 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
575 } else if (trackType == OHOS::Media::MEDIA_TYPE_AUD) {
576 ret = demuxer->GetIndexByRelativePresentationTimeUs(index, 20000000, frameIndex);
577 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
578 }
579 }
580 close(fd);
581 }
582
583 /**
584 * @tc.number : DEMUXER_PTS_INDEX_INNER_API_0040
585 * @tc.name : GetPresentationTimeUsByFrameIndex with unSupported source
586 * @tc.desc : api test
587 */
588 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_PTS_INDEX_INNER_API_0040, TestSize.Level1)
589 {
590 int32_t trackType;
591 int32_t trackCount;
592 Format source_format;
593 Format track_format;
594 uint64_t presentationTimeUs;
595 const char *file = "/data/test/media/h264_aac_1280.ts";
596 int fd = open(file, O_RDONLY);
597 struct stat fileStatus {};
598 if (stat(file, &fileStatus) == 0) {
599 size = static_cast<int64_t>(fileStatus.st_size);
600 }
601 cout << file << "----------------------" << fd << "---------" << size << endl;
602 source = AVSourceFactory::CreateWithFD(fd, 0, size);
603 ASSERT_NE(source, nullptr);
604
605 demuxer = AVDemuxerFactory::CreateWithSource(source);
606 ASSERT_NE(demuxer, nullptr);
607
608 int32_t ret = source->GetSourceFormat(source_format);
609 ASSERT_EQ(AVCS_ERR_OK, ret);
610
611 ASSERT_TRUE(source_format.GetIntValue(OH_MD_KEY_TRACK_COUNT, trackCount));
612 for (int32_t index = 0; index < trackCount; index++) {
613 ret = demuxer->SelectTrackByID(index);
614 ASSERT_EQ(AVCS_ERR_OK, ret);
615 ret = source->GetTrackFormat(track_format, index);
616 ASSERT_EQ(AVCS_ERR_OK, ret);
617 track_format.GetIntValue(OH_MD_KEY_TRACK_TYPE, trackType);
618 if (trackType == OHOS::Media::MEDIA_TYPE_VID) {
619 ret = demuxer->GetRelativePresentationTimeUsByIndex(index, 5, presentationTimeUs);
620 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
621 }else if (trackType == OHOS::Media::MEDIA_TYPE_AUD) {
622 ret = demuxer->GetRelativePresentationTimeUsByIndex(index, 172, presentationTimeUs);
623 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
624 }
625 }
626 close(fd);
627 }
628
629 /**
630 * @tc.number : DEMUXER_PTS_INDEX_INNER_API_0050
631 * @tc.name : GetPresentationTimeUsByFrameIndex with non-existent trackIndex
632 * @tc.desc : api test
633 */
634 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_PTS_INDEX_INNER_API_0050, TestSize.Level1)
635 {
636 int32_t trackCount;
637 Format source_format;
638 Format track_format;
639 uint64_t presentationTimeUs;
640 const char *file = "/data/test/media/demuxer_parser_ipb_frame_avc.mp4";
641 int fd = open(file, O_RDONLY);
642 struct stat fileStatus {};
643 if (stat(file, &fileStatus) == 0) {
644 size = static_cast<int64_t>(fileStatus.st_size);
645 }
646 cout << file << "----------------------" << fd << "---------" << size << endl;
647 source = AVSourceFactory::CreateWithFD(fd, 0, size);
648 ASSERT_NE(source, nullptr);
649
650 demuxer = AVDemuxerFactory::CreateWithSource(source);
651 ASSERT_NE(demuxer, nullptr);
652
653 int32_t ret = source->GetSourceFormat(source_format);
654 ASSERT_EQ(AVCS_ERR_OK, ret);
655
656 ASSERT_TRUE(source_format.GetIntValue(OH_MD_KEY_TRACK_COUNT, trackCount));
657 ret = demuxer->GetRelativePresentationTimeUsByIndex(trackCount+1, 15, presentationTimeUs);
658 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
659 close(fd);
660 }
661
662 /**
663 * @tc.number : DEMUXER_PTS_INDEX_INNER_API_0060
664 * @tc.name : GetPresentationTimeUsByFrameIndex with non-existent frameIndex
665 * @tc.desc : api test
666 */
667 HWTEST_F(DemuxerInnerApiNdkTest, DEMUXER_PTS_INDEX_INNER_API_0060, TestSize.Level1)
668 {
669 int32_t trackType;
670 int32_t trackCount;
671 Format source_format;
672 Format track_format;
673 uint64_t presentationTimeUs;
674 const char *file = "/data/test/media/demuxer_parser_ipb_frame_avc.mp4";
675 int fd = open(file, O_RDONLY);
676 struct stat fileStatus {};
677 if (stat(file, &fileStatus) == 0) {
678 size = static_cast<int64_t>(fileStatus.st_size);
679 }
680 cout << file << "----------------------" << fd << "---------" << size << endl;
681 source = AVSourceFactory::CreateWithFD(fd, 0, size);
682 ASSERT_NE(source, nullptr);
683
684 demuxer = AVDemuxerFactory::CreateWithSource(source);
685 ASSERT_NE(demuxer, nullptr);
686
687 int32_t ret = source->GetSourceFormat(source_format);
688 ASSERT_EQ(AVCS_ERR_OK, ret);
689
690 ASSERT_TRUE(source_format.GetIntValue(OH_MD_KEY_TRACK_COUNT, trackCount));
691 for (int32_t index = 0; index < trackCount; index++) {
692 ret = demuxer->SelectTrackByID(index);
693 ASSERT_EQ(AVCS_ERR_OK, ret);
694 ret = source->GetTrackFormat(track_format, index);
695 ASSERT_EQ(AVCS_ERR_OK, ret);
696 track_format.GetIntValue(OH_MD_KEY_TRACK_TYPE, trackType);
697 if (trackType == OHOS::Media::MEDIA_TYPE_VID) {
698 ret = demuxer->GetRelativePresentationTimeUsByIndex(index, 600, presentationTimeUs);
699 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
700 }else if (trackType == OHOS::Media::MEDIA_TYPE_AUD) {
701 ret = demuxer->GetRelativePresentationTimeUsByIndex(index, 600, presentationTimeUs);
702 ASSERT_EQ(AVCS_ERR_INVALID_VAL, ret);
703 }
704 }
705 close(fd);
706 }
707 } // namespace
708