1/*
2 * Copyright (c) 2021-2021 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 HISTREAMER_PLUGIN_INTF_SOURCE_PLUGIN_H
17#define HISTREAMER_PLUGIN_INTF_SOURCE_PLUGIN_H
18
19#include <map>
20#include <string>
21#include "plugin/common/media_source.h"
22#include "plugin/common/plugin_caps.h"
23#include "plugin/common/plugin_source_tags.h"
24#include "plugin/interface/plugin_base.h"
25#include "plugin/interface/plugin_definition.h"
26
27namespace OHOS {
28namespace Media {
29namespace Plugin {
30/**
31 * @brief Source Plugin Interface.
32 *
33 * The data source may be network push or active read.
34 *
35 * @since 1.0
36 * @version 1.0
37 */
38struct SourcePlugin : public PluginBase {
39    /// constructor
40    explicit SourcePlugin(std::string name): PluginBase(std::move(name)) {}
41    /**
42     * @brief Set the data source to source plugin.
43     *
44     * The function is valid only in the CREATED state.
45     *
46     * @param source data source, uri or stream source
47     * @return  Execution status return
48     *  @retval OK: Plugin SetSource succeeded.
49     *  @retval ERROR_WRONG_STATE: Call this function in non wrong state
50     *  @retval ERROR_NOT_EXISTED: Uri is not existed.
51     *  @retval ERROR_UNSUPPORTED_FORMAT: Uri is not supported.
52     *  @retval ERROR_INVALID_PARAMETER: Uri is invalid.
53     */
54    virtual OHOS::Media::Plugin::Status SetSource(std::shared_ptr<MediaSource> source) = 0;
55
56    /**
57     * @brief Read data from data source.
58     *
59     * The function is valid only after RUNNING state.
60     *
61     * @param buffer Buffer to store the data, it can be nullptr or empty to get the buffer from plugin.
62     * @param expectedLen   Expected data size to be read
63     * @return  Execution status return
64     *  @retval OK: Plugin Read succeeded.
65     *  @retval ERROR_NOT_ENOUGH_DATA: Data not enough
66     *  @retval END_OF_STREAM: End of stream
67     */
68    virtual Status Read(std::shared_ptr<Buffer>& buffer, size_t expectedLen) = 0;
69
70    /**
71     * @brief Get data source size.
72     *
73     * The function is valid only after INITIALIZED state.
74     *
75     * @param size data source size.
76     * @return  Execution status return.
77     *  @retval OK: Plugin GetSize succeeded.
78     */
79    virtual Status GetSize(uint64_t& size) = 0;
80
81    /**
82     * @brief Indicates that the current source can be seek.
83     *
84     * The function is valid only after INITIALIZED state.
85     *
86     * @return  Execution status return
87     *  @retval OK: Plugin GetSeekable succeeded.
88     */
89    virtual Seekable GetSeekable() = 0;
90
91    /**
92     * @brief Seeks for a specified position for the source.
93     *
94     * After being started, the source seeks for a specified position to read data frames.
95     *
96     * The function is valid only after RUNNING state.
97     *
98     * @param offset position to read data frames
99     * @return  Execution status return
100     *  @retval OK: Plugin SeekTo succeeded.
101     *  @retval ERROR_INVALID_DATA: The offset is invalid.
102     */
103    virtual Status SeekToPos(int64_t offset)
104    {
105        return Status::ERROR_UNIMPLEMENTED;
106    }
107
108    /**
109     * @brief seek to pos by time
110     * @return Execution status return
111     */
112    virtual Status SeekToTime(int64_t offset)
113    {
114        return Status::ERROR_UNIMPLEMENTED;
115    }
116
117    /**
118     * @brief Get duration from current source
119     * @return Execution status return
120     */
121    virtual Status GetDuration(int64_t& duration)
122    {
123        return Status::ERROR_UNIMPLEMENTED;
124    }
125
126    /**
127     * @brief Get bitrates that current source supports
128     * @return Execution status return
129    */
130    virtual Status GetBitRates(std::vector<uint32_t>& bitRates)
131    {
132        return Status::ERROR_UNIMPLEMENTED;
133    }
134
135    /**
136     * @brief Select bitrate for current source
137     * @param bitRate user selects a bitrate
138     * @return Execution status return
139    */
140    virtual Status SelectBitRate(uint32_t bitRate)
141    {
142        return Status::ERROR_UNIMPLEMENTED;
143    }
144};
145
146/// Source plugin api major number.
147#define SOURCE_API_VERSION_MAJOR (1)
148
149/// Source plugin api minor number
150#define SOURCE_API_VERSION_MINOR (0)
151
152/// Source plugin version
153#define SOURCE_API_VERSION MAKE_VERSION(SOURCE_API_VERSION_MAJOR, SOURCE_API_VERSION_MINOR)
154
155/**
156 * @brief Describes the source plugin information.
157 *
158 * @since 1.0
159 * @version 1.0
160 */
161struct SourcePluginDef : public PluginDefBase {
162    std::vector<ProtocolType> protocol;      ///< Protocols supported by playback source
163    SrcInputType inputType;                   ///< Input type supported by record source
164    CapabilitySet outCaps;                   ///< Plug-in output capability, For details, @see Capability.
165    PluginCreatorFunc<SourcePlugin> creator {nullptr}; ///< Source plugin create function.
166    SourcePluginDef()
167    {
168        apiVersion = SOURCE_API_VERSION; ///< Source plugin version.
169        pluginType = PluginType::SOURCE; ///< Plugin type, MUST be SOURCE.
170    }
171};
172} // namespace Plugin
173} // namespace Media
174} // namespace OHOS
175#endif // HISTREAMER_PLUGIN_INTF_SOURCE_PLUGIN_H
176