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