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 #ifndef INNER_DEMUXER_DEMO_H
17 #define INNER_DEMUXER_DEMO_H
18 
19 #include <iostream>
20 #include "avdemuxer.h"
21 
22 namespace OHOS {
23 namespace MediaAVCodec {
24 using AVBuffer = OHOS::Media::AVBuffer;
25 using AVAllocator = OHOS::Media::AVAllocator;
26 using AVAllocatorFactory = OHOS::Media::AVAllocatorFactory;
27 using MemoryFlag = OHOS::Media::MemoryFlag;
28 using FormatDataType = OHOS::Media::FormatDataType;
29 class InnerDemuxerDemo {
30 public:
31     InnerDemuxerDemo();
32     ~InnerDemuxerDemo();
33     int32_t CreateWithSource(std::shared_ptr<AVSource> source);
34     void Destroy();
35     int32_t SelectTrackByID(uint32_t trackIndex);
36     int32_t UnselectTrackByID(uint32_t trackIndex);
37     int32_t PrintInfo(int32_t tracks);
38     int32_t ReadAllSamples(std::shared_ptr<AVSharedMemory> mem, int32_t tracks);
39     int32_t ReadSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> mem,
40                         AVCodecBufferInfo &bufInfo, uint32_t &bufferFlag);
41     int32_t SeekToTime(int64_t millisecond, Media::SeekMode mode);
42     int32_t GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
43         const uint64_t relativePresentationTimeUs, uint32_t &index);
44     int32_t GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
45         const uint32_t index, uint64_t &relativePresentationTimeUs);
46     bool isEOS(std::map<uint32_t, bool>& countFlag);
47     int32_t StartReferenceParser(int64_t startTimeMs);
48     int32_t GetFrameLayerInfo(std::shared_ptr<AVBuffer> videoSample, FrameLayerInfo &frameLayerInfo);
49     int32_t GetGopLayerInfo(uint32_t gopId, GopLayerInfo &gopLayerInfo);
50     int32_t ReadSampleBuffer(uint32_t trackIndex, std::shared_ptr<AVBuffer> sample);
51 
52     AVCodecBufferInfo sampleInfo;
53     std::map<int32_t, int64_t> frames_;
54     std::map<int32_t, int64_t> key_frames_;
55 private:
56     std::shared_ptr<AVSource> avsource_ = nullptr;
57     std::shared_ptr<AVDemuxer> demuxer_ = nullptr;
58     std::shared_ptr<OHOS::MediaAVCodec::AVSharedMemory> sharedMemory_ = nullptr;
59     Format source_format_;
60     Format track_format_;
61 };
62 }
63 }
64 #endif
65