• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 AVCODEC_SOURCE_PLUGIN_H
17 #define AVCODEC_SOURCE_PLUGIN_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "common/media_source.h"
23 #include "plugin/plugin_base.h"
24 #include "plugin/plugin_buffer.h"
25 #include "plugin/plugin_caps.h"
26 #include "plugin/plugin_definition.h"
27 #include "meta/media_types.h"
28 #include "plugin/plugin_time.h"
29 
30 namespace OHOS {
31 namespace Media {
32 namespace Plugins {
33 
34 enum StreamType {
35     MIXED = 0,
36     VIDEO,
37     AUDIO,
38     SUBTITLE
39 };
40 
41 enum VideoType {
42     VIDEO_TYPE_SDR = 0,
43     VIDEO_TYPE_HDR_VIVID = 1,
44     VIDEO_TYPE_HDR_10
45 };
46 
47 class StreamInfo {
48 public:
49     int32_t streamId;
50     StreamType type;
51     uint32_t bitRate;
52 
53     int32_t videoHeight = 0;
54     int32_t videoWidth = 0;
55     std::string lang = "";
56     VideoType videoType = VideoType::VIDEO_TYPE_SDR;
57     std::string trackName = "";
58 };
59 
60 /**
61  * @brief Source Plugin Interface.
62  *
63  * The data source may be network push or active read.
64  *
65  * @since 1.0
66  * @version 1.0
67  */
68 class SourcePlugin : public PluginBase {
69     /// constructor
70 public:
SourcePlugin(std::string name)71     explicit SourcePlugin(std::string name): PluginBase(std::move(name)) {}
72     /**
73      * @brief Set the data source to source plugin.
74      *
75      * The function is valid only in the CREATED state.
76      *
77      * @param source data source, uri or stream source
78      * @return  Execution status return
79      *  @retval OK: Plugin SetSource succeeded.
80      *  @retval ERROR_WRONG_STATE: Call this function in non wrong state
81      *  @retval ERROR_NOT_EXISTED: Uri is not existed.
82      *  @retval ERROR_UNSUPPORTED_FORMAT: Uri is not supported.
83      *  @retval ERROR_INVALID_PARAMETER: Uri is invalid.
84      */
85     virtual Status SetSource(std::shared_ptr<MediaSource> source) = 0;
86 
87     /**
88      * @brief Read data from data source.
89      *
90      * The function is valid only after RUNNING state.
91      *
92      * @param buffer Buffer to store the data, it can be nullptr or empty to get the buffer from plugin.
93      * @param expectedLen   Expected data size to be read
94      * @return  Execution status return
95      *  @retval OK: Plugin Read succeeded.
96      *  @retval ERROR_NOT_ENOUGH_DATA: Data not enough
97      *  @retval END_OF_STREAM: End of stream
98      */
99     virtual Status Read(std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen) = 0;
100 
101     /**
102      * @brief Read data from data source.
103      *
104      * The function is valid only after RUNNING state.
105      *
106      * @param streamId stream index.
107      * @param buffer Buffer to store the data, it can be nullptr or empty to get the buffer from plugin.
108      * @param expectedLen   Expected data size to be read
109      * @return  Execution status return
110      *  @retval OK: Plugin Read succeeded.
111      *  @retval ERROR_NOT_ENOUGH_DATA: Data not enough
112      *  @retval END_OF_STREAM: End of stream
113      */
Read(int32_t streamId,std::shared_ptr<Buffer> & buffer,uint64_t offset,size_t expectedLen)114     virtual Status Read(int32_t streamId, std::shared_ptr<Buffer>& buffer, uint64_t offset, size_t expectedLen)
115     {
116         return Status::OK;
117     }
118 
119     /**
120      * @brief Get data source size.
121      *
122      * The function is valid only after INITIALIZED state.
123      *
124      * @param size data source size.
125      * @return  Execution status return.
126      *  @retval OK: Plugin GetSize succeeded.
127      */
128     virtual Status GetSize(uint64_t& size) = 0;
129 
130     /**
131      * @brief Indicates that the current source can be seek.
132      *
133      * The function is valid only after INITIALIZED state.
134      *
135      * @return  Execution status return
136      *  @retval OK: Plugin GetSeekable succeeded.
137      */
138     virtual Seekable GetSeekable() = 0;
139 
140     /**
141      * @brief Seeks for a specified position for the source.
142      *
143      * After being started, the source seeks for a specified position to read data frames.
144      *
145      * The function is valid only after RUNNING state.
146      *
147      * @param offset position to read data frames
148      * @return  Execution status return
149      *  @retval OK: Plugin SeekTo succeeded.
150      *  @retval ERROR_INVALID_DATA: The offset is invalid.
151      */
152     virtual Status SeekTo(uint64_t offset) = 0;
153 
154     virtual Status Reset() = 0;
155 
SetDemuxerState(int32_t streamId)156     virtual void SetDemuxerState(int32_t streamId) {}
157 
SetDownloadErrorState()158     virtual void SetDownloadErrorState() {}
159 
SetBundleName(const std::string & bundleName)160     virtual void SetBundleName(const std::string& bundleName) {}
161 
GetDownloadInfo(DownloadInfo & downloadInfo)162     virtual Status GetDownloadInfo(DownloadInfo& downloadInfo)
163     {
164         return Status::OK;
165     }
166 
GetPlaybackInfo(PlaybackInfo & playbackInfo)167     virtual Status GetPlaybackInfo(PlaybackInfo& playbackInfo)
168     {
169         return Status::OK;
170     }
171 
GetBitRates(std::vector<uint32_t> & bitRates)172     virtual Status GetBitRates(std::vector<uint32_t>& bitRates)
173     {
174         return Status::OK;
175     }
176 
SetStartPts(int64_t startPts)177     virtual Status SetStartPts(int64_t startPts)
178     {
179         return Status::OK;
180     }
181 
SetExtraCache(uint64_t cacheDuration)182     virtual Status SetExtraCache(uint64_t cacheDuration)
183     {
184         return Status::OK;
185     }
186 
SelectBitRate(uint32_t bitRate)187     virtual Status SelectBitRate(uint32_t bitRate)
188     {
189         return Status::OK;
190     }
191 
IsSeekToTimeSupported()192     virtual bool IsSeekToTimeSupported()
193     {
194         return false;
195     }
196 
SeekToTime(int64_t seekTime,SeekMode mode)197     virtual Status SeekToTime(int64_t seekTime, SeekMode mode)
198     {
199         return Status::OK;
200     }
201 
GetDuration(int64_t & duration)202     virtual Status GetDuration(int64_t& duration)
203     {
204         duration = Plugins::HST_TIME_NONE;
205         return Status::OK;
206     }
207 
IsNeedPreDownload()208     virtual bool IsNeedPreDownload()
209     {
210         return false;
211     }
212 
SetReadBlockingFlag(bool isReadBlockingAllowed)213     virtual Status SetReadBlockingFlag(bool isReadBlockingAllowed)
214     {
215         return Status::OK;
216     }
217 
SetInterruptState(bool isInterruptNeeded)218     virtual void SetInterruptState(bool isInterruptNeeded) {}
219 
SetCurrentBitRate(int32_t bitRate,int32_t streamID)220     virtual Status SetCurrentBitRate(int32_t bitRate, int32_t streamID)
221     {
222         return Status::OK;
223     }
224 
GetStreamInfo(std::vector<StreamInfo> & streams)225     virtual Status GetStreamInfo(std::vector<StreamInfo>& streams)
226     {
227         return Status::OK;
228     }
229 
SelectStream(int32_t streamID)230     virtual Status SelectStream(int32_t streamID)
231     {
232         return Status::OK;
233     }
234 
Pause()235     virtual Status Pause()
236     {
237         return Status::OK;
238     }
239 
Resume()240     virtual Status Resume()
241     {
242         return Status::OK;
243     }
244 
SetEnableOnlineFdCache(bool isEnableFdCache)245     virtual void SetEnableOnlineFdCache(bool isEnableFdCache)
246     {
247         (void)isEnableFdCache;
248     }
249 
GetSegmentOffset()250     virtual size_t GetSegmentOffset()
251     {
252         return 0;
253     }
254 
GetHLSDiscontinuity()255     virtual bool GetHLSDiscontinuity()
256     {
257         return false;
258     }
259 
StopBufferring(bool isAppBackground)260     virtual Status StopBufferring(bool isAppBackground)
261     {
262         return Status::OK;
263     }
264 
WaitForBufferingEnd()265     virtual void WaitForBufferingEnd() {}
SetSourceInitialBufferSize(int32_t offset,int32_t size)266     virtual bool SetSourceInitialBufferSize(int32_t offset, int32_t size)
267     {
268         return false;
269     }
270 
NotifyInitSuccess()271     virtual void NotifyInitSuccess() {}
272 
IsLocalFd()273     virtual bool IsLocalFd()
274     {
275         return false;
276     }
GetCachedDuration()277     virtual uint64_t GetCachedDuration()
278     {
279         return 0;
280     }
RestartAndClearBuffer()281     virtual void RestartAndClearBuffer() {}
IsFlvLive()282     virtual bool IsFlvLive()
283     {
284         return false;
285     }
IsHlsFmp4()286     virtual bool IsHlsFmp4()
287     {
288         return false;
289     }
290 };
291 
292 /// Source plugin api major number.
293 #define SOURCE_API_VERSION_MAJOR (1)
294 
295 /// Source plugin api minor number
296 #define SOURCE_API_VERSION_MINOR (0)
297 
298 /// Source plugin version
299 #define SOURCE_API_VERSION MAKE_VERSION(SOURCE_API_VERSION_MAJOR, SOURCE_API_VERSION_MINOR)
300 
301 /**
302  * @brief Describes the source plugin information.
303  *
304  * @since 1.0
305  * @version 1.0
306  */
307 struct SourcePluginDef : public PluginDefBase {
SourcePluginDefSourcePluginDef308     SourcePluginDef()
309         : PluginDefBase()
310     {
311         apiVersion = SOURCE_API_VERSION; ///< Source plugin version.
312         pluginType = PluginType::SOURCE; ///< Plugin type, MUST be SOURCE.
313     }
314 };
315 } // namespace Plugins
316 } // namespace Media
317 } // namespace OHOS
318 #endif // AVCODEC_SOURCE_PLUGIN_H
319