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 
SelectBitRate(uint32_t bitRate)177     virtual Status SelectBitRate(uint32_t bitRate)
178     {
179         return Status::OK;
180     }
181 
IsSeekToTimeSupported()182     virtual bool IsSeekToTimeSupported()
183     {
184         return false;
185     }
186 
SeekToTime(int64_t seekTime,SeekMode mode)187     virtual Status SeekToTime(int64_t seekTime, SeekMode mode)
188     {
189         return Status::OK;
190     }
191 
GetDuration(int64_t & duration)192     virtual Status GetDuration(int64_t& duration)
193     {
194         duration = Plugins::HST_TIME_NONE;
195         return Status::OK;
196     }
197 
IsNeedPreDownload()198     virtual bool IsNeedPreDownload()
199     {
200         return false;
201     }
202 
SetReadBlockingFlag(bool isReadBlockingAllowed)203     virtual Status SetReadBlockingFlag(bool isReadBlockingAllowed)
204     {
205         return Status::OK;
206     }
207 
SetInterruptState(bool isInterruptNeeded)208     virtual void SetInterruptState(bool isInterruptNeeded) {}
209 
SetCurrentBitRate(int32_t bitRate,int32_t streamID)210     virtual Status SetCurrentBitRate(int32_t bitRate, int32_t streamID)
211     {
212         return Status::OK;
213     }
214 
GetStreamInfo(std::vector<StreamInfo> & streams)215     virtual Status GetStreamInfo(std::vector<StreamInfo>& streams)
216     {
217         return Status::OK;
218     }
219 
Pause()220     virtual Status Pause()
221     {
222         return Status::OK;
223     }
224 
Resume()225     virtual Status Resume()
226     {
227         return Status::OK;
228     }
SelectStream(int32_t streamID)229     virtual Status SelectStream(int32_t streamID)
230     {
231         return Status::OK;
232     }
SetEnableOnlineFdCache(bool isEnableFdCache)233     virtual void SetEnableOnlineFdCache(bool isEnableFdCache)
234     {
235         (void)isEnableFdCache;
236     }
237 
GetSegmentOffset()238     virtual size_t GetSegmentOffset()
239     {
240         return 0;
241     }
242 
GetHLSDiscontinuity()243     virtual bool GetHLSDiscontinuity()
244     {
245         return false;
246     }
247 
WaitForBufferingEnd()248     virtual void WaitForBufferingEnd() {}
249 };
250 
251 /// Source plugin api major number.
252 #define SOURCE_API_VERSION_MAJOR (1)
253 
254 /// Source plugin api minor number
255 #define SOURCE_API_VERSION_MINOR (0)
256 
257 /// Source plugin version
258 #define SOURCE_API_VERSION MAKE_VERSION(SOURCE_API_VERSION_MAJOR, SOURCE_API_VERSION_MINOR)
259 
260 /**
261  * @brief Describes the source plugin information.
262  *
263  * @since 1.0
264  * @version 1.0
265  */
266 struct SourcePluginDef : public PluginDefBase {
SourcePluginDefSourcePluginDef267     SourcePluginDef()
268         : PluginDefBase()
269     {
270         apiVersion = SOURCE_API_VERSION; ///< Source plugin version.
271         pluginType = PluginType::SOURCE; ///< Plugin type, MUST be SOURCE.
272     }
273 };
274 } // namespace Plugins
275 } // namespace Media
276 } // namespace OHOS
277 #endif // AVCODEC_SOURCE_PLUGIN_H
278