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 "native_avcodec_base.h"
19#include "native_avdemuxer.h"
20#include "native_avformat.h"
21#include "native_avsource.h"
22#include "native_avmemory.h"
23
24#include <iostream>
25#include <cstdio>
26#include <string>
27#include <fcntl.h>
28#include <cmath>
29#include <thread>
30namespace OHOS {
31namespace Media {
32class DemuxerFuncNdkTest : public testing::Test {
33public:
34    // SetUpTestCase: Called before all test cases
35    static void SetUpTestCase(void);
36    // TearDownTestCase: Called after all test case
37    static void TearDownTestCase(void);
38    // SetUp: Called before each test cases
39    void SetUp(void);
40    // TearDown: Called after each test cases
41    void TearDown(void);
42};
43
44static OH_AVMemory *memory = nullptr;
45static OH_AVSource *source = nullptr;
46static OH_AVErrCode ret = AV_ERR_OK;
47static OH_AVDemuxer *demuxer = nullptr;
48static OH_AVFormat *sourceFormat = nullptr;
49static OH_AVFormat *trackFormat = nullptr;
50static OH_AVBuffer *avBuffer = nullptr;
51static OH_AVFormat *format = nullptr;
52static int32_t g_trackCount;
53static int32_t g_width = 3840;
54static int32_t g_height = 2160;
55const std::string HEVC_LIB_PATH = std::string(AV_CODEC_PATH) + "/libav_codec_hevc_parser.z.so";
56void DemuxerFuncNdkTest::SetUpTestCase() {}
57void DemuxerFuncNdkTest::TearDownTestCase() {}
58void DemuxerFuncNdkTest::SetUp()
59{
60    memory = OH_AVMemory_Create(g_width * g_height);
61    g_trackCount = 0;
62}
63void DemuxerFuncNdkTest::TearDown()
64{
65    if (trackFormat != nullptr) {
66        OH_AVFormat_Destroy(trackFormat);
67        trackFormat = nullptr;
68    }
69
70    if (sourceFormat != nullptr) {
71        OH_AVFormat_Destroy(sourceFormat);
72        sourceFormat = nullptr;
73    }
74    if (format != nullptr) {
75        OH_AVFormat_Destroy(format);
76        format = nullptr;
77    }
78
79    if (memory != nullptr) {
80        OH_AVMemory_Destroy(memory);
81        memory = nullptr;
82    }
83    if (source != nullptr) {
84        OH_AVSource_Destroy(source);
85        source = nullptr;
86    }
87    if (demuxer != nullptr) {
88        OH_AVDemuxer_Destroy(demuxer);
89        demuxer = nullptr;
90    }
91    if (avBuffer != nullptr) {
92        OH_AVBuffer_Destroy(avBuffer);
93        avBuffer = nullptr;
94    }
95}
96} // namespace Media
97} // namespace OHOS
98
99using namespace std;
100using namespace OHOS;
101using namespace OHOS::Media;
102using namespace testing::ext;
103
104static int64_t GetFileSize(const char *fileName)
105{
106    int64_t fileSize = 0;
107    if (fileName != nullptr) {
108        struct stat fileStatus {};
109        if (stat(fileName, &fileStatus) == 0) {
110            fileSize = static_cast<int64_t>(fileStatus.st_size);
111        }
112    }
113    return fileSize;
114}
115
116static void SetAudioValue(OH_AVCodecBufferAttr attr, bool &audioIsEnd, int &audioFrame, int &aKeyCount)
117{
118    if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
119        audioIsEnd = true;
120        cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
121    } else {
122        audioFrame++;
123        if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
124            aKeyCount++;
125        }
126    }
127}
128
129static void SetVideoValue(OH_AVCodecBufferAttr attr, bool &videoIsEnd, int &videoFrame, int &vKeyCount)
130{
131    if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
132        videoIsEnd = true;
133        cout << videoFrame << "   video is end !!!!!!!!!!!!!!!" << endl;
134    } else {
135        videoFrame++;
136        cout << "video track !!!!!" << endl;
137        if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
138            vKeyCount++;
139        }
140    }
141}
142
143static void SetVarValue(OH_AVCodecBufferAttr attr, const int &tarckType, bool &audioIsEnd, bool &videoIsEnd)
144{
145    if (tarckType == MEDIA_TYPE_AUD && (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS)) {
146        audioIsEnd = true;
147        cout << "audio is end !!!!!!!!!!!!!!!" << endl;
148    }
149
150    if (tarckType == MEDIA_TYPE_VID && (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS)) {
151        videoIsEnd = true;
152        cout << "video is end !!!!!!!!!!!!!!!" << endl;
153    }
154}
155
156static void SetFirstFrameFlag(bool &isFirstFrame)
157{
158    if (isFirstFrame) {
159        isFirstFrame = false;
160    }
161}
162
163static void SetEndFlag(bool &audioIsEnd, bool &videoIsEnd)
164{
165    audioIsEnd = true;
166    videoIsEnd = true;
167}
168
169/**
170 * @tc.number    : DEMUXER_FUNCTION_0200
171 * @tc.name      : create source with no permission URI
172 * @tc.desc      : function test
173 */
174HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_0200, TestSize.Level0)
175{
176    const char *uri = "http://10.174.172.228:8080/share/audio/AAC_48000_1.aac";
177    source = OH_AVSource_CreateWithURI(const_cast<char *>(uri));
178    ASSERT_EQ(nullptr, source);
179}
180
181/**
182 * @tc.number    : DEMUXER_FUNCTION_0300
183 * @tc.name      : create source with invalid uri
184 * @tc.desc      : function test
185 */
186HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_0300, TestSize.Level1)
187{
188    const char *uri = "http://invalidPath/invalid.mp4";
189    source = OH_AVSource_CreateWithURI(const_cast<char *>(uri));
190    ASSERT_EQ(nullptr, source);
191}
192
193/**
194 * @tc.number    : DEMUXER_FUNCTION_0400
195 * @tc.name      : create source with fd but no permission
196 * @tc.desc      : function test
197 */
198HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_0400, TestSize.Level1)
199{
200    const char *file = "/data/media/noPermission.mp4";
201    int fd = open(file, O_RDONLY);
202    int64_t size = GetFileSize(file);
203    cout << file << "----------------------" << fd << "---------" << size << endl;
204    cout << file << "------" << size << endl;
205    source = OH_AVSource_CreateWithFD(fd, 0, size);
206    demuxer = OH_AVDemuxer_CreateWithSource(source);
207    ASSERT_EQ(demuxer, nullptr);
208
209    close(fd);
210}
211
212/**
213 * @tc.number    : DEMUXER_FUNCTION_0500
214 * @tc.name      : create source with invalid fd
215 * @tc.desc      : function test
216 */
217HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_0500, TestSize.Level1)
218{
219    const char *file = "/data/test/media/invalid.mp4";
220    int fd = open(file, O_RDONLY);
221    int64_t size = GetFileSize(file);
222    cout << file << "----------------------" << fd << "---------" << size << endl;
223    source = OH_AVSource_CreateWithFD(fd, 0, size);
224    ASSERT_EQ(source, nullptr);
225    close(fd);
226}
227
228/**
229 * @tc.number    : DEMUXER_FUNCTION_0700
230 * @tc.name      : create source with fd, mp4
231 * @tc.desc      : function test
232 */
233HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_0700, TestSize.Level0)
234{
235    int tarckType = 0;
236    OH_AVCodecBufferAttr attr;
237    bool audioIsEnd = false;
238    bool videoIsEnd = false;
239    const char *file = "/data/test/media/01_video_audio.mp4";
240    int fd = open(file, O_RDONLY);
241    int64_t size = GetFileSize(file);
242    cout << file << "----------------------" << fd << "---------" << size << endl;
243    source = OH_AVSource_CreateWithFD(fd, 0, size);
244    ASSERT_NE(source, nullptr);
245
246    demuxer = OH_AVDemuxer_CreateWithSource(source);
247    ASSERT_NE(demuxer, nullptr);
248
249    sourceFormat = OH_AVSource_GetSourceFormat(source);
250    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
251    ASSERT_EQ(2, g_trackCount);
252    for (int32_t index = 0; index < g_trackCount; index++) {
253        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
254    }
255    int64_t starttime = 0;
256    while (!audioIsEnd || !videoIsEnd) {
257        for (int32_t index = 0; index < g_trackCount; index++) {
258            trackFormat = OH_AVSource_GetTrackFormat(source, index);
259            ASSERT_NE(trackFormat, nullptr);
260            ASSERT_TRUE(OH_AVFormat_GetLongValue(trackFormat, OH_MD_KEY_TRACK_START_TIME, &starttime));
261            ASSERT_EQ(0, starttime);
262            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
263            OH_AVFormat_Destroy(trackFormat);
264            trackFormat = nullptr;
265
266            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
267                continue;
268            }
269            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
270
271            SetVarValue(attr, tarckType, audioIsEnd, videoIsEnd);
272        }
273    }
274    close(fd);
275}
276
277/**
278 * @tc.number    : DEMUXER_FUNCTION_0800
279 * @tc.name      : create source with fd,avcc mp4
280 * @tc.desc      : function test
281 */
282HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_0800, TestSize.Level0)
283{
284    int tarckType = 0;
285    OH_AVCodecBufferAttr attr;
286    bool audioIsEnd = false;
287    bool videoIsEnd = false;
288    int audioFrame = 0;
289    int videoFrame = 0;
290    const char *file = "/data/test/media/avcc_10sec.mp4";
291    int fd = open(file, O_RDONLY);
292    int64_t size = GetFileSize(file);
293    cout << file << "----------------------" << fd << "---------" << size << endl;
294    source = OH_AVSource_CreateWithFD(fd, 0, size);
295    ASSERT_NE(source, nullptr);
296
297    demuxer = OH_AVDemuxer_CreateWithSource(source);
298    ASSERT_NE(demuxer, nullptr);
299
300    sourceFormat = OH_AVSource_GetSourceFormat(source);
301    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
302    ASSERT_EQ(2, g_trackCount);
303
304    for (int32_t index = 0; index < g_trackCount; index++) {
305        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
306    }
307
308    int aKeyCount = 0;
309    int vKeyCount = 0;
310    while (!audioIsEnd || !videoIsEnd) {
311        for (int32_t index = 0; index < g_trackCount; index++) {
312            trackFormat = OH_AVSource_GetTrackFormat(source, index);
313            ASSERT_NE(trackFormat, nullptr);
314            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
315            OH_AVFormat_Destroy(trackFormat);
316            trackFormat = nullptr;
317            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
318                continue;
319            }
320            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
321
322            if (tarckType == MEDIA_TYPE_AUD) {
323                SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
324            } else if (tarckType == MEDIA_TYPE_VID) {
325                SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
326            }
327        }
328    }
329    ASSERT_EQ(audioFrame, 431);
330    ASSERT_EQ(videoFrame, 600);
331    ASSERT_EQ(aKeyCount, 431);
332    ASSERT_EQ(vKeyCount, 10);
333    close(fd);
334}
335
336/**
337 * @tc.number    : DEMUXER_FUNCTION_0900
338 * @tc.name      : create source with fd,hvcc mp4
339 * @tc.desc      : function test
340 */
341HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_0900, TestSize.Level0)
342{
343    int tarckType = 0;
344    OH_AVCodecBufferAttr attr;
345    bool audioIsEnd = false;
346    bool videoIsEnd = false;
347    int audioFrame = 0;
348    int videoFrame = 0;
349    const char *file = "/data/test/media/hvcc.mp4";
350    int fd = open(file, O_RDONLY);
351    int64_t size = GetFileSize(file);
352    source = OH_AVSource_CreateWithFD(fd, 0, size);
353    ASSERT_NE(source, nullptr);
354
355    demuxer = OH_AVDemuxer_CreateWithSource(source);
356    ASSERT_NE(demuxer, nullptr);
357
358    sourceFormat = OH_AVSource_GetSourceFormat(source);
359    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
360    ASSERT_EQ(2, g_trackCount);
361
362    for (int32_t index = 0; index < g_trackCount; index++) {
363        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
364    }
365
366    int aKeyCount = 0;
367    int vKeyCount = 0;
368    while (!audioIsEnd || !videoIsEnd) {
369        for (int32_t index = 0; index < g_trackCount; index++) {
370            trackFormat = OH_AVSource_GetTrackFormat(source, index);
371            ASSERT_NE(trackFormat, nullptr);
372            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
373            OH_AVFormat_Destroy(trackFormat);
374            trackFormat = nullptr;
375            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
376                continue;
377            }
378            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
379
380            if (tarckType == MEDIA_TYPE_AUD) {
381                SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
382            } else if (tarckType == MEDIA_TYPE_VID) {
383                SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
384            }
385        }
386    }
387    ASSERT_EQ(audioFrame, 433);
388    ASSERT_EQ(videoFrame, 602);
389    ASSERT_EQ(aKeyCount, 433);
390    ASSERT_EQ(vKeyCount, 3);
391    close(fd);
392}
393
394/**
395 * @tc.number    : DEMUXER_FUNCTION_1000
396 * @tc.name      : create source with fd,mpeg mp4
397 * @tc.desc      : function test
398 */
399HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1000, TestSize.Level0)
400{
401    int tarckType = 0;
402    OH_AVCodecBufferAttr attr;
403    bool audioIsEnd = false;
404    bool videoIsEnd = false;
405    int audioFrame = 0;
406    int videoFrame = 0;
407    const char *file = "/data/test/media/mpeg2.mp4";
408    int fd = open(file, O_RDONLY);
409    int64_t size = GetFileSize(file);
410    cout << file << "----------------------" << fd << "---------" << size << endl;
411    source = OH_AVSource_CreateWithFD(fd, 0, size);
412    ASSERT_NE(source, nullptr);
413
414    demuxer = OH_AVDemuxer_CreateWithSource(source);
415    ASSERT_NE(demuxer, nullptr);
416
417    sourceFormat = OH_AVSource_GetSourceFormat(source);
418    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
419    ASSERT_EQ(2, g_trackCount);
420
421    for (int32_t index = 0; index < g_trackCount; index++) {
422        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
423    }
424
425    int aKeyCount = 0;
426    int vKeyCount = 0;
427    while (!audioIsEnd || !videoIsEnd) {
428        for (int32_t index = 0; index < g_trackCount; index++) {
429            trackFormat = OH_AVSource_GetTrackFormat(source, index);
430            ASSERT_NE(trackFormat, nullptr);
431            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
432            OH_AVFormat_Destroy(trackFormat);
433            trackFormat = nullptr;
434            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
435                continue;
436            }
437            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
438
439            if (tarckType == MEDIA_TYPE_AUD) {
440                SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
441            } else if (tarckType == MEDIA_TYPE_VID) {
442                SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
443            }
444        }
445    }
446
447    ASSERT_EQ(audioFrame, 433);
448    ASSERT_EQ(videoFrame, 303);
449    ASSERT_EQ(aKeyCount, 433);
450    ASSERT_EQ(vKeyCount, 26);
451    close(fd);
452}
453
454/**
455 * @tc.number    : DEMUXER_FUNCTION_1100
456 * @tc.name      : demux m4a
457 * @tc.desc      : function test
458 */
459HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1100, TestSize.Level0)
460{
461    OH_AVCodecBufferAttr attr;
462    bool audioIsEnd = false;
463    int audioFrame = 0;
464
465    const char *file = "/data/test/media/audio/M4A_48000_1.m4a";
466    int fd = open(file, O_RDONLY);
467    int64_t size = GetFileSize(file);
468    cout << file << "----------------------" << fd << "---------" << size << endl;
469    source = OH_AVSource_CreateWithFD(fd, 0, size);
470    ASSERT_NE(source, nullptr);
471
472    demuxer = OH_AVDemuxer_CreateWithSource(source);
473    ASSERT_NE(demuxer, nullptr);
474
475    sourceFormat = OH_AVSource_GetSourceFormat(source);
476    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
477    ASSERT_EQ(1, g_trackCount);
478
479    for (int32_t index = 0; index < g_trackCount; index++) {
480        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
481    }
482
483    int keyCount = 0;
484    while (!audioIsEnd) {
485        for (int32_t index = 0; index < g_trackCount; index++) {
486            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
487            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
488                audioIsEnd = true;
489                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
490                continue;
491            }
492
493            audioFrame++;
494            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
495                keyCount++;
496            }
497        }
498    }
499
500    ASSERT_EQ(audioFrame, 10293);
501    ASSERT_EQ(keyCount, 10293);
502    close(fd);
503}
504
505/**
506 * @tc.number    : DEMUXER_FUNCTION_1200
507 * @tc.name      : demux aac
508 * @tc.desc      : function test
509 */
510HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1200, TestSize.Level0)
511{
512    OH_AVCodecBufferAttr attr;
513    bool audioIsEnd = false;
514    int audioFrame = 0;
515
516    const char *file = "/data/test/media/audio/AAC_48000_1.aac";
517    int fd = open(file, O_RDONLY);
518    int64_t size = GetFileSize(file);
519    cout << file << "----------------------" << fd << "---------" << size << endl;
520    source = OH_AVSource_CreateWithFD(fd, 0, size);
521    ASSERT_NE(source, nullptr);
522
523    demuxer = OH_AVDemuxer_CreateWithSource(source);
524    ASSERT_NE(demuxer, nullptr);
525
526    sourceFormat = OH_AVSource_GetSourceFormat(source);
527    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
528    ASSERT_EQ(1, g_trackCount);
529
530    for (int32_t index = 0; index < g_trackCount; index++) {
531        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
532    }
533    int keyCount = 0;
534    while (!audioIsEnd) {
535        for (int32_t index = 0; index < g_trackCount; index++) {
536            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
537            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
538                audioIsEnd = true;
539                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
540                continue;
541            }
542
543            audioFrame++;
544            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
545                keyCount++;
546            }
547        }
548    }
549    ASSERT_EQ(audioFrame, 9457);
550    ASSERT_EQ(keyCount, 9457);
551    close(fd);
552}
553
554/**
555 * @tc.number    : DEMUXER_FUNCTION_1300
556 * @tc.name      : demux mp3
557 * @tc.desc      : function test
558 */
559HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1300, TestSize.Level0)
560{
561    OH_AVCodecBufferAttr attr;
562    bool audioIsEnd = false;
563    int audioFrame = 0;
564
565    const char *file = "/data/test/media/audio/MP3_48000_1.mp3";
566    int fd = open(file, O_RDONLY);
567    int64_t size = GetFileSize(file);
568    cout << file << "----------------------" << fd << "---------" << size << endl;
569    source = OH_AVSource_CreateWithFD(fd, 0, size);
570    ASSERT_NE(source, nullptr);
571
572    demuxer = OH_AVDemuxer_CreateWithSource(source);
573    ASSERT_NE(demuxer, nullptr);
574
575    sourceFormat = OH_AVSource_GetSourceFormat(source);
576    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
577    ASSERT_EQ(1, g_trackCount);
578
579    for (int32_t index = 0; index < g_trackCount; index++) {
580        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
581    }
582    int keyCount = 0;
583    while (!audioIsEnd) {
584        for (int32_t index = 0; index < g_trackCount; index++) {
585            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
586            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
587                audioIsEnd = true;
588                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
589                continue;
590            }
591
592            audioFrame++;
593            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
594                keyCount++;
595            }
596        }
597    }
598    ASSERT_EQ(audioFrame, 9150);
599    ASSERT_EQ(keyCount, 9150);
600    close(fd);
601}
602
603/**
604 * @tc.number    : DEMUXER_FUNCTION_1400
605 * @tc.name      : demux ogg
606 * @tc.desc      : function test
607 */
608HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1400, TestSize.Level0)
609{
610    OH_AVCodecBufferAttr attr;
611    bool audioIsEnd = false;
612    int audioFrame = 0;
613
614    const char *file = "/data/test/media/audio/OGG_48000_1.ogg";
615    int fd = open(file, O_RDONLY);
616    int64_t size = GetFileSize(file);
617    cout << file << "----------------------" << fd << "---------" << size << endl;
618    source = OH_AVSource_CreateWithFD(fd, 0, size);
619    ASSERT_NE(source, nullptr);
620
621    demuxer = OH_AVDemuxer_CreateWithSource(source);
622    ASSERT_NE(demuxer, nullptr);
623
624    sourceFormat = OH_AVSource_GetSourceFormat(source);
625    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
626    ASSERT_EQ(1, g_trackCount);
627
628    for (int32_t index = 0; index < g_trackCount; index++) {
629        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
630    }
631    int keyCount = 0;
632    while (!audioIsEnd) {
633        for (int32_t index = 0; index < g_trackCount; index++) {
634            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
635            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
636                audioIsEnd = true;
637                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
638                continue;
639            }
640
641            audioFrame++;
642            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
643                keyCount++;
644            }
645        }
646    }
647    ASSERT_EQ(audioFrame, 11439);
648    ASSERT_EQ(keyCount, 11439);
649    close(fd);
650}
651
652/**
653 * @tc.number    : DEMUXER_FUNCTION_1500
654 * @tc.name      : demux flac
655 * @tc.desc      : function test
656 */
657HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1500, TestSize.Level0)
658{
659    OH_AVCodecBufferAttr attr;
660    bool audioIsEnd = false;
661    int audioFrame = 0;
662
663    const char *file = "/data/test/media/audio/FLAC_48000_1.flac";
664    int fd = open(file, O_RDONLY);
665    int64_t size = GetFileSize(file);
666    cout << file << "----------------------" << fd << "---------" << size << endl;
667    source = OH_AVSource_CreateWithFD(fd, 0, size);
668    ASSERT_NE(source, nullptr);
669
670    demuxer = OH_AVDemuxer_CreateWithSource(source);
671    ASSERT_NE(demuxer, nullptr);
672
673    sourceFormat = OH_AVSource_GetSourceFormat(source);
674    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
675    ASSERT_EQ(1, g_trackCount);
676
677    for (int32_t index = 0; index < g_trackCount; index++) {
678        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
679    }
680
681    int keyCount = 0;
682    while (!audioIsEnd) {
683        for (int32_t index = 0; index < g_trackCount; index++) {
684            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
685            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
686                audioIsEnd = true;
687                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
688                continue;
689            }
690
691            audioFrame++;
692            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
693                keyCount++;
694            }
695        }
696    }
697    ASSERT_EQ(audioFrame, 2288);
698    ASSERT_EQ(keyCount, 2288);
699    close(fd);
700}
701
702/**
703 * @tc.number    : DEMUXER_FUNCTION_1600
704 * @tc.name      : demux wav
705 * @tc.desc      : function test
706 */
707HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1600, TestSize.Level0)
708{
709    OH_AVCodecBufferAttr attr;
710    bool audioIsEnd = false;
711    int audioFrame = 0;
712
713    const char *file = "/data/test/media/audio/wav_48000_1.wav";
714    int fd = open(file, O_RDONLY);
715    int64_t size = GetFileSize(file);
716    cout << file << "----------------------" << fd << "---------" << size << endl;
717    source = OH_AVSource_CreateWithFD(fd, 0, size);
718    ASSERT_NE(source, nullptr);
719
720    demuxer = OH_AVDemuxer_CreateWithSource(source);
721    ASSERT_NE(demuxer, nullptr);
722
723    sourceFormat = OH_AVSource_GetSourceFormat(source);
724    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
725    ASSERT_EQ(1, g_trackCount);
726
727    for (int32_t index = 0; index < g_trackCount; index++) {
728        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
729    }
730
731    int keyCount = 0;
732    while (!audioIsEnd) {
733        for (int32_t index = 0; index < g_trackCount; index++) {
734            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
735            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
736                audioIsEnd = true;
737                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
738                continue;
739            }
740
741            audioFrame++;
742            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
743                keyCount++;
744            }
745        }
746    }
747    ASSERT_EQ(audioFrame, 5146);
748    ASSERT_EQ(keyCount, 5146);
749    close(fd);
750}
751
752/**
753 * @tc.number    : DEMUXER_FUNCTION_1700
754 * @tc.name      : demux mpeg-ts
755 * @tc.desc      : function test
756 */
757HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1700, TestSize.Level0)
758{
759    int tarckType = 0;
760    OH_AVCodecBufferAttr attr;
761    bool audioIsEnd = false;
762    bool videoIsEnd = false;
763    int audioFrame = 0;
764    int videoFrame = 0;
765    const char *file = "/data/test/media/ts_video.ts";
766    int fd = open(file, O_RDONLY);
767    int64_t size = GetFileSize(file);
768    cout << file << "----------------------" << fd << "---------" << size << endl;
769    source = OH_AVSource_CreateWithFD(fd, 0, size);
770    ASSERT_NE(source, nullptr);
771
772    demuxer = OH_AVDemuxer_CreateWithSource(source);
773    ASSERT_NE(demuxer, nullptr);
774
775    sourceFormat = OH_AVSource_GetSourceFormat(source);
776    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
777    ASSERT_EQ(2, g_trackCount);
778
779    for (int32_t index = 0; index < g_trackCount; index++) {
780        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
781    }
782
783    int vKeyCount = 0;
784    int aKeyCount = 0;
785    while (!audioIsEnd || !videoIsEnd) {
786        for (int32_t index = 0; index < g_trackCount; index++) {
787
788            trackFormat = OH_AVSource_GetTrackFormat(source, index);
789            ASSERT_NE(trackFormat, nullptr);
790            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
791            OH_AVFormat_Destroy(trackFormat);
792            trackFormat = nullptr;
793            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
794                continue;
795            }
796            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
797
798            if (tarckType == MEDIA_TYPE_AUD) {
799                SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
800            } else if (tarckType == MEDIA_TYPE_VID) {
801                SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
802            }
803        }
804    }
805    ASSERT_EQ(audioFrame, 384);
806    ASSERT_EQ(aKeyCount, 384);
807    ASSERT_EQ(videoFrame, 602);
808    ASSERT_EQ(vKeyCount, 51);
809    close(fd);
810}
811
812/**
813 * @tc.number    : DEMUXER_FUNCTION_1800
814 * @tc.name      : demux unsupported source
815 * @tc.desc      : function test
816 */
817HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1800, TestSize.Level2)
818{
819    const char *file = "/data/test/media/mkv.mkv";
820    int fd = open(file, O_RDONLY);
821    int64_t size = GetFileSize(file);
822    cout << file << "----------------------" << fd << "---------" << size << endl;
823    source = OH_AVSource_CreateWithFD(fd, 0, size);
824    ASSERT_NE(source, nullptr);
825    close(fd);
826}
827
828/**
829 * @tc.number    : DEMUXER_FUNCTION_1900
830 * @tc.name      : demux mp4, zero track
831 * @tc.desc      : function test
832 */
833HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_1900, TestSize.Level1)
834{
835    const char *file = "/data/test/media/zero_track.mp4";
836    int fd = open(file, O_RDONLY);
837    int64_t size = GetFileSize(file);
838    cout << file << "----------------------" << fd << "---------" << size << endl;
839    source = OH_AVSource_CreateWithFD(fd, 0, size);
840    ASSERT_NE(source, nullptr);
841    demuxer = OH_AVDemuxer_CreateWithSource(source);
842    ASSERT_NE(demuxer, nullptr);
843
844    sourceFormat = OH_AVSource_GetSourceFormat(source);
845    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
846    ASSERT_EQ(g_trackCount, 0);
847
848    ASSERT_EQ(AV_ERR_INVALID_VAL, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
849    close(fd);
850}
851
852/**
853 * @tc.number    : DEMUXER_FUNCTION_2000
854 * @tc.name      : OH_AVSource_CreateWithFD test
855 * @tc.desc      : function test
856 */
857HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_2000, TestSize.Level0)
858{
859    OH_AVCodecBufferAttr attr;
860    bool audioIsEnd = false;
861    int audioFrame = 0;
862
863    const char *file1 = "/data/test/media/audio/MP3_48000_1.mp3";
864    int64_t size1 = GetFileSize(file1);
865
866    const char *file = "/data/test/media/MP3_avcc_10sec.bin";
867    int fd = open(file, O_RDONLY);
868    int64_t size = GetFileSize(file);
869    cout << file << "----------------------" << fd << "---------" << size << endl;
870    source = OH_AVSource_CreateWithFD(fd, 0, size1);
871    ASSERT_NE(source, nullptr);
872
873    demuxer = OH_AVDemuxer_CreateWithSource(source);
874    ASSERT_NE(demuxer, nullptr);
875
876    sourceFormat = OH_AVSource_GetSourceFormat(source);
877    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
878    ASSERT_EQ(1, g_trackCount);
879
880    for (int32_t index = 0; index < g_trackCount; index++) {
881        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
882    }
883
884    int keyCount = 0;
885    while (!audioIsEnd) {
886        for (int32_t index = 0; index < g_trackCount; index++) {
887            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
888            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
889                audioIsEnd = true;
890                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
891                continue;
892            }
893
894            audioFrame++;
895            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
896                keyCount++;
897            }
898        }
899    }
900    ASSERT_EQ(audioFrame, 9150);
901    ASSERT_EQ(keyCount, 9150);
902    close(fd);
903}
904
905/**
906 * @tc.number    : DEMUXER_FUNCTION_2100
907 * @tc.name      : OH_AVSource_CreateWithFD test
908 * @tc.desc      : function test
909 */
910HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_2100, TestSize.Level0)
911{
912    OH_AVCodecBufferAttr attr;
913    bool audioIsEnd = false;
914    bool videoIsEnd = false;
915    int audioFrame = 0;
916    int videoFrame = 0;
917    const char *file1 = "/data/test/media/audio/MP3_48000_1.mp3";
918    int64_t size1 = GetFileSize(file1);
919    const char *file2 = "/data/test/media/avcc_10sec.mp4";
920    int64_t size2 = GetFileSize(file2);
921    const char *file = "/data/test/media/MP3_avcc_10sec.bin";
922    int fd = open(file, O_RDONLY);
923    int64_t size = GetFileSize(file);
924    cout << file << "----------------------" << fd << "---------" << size << endl;
925    source = OH_AVSource_CreateWithFD(fd, size1, size2);
926    ASSERT_NE(source, nullptr);
927    demuxer = OH_AVDemuxer_CreateWithSource(source);
928    ASSERT_NE(demuxer, nullptr);
929    sourceFormat = OH_AVSource_GetSourceFormat(source);
930    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
931    ASSERT_EQ(2, g_trackCount);
932    for (int32_t index = 0; index < g_trackCount; index++) {
933        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
934    }
935    int tarckType = 0;
936    int aKeyCount = 0;
937    int vKeyCount = 0;
938    while (!audioIsEnd || !videoIsEnd) {
939        for (int32_t index = 0; index < g_trackCount; index++) {
940            trackFormat = OH_AVSource_GetTrackFormat(source, index);
941            ASSERT_NE(trackFormat, nullptr);
942            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
943            OH_AVFormat_Destroy(trackFormat);
944            trackFormat = nullptr;
945            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
946                continue;
947            }
948            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
949            if (tarckType == MEDIA_TYPE_AUD) {
950                SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
951            } else if (tarckType == MEDIA_TYPE_VID) {
952                SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
953            }
954        }
955    }
956    ASSERT_EQ(audioFrame, 431);
957    ASSERT_EQ(videoFrame, 600);
958    ASSERT_EQ(aKeyCount, 431);
959    ASSERT_EQ(vKeyCount, 10);
960    close(fd);
961}
962
963/**
964 * @tc.number    : DEMUXER_FUNCTION_2200
965 * @tc.name      : OH_AVSource_CreateWithFD test
966 * @tc.desc      : function test
967 */
968HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_2200, TestSize.Level0)
969{
970    OH_AVCodecBufferAttr attr;
971    bool audioIsEnd = false;
972    int audioFrame = 0;
973
974    const char *file1 = "/data/test/media/audio/MP3_48000_1.mp3";
975    int64_t size1 = GetFileSize(file1);
976
977    const char *file = "/data/test/media/MP3_OGG_48000_1.bin";
978    int fd = open(file, O_RDONLY);
979    int64_t size = GetFileSize(file);
980    cout << file << "----------------------" << fd << "---------" << size << endl;
981    source = OH_AVSource_CreateWithFD(fd, 0, size1);
982    ASSERT_NE(source, nullptr);
983
984    demuxer = OH_AVDemuxer_CreateWithSource(source);
985    ASSERT_NE(demuxer, nullptr);
986
987    sourceFormat = OH_AVSource_GetSourceFormat(source);
988    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
989    ASSERT_EQ(1, g_trackCount);
990
991    for (int32_t index = 0; index < g_trackCount; index++) {
992        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
993    }
994
995    int keyCount = 0;
996    while (!audioIsEnd) {
997        for (int32_t index = 0; index < g_trackCount; index++) {
998            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
999            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1000                audioIsEnd = true;
1001                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
1002                continue;
1003            }
1004
1005            audioFrame++;
1006            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
1007                keyCount++;
1008            }
1009        }
1010    }
1011    ASSERT_EQ(audioFrame, 9150);
1012    ASSERT_EQ(keyCount, 9150);
1013    close(fd);
1014}
1015
1016/**
1017 * @tc.number    : DEMUXER_FUNCTION_2300
1018 * @tc.name      : OH_AVSource_CreateWithFD test
1019 * @tc.desc      : function test
1020 */
1021HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_2300, TestSize.Level0)
1022{
1023    OH_AVCodecBufferAttr attr;
1024    bool audioIsEnd = false;
1025    int audioFrame = 0;
1026
1027    const char *file1 = "/data/test/media/audio/MP3_48000_1.mp3";
1028    int64_t size1 = GetFileSize(file1);
1029
1030    const char *file2 = "/data/test/media/audio/OGG_48000_1.ogg";
1031    int64_t size2 = GetFileSize(file2);
1032
1033    const char *file = "/data/test/media/MP3_OGG_48000_1.bin";
1034    int fd = open(file, O_RDONLY);
1035    int64_t size = GetFileSize(file);
1036    cout << file << "----------------------" << fd << "---------" << size << endl;
1037    source = OH_AVSource_CreateWithFD(fd, size1, size2);
1038    ASSERT_NE(source, nullptr);
1039
1040    demuxer = OH_AVDemuxer_CreateWithSource(source);
1041    ASSERT_NE(demuxer, nullptr);
1042
1043    sourceFormat = OH_AVSource_GetSourceFormat(source);
1044    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1045    ASSERT_EQ(1, g_trackCount);
1046
1047    for (int32_t index = 0; index < g_trackCount; index++) {
1048        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1049    }
1050
1051    int keyCount = 0;
1052    while (!audioIsEnd) {
1053        for (int32_t index = 0; index < g_trackCount; index++) {
1054            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1055            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1056                audioIsEnd = true;
1057                cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
1058                continue;
1059            }
1060
1061            audioFrame++;
1062            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
1063                keyCount++;
1064            }
1065        }
1066    }
1067    ASSERT_EQ(audioFrame, 11439);
1068    ASSERT_EQ(keyCount, 11439);
1069    close(fd);
1070}
1071
1072/**
1073 * @tc.number    : DEMUXER_FUNCTION_2400
1074 * @tc.name      : OH_AVSource_CreateWithFD test
1075 * @tc.desc      : function test
1076 */
1077HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_2400, TestSize.Level0)
1078{
1079    OH_AVCodecBufferAttr attr;
1080    bool audioIsEnd = false;
1081    bool videoIsEnd = false;
1082    int audioFrame = 0;
1083    int videoFrame = 0;
1084    const char *file1 = "/data/test/media/ts_video.ts";
1085    int64_t size1 = GetFileSize(file1);
1086
1087    const char *file = "/data/test/media/test_video_avcc_10sec.bin";
1088    int fd = open(file, O_RDONLY);
1089    int64_t size = GetFileSize(file);
1090    cout << file << "----------------------" << fd << "---------" << size << endl;
1091    source = OH_AVSource_CreateWithFD(fd, 0, size1);
1092    ASSERT_NE(source, nullptr);
1093
1094    demuxer = OH_AVDemuxer_CreateWithSource(source);
1095    ASSERT_NE(demuxer, nullptr);
1096
1097    sourceFormat = OH_AVSource_GetSourceFormat(source);
1098    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1099    ASSERT_EQ(2, g_trackCount);
1100
1101    for (int32_t index = 0; index < g_trackCount; index++) {
1102        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1103    }
1104    int tarckType = 0;
1105    int aKeyCount = 0;
1106    int vKeyCount = 0;
1107    while (!audioIsEnd || !videoIsEnd) {
1108        for (int32_t index = 0; index < g_trackCount; index++) {
1109            trackFormat = OH_AVSource_GetTrackFormat(source, index);
1110            ASSERT_NE(trackFormat, nullptr);
1111            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
1112            OH_AVFormat_Destroy(trackFormat);
1113            trackFormat = nullptr;
1114            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
1115                continue;
1116            }
1117            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1118
1119            if (tarckType == MEDIA_TYPE_AUD) {
1120                SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
1121            } else if (tarckType == MEDIA_TYPE_VID) {
1122                SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
1123            }
1124        }
1125    }
1126    ASSERT_EQ(audioFrame, 384);
1127    ASSERT_EQ(videoFrame, 602);
1128    ASSERT_EQ(aKeyCount, 384);
1129    ASSERT_EQ(vKeyCount, 51);
1130    close(fd);
1131}
1132
1133/**
1134 * @tc.number    : DEMUXER_FUNCTION_3100
1135 * @tc.name      : seek to the start time, previous mode
1136 * @tc.desc      : function test
1137 */
1138HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3100, TestSize.Level1)
1139{
1140    uint32_t trackIndex = 0;
1141    OH_AVCodecBufferAttr attr;
1142    const char *file = "/data/test/media/01_video_audio.mp4";
1143    int count = 0;
1144    srand(time(nullptr));
1145    int fd = open(file, O_RDONLY);
1146    int64_t size = GetFileSize(file);
1147    cout << file << "----------------------" << fd << "---------" << size << endl;
1148    source = OH_AVSource_CreateWithFD(fd, 0, size);
1149    ASSERT_NE(source, nullptr);
1150
1151    demuxer = OH_AVDemuxer_CreateWithSource(source);
1152    ASSERT_NE(demuxer, nullptr);
1153
1154    int pos = rand() % 250;
1155    int64_t startPts = 0;
1156    bool isFirstFrame = true;
1157    bool isEnd = false;
1158    sourceFormat = OH_AVSource_GetSourceFormat(source);
1159    ASSERT_NE(sourceFormat, nullptr);
1160    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1161    cout << g_trackCount << "####################" << endl;
1162
1163    for (int32_t index = 0; index < g_trackCount; index++) {
1164        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1165    }
1166
1167    while (!isEnd) {
1168        for (int32_t index = 0; index < g_trackCount; index++) {
1169            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1170
1171            if (isFirstFrame) {
1172                startPts = attr.pts;
1173                isFirstFrame = false;
1174            }
1175            if (count == pos) {
1176                isEnd = true;
1177                cout << pos << " =====curr_pts = attr.pts" << endl;
1178                break;
1179            }
1180            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1181                isEnd = true;
1182                cout << "is end!!!!!!!!" << endl;
1183            }
1184            count++;
1185        }
1186        cout << "count: " << count << endl;
1187    }
1188    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, startPts / 1000, SEEK_MODE_PREVIOUS_SYNC));
1189    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr));
1190    ASSERT_EQ(attr.pts, startPts);
1191    close(fd);
1192}
1193
1194/**
1195 * @tc.number    : DEMUXER_FUNCTION_3200
1196 * @tc.name      : seek to the start time, next mode
1197 * @tc.desc      : function test
1198 */
1199HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3200, TestSize.Level1)
1200{
1201    bool isEnd = false;
1202    uint32_t trackIndex = 0;
1203    OH_AVCodecBufferAttr attr;
1204    const char *file = "/data/test/media/01_video_audio.mp4";
1205    int count = 0;
1206    srand(time(nullptr));
1207    int fd = open(file, O_RDONLY);
1208    int64_t size = GetFileSize(file);
1209    cout << file << "----------------------" << fd << "---------" << size << endl;
1210    source = OH_AVSource_CreateWithFD(fd, 0, size);
1211    ASSERT_NE(source, nullptr);
1212
1213    demuxer = OH_AVDemuxer_CreateWithSource(source);
1214    ASSERT_NE(demuxer, nullptr);
1215
1216    int pos = rand() % 250;
1217    int64_t startPts = 0;
1218    bool isFirstFrame = true;
1219    sourceFormat = OH_AVSource_GetSourceFormat(source);
1220    ASSERT_NE(sourceFormat, nullptr);
1221    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1222
1223    for (int32_t index = 0; index < g_trackCount; index++) {
1224        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1225    }
1226
1227    while (!isEnd) {
1228        for (int32_t index = 0; index < g_trackCount; index++) {
1229            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1230
1231            if (isFirstFrame) {
1232                startPts = attr.pts;
1233                isFirstFrame = false;
1234            }
1235            if (count == pos) {
1236                isEnd = true;
1237                cout << "curr_pts = attr.pts" << endl;
1238                break;
1239            }
1240            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1241                isEnd = true;
1242                cout << "is end!!!!!!!!" << endl;
1243            }
1244            count++;
1245        }
1246        cout << "count: " << count << endl;
1247    }
1248
1249    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, startPts / 1000, SEEK_MODE_NEXT_SYNC));
1250    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr));
1251    ASSERT_EQ(attr.pts, startPts);
1252    close(fd);
1253}
1254
1255/**
1256 * @tc.number    : DEMUXER_FUNCTION_3300
1257 * @tc.name      : seek to the start time, closest mode
1258 * @tc.desc      : function test
1259 */
1260HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3300, TestSize.Level1)
1261{
1262    bool isEnd = false;
1263    uint32_t trackIndex = 0;
1264    OH_AVCodecBufferAttr attr;
1265    const char *file = "/data/test/media/01_video_audio.mp4";
1266    int count = 0;
1267    srand(time(nullptr));
1268    int fd = open(file, O_RDONLY);
1269    int64_t size = GetFileSize(file);
1270    cout << file << "----------------------" << fd << "---------" << size << endl;
1271    source = OH_AVSource_CreateWithFD(fd, 0, size);
1272    ASSERT_NE(source, nullptr);
1273
1274    demuxer = OH_AVDemuxer_CreateWithSource(source);
1275    ASSERT_NE(demuxer, nullptr);
1276
1277    int pos = rand() % 250;
1278    int64_t startPts = 0;
1279    bool isFirstFrame = true;
1280    sourceFormat = OH_AVSource_GetSourceFormat(source);
1281    ASSERT_NE(sourceFormat, nullptr);
1282    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1283
1284    for (int32_t index = 0; index < g_trackCount; index++) {
1285        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1286    }
1287
1288    while (!isEnd) {
1289        for (int32_t index = 0; index < g_trackCount; index++) {
1290            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1291
1292            if (isFirstFrame) {
1293                startPts = attr.pts;
1294                isFirstFrame = false;
1295            }
1296            if (count == pos) {
1297                isEnd = true;
1298                cout << "curr_pts = attr.pts" << endl;
1299                break;
1300            }
1301            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1302                isEnd = true;
1303                cout << "is end!!!!!!!!" << endl;
1304            }
1305            count++;
1306        }
1307        cout << "count: " << count << endl;
1308    }
1309    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, startPts / 1000, SEEK_MODE_CLOSEST_SYNC));
1310    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr));
1311    ASSERT_EQ(attr.pts, startPts);
1312    close(fd);
1313}
1314
1315/**
1316 * @tc.number    : DEMUXER_FUNCTION_3400
1317 * @tc.name      : seek to the end time, previous mode
1318 * @tc.desc      : function test
1319 */
1320HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3400, TestSize.Level1)
1321{
1322    bool isEnd = false;
1323    uint32_t trackIndex = 1;
1324    OH_AVCodecBufferAttr attr;
1325    const char *file = "/data/test/media/01_video_audio.mp4";
1326    int count = 0;
1327    srand(time(nullptr));
1328    int fd = open(file, O_RDONLY);
1329    int64_t size = GetFileSize(file);
1330    cout << file << "----------------------" << fd << "---------" << size << endl;
1331    source = OH_AVSource_CreateWithFD(fd, 0, size);
1332    ASSERT_NE(source, nullptr);
1333
1334    demuxer = OH_AVDemuxer_CreateWithSource(source);
1335    ASSERT_NE(demuxer, nullptr);
1336
1337    sourceFormat = OH_AVSource_GetSourceFormat(source);
1338    ASSERT_NE(sourceFormat, nullptr);
1339    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1340    cout << g_trackCount << "####################" << endl;
1341
1342    for (int32_t index = 0; index < g_trackCount; index++) {
1343        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1344    }
1345
1346    int64_t endPts = 0;
1347    while (!isEnd) {
1348        for (int32_t index = 0; index < g_trackCount; index++) {
1349            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1350
1351            if (static_cast<uint32_t>(index) != trackIndex) {
1352                continue;
1353            }
1354            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1355                isEnd = true;
1356                cout << "is end!!!!!!!!" << endl;
1357            } else {
1358                endPts = attr.pts;
1359            }
1360            count++;
1361        }
1362        cout << "count: " << count << endl;
1363    }
1364    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, endPts / 1000, SEEK_MODE_PREVIOUS_SYNC));
1365    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr));
1366    ASSERT_EQ(attr.pts, endPts);
1367    close(fd);
1368}
1369
1370/**
1371 * @tc.number    : DEMUXER_FUNCTION_3500
1372 * @tc.name      : seek to the end time, next mode
1373 * @tc.desc      : function test
1374 */
1375HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3500, TestSize.Level1)
1376{
1377    bool isEnd = false;
1378    uint32_t trackIndex = 1;
1379    OH_AVCodecBufferAttr attr;
1380    const char *file = "/data/test/media/01_video_audio.mp4";
1381    int count = 0;
1382    srand(time(nullptr));
1383    int fd = open(file, O_RDONLY);
1384    int64_t size = GetFileSize(file);
1385    cout << file << "----------------------" << fd << "---------" << size << endl;
1386    source = OH_AVSource_CreateWithFD(fd, 0, size);
1387    ASSERT_NE(source, nullptr);
1388
1389    demuxer = OH_AVDemuxer_CreateWithSource(source);
1390    ASSERT_NE(demuxer, nullptr);
1391    sourceFormat = OH_AVSource_GetSourceFormat(source);
1392    ASSERT_NE(sourceFormat, nullptr);
1393    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1394    cout << g_trackCount << "####################" << endl;
1395
1396    for (int32_t index = 0; index < g_trackCount; index++) {
1397        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1398    }
1399    int64_t endPts = 0;
1400    while (!isEnd) {
1401        for (int32_t index = 0; index < g_trackCount; index++) {
1402            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1403            if (static_cast<uint32_t>(index) != trackIndex) {
1404                continue;
1405            }
1406            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1407                isEnd = true;
1408                cout << "is end!!!!!!!!" << endl;
1409            } else {
1410                endPts = attr.pts;
1411            }
1412            count++;
1413        }
1414        cout << "count: " << count << endl;
1415    }
1416    // end I
1417    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, endPts / 1000, SEEK_MODE_NEXT_SYNC));
1418    endPts += 1000;
1419    ASSERT_EQ(AV_ERR_UNKNOWN, OH_AVDemuxer_SeekToTime(demuxer, endPts / 1000, SEEK_MODE_NEXT_SYNC));
1420    endPts += 1000000;
1421    ASSERT_EQ(AV_ERR_OPERATE_NOT_PERMIT, OH_AVDemuxer_SeekToTime(demuxer, endPts / 1000, SEEK_MODE_NEXT_SYNC));
1422    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr));
1423    close(fd);
1424}
1425
1426/**
1427 * @tc.number    : DEMUXER_FUNCTION_3600
1428 * @tc.name      : seek to the end time, closest mode
1429 * @tc.desc      : function test
1430 */
1431HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3600, TestSize.Level1)
1432{
1433    bool isEnd = false;
1434    uint32_t trackIndex = 1;
1435    OH_AVCodecBufferAttr attr;
1436    const char *file = "/data/test/media/01_video_audio.mp4";
1437    int count = 0;
1438    srand(time(nullptr));
1439    int fd = open(file, O_RDONLY);
1440    int64_t size = GetFileSize(file);
1441    cout << file << "----------------------" << fd << "---------" << size << endl;
1442    source = OH_AVSource_CreateWithFD(fd, 0, size);
1443    ASSERT_NE(source, nullptr);
1444
1445    demuxer = OH_AVDemuxer_CreateWithSource(source);
1446    ASSERT_NE(demuxer, nullptr);
1447    sourceFormat = OH_AVSource_GetSourceFormat(source);
1448    ASSERT_NE(sourceFormat, nullptr);
1449    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1450    cout << g_trackCount << "####################" << endl;
1451
1452    for (int32_t index = 0; index < g_trackCount; index++) {
1453        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1454    }
1455
1456    int64_t endPts = 0;
1457    while (!isEnd) {
1458        for (int32_t index = 0; index < g_trackCount; index++) {
1459            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1460            if (static_cast<uint32_t>(index) != trackIndex) {
1461                continue;
1462            }
1463            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1464                isEnd = true;
1465                cout << "is end!!!!!!!!" << endl;
1466            } else {
1467                endPts = attr.pts;
1468            }
1469            count++;
1470        }
1471        cout << "count: " << count << endl;
1472    }
1473    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, endPts / 1000, SEEK_MODE_CLOSEST_SYNC));
1474    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr));
1475    ASSERT_EQ(attr.pts, endPts);
1476    close(fd);
1477}
1478
1479/**
1480 * @tc.number    : DEMUXER_FUNCTION_3700
1481 * @tc.name      : seek to a random time, previous mode
1482 * @tc.desc      : function test
1483 */
1484HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3700, TestSize.Level0)
1485{
1486    bool isFirstFrame = true;
1487    uint32_t trackIndex = 1;
1488    int count = 0;
1489    srand(time(nullptr));
1490    int pos = rand() % 250;
1491    int posTo = rand() % 250;
1492    int64_t toMs = posTo * 40000;
1493    int tarckType = 0;
1494    OH_AVCodecBufferAttr attr;
1495    bool audioIsEnd = false;
1496    bool videoIsEnd = false;
1497    const char *file = "/data/test/media/01_video_audio.mp4";
1498    int fd = open(file, O_RDONLY);
1499    cout << file << "pos: " << pos << "toMs: " << toMs << " fd:" << fd << " size:" << GetFileSize(file) << endl;
1500    source = OH_AVSource_CreateWithFD(fd, 0, GetFileSize(file));
1501    ASSERT_NE(source, nullptr);
1502    demuxer = OH_AVDemuxer_CreateWithSource(source);
1503    ASSERT_NE(demuxer, nullptr);
1504    sourceFormat = OH_AVSource_GetSourceFormat(source);
1505    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1506    ASSERT_EQ(2, g_trackCount);
1507    for (int32_t index = 0; index < g_trackCount; index++) {
1508        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1509    }
1510    while (!audioIsEnd || !videoIsEnd) {
1511        for (int32_t index = 0; index < g_trackCount; index++) {
1512            trackFormat = OH_AVSource_GetTrackFormat(source, index);
1513            ASSERT_NE(trackFormat, nullptr);
1514            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
1515            OH_AVFormat_Destroy(trackFormat);
1516            trackFormat = nullptr;
1517            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
1518                continue;
1519            }
1520            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1521            SetFirstFrameFlag(isFirstFrame);
1522            if (count == pos) {
1523                SetEndFlag(audioIsEnd, videoIsEnd);
1524                break;
1525            }
1526            SetVarValue(attr, tarckType, audioIsEnd, videoIsEnd);
1527        }
1528        count++;
1529    }
1530    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, toMs / 1000, SEEK_MODE_PREVIOUS_SYNC));
1531    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr));
1532    bool ans = abs(toMs - attr.pts) < 40000 ? true : false;
1533    ASSERT_EQ(ans, true);
1534    close(fd);
1535}
1536
1537/**
1538 * @tc.number    : DEMUXER_FUNCTION_3800
1539 * @tc.name      : seek to a random time, next mode
1540 * @tc.desc      : function test
1541 */
1542HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3800, TestSize.Level0)
1543{
1544    bool isEnd = false;
1545    bool isFirstFrame = true;
1546    uint32_t trackIndex = 1;
1547    OH_AVCodecBufferAttr attr;
1548    const char *file = "/data/test/media/01_video_audio.mp4";
1549    int count = 0;
1550    srand(time(nullptr));
1551    int fd = open(file, O_RDONLY);
1552    int64_t size = GetFileSize(file);
1553    cout << file << "----------------------" << fd << "---------" << size << endl;
1554    source = OH_AVSource_CreateWithFD(fd, 0, size);
1555    ASSERT_NE(source, nullptr);
1556
1557    demuxer = OH_AVDemuxer_CreateWithSource(source);
1558    ASSERT_NE(demuxer, nullptr);
1559    sourceFormat = OH_AVSource_GetSourceFormat(source);
1560    ASSERT_NE(sourceFormat, nullptr);
1561    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1562
1563    for (int32_t index = 0; index < g_trackCount; index++) {
1564        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1565    }
1566    int pos = rand() % 250;
1567    int posTo = rand() % 250;
1568    int64_t toMs = posTo * 40000;
1569    while (!isEnd) {
1570        for (int32_t index = 0; index < g_trackCount; index++) {
1571            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1572            if (isFirstFrame) {
1573                isFirstFrame = false;
1574            }
1575            if (count == pos) {
1576                isEnd = true;
1577                break;
1578            }
1579            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1580                isEnd = true;
1581            }
1582            count++;
1583        }
1584    }
1585    int64_t nextIdrPts = toMs;
1586    ret = OH_AVDemuxer_SeekToTime(demuxer, toMs / 1000, SEEK_MODE_NEXT_SYNC);
1587    ASSERT_EQ(ret, AV_ERR_OK);
1588    ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
1589    ASSERT_EQ(ret, AV_ERR_OK);
1590    bool ans = abs(nextIdrPts - attr.pts) < 40000 ? true : false;
1591    ASSERT_EQ(ans, true);
1592    close(fd);
1593}
1594
1595/**
1596 * @tc.number    : DEMUXER_FUNCTION_3900
1597 * @tc.name      : seek to a random time, closest mode
1598 * @tc.desc      : function test
1599 */
1600HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_3900, TestSize.Level0)
1601{
1602    bool isEnd = false;
1603    bool isFirstFrame = true;
1604    uint32_t trackIndex = 1;
1605    OH_AVCodecBufferAttr attr;
1606    const char *file = "/data/test/media/01_video_audio.mp4";
1607    int count = 0;
1608    srand(time(nullptr));
1609    int fd = open(file, O_RDONLY);
1610    int64_t size = GetFileSize(file);
1611    cout << file << "----------------------" << fd << "---------" << size << endl;
1612    source = OH_AVSource_CreateWithFD(fd, 0, size);
1613    ASSERT_NE(source, nullptr);
1614
1615    demuxer = OH_AVDemuxer_CreateWithSource(source);
1616    ASSERT_NE(demuxer, nullptr);
1617    sourceFormat = OH_AVSource_GetSourceFormat(source);
1618    ASSERT_NE(sourceFormat, nullptr);
1619    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1620
1621    for (int32_t index = 0; index < g_trackCount; index++) {
1622        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1623    }
1624    int pos = rand() % 250;
1625    int posTo = rand() % 250;
1626    int64_t toMs = posTo * 40000;
1627    while (!isEnd) {
1628        for (int32_t index = 0; index < g_trackCount; index++) {
1629            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1630            if (isFirstFrame) {
1631                isFirstFrame = false;
1632            }
1633            if (count == pos) {
1634                isEnd = true;
1635                cout << "curr_pts = attr.pts" << endl;
1636                break;
1637            }
1638            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1639                isEnd = true;
1640            }
1641            count++;
1642        }
1643    }
1644    int64_t closestIdrPts = toMs;
1645    ret = OH_AVDemuxer_SeekToTime(demuxer, toMs / 1000, SEEK_MODE_CLOSEST_SYNC);
1646    ASSERT_EQ(ret, AV_ERR_OK);
1647    ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
1648    ASSERT_EQ(ret, AV_ERR_OK);
1649    bool ans = abs(closestIdrPts - attr.pts) < 40000 ? true : false;
1650    ASSERT_EQ(ans, true);
1651    close(fd);
1652}
1653
1654/**
1655 * @tc.number    : DEMUXER_FUNCTION_4000
1656 * @tc.name      : seek to a invalid time, closest mode
1657 * @tc.desc      : function test
1658 */
1659HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_4000, TestSize.Level2)
1660{
1661    const char *file = "/data/test/media/01_video_audio.mp4";
1662    srand(time(nullptr));
1663    int fd = open(file, O_RDONLY);
1664    int64_t size = GetFileSize(file);
1665    cout << file << "----------------------" << fd << "---------" << size << endl;
1666    source = OH_AVSource_CreateWithFD(fd, 0, size);
1667    ASSERT_NE(source, nullptr);
1668
1669    demuxer = OH_AVDemuxer_CreateWithSource(source);
1670    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
1671
1672    ASSERT_NE(demuxer, nullptr);
1673    int64_t invalidPts = 12000 * 16666;
1674    ret = OH_AVDemuxer_SeekToTime(demuxer, invalidPts, SEEK_MODE_CLOSEST_SYNC);
1675    ASSERT_NE(ret, AV_ERR_OK);
1676    close(fd);
1677}
1678
1679/**
1680 * @tc.number    : DEMUXER_FUNCTION_4100
1681 * @tc.name      : remove track before add track
1682 * @tc.desc      : function test
1683 */
1684HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_4100, TestSize.Level2)
1685{
1686    const char *file = "/data/test/media/01_video_audio.mp4";
1687    srand(time(nullptr));
1688    int fd = open(file, O_RDONLY);
1689    int64_t size = GetFileSize(file);
1690    cout << file << "----------------------" << fd << "---------" << size << endl;
1691    source = OH_AVSource_CreateWithFD(fd, 0, size);
1692    ASSERT_NE(source, nullptr);
1693
1694    demuxer = OH_AVDemuxer_CreateWithSource(source);
1695    ASSERT_NE(demuxer, nullptr);
1696    ret = OH_AVDemuxer_UnselectTrackByID(demuxer, 0);
1697    ASSERT_EQ(ret, AV_ERR_OK);
1698    ret = OH_AVDemuxer_SelectTrackByID(demuxer, 0);
1699    ASSERT_EQ(ret, AV_ERR_OK);
1700    close(fd);
1701}
1702
1703/**
1704 * @tc.number    : DEMUXER_FUNCTION_4200
1705 * @tc.name      : remove all tracks after demux finish
1706 * @tc.desc      : function test
1707 */
1708HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_4200, TestSize.Level1)
1709{
1710    OH_AVCodecBufferAttr attr;
1711    const char *file = "/data/test/media/01_video_audio.mp4";
1712    bool isEnd = false;
1713
1714    int fd = open(file, O_RDONLY);
1715    int64_t size = GetFileSize(file);
1716    cout << file << "----------------------" << fd << "---------" << size << endl;
1717    source = OH_AVSource_CreateWithFD(fd, 0, size);
1718    ASSERT_NE(source, nullptr);
1719
1720    demuxer = OH_AVDemuxer_CreateWithSource(source);
1721    ASSERT_NE(demuxer, nullptr);
1722
1723    sourceFormat = OH_AVSource_GetSourceFormat(source);
1724    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1725    ASSERT_EQ(2, g_trackCount);
1726    for (int32_t index = 0; index < g_trackCount; index++) {
1727        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1728    }
1729    while (!isEnd) {
1730        for (int32_t index = 0; index < g_trackCount; index++) {
1731            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1732            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1733                isEnd = true;
1734                cout << "is end !!!!!!!!!!!!!!!" << endl;
1735            }
1736        }
1737    }
1738
1739    for (int32_t index = 0; index < g_trackCount; index++) {
1740        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_UnselectTrackByID(demuxer, index));
1741    }
1742
1743    int32_t memorySize = OH_AVMemory_GetSize(memory);
1744    ASSERT_NE(0, memorySize);
1745    close(fd);
1746}
1747
1748/**
1749 * @tc.number    : DEMUXER_FUNCTION_4300
1750 * @tc.name      : remove all tracks before demux finish
1751 * @tc.desc      : function test
1752 */
1753HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_4300, TestSize.Level1)
1754{
1755    OH_AVCodecBufferAttr attr;
1756    const char *file = "/data/test/media/01_video_audio.mp4";
1757    bool isEnd = false;
1758    int count = 0;
1759    int fd = open(file, O_RDONLY);
1760    int64_t size = GetFileSize(file);
1761    cout << file << "----------------------" << fd << "---------" << size << endl;
1762    source = OH_AVSource_CreateWithFD(fd, 0, size);
1763    ASSERT_NE(source, nullptr);
1764
1765    demuxer = OH_AVDemuxer_CreateWithSource(source);
1766    ASSERT_NE(demuxer, nullptr);
1767
1768    sourceFormat = OH_AVSource_GetSourceFormat(source);
1769    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1770    ASSERT_EQ(2, g_trackCount);
1771    for (int32_t index = 0; index < g_trackCount; index++) {
1772        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1773    }
1774    srand(time(nullptr));
1775    int pos = rand() % 250;
1776    cout << " pos= " << pos << endl;
1777    while (!isEnd) {
1778        for (int32_t index = 0; index < g_trackCount; index++) {
1779            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1780            if (count == pos) {
1781                cout << count << " count == pos!!!!!!!!!" << endl;
1782                ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_UnselectTrackByID(demuxer, 0));
1783                ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_UnselectTrackByID(demuxer, 1));
1784                ASSERT_EQ(AV_ERR_OPERATE_NOT_PERMIT, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
1785                isEnd = true;
1786                break;
1787            }
1788
1789            if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1790                isEnd = true;
1791                cout << "is end !!!!!!!!!!!!!!!" << endl;
1792            }
1793            if (index == MEDIA_TYPE_AUD) {
1794                count++;
1795            }
1796        }
1797    }
1798    close(fd);
1799}
1800
1801/**
1802 * @tc.number    : DEMUXER_FUNCTION_4400
1803 * @tc.name      : remove audio track before demux finish
1804 * @tc.desc      : function test
1805 */
1806HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_4400, TestSize.Level1)
1807{
1808    OH_AVCodecBufferAttr attr;
1809    const char *file = "/data/test/media/01_video_audio.mp4";
1810
1811    int audioCount = 0;
1812    int fd = open(file, O_RDONLY);
1813    int64_t size = GetFileSize(file);
1814    cout << file << "----------------------" << fd << "---------" << size << endl;
1815    source = OH_AVSource_CreateWithFD(fd, 0, size);
1816    ASSERT_NE(source, nullptr);
1817
1818    demuxer = OH_AVDemuxer_CreateWithSource(source);
1819    ASSERT_NE(demuxer, nullptr);
1820
1821    sourceFormat = OH_AVSource_GetSourceFormat(source);
1822    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1823    ASSERT_EQ(2, g_trackCount);
1824    for (int32_t index = 0; index < g_trackCount; index++) {
1825        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
1826    }
1827    srand(time(nullptr));
1828    int pos = rand() % 250;
1829    cout << " pos= " << pos << endl;
1830
1831    while (true) {
1832        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
1833        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 1, memory, &attr));
1834        if (audioCount == pos) {
1835            cout << audioCount << " audioCount == pos remove audio track!!!!!!!!!" << endl;
1836            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_UnselectTrackByID(demuxer, 1));
1837            ASSERT_NE(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 1, memory, &attr));
1838            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
1839            break;
1840        }
1841        audioCount++;
1842    }
1843
1844    while (true) {
1845        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
1846        if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
1847            cout << "is end !!!!!!!!!!!!!!!" << endl;
1848            break;
1849        }
1850    }
1851    close(fd);
1852}
1853
1854/**
1855 * @tc.number    : DEMUXER_FUNCTION_4500
1856 * @tc.name      : start demux bufore add track
1857 * @tc.desc      : function test
1858 */
1859HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_4500, TestSize.Level2)
1860{
1861    uint32_t trackIndex = 0;
1862    OH_AVCodecBufferAttr attr;
1863    const char *file = "/data/test/media/01_video_audio.mp4";
1864    srand(time(nullptr));
1865    int fd = open(file, O_RDONLY);
1866    int64_t size = GetFileSize(file);
1867    cout << file << "----------------------" << fd << "---------" << size << endl;
1868    source = OH_AVSource_CreateWithFD(fd, 0, size);
1869    ASSERT_NE(source, nullptr);
1870
1871    demuxer = OH_AVDemuxer_CreateWithSource(source);
1872    ASSERT_NE(demuxer, nullptr);
1873    ret = OH_AVDemuxer_ReadSample(demuxer, trackIndex, memory, &attr);
1874    ASSERT_EQ(ret, AV_ERR_OPERATE_NOT_PERMIT);
1875    close(fd);
1876}
1877
1878/**
1879 * @tc.number    : DEMUXER_FUNCTION_7000
1880 * @tc.name      : demuxer MP4 ,GetSourceFormat,OH_MD_KEY_TRACK_COUNT
1881 * @tc.desc      : function test
1882 */
1883HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7000, TestSize.Level0)
1884{
1885    OH_AVFormat *trackFormat2 = nullptr;
1886    const char *file = "/data/test/media/01_video_audio.mp4";
1887    int fd = open(file, O_RDONLY);
1888    int64_t size = GetFileSize(file);
1889    cout << file << "----------------------" << fd << "---------" << size << endl;
1890    source = OH_AVSource_CreateWithFD(fd, 0, size);
1891    ASSERT_NE(source, nullptr);
1892    sourceFormat = OH_AVSource_GetSourceFormat(source);
1893    ASSERT_NE(sourceFormat, nullptr);
1894    cout << OH_AVFormat_DumpInfo(sourceFormat) << "sourceFormat" << endl;
1895
1896    trackFormat = OH_AVSource_GetTrackFormat(source, 1);
1897    ASSERT_NE(trackFormat, nullptr);
1898    cout << OH_AVFormat_DumpInfo(trackFormat) << "trackformat1" << endl;
1899
1900    trackFormat2 = OH_AVSource_GetTrackFormat(source, 0);
1901    cout << OH_AVFormat_DumpInfo(trackFormat2) << "trackformat0" << endl;
1902    OH_AVFormat_Destroy(trackFormat2);
1903    trackFormat2 = nullptr;
1904    close(fd);
1905}
1906
1907/**
1908 * @tc.number    : DEMUXER_FUNCTION_7100
1909 * @tc.name      : demuxer MP4 ,GetSourceFormat,OH_MD_KEY_TRACK_COUNT
1910 * @tc.desc      : function test
1911 */
1912HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7100, TestSize.Level0)
1913{
1914    const char *file = "/data/test/media/01_video_audio.mp4";
1915    int fd = open(file, O_RDONLY);
1916    int64_t size = GetFileSize(file);
1917    cout << file << "----------------------" << fd << "---------" << size << endl;
1918    source = OH_AVSource_CreateWithFD(fd, 0, size);
1919    ASSERT_NE(source, nullptr);
1920
1921    sourceFormat = OH_AVSource_GetSourceFormat(source);
1922    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
1923    ASSERT_EQ(g_trackCount, 2);
1924    close(fd);
1925}
1926
1927/**
1928 * @tc.number    : DEMUXER_FUNCTION_7200
1929 * @tc.name      : demuxer MP4 ,GetAudioTrackFormat,MD_KEY_AAC_IS_ADTS
1930 * @tc.desc      : function test
1931 */
1932HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7200, TestSize.Level0)
1933{
1934    const char *stringVal;
1935    const char *file = "/data/test/media/01_video_audio.mp4";
1936    int fd = open(file, O_RDONLY);
1937    int64_t size = GetFileSize(file);
1938    cout << file << "----------------------" << fd << "---------" << size << endl;
1939    source = OH_AVSource_CreateWithFD(fd, 0, size);
1940    ASSERT_NE(source, nullptr);
1941
1942    trackFormat = OH_AVSource_GetTrackFormat(source, 0);
1943    ASSERT_NE(trackFormat, nullptr);
1944    ASSERT_FALSE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_AAC_IS_ADTS, &stringVal));
1945    close(fd);
1946}
1947
1948/**
1949 * @tc.number    : DEMUXER_FUNCTION_7300
1950 * @tc.name      : demuxer MP4 ,GetAudioTrackFormat ,MD_KEY_BITRATE
1951 * @tc.desc      : function test
1952 */
1953HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7300, TestSize.Level0)
1954{
1955    int64_t br = 0;
1956    const char *file = "/data/test/media/01_video_audio.mp4";
1957    int fd = open(file, O_RDONLY);
1958    int64_t size = GetFileSize(file);
1959    cout << file << "----------------------" << fd << "---------" << size << endl;
1960    source = OH_AVSource_CreateWithFD(fd, 0, size);
1961    ASSERT_NE(source, nullptr);
1962
1963    trackFormat = OH_AVSource_GetTrackFormat(source, 0);
1964    ASSERT_NE(trackFormat, nullptr);
1965    ASSERT_TRUE(OH_AVFormat_GetLongValue(trackFormat, OH_MD_KEY_BITRATE, &br));
1966    ASSERT_EQ(br, 319999);
1967    close(fd);
1968}
1969
1970/**
1971 * @tc.number    : DEMUXER_FUNCTION_7400
1972 * @tc.name      : demuxer MP4 ,GetAudioTrackFormat,MD_KEY_CHANNEL_COUNT
1973 * @tc.desc      : function test
1974 */
1975HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7400, TestSize.Level0)
1976{
1977    int32_t cc = 0;
1978    const char *file = "/data/test/media/01_video_audio.mp4";
1979    int fd = open(file, O_RDONLY);
1980    int64_t size = GetFileSize(file);
1981    cout << file << "----------------------" << fd << "---------" << size << endl;
1982    source = OH_AVSource_CreateWithFD(fd, 0, size);
1983    ASSERT_NE(source, nullptr);
1984
1985    trackFormat = OH_AVSource_GetTrackFormat(source, 0);
1986    ASSERT_NE(trackFormat, nullptr);
1987    ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, &cc));
1988
1989    ASSERT_EQ(cc, 2);
1990    close(fd);
1991}
1992
1993/**
1994 * @tc.number    : DEMUXER_FUNCTION_7500
1995 * @tc.name      : demuxer MP4 ,GetAudioTrackFormat,MD_KEY_SAMPLE_RATE
1996 * @tc.desc      : function test
1997 */
1998HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7500, TestSize.Level0)
1999{
2000    int32_t sr = 0;
2001    const char *file = "/data/test/media/01_video_audio.mp4";
2002    int fd = open(file, O_RDONLY);
2003    int64_t size = GetFileSize(file);
2004    cout << file << "----------------------" << fd << "---------" << size << endl;
2005    source = OH_AVSource_CreateWithFD(fd, 0, size);
2006    ASSERT_NE(source, nullptr);
2007
2008    trackFormat = OH_AVSource_GetTrackFormat(source, 0);
2009    ASSERT_NE(trackFormat, nullptr);
2010    ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, &sr));
2011    ASSERT_EQ(sr, 44100);
2012    close(fd);
2013}
2014
2015/**
2016 * @tc.number    : DEMUXER_FUNCTION_7600
2017 * @tc.name      : demuxer MP4 ,GetVideoTrackFormat,MD_KEY_HEIGHT
2018 * @tc.desc      : function test
2019 */
2020HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7600, TestSize.Level0)
2021{
2022    int32_t height = 0;
2023    const char *file = "/data/test/media/01_video_audio.mp4";
2024    int fd = open(file, O_RDONLY);
2025    int64_t size = GetFileSize(file);
2026    cout << file << "----------------------" << fd << "---------" << size << endl;
2027    source = OH_AVSource_CreateWithFD(fd, 0, size);
2028    ASSERT_NE(source, nullptr);
2029
2030    trackFormat = OH_AVSource_GetTrackFormat(source, 1);
2031    ASSERT_NE(trackFormat, nullptr);
2032    ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_HEIGHT, &height));
2033    ASSERT_EQ(height, 0);
2034    close(fd);
2035}
2036
2037/**
2038 * @tc.number    : DEMUXER_FUNCTION_7700
2039 * @tc.name      : demuxer MP4 ,GetVideoTrackFormat,MD_KEY_WIDTH
2040 * @tc.desc      : function test
2041 */
2042HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7700, TestSize.Level0)
2043{
2044    int32_t weight = 0;
2045    const char *file = "/data/test/media/01_video_audio.mp4";
2046    int fd = open(file, O_RDONLY);
2047    int64_t size = GetFileSize(file);
2048    cout << file << "----------------------" << fd << "---------" << size << endl;
2049    source = OH_AVSource_CreateWithFD(fd, 0, size);
2050    ASSERT_NE(source, nullptr);
2051
2052    trackFormat = OH_AVSource_GetTrackFormat(source, 1);
2053    ASSERT_NE(trackFormat, nullptr);
2054    ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_WIDTH, &weight));
2055    ASSERT_EQ(weight, 0);
2056    close(fd);
2057}
2058
2059/**
2060 * @tc.number    : DEMUXER_FUNCTION_7800
2061 * @tc.name      : demuxer MP4 GetPublicTrackFormat,MD_KEY_CODEC_MIME
2062 * @tc.desc      : function test
2063 */
2064HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7800, TestSize.Level0)
2065{
2066    const char *stringVal;
2067    const char *file = "/data/test/media/01_video_audio.mp4";
2068    int fd = open(file, O_RDONLY);
2069    int64_t size = GetFileSize(file);
2070    cout << file << "----------------------" << fd << "---------" << size << endl;
2071    source = OH_AVSource_CreateWithFD(fd, 0, size);
2072    ASSERT_NE(source, nullptr);
2073
2074    trackFormat = OH_AVSource_GetTrackFormat(source, 1);
2075    ASSERT_NE(trackFormat, nullptr);
2076    ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &stringVal));
2077    ASSERT_EQ(0, strcmp(stringVal, OH_AVCODEC_MIMETYPE_VIDEO_MPEG4));
2078    close(fd);
2079}
2080
2081/**
2082 * @tc.number    : DEMUXER_FUNCTION_7900
2083 * @tc.name      : demuxer MP4 ,GetPublicTrackFormat,MD_KEY_TRACK_TYPE
2084 * @tc.desc      : function test
2085 */
2086HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_7900, TestSize.Level0)
2087{
2088    int32_t type = 0;
2089    const char *file = "/data/test/media/01_video_audio.mp4";
2090    int fd = open(file, O_RDONLY);
2091    int64_t size = GetFileSize(file);
2092    cout << file << "----------------------" << fd << "---------" << size << endl;
2093    source = OH_AVSource_CreateWithFD(fd, 0, size);
2094    ASSERT_NE(source, nullptr);
2095
2096    trackFormat = OH_AVSource_GetTrackFormat(source, 0);
2097    ASSERT_NE(trackFormat, nullptr);
2098    ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &type));
2099
2100    ASSERT_EQ(type, MEDIA_TYPE_AUD);
2101    close(fd);
2102}
2103
2104/**
2105 * @tc.number    : DEMUXER_FUNCTION_8000
2106 * @tc.name      : demuxer MP4 ,check source format,OH_MD_KEY_TITLE
2107 * @tc.desc      : function test
2108 */
2109HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8000, TestSize.Level0)
2110{
2111    const char *stringVal;
2112    const char *file = "/data/test/media/01_video_audio.mp4";
2113    int fd = open(file, O_RDONLY);
2114    int64_t size = GetFileSize(file);
2115    cout << file << "----------------------" << fd << "---------" << size << endl;
2116    source = OH_AVSource_CreateWithFD(fd, 0, size);
2117    ASSERT_NE(source, nullptr);
2118
2119    sourceFormat = OH_AVSource_GetSourceFormat(source);
2120    ASSERT_NE(sourceFormat, nullptr);
2121    ASSERT_TRUE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_TITLE, &stringVal));
2122    ASSERT_EQ(0, strcmp(stringVal, "title"));
2123    close(fd);
2124}
2125
2126/**
2127 * @tc.number    : DEMUXER_FUNCTION_8100
2128 * @tc.name      : demuxer MP4 ,check source format,OH_MD_KEY_ALBUM
2129 * @tc.desc      : function test
2130 */
2131HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8100, TestSize.Level0)
2132{
2133    const char *file = "/data/test/media/01_video_audio.mp4";
2134    int fd = open(file, O_RDONLY);
2135    int64_t size = GetFileSize(file);
2136    cout << file << "----------------------" << fd << "---------" << size << endl;
2137    source = OH_AVSource_CreateWithFD(fd, 0, size);
2138    ASSERT_NE(source, nullptr);
2139    sourceFormat = OH_AVSource_GetSourceFormat(source);
2140    ASSERT_NE(sourceFormat, nullptr);
2141    const char *stringVal;
2142    ASSERT_FALSE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_ALBUM, &stringVal));
2143    close(fd);
2144}
2145
2146/**
2147 * @tc.number    : DEMUXER_FUNCTION_8200
2148 * @tc.name      : demuxer MP4 ,check source format,OH_MD_KEY_ALBUM_ARTIST
2149 * @tc.desc      : function test
2150 */
2151HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8200, TestSize.Level0)
2152{
2153    const char *file = "/data/test/media/01_video_audio.mp4";
2154    int fd = open(file, O_RDONLY);
2155    int64_t size = GetFileSize(file);
2156    cout << file << "----------------------" << fd << "---------" << size << endl;
2157    source = OH_AVSource_CreateWithFD(fd, 0, size);
2158    ASSERT_NE(source, nullptr);
2159    sourceFormat = OH_AVSource_GetSourceFormat(source);
2160    ASSERT_NE(sourceFormat, nullptr);
2161    const char *stringVal;
2162    ASSERT_FALSE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_ALBUM_ARTIST, &stringVal));
2163    close(fd);
2164}
2165
2166/**
2167 * @tc.number    : DEMUXER_FUNCTION_8300
2168 * @tc.name      : demuxer MP4 ,check track format,OH_MD_KEY_DATE
2169 * @tc.desc      : function test
2170 */
2171HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8300, TestSize.Level0)
2172{
2173    const char *file = "/data/test/media/01_video_audio.mp4";
2174    int fd = open(file, O_RDONLY);
2175    int64_t size = GetFileSize(file);
2176    cout << file << "----------------------" << fd << "---------" << size << endl;
2177    source = OH_AVSource_CreateWithFD(fd, 0, size);
2178    ASSERT_NE(source, nullptr);
2179    sourceFormat = OH_AVSource_GetSourceFormat(source);
2180    ASSERT_NE(sourceFormat, nullptr);
2181    const char *stringVal;
2182    ASSERT_TRUE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_DATE, &stringVal));
2183    ASSERT_EQ(0, strcmp(stringVal, "2023"));
2184    close(fd);
2185}
2186
2187/**
2188 * @tc.number    : DEMUXER_FUNCTION_8400
2189 * @tc.name      : demuxer MP4 ,check track format,OH_MD_KEY_COMMENT
2190 * @tc.desc      : function test
2191 */
2192HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8400, TestSize.Level0)
2193{
2194    const char *file = "/data/test/media/01_video_audio.mp4";
2195    int fd = open(file, O_RDONLY);
2196    int64_t size = GetFileSize(file);
2197    cout << file << "----------------------" << fd << "---------" << size << endl;
2198    source = OH_AVSource_CreateWithFD(fd, 0, size);
2199    ASSERT_NE(source, nullptr);
2200    sourceFormat = OH_AVSource_GetSourceFormat(source);
2201    ASSERT_NE(sourceFormat, nullptr);
2202    const char *stringVal;
2203    ASSERT_TRUE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_COMMENT, &stringVal));
2204    ASSERT_EQ(0, strcmp(stringVal, "COMMENT"));
2205    close(fd);
2206}
2207
2208/**
2209 * @tc.number    : DEMUXER_FUNCTION_8500
2210 * @tc.name      : demuxer MP4 ,check track format,OH_MD_KEY_GENRE
2211 * @tc.desc      : function test
2212 */
2213HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8500, TestSize.Level0)
2214{
2215    const char *file = "/data/test/media/01_video_audio.mp4";
2216    int fd = open(file, O_RDONLY);
2217    int64_t size = GetFileSize(file);
2218    cout << file << "----------------------" << fd << "---------" << size << endl;
2219    source = OH_AVSource_CreateWithFD(fd, 0, size);
2220    ASSERT_NE(source, nullptr);
2221    sourceFormat = OH_AVSource_GetSourceFormat(source);
2222    ASSERT_NE(sourceFormat, nullptr);
2223    const char *stringVal;
2224    ASSERT_TRUE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_GENRE, &stringVal));
2225    ASSERT_EQ(0, strcmp(stringVal, "Classical"));
2226    close(fd);
2227}
2228
2229/**
2230 * @tc.number    : DEMUXER_FUNCTION_8600
2231 * @tc.name      : demuxer MP4 ,check track format,OH_MD_KEY_COPYRIGHT
2232 * @tc.desc      : function test
2233 */
2234HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8600, TestSize.Level0)
2235{
2236    const char *file = "/data/test/media/01_video_audio.mp4";
2237    int fd = open(file, O_RDONLY);
2238    int64_t size = GetFileSize(file);
2239    cout << file << "----------------------" << fd << "---------" << size << endl;
2240    source = OH_AVSource_CreateWithFD(fd, 0, size);
2241    ASSERT_NE(source, nullptr);
2242    sourceFormat = OH_AVSource_GetSourceFormat(source);
2243    ASSERT_NE(sourceFormat, nullptr);
2244    const char *stringVal;
2245    ASSERT_FALSE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_COPYRIGHT, &stringVal));
2246    close(fd);
2247}
2248
2249/**
2250 * @tc.number    : DEMUXER_FUNCTION_8700
2251 * @tc.name      : demuxer MP4 ,check track format,OH_MD_KEY_LANGUAGE
2252 * @tc.desc      : function test
2253 */
2254HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8700, TestSize.Level0)
2255{
2256    const char *file = "/data/test/media/01_video_audio.mp4";
2257    int fd = open(file, O_RDONLY);
2258    int64_t size = GetFileSize(file);
2259    cout << file << "----------------------" << fd << "---------" << size << endl;
2260    source = OH_AVSource_CreateWithFD(fd, 0, size);
2261    ASSERT_NE(source, nullptr);
2262    sourceFormat = OH_AVSource_GetSourceFormat(source);
2263    ASSERT_NE(sourceFormat, nullptr);
2264    const char *stringVal;
2265    ASSERT_FALSE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_LANGUAGE, &stringVal));
2266    close(fd);
2267}
2268
2269/**
2270 * @tc.number    : DEMUXER_FUNCTION_8800
2271 * @tc.name      : demuxer MP4 ,check track format,OH_MD_KEY_DESCRIPTION
2272 * @tc.desc      : function test
2273 */
2274HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8800, TestSize.Level0)
2275{
2276    const char *file = "/data/test/media/01_video_audio.mp4";
2277    int fd = open(file, O_RDONLY);
2278    int64_t size = GetFileSize(file);
2279    cout << file << "----------------------" << fd << "---------" << size << endl;
2280    source = OH_AVSource_CreateWithFD(fd, 0, size);
2281    ASSERT_NE(source, nullptr);
2282    sourceFormat = OH_AVSource_GetSourceFormat(source);
2283    ASSERT_NE(sourceFormat, nullptr);
2284    const char *stringVal;
2285    ASSERT_FALSE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_DESCRIPTION, &stringVal));
2286    close(fd);
2287}
2288
2289/**
2290 * @tc.number    : DEMUXER_FUNCTION_8800
2291 * @tc.name      : demuxer MP4 ,check track format,OH_MD_KEY_LYRICS
2292 * @tc.desc      : function test
2293 */
2294HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_8900, TestSize.Level0)
2295{
2296    const char *file = "/data/test/media/01_video_audio.mp4";
2297    int fd = open(file, O_RDONLY);
2298    int64_t size = GetFileSize(file);
2299    cout << file << "----------------------" << fd << "---------" << size << endl;
2300    source = OH_AVSource_CreateWithFD(fd, 0, size);
2301    ASSERT_NE(source, nullptr);
2302    sourceFormat = OH_AVSource_GetSourceFormat(source);
2303    ASSERT_NE(sourceFormat, nullptr);
2304    const char *stringVal;
2305    ASSERT_FALSE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_LYRICS, &stringVal));
2306    close(fd);
2307}
2308
2309/**
2310 * @tc.number    : DEMUXER_FUNCTION_9000
2311 * @tc.name      : demuxer MP4 ,check source format,OH_MD_KEY_ARTIST
2312 * @tc.desc      : function test
2313 */
2314HWTEST_F(DemuxerFuncNdkTest, DEMUXER_FUNCTION_9000, TestSize.Level0)
2315{
2316    const char *file = "/data/test/media/01_video_audio.mp4";
2317    int fd = open(file, O_RDONLY);
2318    int64_t size = GetFileSize(file);
2319    cout << file << "----------------------" << fd << "---------" << size << endl;
2320    source = OH_AVSource_CreateWithFD(fd, 0, size);
2321    ASSERT_NE(source, nullptr);
2322    sourceFormat = OH_AVSource_GetSourceFormat(source);
2323    ASSERT_NE(sourceFormat, nullptr);
2324    const char *stringVal;
2325    ASSERT_TRUE(OH_AVFormat_GetStringValue(sourceFormat, OH_MD_KEY_ARTIST, &stringVal));
2326    ASSERT_EQ(0, strcmp(stringVal, "sam"));
2327    close(fd);
2328}
2329/**
2330 * @tc.number    : SUB_MEDIA_DEMUXER_FUNCTION_0200
2331 * @tc.name      : demuxer video amr nb
2332 * @tc.desc      : function test
2333 */
2334HWTEST_F(DemuxerFuncNdkTest, SUB_MEDIA_DEMUXER_FUNCTION_0200, TestSize.Level0)
2335{
2336    OH_AVCodecBufferAttr attr;
2337    bool audioIsEnd = false;
2338    int audioFrame = 0;
2339    const char *file = "/data/test/media/audio/amr_nb_8000_1.amr";
2340    int fd = open(file, O_RDONLY);
2341    int64_t size = GetFileSize(file);
2342    cout << file << "----------------------" << fd << "---------" << size << endl;
2343    source = OH_AVSource_CreateWithFD(fd, 0, size);
2344    ASSERT_NE(source, nullptr);
2345    demuxer = OH_AVDemuxer_CreateWithSource(source);
2346    ASSERT_NE(demuxer, nullptr);
2347    sourceFormat = OH_AVSource_GetSourceFormat(source);
2348    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
2349    ASSERT_EQ(1, g_trackCount);
2350    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
2351    int aKeyCount = 0;
2352    while (!audioIsEnd) {
2353        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
2354        SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
2355    }
2356    cout << file << "audioFrame " << audioFrame <<   "   aKeyCount " << aKeyCount  << endl;
2357    ASSERT_EQ(audioFrame, 1501);
2358    ASSERT_EQ(aKeyCount, 1501);
2359    close(fd);
2360}
2361
2362/**
2363 * @tc.number    : SUB_MEDIA_DEMUXER_FUNCTION_0300
2364 * @tc.name      : demuxer video amr wb
2365 * @tc.desc      : function test
2366 */
2367HWTEST_F(DemuxerFuncNdkTest, SUB_MEDIA_DEMUXER_FUNCTION_0300, TestSize.Level0)
2368{
2369    OH_AVCodecBufferAttr attr;
2370    bool audioIsEnd = false;
2371    int audioFrame = 0;
2372    const char *file = "/data/test/media/audio/amr_wb_16000_1.amr";
2373    int fd = open(file, O_RDONLY);
2374    int64_t size = GetFileSize(file);
2375    cout << file << "----------------------" << fd << "---------" << size << endl;
2376    source = OH_AVSource_CreateWithFD(fd, 0, size);
2377    ASSERT_NE(source, nullptr);
2378    demuxer = OH_AVDemuxer_CreateWithSource(source);
2379    ASSERT_NE(demuxer, nullptr);
2380    sourceFormat = OH_AVSource_GetSourceFormat(source);
2381    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
2382    ASSERT_EQ(1, g_trackCount);
2383    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
2384    int aKeyCount = 0;
2385    while (!audioIsEnd) {
2386        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
2387        SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
2388    }
2389    cout << file << "audioFrame " << audioFrame <<  "   aKeyCount " << aKeyCount << endl;
2390    ASSERT_EQ(audioFrame, 1500);
2391    ASSERT_EQ(aKeyCount, 1500);
2392    close(fd);
2393}
2394
2395/**
2396 * @tc.number    : SUB_MEDIA_DEMUXER_FUNCTION_1000
2397 * @tc.name      : demuxer amr_nb format
2398 * @tc.desc      : function test
2399 */
2400HWTEST_F(DemuxerFuncNdkTest, SUB_MEDIA_DEMUXER_FUNCTION_1000, TestSize.Level2)
2401{
2402    const char *file = "/data/test/media/audio/amr_nb_8000_1.amr";
2403    int fd = open(file, O_RDONLY);
2404    int64_t size = GetFileSize(file);
2405    cout << file << "----------------------" << fd << "---------" << size << endl;
2406    source = OH_AVSource_CreateWithFD(fd, 0, size);
2407    ASSERT_NE(source, nullptr);
2408    demuxer = OH_AVDemuxer_CreateWithSource(source);
2409    ASSERT_NE(demuxer, nullptr);
2410    trackFormat = OH_AVSource_GetTrackFormat(source, 0);
2411    ASSERT_NE(trackFormat, nullptr);
2412    const char *codecMime = "";
2413    ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &codecMime));
2414    cout << "codecMime" << codecMime << endl;
2415    ASSERT_EQ(0, strcmp(codecMime, OH_AVCODEC_MIMETYPE_AUDIO_AMR_NB));
2416    close(fd);
2417}
2418
2419/**
2420 * @tc.number    : SUB_MEDIA_DEMUXER_FUNCTION_1100
2421 * @tc.name      : demuxer amr_wb format
2422 * @tc.desc      : function test
2423 */
2424HWTEST_F(DemuxerFuncNdkTest, SUB_MEDIA_DEMUXER_FUNCTION_1100, TestSize.Level2)
2425{
2426    const char *file = "/data/test/media/audio/amr_wb_16000_1.amr";
2427    int fd = open(file, O_RDONLY);
2428    int64_t size = GetFileSize(file);
2429    cout << file << "----------------------" << fd << "---------" << size << endl;
2430    source = OH_AVSource_CreateWithFD(fd, 0, size);
2431    ASSERT_NE(source, nullptr);
2432    demuxer = OH_AVDemuxer_CreateWithSource(source);
2433    ASSERT_NE(demuxer, nullptr);
2434    trackFormat = OH_AVSource_GetTrackFormat(source, 0);
2435    ASSERT_NE(trackFormat, nullptr);
2436    const char *codecMime = "";
2437    ASSERT_TRUE(OH_AVFormat_GetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, &codecMime));
2438    cout << "codecMime" << codecMime << endl;
2439    ASSERT_EQ(0, strcmp(codecMime, OH_AVCODEC_MIMETYPE_AUDIO_AMR_WB));
2440    close(fd);
2441}
2442
2443
2444/**
2445 * @tc.number    : SUB_MEDIA_DEMUXER_FUNCTION_1200
2446 * @tc.name      : demux hevc ts video and audio
2447 * @tc.desc      : function test
2448 */
2449HWTEST_F(DemuxerFuncNdkTest, SUB_MEDIA_DEMUXER_FUNCTION_1200, TestSize.Level0)
2450{
2451    if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
2452        return;
2453    }
2454    int tarckType = 0;
2455    OH_AVCodecBufferAttr attr;
2456    bool audioIsEnd = false;
2457    bool videoIsEnd = false;
2458    int audioFrame = 0;
2459    int videoFrame = 0;
2460    const char *file = "/data/test/media/hevc_v_a.ts";
2461    int fd = open(file, O_RDONLY);
2462    int64_t size = GetFileSize(file);
2463    cout << file << "----------------------" << fd << "---------" << size << endl;
2464    source = OH_AVSource_CreateWithFD(fd, 0, size);
2465    ASSERT_NE(source, nullptr);
2466
2467    demuxer = OH_AVDemuxer_CreateWithSource(source);
2468    ASSERT_NE(demuxer, nullptr);
2469
2470    sourceFormat = OH_AVSource_GetSourceFormat(source);
2471    ASSERT_TRUE(OH_AVFormat_GetIntValue(sourceFormat, OH_MD_KEY_TRACK_COUNT, &g_trackCount));
2472    ASSERT_EQ(2, g_trackCount);
2473
2474    for (int32_t index = 0; index < g_trackCount; index++) {
2475        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
2476    }
2477    int vKeyCount = 0;
2478    int aKeyCount = 0;
2479    while (!audioIsEnd || !videoIsEnd) {
2480        for (int32_t index = 0; index < g_trackCount; index++) {
2481            trackFormat = OH_AVSource_GetTrackFormat(source, index);
2482            ASSERT_NE(trackFormat, nullptr);
2483            ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
2484            OH_AVFormat_Destroy(trackFormat);
2485            trackFormat = nullptr;
2486            if ((audioIsEnd && (tarckType == MEDIA_TYPE_AUD)) || (videoIsEnd && (tarckType == MEDIA_TYPE_VID))) {
2487                continue;
2488            }
2489            ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, index, memory, &attr));
2490            if (tarckType == MEDIA_TYPE_AUD) {
2491                SetAudioValue(attr, audioIsEnd, audioFrame, aKeyCount);
2492            } else if (tarckType == MEDIA_TYPE_VID) {
2493                SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
2494            }
2495        }
2496    }
2497    ASSERT_EQ(audioFrame, 384);
2498    ASSERT_EQ(aKeyCount, 384);
2499    ASSERT_EQ(videoFrame, 602);
2500    ASSERT_EQ(vKeyCount, 3);
2501    close(fd);
2502}
2503
2504/**
2505 * @tc.number    : SUB_MEDIA_DEMUXER_FUNCTION_1300
2506 * @tc.name      : demux hevc ts video
2507 * @tc.desc      : function test
2508 */
2509HWTEST_F(DemuxerFuncNdkTest, SUB_MEDIA_DEMUXER_FUNCTION_1300, TestSize.Level0)
2510{
2511    if (access(HEVC_LIB_PATH.c_str(), F_OK) != 0) {
2512        return;
2513    }
2514    int tarckType = 0;
2515    OH_AVCodecBufferAttr attr;
2516    bool videoIsEnd = false;
2517    int videoFrame = 0;
2518    const char *file = "/data/test/media/hevc_v.ts";
2519    int fd = open(file, O_RDONLY);
2520    int64_t size = GetFileSize(file);
2521    cout << file << "----------------------" << fd << "---------" << size << endl;
2522    source = OH_AVSource_CreateWithFD(fd, 0, size);
2523    ASSERT_NE(source, nullptr);
2524
2525    demuxer = OH_AVDemuxer_CreateWithSource(source);
2526    ASSERT_NE(demuxer, nullptr);
2527
2528    ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, 0));
2529    int vKeyCount = 0;
2530    while (!videoIsEnd) {
2531        trackFormat = OH_AVSource_GetTrackFormat(source, 0);
2532        ASSERT_NE(trackFormat, nullptr);
2533        ASSERT_TRUE(OH_AVFormat_GetIntValue(trackFormat, OH_MD_KEY_TRACK_TYPE, &tarckType));
2534        OH_AVFormat_Destroy(trackFormat);
2535        trackFormat = nullptr;
2536        if (videoIsEnd && (tarckType == MEDIA_TYPE_VID)) {
2537            continue;
2538        }
2539        ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(demuxer, 0, memory, &attr));
2540        SetVideoValue(attr, videoIsEnd, videoFrame, vKeyCount);
2541    }
2542    ASSERT_EQ(videoFrame, 602);
2543    ASSERT_EQ(vKeyCount, 3);
2544    close(fd);
2545}