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 
17 #ifndef MEDIA_AVCODEC_AVDEMUXER_H
18 #define MEDIA_AVCODEC_AVDEMUXER_H
19 
20 #include <memory>
21 #include "avcodec_common.h"
22 #include "buffer/avbuffer.h"
23 #include "buffer/avsharedmemory.h"
24 #include "avsource.h"
25 #include "meta/media_types.h"
26 
27 namespace OHOS {
28 namespace MediaAVCodec {
29 using namespace Media;
30 class AVDemuxer {
31 public:
32     ~AVDemuxer() = default;
33 
34     /**
35      * @brief Select the sourceTrack by track index.
36      * This function can only by called before {@link ReadSample}.
37      * @param trackIndex The track index for being selected.
38      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
39      * @since 4.0
40      */
41     virtual int32_t SelectTrackByID(uint32_t trackIndex) = 0;
42 
43     /**
44      * @brief Unselect the sourceTrack by track index.
45      * This function can only by called before {@link ReadSample}.
46      * @param trackIndex The track index for being unselected.
47      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
48      * @since 4.0
49      */
50     virtual int32_t UnselectTrackByID(uint32_t trackIndex) = 0;
51 
52     /**
53      * @brief Retrieve the sample in selected tracks and store it in buffer, and store buffer's info to attr.
54      * @param trackIndex Get the sampleBuffer from this track.
55      * @param sample The AVSharedMemory handle pointer to get buffer data.
56      * @param info The CodecBufferAttr handle pointer to get buffer info.
57      * @param flag The buffer flags.
58      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
59      * @since 4.0
60      */
61     virtual int32_t ReadSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> sample,
62         AVCodecBufferInfo &info, uint32_t &flag) = 0;
63 
64     /**
65      * @brief Retrieve the sample in selected tracks and store it in buffer, and store buffer's info to attr.
66      * @param trackIndex Get the sampleBuffer from this track.
67      * @param sample The AVSharedMemory handle pointer to get buffer data.
68      * @param info The CodecBufferAttr handle pointer to get buffer info.
69      * @param flag The buffer flags.
70      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
71      * @since 4.0
72      */
73     virtual int32_t ReadSample(uint32_t trackIndex, std::shared_ptr<AVSharedMemory> sample,
74         AVCodecBufferInfo &info, AVCodecBufferFlag &flag) = 0;
75 
76      /**
77      * @brief Retrieve the sample in selected tracks and store it in buffer, and store buffer's info to attr.
78      * @param trackIndex Get the sampleBuffer from this track.
79      * @param sample The AVBuffer handle pointer to get buffer data.
80      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
81      * @since 4.1
82      */
83     virtual int32_t ReadSampleBuffer(uint32_t trackIndex, std::shared_ptr<AVBuffer> sample) = 0;
84 
85     /**
86      * @brief All selected tracks seek near to the requested time according to the seek mode.
87      * @param millisecond The timestamp for seeking which is the position relative to the beginning of the file.
88      * @param mode The mode for seeking. Value. For details, see {@link SeekMode}.
89      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
90      * @since 4.0
91      */
92     virtual int32_t SeekToTime(int64_t millisecond, SeekMode mode) = 0;
93 
94     /**
95      * @brief Registers a demuxer listener.
96      *
97      * @param callback Indicates the demuxer listener to register. For details, see {@link AVDemuxerCallback}.
98      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
99      * @since 4.1
100      * @version 4.1
101      */
102     virtual int32_t SetCallback(const std::shared_ptr<AVDemuxerCallback> &callback) = 0;
103 
104     virtual int32_t GetMediaKeySystemInfo(std::multimap<std::string, std::vector<uint8_t>> &infos) = 0;
105     virtual int32_t StartReferenceParser(int64_t startTimeMs) = 0;
106     virtual int32_t GetFrameLayerInfo(std::shared_ptr<AVBuffer> videoSample, FrameLayerInfo &frameLayerInfo) = 0;
107     virtual int32_t GetGopLayerInfo(uint32_t gopId, GopLayerInfo &gopLayerInfo) = 0;
108 
109     /**
110      * @brief Obtian index by relative pts.
111      * @param trackIndex Get the index from this track.
112      * @param relativePresentationTimeUs Relative pts which obtian index by.
113      * @param index Frame index of obtian result.
114      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
115      * @since 5.0
116      */
117     virtual int32_t GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
118         const uint64_t relativePresentationTimeUs, uint32_t &index) = 0;
119 
120     /**
121      * @brief Obtian relative pts by index.
122      * @param trackIndex Get the relative pts from this track.
123      * @param index Frame index which obtian relative pts by.
124      * @param relativePresentationTimeUs Pts of obtian reuslt.
125      * @return Returns {@link AVCS_ERR_OK} if success; returns an error code otherwise.
126      * @since 5.0
127      */
128     virtual int32_t GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
129         const uint32_t index, uint64_t &relativePresentationTimeUs) = 0;
130 };
131 
132 class __attribute__((visibility("default"))) AVDemuxerFactory {
133 public:
134 #ifdef UNSUPPORT_DEMUXER
CreateWithSource(std::shared_ptr<AVSource> source)135     static std::shared_ptr<AVDemuxer> CreateWithSource(std::shared_ptr<AVSource> source)
136     {
137         return nullptr;
138     }
139 #else
140     /**
141      * @brief Instantiate the preferred demuxer of the given source instance.
142      * @param sourceAddr The address for source instance.
143      * @return Returns the preferred demuxer.
144      * @since 4.0
145      */
146     static std::shared_ptr<AVDemuxer> CreateWithSource(std::shared_ptr<AVSource> source);
147 #endif
148 private:
149     AVDemuxerFactory() = default;
150     ~AVDemuxerFactory() = default;
151 };
152 } // namespace MediaAVCodec
153 } // namespace OHOS
154 #endif // MEDIA_AVCODEC_AVDEMUXER_H
155