• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 AVCODEC_DEMUXER_PLUGIN_H
17 #define AVCODEC_DEMUXER_PLUGIN_H
18 
19 #include <memory>
20 #include <vector>
21 #include "meta/meta.h"
22 #include "plugin/plugin_base.h"
23 #include "plugin/plugin_caps.h"
24 #include "plugin/plugin_definition.h"
25 #include "plugin/plugin_info.h"
26 #include "common/media_core.h"
27 
28 namespace OHOS {
29 namespace Media {
30 namespace Plugins {
31 /**
32  * @brief Demuxer Plugin Interface.
33  *
34  * Used for audio and video media file parse.
35  *
36  * @since 1.0
37  * @version 1.0
38  */
39 struct DemuxerPlugin : public PluginBase {
40     /// constructor
DemuxerPluginDemuxerPlugin41     explicit DemuxerPlugin(std::string name): PluginBase(std::move(name)) {}
42     /**
43      * @brief Set the data source to demuxer component.
44      *
45      * The function is valid only in the CREATED state.
46      *
47      * @param source Data source where data read from.
48      * @return  Execution Status return
49      *  @retval OK: Plugin SetDataSource succeeded.
50      */
51     virtual Status SetDataSource(const std::shared_ptr<DataSource>& source) = 0;
52 
53     /**
54      * @brief Get the attributes of a media file.
55      *
56      * The attributes contain file and stream attributes.
57      * The function is valid only after INITIALIZED state.
58      *
59      * @param mediaInfo Indicates the pointer to the source attributes
60      * @return  Execution status return
61      *  @retval OK: Plugin GetMediaInfo succeeded.
62      */
63     virtual Status GetMediaInfo(MediaInfo& mediaInfo) = 0;
64 
65     /**
66      * @brief Get the user meta of a media file.
67      *
68      * The attributes contain file and stream attributes.
69      * The function is valid only after INITIALIZED state.
70      *
71      * @param mediaInfo Indicates the pointer to the user meta attributes
72      * @return  Execution status return
73      *  @retval OK: Plugin GetUserMeta succeeded.
74      */
75     virtual Status GetUserMeta(std::shared_ptr<Meta> meta) = 0;
76 
77     /**
78      * @brief Select a specified media track.
79      *
80      * The function is valid only after RUNNING state.
81      *
82      * @param trackId Identifies the media track. If an invalid value is passed, the default media track specified.
83      * @return  Execution Status return
84      *  @retval OK: Plugin SelectTrack succeeded.
85      */
86     virtual Status SelectTrack(uint32_t trackId) = 0;
87 
88     /**
89      * @brief Unselect a specified media track from which the demuxer reads data frames.
90      *
91      * The function is valid only after RUNNING state.
92      *
93      * @param trackId Identifies the media track. ignore the invalid value is passed.
94      * @return  Execution Status return
95      *  @retval OK: Plugin UnselectTrack succeeded.
96      */
97     virtual Status UnselectTrack(uint32_t trackId) = 0;
98 
99     /**
100      * @brief Reads data frames (synchronous version).
101      *
102      * This function uses a synchronous implementation mechanism. It is valid only after the RUNNING state.
103      *
104      * @note This synchronous interface must be used together with the synchronous version of GetNextSampleSize.
105      *       Synchronous and asynchronous interfaces (with and without timeout) cannot be mixed in the same instance.
106      *
107      * @param trackId Identifies the media track. ignore the invalid value is passed.
108      * @param sample Buffer where store data frames.
109      * @return  Execution Status return
110      *  @retval OK: Plugin ReadFrame succeeded.
111      *  @retval ERROR_TIMED_OUT: Operation timeout.
112      */
113     virtual Status ReadSample(uint32_t trackId, std::shared_ptr<AVBuffer> sample) = 0;
114 
115     /**
116      * @brief Reads data frames within @param timeout milliseconds (asynchronous version).
117      *
118      * This function uses an asynchronous implementation mechanism. It is valid only after the RUNNING state.
119      *
120      * @note This asynchronous interface must be used together with the asynchronous version of GetNextSampleSize.
121      *       Synchronous and asynchronous interfaces (with and without timeout) cannot be mixed in the same instance.
122      *
123      * @param trackId Identifies the media track. ignore the invalid value is passed.
124      * @param sample Buffer where store data frames.
125      * @param timeout If no result is available after @param timeout milliseconds, the function returns.
126      * @return  Execution Status return
127      *  @retval OK: Plugin ReadFrame succeeded.
128      *  @retval ERROR_WAIT_TIMEOUT: Operation timeout.
129      *  @retval END_OF_STREAM: Read end (EOS).
130      *  @retval ERROR_UNKNOWN: Call av_read_frame failed.
131      *  @retval ERROR_NULL_POINTER: Call av_packet_alloc failed.
132      */
133     virtual Status ReadSample(uint32_t trackId, std::shared_ptr<AVBuffer> sample, uint32_t timeout) = 0;
134 
135     /**
136      * @brief Get next sample size (synchronous version).
137      *
138      * This function uses a synchronous implementation mechanism. It is valid only after the RUNNING state.
139      *
140      * @note This synchronous interface must be used together with the synchronous version of ReadSample.
141      *       Synchronous and asynchronous interfaces (with and without timeout) cannot be mixed in the same instance.
142      *
143      * @param trackId Identifies the media track. ignore the invalid value is passed.
144      * @param size Output parameter for the next sample size.
145      * @return Execution Status
146      */
147     virtual Status GetNextSampleSize(uint32_t trackId, int32_t& size) = 0;
148 
149     /**
150      * @brief Get next sample size within @param timeout milliseconds (asynchronous version).
151      *
152      * This function uses an asynchronous implementation mechanism. It is valid only after the RUNNING state.
153      *
154      * @note This asynchronous interface must be used together with the asynchronous version of ReadSample.
155      *       Synchronous and asynchronous interfaces (with and without timeout) cannot be mixed in the same instance.
156      *
157      * @param trackId Identifies the media track. ignore the invalid value is passed.
158      * @param size Output parameter for the next sample size.
159      * @param timeout If no result is available after @param timeout milliseconds, the function returns.
160      * @return Execution Status
161      */
162     virtual Status GetNextSampleSize(uint32_t trackId, int32_t& size, uint32_t timeout) = 0;
163 
164     /**
165      * @brief Pause reading data from ffmpeg in another thread.
166      *
167      * @return Execution Status
168      */
169     virtual Status Pause() = 0;
170 
171     /**
172      * @brief Get the latest PTS by trackId.
173      *
174      * @param trackId Identifies the media track. ignore the invalid value is passed.
175      * @param lastPTS the latest PTS.
176      * @return Execution Status
177      */
178     virtual Status GetLastPTSByTrackId(uint32_t trackId, int64_t &lastPTS) = 0;
179 
180     /**
181      * @brief Seeks for a specified position for the demuxer.
182      *
183      * After being started, the demuxer seeks for a specified position to read data frames.
184      *
185      * The function is valid only after RUNNING state.
186      *
187      * @param trackId Identifies the stream in the media file.
188      * @param seekTime Indicates the target position, based on {@link HST_TIME_BASE} .
189      * @param mode Indicates the seek mode.
190      * @param realSeekTime Indicates the accurate target position, based on {@link HST_TIME_BASE} .
191      * @return  Execution Status return
192      *  @retval OK: Plugin SeekTo succeeded.
193      *  @retval ERROR_INVALID_DATA: The input data is invalid.
194      */
195     virtual Status SeekTo(int32_t trackId, int64_t seekTime, SeekMode mode, int64_t& realSeekTime) = 0;
196 
197     virtual Status Reset() = 0;
198 
199     virtual Status Start() = 0;
200 
201     virtual Status Stop() = 0;
202 
203     virtual Status Flush() = 0;
204 
205     virtual void ResetEosStatus() = 0;
206 
IsRefParserSupportedDemuxerPlugin207     virtual bool IsRefParserSupported() { return false; };
208     virtual Status ParserRefUpdatePos(int64_t timeStampMs, bool isForward = true) = 0;
209     virtual Status ParserRefInfo() = 0;
210     virtual Status GetFrameLayerInfo(std::shared_ptr<AVBuffer> videoSample, FrameLayerInfo &frameLayerInfo) = 0;
211     virtual Status GetFrameLayerInfo(uint32_t frameId, FrameLayerInfo &frameLayerInfo) = 0;
212     virtual Status GetGopLayerInfo(uint32_t gopId, GopLayerInfo &gopLayerInfo) = 0;
213     virtual Status GetIFramePos(std::vector<uint32_t> &IFramePos) = 0;
214     virtual Status Dts2FrameId(int64_t dts, uint32_t &frameId) = 0;
215     virtual Status SeekMs2FrameId(int64_t seekMs, uint32_t &frameId) = 0;
216     virtual Status FrameId2SeekMs(uint32_t frameId, int64_t &seekMs) = 0;
217 
GetDrmInfoDemuxerPlugin218     virtual Status GetDrmInfo(std::multimap<std::string, std::vector<uint8_t>>& drmInfo)
219     {
220         (void)drmInfo;
221         return Status::OK;
222     }
223 
SetInterruptStateDemuxerPlugin224     virtual void SetInterruptState(bool isInterruptNeeded) {}
225 
226     virtual Status GetIndexByRelativePresentationTimeUs(const uint32_t trackIndex,
227         const uint64_t relativePresentationTimeUs, uint32_t &index) = 0;
228 
229     virtual Status GetRelativePresentationTimeUsByIndex(const uint32_t trackIndex,
230         const uint32_t index, uint64_t &relativePresentationTimeUs) = 0;
231 
232     virtual void SetCacheLimit(uint32_t limitSize) = 0;
GetCurrentCacheSizeDemuxerPlugin233     virtual Status GetCurrentCacheSize(uint32_t trackId, uint32_t& size)
234     {
235         size = 0;
236         return Status::OK;
237     };
GetProbeSizeDemuxerPlugin238     virtual bool GetProbeSize(int32_t &offset, int32_t &size) { return false; };
239     virtual Status SetDataSourceWithProbSize(const std::shared_ptr<DataSource>& source,
240         const int32_t probSize) = 0;
241 
242     /**
243      * @brief Boosts the asynchronous read thread priority.
244      *
245      * Elevates thread priority if called with proper permissions and when thread is not RUNNING.
246      * By default, the thread operates at normal priority.
247      *
248      * @return Execution status
249      */
250     virtual Status BoostReadThreadPriority() = 0;
251 };
252 
253 /// Demuxer plugin api major number.
254 #define DEMUXER_API_VERSION_MAJOR (1)
255 
256 /// Demuxer plugin api minor number
257 #define DEMUXER_API_VERSION_MINOR (0)
258 
259 /// Demuxer plugin version
260 #define DEMUXER_API_VERSION MAKE_VERSION(DEMUXER_API_VERSION_MAJOR, DEMUXER_API_VERSION_MINOR)
261 
262 /**
263  * @brief Describes the demuxer plugin information.
264  *
265  * @since 1.0
266  * @version 1.0
267  */
268 struct DemuxerPluginDef : public PluginDefBase {
DemuxerPluginDefDemuxerPluginDef269     DemuxerPluginDef()
270         : PluginDefBase()
271     {
272         apiVersion = DEMUXER_API_VERSION; ///< Demuxer plugin version.
273         pluginType = PluginType::DEMUXER; ///< Plugin type, MUST be DEMUXER.
274     }
275 };
276 } // namespace Plugins
277 } // namespace Media
278 } // namespace OHOS
279 #endif // AVCODEC_DEMUXER_PLUGIN_H
280