1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#ifndef HISTREAMER_PLUGIN_INTF_PLUGIN_DEFINITION_H
17fa7767c5Sopenharmony_ci#define HISTREAMER_PLUGIN_INTF_PLUGIN_DEFINITION_H
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_ci#include <functional>
20fa7767c5Sopenharmony_ci#include <string>
21fa7767c5Sopenharmony_ci#include <memory>
22fa7767c5Sopenharmony_ci#include "plugin_caps.h"
23fa7767c5Sopenharmony_ci#include "plugin_base.h"
24fa7767c5Sopenharmony_ci#include "plugin/plugin_buffer.h"
25fa7767c5Sopenharmony_ci
26fa7767c5Sopenharmony_cinamespace OHOS {
27fa7767c5Sopenharmony_cinamespace Media {
28fa7767c5Sopenharmony_cinamespace Plugins {
29fa7767c5Sopenharmony_ci/**
30fa7767c5Sopenharmony_ci * @brief Macro definition, creating the version information.
31fa7767c5Sopenharmony_ci *
32fa7767c5Sopenharmony_ci * @details The versioning is the process of assigning a unique version number to a unique state
33fa7767c5Sopenharmony_ci * of plugin interface. Within a given version number category (major, minor), these numbers are
34fa7767c5Sopenharmony_ci * usually assigned in ascending order and correspond to new developments in the plugin.
35fa7767c5Sopenharmony_ci *
36fa7767c5Sopenharmony_ci * Given a version number MAJOR.MINOR:
37fa7767c5Sopenharmony_ci *  - MAJOR: When you make incompatible API changes.
38fa7767c5Sopenharmony_ci *  - MINOR: When you add features in a backwards-compatible manner or do backwards-compatible bug fixes.
39fa7767c5Sopenharmony_ci */
40fa7767c5Sopenharmony_ci#define MAKE_VERSION(MAJOR, MINOR) ((((MAJOR)&0xFFFF) << 16) | ((MINOR)&0xFFFF))
41fa7767c5Sopenharmony_ci
42fa7767c5Sopenharmony_ci/// Plugin interface major number
43fa7767c5Sopenharmony_ci#define PLUGIN_INTERFACE_VERSION_MAJOR (1)
44fa7767c5Sopenharmony_ci
45fa7767c5Sopenharmony_ci/// Plugin interface minor number
46fa7767c5Sopenharmony_ci#define PLUGIN_INTERFACE_VERSION_MINOR (0)
47fa7767c5Sopenharmony_ci
48fa7767c5Sopenharmony_ci/// Plugin interface version
49fa7767c5Sopenharmony_ci#define PLUGIN_INTERFACE_VERSION MAKE_VERSION(PLUGIN_INTERFACE_VERSION_MAJOR, PLUGIN_INTERFACE_VERSION_MINOR)
50fa7767c5Sopenharmony_ci
51fa7767c5Sopenharmony_ci/**
52fa7767c5Sopenharmony_ci * @enum License Type.
53fa7767c5Sopenharmony_ci * an official permission or permit.
54fa7767c5Sopenharmony_ci *
55fa7767c5Sopenharmony_ci * @since 1.0
56fa7767c5Sopenharmony_ci * @version 1.0
57fa7767c5Sopenharmony_ci */
58fa7767c5Sopenharmony_cienum struct LicenseType : uint8_t {
59fa7767c5Sopenharmony_ci    APACHE_V2, ///< The Apache License 2.0
60fa7767c5Sopenharmony_ci    LGPL,      ///< The GNU Lesser General Public License
61fa7767c5Sopenharmony_ci    GPL,       ///< The GNU General Public License
62fa7767c5Sopenharmony_ci    CC0,       ///< The Creative Commons Zero v1.0 Universal
63fa7767c5Sopenharmony_ci    VENDOR,    ///< Offered by Vendor
64fa7767c5Sopenharmony_ci    UNKNOWN,   ///< Unknown License
65fa7767c5Sopenharmony_ci};
66fa7767c5Sopenharmony_ci
67fa7767c5Sopenharmony_ci/**
68fa7767c5Sopenharmony_ci * @brief Definition of plugin packaging information.
69fa7767c5Sopenharmony_ci *
70fa7767c5Sopenharmony_ci * @since 1.0
71fa7767c5Sopenharmony_ci * @version 1.0
72fa7767c5Sopenharmony_ci */
73fa7767c5Sopenharmony_cistruct PackageDef {
74fa7767c5Sopenharmony_ci    uint32_t pkgVersion;    ///< Package information version, which indicates the latest plug-in interface version
75fa7767c5Sopenharmony_ci                            ///< used by the plugin in the package. The default value is PLUGIN_INTERFACE_VERSION.
76fa7767c5Sopenharmony_ci
77fa7767c5Sopenharmony_ci    std::string name;   ///< Package name. The plugin framework registers the plugin using this name.
78fa7767c5Sopenharmony_ci                        ///< If the plugins are packaged as a dynamic library, the name of library
79fa7767c5Sopenharmony_ci                        ///< must be in the format of "libplugin_<name>.so".
80fa7767c5Sopenharmony_ci
81fa7767c5Sopenharmony_ci    LicenseType
82fa7767c5Sopenharmony_ci        licenseType;    ///< The License information of the plugin in the package.
83fa7767c5Sopenharmony_ci                        ///< The different plugins must be the same.
84fa7767c5Sopenharmony_ci                        ///< The plugin framework processing in the plugin running state based on different license.
85fa7767c5Sopenharmony_ci};
86fa7767c5Sopenharmony_ci
87fa7767c5Sopenharmony_ci/**
88fa7767c5Sopenharmony_ci * @brief Data source operation interface.
89fa7767c5Sopenharmony_ci *
90fa7767c5Sopenharmony_ci * @since 1.0
91fa7767c5Sopenharmony_ci * @version 1.0
92fa7767c5Sopenharmony_ci */
93fa7767c5Sopenharmony_cistruct DataSource {
94fa7767c5Sopenharmony_ci    /// Destructor
95fa7767c5Sopenharmony_ci    virtual ~DataSource() = default;
96fa7767c5Sopenharmony_ci
97fa7767c5Sopenharmony_ci    /**
98fa7767c5Sopenharmony_ci     * @brief Read data from data source.
99fa7767c5Sopenharmony_ci     *
100fa7767c5Sopenharmony_ci     * @param offset    Offset of read position
101fa7767c5Sopenharmony_ci     * @param buffer    Storage of the read data
102fa7767c5Sopenharmony_ci     * @param expectedLen   Expected data size to be read
103fa7767c5Sopenharmony_ci     * @return  Execution status return
104fa7767c5Sopenharmony_ci     *  @retval OK: Plugin ReadAt succeeded.
105fa7767c5Sopenharmony_ci     *  @retval ERROR_NOT_ENOUGH_DATA: Data not enough
106fa7767c5Sopenharmony_ci     *  @retval END_OF_STREAM: End of stream
107fa7767c5Sopenharmony_ci     */
108fa7767c5Sopenharmony_ci    virtual Status ReadAt(int64_t offset, std::shared_ptr<Buffer>& buffer, size_t expectedLen) = 0;
109fa7767c5Sopenharmony_ci
110fa7767c5Sopenharmony_ci    /**
111fa7767c5Sopenharmony_ci     * @brief Get data source size.
112fa7767c5Sopenharmony_ci     *
113fa7767c5Sopenharmony_ci     * @param size data source size.
114fa7767c5Sopenharmony_ci     * @return  Execution status return.
115fa7767c5Sopenharmony_ci     *  @retval OK: Plugin GetSize succeeded.
116fa7767c5Sopenharmony_ci     */
117fa7767c5Sopenharmony_ci    virtual Status GetSize(uint64_t& size) = 0;
118fa7767c5Sopenharmony_ci
119fa7767c5Sopenharmony_ci    /**
120fa7767c5Sopenharmony_ci     * @brief Indicates that the current data source seekable or not.
121fa7767c5Sopenharmony_ci     *
122fa7767c5Sopenharmony_ci     * The function is valid only after INITIALIZED state.
123fa7767c5Sopenharmony_ci     *
124fa7767c5Sopenharmony_ci     * @return  Seekable status
125fa7767c5Sopenharmony_ci     */
126fa7767c5Sopenharmony_ci    virtual Seekable GetSeekable() = 0;
127fa7767c5Sopenharmony_ci
128fa7767c5Sopenharmony_ci    virtual int32_t GetStreamID() = 0;
129fa7767c5Sopenharmony_ci
130fa7767c5Sopenharmony_ci    virtual bool IsDash() = 0;
131fa7767c5Sopenharmony_ci};
132fa7767c5Sopenharmony_ci
133fa7767c5Sopenharmony_ci/// Plugin create function. All plugins must implement this function.
134fa7767c5Sopenharmony_citemplate <typename T>
135fa7767c5Sopenharmony_ciusing PluginCreatorFunc = std::function<std::shared_ptr<T>(const std::string& name)>;
136fa7767c5Sopenharmony_ci
137fa7767c5Sopenharmony_ci/// Sniff function
138fa7767c5Sopenharmony_ciusing PluginSnifferFunc = int (*)(const std::string& name, std::shared_ptr<DataSource> dataSource);
139fa7767c5Sopenharmony_ci
140fa7767c5Sopenharmony_ci/**
141fa7767c5Sopenharmony_ci * @brief Describes the basic information about the plugin.
142fa7767c5Sopenharmony_ci *
143fa7767c5Sopenharmony_ci * @since 1.0
144fa7767c5Sopenharmony_ci * @version 1.0
145fa7767c5Sopenharmony_ci */
146fa7767c5Sopenharmony_cistruct PluginDefBase {
147fa7767c5Sopenharmony_ci    uint32_t apiVersion{0}; ///< Versions of different plugins. Different types of plugin have their own versions.
148fa7767c5Sopenharmony_ci
149fa7767c5Sopenharmony_ci    PluginType pluginType = PluginType::INVALID_TYPE; ///< Describe the plugin type, e.g. 'source', 'codec'.
150fa7767c5Sopenharmony_ci
151fa7767c5Sopenharmony_ci    std::string name;   ///< Indicates the name of a plugin. The name of the same type plugins must be unique.
152fa7767c5Sopenharmony_ci                        ///< Plugins with the same name may fail to be registered.
153fa7767c5Sopenharmony_ci
154fa7767c5Sopenharmony_ci    std::string description; ///< Detailed description of the plugin.
155fa7767c5Sopenharmony_ci
156fa7767c5Sopenharmony_ci    uint32_t rank{0};  ///< Plugin score. The plugin with a high score may be preferred. You can evaluate the
157fa7767c5Sopenharmony_ci                    ///< plugin score in terms of performance, version support, and license. Range: 0 to 100.
158fa7767c5Sopenharmony_ci
159fa7767c5Sopenharmony_ci    PluginDefBase()
160fa7767c5Sopenharmony_ci    {
161fa7767c5Sopenharmony_ci        pluginType = PluginType::INVALID_TYPE;
162fa7767c5Sopenharmony_ci    }
163fa7767c5Sopenharmony_ci
164fa7767c5Sopenharmony_ci    virtual ~PluginDefBase() {}
165fa7767c5Sopenharmony_ci
166fa7767c5Sopenharmony_ci    virtual void AddInCaps(Capability& capability)
167fa7767c5Sopenharmony_ci    {
168fa7767c5Sopenharmony_ci        inCaps.emplace_back(capability);
169fa7767c5Sopenharmony_ci    }
170fa7767c5Sopenharmony_ci
171fa7767c5Sopenharmony_ci    virtual void AddOutCaps(Capability& capability)
172fa7767c5Sopenharmony_ci    {
173fa7767c5Sopenharmony_ci        outCaps.emplace_back(capability);
174fa7767c5Sopenharmony_ci    }
175fa7767c5Sopenharmony_ci
176fa7767c5Sopenharmony_ci    virtual void AddExtensions(std::vector<std::string> ex)
177fa7767c5Sopenharmony_ci    {
178fa7767c5Sopenharmony_ci        auto iter = ex.begin();
179fa7767c5Sopenharmony_ci        while (iter != ex.end()) {
180fa7767c5Sopenharmony_ci            extensions.emplace_back(*iter);
181fa7767c5Sopenharmony_ci            iter++;
182fa7767c5Sopenharmony_ci        }
183fa7767c5Sopenharmony_ci    }
184fa7767c5Sopenharmony_ci
185fa7767c5Sopenharmony_ci    virtual std::vector<std::string> GetExtensions() const
186fa7767c5Sopenharmony_ci    {
187fa7767c5Sopenharmony_ci        return extensions;
188fa7767c5Sopenharmony_ci    }
189fa7767c5Sopenharmony_ci
190fa7767c5Sopenharmony_ci    virtual CapabilitySet GetInCaps() const
191fa7767c5Sopenharmony_ci    {
192fa7767c5Sopenharmony_ci        return inCaps;
193fa7767c5Sopenharmony_ci    }
194fa7767c5Sopenharmony_ci
195fa7767c5Sopenharmony_ci    virtual CapabilitySet GetOutCaps() const
196fa7767c5Sopenharmony_ci    {
197fa7767c5Sopenharmony_ci        return outCaps;
198fa7767c5Sopenharmony_ci    }
199fa7767c5Sopenharmony_ci
200fa7767c5Sopenharmony_ci    virtual PluginCreatorFunc<PluginBase> GetCreator() const
201fa7767c5Sopenharmony_ci    {
202fa7767c5Sopenharmony_ci        return creator;
203fa7767c5Sopenharmony_ci    }
204fa7767c5Sopenharmony_ci
205fa7767c5Sopenharmony_ci    virtual void SetCreator(PluginCreatorFunc<PluginBase> creatorFunc)
206fa7767c5Sopenharmony_ci    {
207fa7767c5Sopenharmony_ci        creator = creatorFunc;
208fa7767c5Sopenharmony_ci    }
209fa7767c5Sopenharmony_ci
210fa7767c5Sopenharmony_ci    virtual PluginSnifferFunc GetSniffer() const
211fa7767c5Sopenharmony_ci    {
212fa7767c5Sopenharmony_ci        return sniffer;
213fa7767c5Sopenharmony_ci    }
214fa7767c5Sopenharmony_ci
215fa7767c5Sopenharmony_ci    virtual void SetSniffer(PluginSnifferFunc sniffFunc)
216fa7767c5Sopenharmony_ci    {
217fa7767c5Sopenharmony_ci        sniffer = sniffFunc;
218fa7767c5Sopenharmony_ci    }
219fa7767c5Sopenharmony_ci
220fa7767c5Sopenharmony_ciprivate:
221fa7767c5Sopenharmony_ci    std::vector<std::string> extensions;      ///< File extensions
222fa7767c5Sopenharmony_ci    CapabilitySet inCaps;                     ///< Plug-in input capability, For details, @see Capability.
223fa7767c5Sopenharmony_ci    CapabilitySet outCaps;                    ///< Plug-in output capability, For details, @see Capability.
224fa7767c5Sopenharmony_ci    PluginCreatorFunc<PluginBase> creator {nullptr}; ///< plugin create function.
225fa7767c5Sopenharmony_ci    PluginSnifferFunc sniffer {nullptr};         ///< plugin sniff function.
226fa7767c5Sopenharmony_ci};
227fa7767c5Sopenharmony_ci
228fa7767c5Sopenharmony_ci/**
229fa7767c5Sopenharmony_ci * @brief The plugin registration interface.
230fa7767c5Sopenharmony_ci * The plugin framework will provide the implementation.
231fa7767c5Sopenharmony_ci * Developers only need to invoke the API to register the plugin.
232fa7767c5Sopenharmony_ci *
233fa7767c5Sopenharmony_ci * @since 1.0
234fa7767c5Sopenharmony_ci * @version 1.0
235fa7767c5Sopenharmony_ci */
236fa7767c5Sopenharmony_cistruct Register {
237fa7767c5Sopenharmony_ci    virtual ~Register() = default;
238fa7767c5Sopenharmony_ci    /**
239fa7767c5Sopenharmony_ci     * @brief Register the plugin.
240fa7767c5Sopenharmony_ci     *
241fa7767c5Sopenharmony_ci     * @param def   Basic information about the plugin
242fa7767c5Sopenharmony_ci     * @return  Registration status return
243fa7767c5Sopenharmony_ci     *  @retval OK: The plugin is registered succeed.
244fa7767c5Sopenharmony_ci     *  @retval ERROR_PLUGIN_ALREADY_EXISTS: The plugin already exists in plugin registered.
245fa7767c5Sopenharmony_ci     *  @retval ERROR_INCOMPATIBLE_VERSION: Incompatible version during plugin registration.
246fa7767c5Sopenharmony_ci     */
247fa7767c5Sopenharmony_ci    virtual Status AddPlugin(const PluginDefBase& def) = 0;
248fa7767c5Sopenharmony_ci};
249fa7767c5Sopenharmony_ci
250fa7767c5Sopenharmony_ci/**
251fa7767c5Sopenharmony_ci * @brief The package registration interface.
252fa7767c5Sopenharmony_ci * The plugin framework will provide the implementation and auto invoke the API to
253fa7767c5Sopenharmony_ci * finish the package registration when plugin framework first time be initialized.
254fa7767c5Sopenharmony_ci *
255fa7767c5Sopenharmony_ci * @since 1.0
256fa7767c5Sopenharmony_ci * @version 1.0
257fa7767c5Sopenharmony_ci */
258fa7767c5Sopenharmony_cistruct PackageRegister : Register {
259fa7767c5Sopenharmony_ci    ~PackageRegister() override = default;
260fa7767c5Sopenharmony_ci
261fa7767c5Sopenharmony_ci    /**
262fa7767c5Sopenharmony_ci     * @brief Register the package.
263fa7767c5Sopenharmony_ci     * During package registration, all plugins in the package are automatically registered.
264fa7767c5Sopenharmony_ci     *
265fa7767c5Sopenharmony_ci     * @param def   plugin packaging information.
266fa7767c5Sopenharmony_ci     * @return  Registration status return
267fa7767c5Sopenharmony_ci     *  @retval OK: The package is registered succeed without any errors.
268fa7767c5Sopenharmony_ci     *  @retval ERROR_PLUGIN_ALREADY_EXISTS: The package or plugins already exists.
269fa7767c5Sopenharmony_ci     *  @retval ERROR_INCOMPATIBLE_VERSION: Incompatible plugin interface version or api version.
270fa7767c5Sopenharmony_ci     */
271fa7767c5Sopenharmony_ci    virtual Status AddPackage(const PackageDef& def) = 0;
272fa7767c5Sopenharmony_ci};
273fa7767c5Sopenharmony_ci
274fa7767c5Sopenharmony_ci/// Plugin registration function, all plugins must be implemented.
275fa7767c5Sopenharmony_ciusing RegisterFunc = Status (*)(const std::shared_ptr<PackageRegister>& reg);
276fa7767c5Sopenharmony_ci
277fa7767c5Sopenharmony_ci/// Plugin deregister function, all plugins must be implemented.
278fa7767c5Sopenharmony_ciusing UnregisterFunc = void (*)();
279fa7767c5Sopenharmony_ci
280fa7767c5Sopenharmony_ci#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
281fa7767c5Sopenharmony_ci#define PLUGIN_EXPORT extern "C" __declspec(dllexport)
282fa7767c5Sopenharmony_ci#else
283fa7767c5Sopenharmony_ci#if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
284fa7767c5Sopenharmony_ci#define PLUGIN_EXPORT extern "C" __attribute__((visibility("default")))
285fa7767c5Sopenharmony_ci#else
286fa7767c5Sopenharmony_ci#define PLUGIN_EXPORT
287fa7767c5Sopenharmony_ci#endif
288fa7767c5Sopenharmony_ci#endif
289fa7767c5Sopenharmony_ci
290fa7767c5Sopenharmony_ci/// Macro definition, string concatenation
291fa7767c5Sopenharmony_ci#define PLUGIN_PASTE_ARGS(str1, str2) str1##str2
292fa7767c5Sopenharmony_ci
293fa7767c5Sopenharmony_ci/// Macro definition, string concatenation
294fa7767c5Sopenharmony_ci#define PLUGIN_PASTE(str1, str2) PLUGIN_PASTE_ARGS(str1, str2)
295fa7767c5Sopenharmony_ci
296fa7767c5Sopenharmony_ci/// Macro definition, stringify
297fa7767c5Sopenharmony_ci#define PLUGIN_STRINGIFY_ARG(str) #str
298fa7767c5Sopenharmony_ci
299fa7767c5Sopenharmony_ci/// Macro definition, stringify
300fa7767c5Sopenharmony_ci#define PLUGIN_STRINGIFY(str) PLUGIN_STRINGIFY_ARG(str)
301fa7767c5Sopenharmony_ci
302fa7767c5Sopenharmony_ci/**
303fa7767c5Sopenharmony_ci * @brief Macro definition, Defines basic plugin information.
304fa7767c5Sopenharmony_ci * Which is invoked during plugin package registration. All plugin packages must be implemented.
305fa7767c5Sopenharmony_ci *
306fa7767c5Sopenharmony_ci * @param name              Package name. For details, @see PackageDef::name
307fa7767c5Sopenharmony_ci * @param license           Package License, For details, @see PackageDef::licenseType
308fa7767c5Sopenharmony_ci * @param registerFunc      Plugin registration function, MUST NOT be NULL.
309fa7767c5Sopenharmony_ci * @param unregisterFunc    Plugin deregister function,MUST NOT be NULL.
310fa7767c5Sopenharmony_ci */
311fa7767c5Sopenharmony_ci#define PLUGIN_DEFINITION(name, license, registerFunc, unregisterFunc)                                          \
312fa7767c5Sopenharmony_ci    PLUGIN_EXPORT OHOS::Media::Status PLUGIN_PASTE(register_, name)(                                            \
313fa7767c5Sopenharmony_ci        const std::shared_ptr<OHOS::Media::Plugins::PackageRegister>& pkgReg)                                   \
314fa7767c5Sopenharmony_ci    {                                                                                                           \
315fa7767c5Sopenharmony_ci        pkgReg->AddPackage({PLUGIN_INTERFACE_VERSION, PLUGIN_STRINGIFY(name), license});                        \
316fa7767c5Sopenharmony_ci        std::shared_ptr<OHOS::Media::Plugins::Register> pluginReg = pkgReg;                                     \
317fa7767c5Sopenharmony_ci        return registerFunc(pluginReg);                                                                         \
318fa7767c5Sopenharmony_ci    }                                                                                                           \
319fa7767c5Sopenharmony_ci    PLUGIN_EXPORT void PLUGIN_PASTE(unregister_, name)()                                                        \
320fa7767c5Sopenharmony_ci    {                                                                                                           \
321fa7767c5Sopenharmony_ci        unregisterFunc();                                                                                       \
322fa7767c5Sopenharmony_ci    }
323fa7767c5Sopenharmony_ci} // namespace Plugins
324fa7767c5Sopenharmony_ci} // namespace Media
325fa7767c5Sopenharmony_ci} // namespace OHOS
326fa7767c5Sopenharmony_ci#endif // HISTREAMER_PLUGIN_INTF_PLUGIN_DEFINITION_H
327