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
30namespace OHOS {
31namespace Media {
32namespace Plugins {
33
34enum StreamType {
35    MIXED = 0,
36    VIDEO,
37    AUDIO,
38    SUBTITLE
39};
40
41enum VideoType {
42    VIDEO_TYPE_SDR = 0,
43    VIDEO_TYPE_HDR_VIVID = 1,
44    VIDEO_TYPE_HDR_10
45};
46
47class StreamInfo {
48public:
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 */
68class SourcePlugin : public PluginBase {
69    /// constructor
70public:
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     */
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
156    virtual void SetDemuxerState(int32_t streamId) {}
157
158    virtual void SetDownloadErrorState() {}
159
160    virtual void SetBundleName(const std::string& bundleName) {}
161
162    virtual Status GetDownloadInfo(DownloadInfo& downloadInfo)
163    {
164        return Status::OK;
165    }
166
167    virtual Status GetPlaybackInfo(PlaybackInfo& playbackInfo)
168    {
169        return Status::OK;
170    }
171
172    virtual Status GetBitRates(std::vector<uint32_t>& bitRates)
173    {
174        return Status::OK;
175    }
176
177    virtual Status SelectBitRate(uint32_t bitRate)
178    {
179        return Status::OK;
180    }
181
182    virtual bool IsSeekToTimeSupported()
183    {
184        return false;
185    }
186
187    virtual Status SeekToTime(int64_t seekTime, SeekMode mode)
188    {
189        return Status::OK;
190    }
191
192    virtual Status GetDuration(int64_t& duration)
193    {
194        duration = Plugins::HST_TIME_NONE;
195        return Status::OK;
196    }
197
198    virtual bool IsNeedPreDownload()
199    {
200        return false;
201    }
202
203    virtual Status SetReadBlockingFlag(bool isReadBlockingAllowed)
204    {
205        return Status::OK;
206    }
207
208    virtual void SetInterruptState(bool isInterruptNeeded) {}
209
210    virtual Status SetCurrentBitRate(int32_t bitRate, int32_t streamID)
211    {
212        return Status::OK;
213    }
214
215    virtual Status GetStreamInfo(std::vector<StreamInfo>& streams)
216    {
217        return Status::OK;
218    }
219
220    virtual Status SelectStream(int32_t streamID)
221    {
222        return Status::OK;
223    }
224
225    virtual Status Pause()
226    {
227        return Status::OK;
228    }
229
230    virtual Status Resume()
231    {
232        return Status::OK;
233    }
234
235    virtual void SetEnableOnlineFdCache(bool isEnableFdCache)
236    {
237        (void)isEnableFdCache;
238    }
239
240    virtual size_t GetSegmentOffset()
241    {
242        return 0;
243    }
244
245    virtual bool GetHLSDiscontinuity()
246    {
247        return false;
248    }
249
250    virtual Status StopBufferring(bool isAppBackground)
251    {
252        return Status::OK;
253    }
254
255    virtual void WaitForBufferingEnd() {}
256};
257
258/// Source plugin api major number.
259#define SOURCE_API_VERSION_MAJOR (1)
260
261/// Source plugin api minor number
262#define SOURCE_API_VERSION_MINOR (0)
263
264/// Source plugin version
265#define SOURCE_API_VERSION MAKE_VERSION(SOURCE_API_VERSION_MAJOR, SOURCE_API_VERSION_MINOR)
266
267/**
268 * @brief Describes the source plugin information.
269 *
270 * @since 1.0
271 * @version 1.0
272 */
273struct SourcePluginDef : public PluginDefBase {
274    SourcePluginDef()
275        : PluginDefBase()
276    {
277        apiVersion = SOURCE_API_VERSION; ///< Source plugin version.
278        pluginType = PluginType::SOURCE; ///< Plugin type, MUST be SOURCE.
279    }
280};
281} // namespace Plugins
282} // namespace Media
283} // namespace OHOS
284#endif // AVCODEC_SOURCE_PLUGIN_H
285