1 /*
2  * Copyright (C) 2024 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 #ifndef INNER_DEMUXER_SAMPLE_H
17 #define INNER_DEMUXER_SAMPLE_H
18 
19 #include <map>
20 #include "avdemuxer.h"
21 #include "avsource.h"
22 
23 namespace OHOS {
24 namespace MediaAVCodec {
25 using AVBuffer = OHOS::Media::AVBuffer;
26 using AVAllocator = OHOS::Media::AVAllocator;
27 using AVAllocatorFactory = OHOS::Media::AVAllocatorFactory;
28 using MemoryFlag = OHOS::Media::MemoryFlag;
29 using FormatDataType = OHOS::Media::FormatDataType;
30 
31 class InnerDemuxerSample {
32 public:
33     InnerDemuxerSample();
34     ~InnerDemuxerSample();
35     size_t GetFileSize(const std::string& filePath);
36     std::shared_ptr<AVDemuxer> demuxer_ = nullptr;
37     std::shared_ptr<OHOS::Media::AVBuffer> avBuffer;
38 private:
39     int32_t InitWithFile(const std::string &path, bool local);
40     int32_t ReadSampleAndSave();
41     int32_t CheckPtsFromIndex();
42     int32_t CheckIndexFromPts();
43     int32_t CheckHasTimedMeta();
44     void CheckLoop(int32_t metaTrack);
45     int32_t CheckTimedMetaFormat(int32_t trackIndex, int32_t srcTrackIndex);
46     int32_t CheckTimedMeta(int32_t metaTrack);
47     void CheckLoopForSave();
48     void CheckLoopForIndexFromPts(int32_t trackIndex);
49     void CheckLoopForPtsFromIndex(int32_t trackIndex);
50     void GetIndexByPtsForAudio(int32_t trackIndex);
51     void GetIndexByPtsForVideo(int32_t trackIndex);
52     void GetIndexFromPtsForVideo(int32_t trackIndex, uint64_t relativePresentationTimeUs, int64_t pair,
53         int division, int value);
54     void GetIndexFromPtsForAudio(int32_t trackIndex, uint64_t relativePresentationTimeUs, int64_t pair,
55         int division, int value);
56     int32_t CheckIndex(uint32_t index);
57     std::list<int64_t> videoIndexPtsList;
58     std::list<int64_t> audioIndexPtsList;
59     std::shared_ptr<AVSource> avsource_ = nullptr;
60     Format source_format_;
61     Format track_format_;
62     int32_t fd;
63     int32_t trackCount;
64     int64_t duration;
65     int32_t videoTrackIdx;
66     int64_t usleepTime = 100000;
67     bool isVideoEosFlagForMeta = false;
68     bool isMetaEosFlagForMeta = false;
69     uint32_t videoIndexForMeta = 0;
70     uint32_t metaIndexForMeta = 0;
71     uint32_t videoIndexForRead = 0;
72     uint32_t audioIndexForRead = 0;
73     int32_t retForMeta = 0;
74     bool isVideoEosFlagForSave = false;
75     bool isAudioEosFlagForSave = false;
76     int32_t retForSave = 0;
77     int32_t retForIndex;
78     int32_t retForPts;
79     uint32_t indexForPts = 0;
80     int64_t videoPtsOffset = 0;
81     int64_t audioPtsOffset = 0;
82     bool isPtsExist = false;
83     bool isPtsCloseRight = false;
84     bool isPtsCloseCenter = false;
85     bool isPtsCloseLeft = false;
86     uint32_t listIndex = 0;
87     uint64_t previousValue = 0;
88     uint32_t indexVideo = 0;
89     uint32_t indexAudio = 0;
90 };
91 }
92 }
93 #endif
94