1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2021-2021 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/common/plugin_types.h"
23fa7767c5Sopenharmony_ci
24fa7767c5Sopenharmony_cinamespace OHOS {
25fa7767c5Sopenharmony_cinamespace Media {
26fa7767c5Sopenharmony_cinamespace Plugin {
27fa7767c5Sopenharmony_ci/**
28fa7767c5Sopenharmony_ci * @brief Macro definition, creating the version information.
29fa7767c5Sopenharmony_ci *
30fa7767c5Sopenharmony_ci * @details The versioning is the process of assigning a unique version number to a unique state
31fa7767c5Sopenharmony_ci * of plugin interface. Within a given version number category (major, minor), these numbers are
32fa7767c5Sopenharmony_ci * usually assigned in ascending order and correspond to new developments in the plugin.
33fa7767c5Sopenharmony_ci *
34fa7767c5Sopenharmony_ci * Given a version number MAJOR.MINOR:
35fa7767c5Sopenharmony_ci *  - MAJOR: When you make incompatible API changes.
36fa7767c5Sopenharmony_ci *  - MINOR: When you add features in a backwards-compatible manner or do backwards-compatible bug fixes.
37fa7767c5Sopenharmony_ci */
38fa7767c5Sopenharmony_ci#define MAKE_VERSION(MAJOR, MINOR) ((((MAJOR)&0xFFFF) << 16) | ((MINOR)&0xFFFF))
39fa7767c5Sopenharmony_ci
40fa7767c5Sopenharmony_ci/// Plugin interface major number
41fa7767c5Sopenharmony_ci#define PLUGIN_INTERFACE_VERSION_MAJOR (1)
42fa7767c5Sopenharmony_ci
43fa7767c5Sopenharmony_ci/// Plugin interface minor number
44fa7767c5Sopenharmony_ci#define PLUGIN_INTERFACE_VERSION_MINOR (0)
45fa7767c5Sopenharmony_ci
46fa7767c5Sopenharmony_ci/// Plugin interface version
47fa7767c5Sopenharmony_ci#define PLUGIN_INTERFACE_VERSION MAKE_VERSION(PLUGIN_INTERFACE_VERSION_MAJOR, PLUGIN_INTERFACE_VERSION_MINOR)
48fa7767c5Sopenharmony_ci
49fa7767c5Sopenharmony_ci/**
50fa7767c5Sopenharmony_ci * @enum License Type.
51fa7767c5Sopenharmony_ci * an official permission or permit.
52fa7767c5Sopenharmony_ci *
53fa7767c5Sopenharmony_ci * @since 1.0
54fa7767c5Sopenharmony_ci * @version 1.0
55fa7767c5Sopenharmony_ci */
56fa7767c5Sopenharmony_cienum struct LicenseType : uint8_t {
57fa7767c5Sopenharmony_ci    APACHE_V2, ///< The Apache License 2.0
58fa7767c5Sopenharmony_ci    LGPL,      ///< The GNU Lesser General Public License
59fa7767c5Sopenharmony_ci    GPL,       ///< The GNU General Public License
60fa7767c5Sopenharmony_ci    CC0,       ///< The Creative Commons Zero v1.0 Universal
61fa7767c5Sopenharmony_ci    UNKNOWN,   ///< Unknown License
62fa7767c5Sopenharmony_ci};
63fa7767c5Sopenharmony_ci
64fa7767c5Sopenharmony_ci/**
65fa7767c5Sopenharmony_ci * @brief Definition of plugin packaging information.
66fa7767c5Sopenharmony_ci *
67fa7767c5Sopenharmony_ci * @since 1.0
68fa7767c5Sopenharmony_ci * @version 1.0
69fa7767c5Sopenharmony_ci */
70fa7767c5Sopenharmony_cistruct PackageDef {
71fa7767c5Sopenharmony_ci    uint32_t pkgVersion;    ///< Package information version, which indicates the latest plug-in interface version
72fa7767c5Sopenharmony_ci                            ///< used by the plugin in the package. The default value is PLUGIN_INTERFACE_VERSION.
73fa7767c5Sopenharmony_ci
74fa7767c5Sopenharmony_ci    std::string name;   ///< Package name. The plugin framework registers the plugin using this name.
75fa7767c5Sopenharmony_ci                        ///< If the plugins are packaged as a dynamic library, the name of library
76fa7767c5Sopenharmony_ci                        ///< must be in the format of "libplugin_<name>.so".
77fa7767c5Sopenharmony_ci
78fa7767c5Sopenharmony_ci    LicenseType
79fa7767c5Sopenharmony_ci        licenseType;    ///< The License information of the plugin in the package.
80fa7767c5Sopenharmony_ci                        ///< The different plugins must be the same.
81fa7767c5Sopenharmony_ci                        ///< The plugin framework processing in the plugin running state based on different license.
82fa7767c5Sopenharmony_ci};
83fa7767c5Sopenharmony_ci
84fa7767c5Sopenharmony_ci/// Plugin create function. All plugins must implement this function.
85fa7767c5Sopenharmony_citemplate <typename T>
86fa7767c5Sopenharmony_ciusing PluginCreatorFunc = std::function<std::shared_ptr<T>(const std::string& name)>;
87fa7767c5Sopenharmony_ci
88fa7767c5Sopenharmony_ci/**
89fa7767c5Sopenharmony_ci * @brief Describes the basic information about the plugin.
90fa7767c5Sopenharmony_ci *
91fa7767c5Sopenharmony_ci * @since 1.0
92fa7767c5Sopenharmony_ci * @version 1.0
93fa7767c5Sopenharmony_ci */
94fa7767c5Sopenharmony_cistruct PluginDefBase {
95fa7767c5Sopenharmony_ci    uint32_t apiVersion; ///< Versions of different plugins. Different types of plugin have their own versions.
96fa7767c5Sopenharmony_ci
97fa7767c5Sopenharmony_ci    PluginType pluginType = PluginType::INVALID_TYPE; ///< Describe the plugin type, e.g. 'source', 'codec'.
98fa7767c5Sopenharmony_ci
99fa7767c5Sopenharmony_ci    std::string name;   ///< Indicates the name of a plugin. The name of the same type plugins must be unique.
100fa7767c5Sopenharmony_ci                        ///< Plugins with the same name may fail to be registered.
101fa7767c5Sopenharmony_ci
102fa7767c5Sopenharmony_ci    std::string description; ///< Detailed description of the plugin.
103fa7767c5Sopenharmony_ci
104fa7767c5Sopenharmony_ci    uint32_t rank;  ///< Plugin score. The plugin with a high score may be preferred. You can evaluate the
105fa7767c5Sopenharmony_ci                    ///< plugin score in terms of performance, version support, and license. Range: 0 to 100.
106fa7767c5Sopenharmony_ci};
107fa7767c5Sopenharmony_ci
108fa7767c5Sopenharmony_ci/**
109fa7767c5Sopenharmony_ci * @brief The plugin registration interface.
110fa7767c5Sopenharmony_ci * The plugin framework will provide the implementation.
111fa7767c5Sopenharmony_ci * Developers only need to invoke the API to register the plugin.
112fa7767c5Sopenharmony_ci *
113fa7767c5Sopenharmony_ci * @since 1.0
114fa7767c5Sopenharmony_ci * @version 1.0
115fa7767c5Sopenharmony_ci */
116fa7767c5Sopenharmony_cistruct Register {
117fa7767c5Sopenharmony_ci    virtual ~Register() = default;
118fa7767c5Sopenharmony_ci    /**
119fa7767c5Sopenharmony_ci     * @brief Register the plugin.
120fa7767c5Sopenharmony_ci     *
121fa7767c5Sopenharmony_ci     * @param def   Basic information about the plugin
122fa7767c5Sopenharmony_ci     * @return  Registration status return
123fa7767c5Sopenharmony_ci     *  @retval OK: The plugin is registered succeed.
124fa7767c5Sopenharmony_ci     *  @retval ERROR_PLUGIN_ALREADY_EXISTS: The plugin already exists in plugin registered.
125fa7767c5Sopenharmony_ci     *  @retval ERROR_INCOMPATIBLE_VERSION: Incompatible version during plugin registration.
126fa7767c5Sopenharmony_ci     */
127fa7767c5Sopenharmony_ci    virtual Status AddPlugin(const PluginDefBase& def) = 0;
128fa7767c5Sopenharmony_ci};
129fa7767c5Sopenharmony_ci
130fa7767c5Sopenharmony_ci/**
131fa7767c5Sopenharmony_ci * @brief The package registration interface.
132fa7767c5Sopenharmony_ci * The plugin framework will provide the implementation and auto invoke the API to
133fa7767c5Sopenharmony_ci * finish the package registration when plugin framework first time be initialized.
134fa7767c5Sopenharmony_ci *
135fa7767c5Sopenharmony_ci * @since 1.0
136fa7767c5Sopenharmony_ci * @version 1.0
137fa7767c5Sopenharmony_ci */
138fa7767c5Sopenharmony_cistruct PackageRegister : Register {
139fa7767c5Sopenharmony_ci    ~PackageRegister() override = default;
140fa7767c5Sopenharmony_ci
141fa7767c5Sopenharmony_ci    /**
142fa7767c5Sopenharmony_ci     * @brief Register the package.
143fa7767c5Sopenharmony_ci     * During package registration, all plugins in the package are automatically registered.
144fa7767c5Sopenharmony_ci     *
145fa7767c5Sopenharmony_ci     * @param def   plugin packaging information.
146fa7767c5Sopenharmony_ci     * @return  Registration status return
147fa7767c5Sopenharmony_ci     *  @retval OK: The package is registered succeed without any errors.
148fa7767c5Sopenharmony_ci     *  @retval ERROR_PLUGIN_ALREADY_EXISTS: The package or plugins already exists.
149fa7767c5Sopenharmony_ci     *  @retval ERROR_INCOMPATIBLE_VERSION: Incompatible plugin interface version or api version.
150fa7767c5Sopenharmony_ci     */
151fa7767c5Sopenharmony_ci    virtual Status AddPackage(const PackageDef& def) = 0;
152fa7767c5Sopenharmony_ci};
153fa7767c5Sopenharmony_ci
154fa7767c5Sopenharmony_ci/// Plugin registration function, all plugins must be implemented.
155fa7767c5Sopenharmony_ciusing RegisterFunc = Status (*)(std::shared_ptr<Register> reg);
156fa7767c5Sopenharmony_ci
157fa7767c5Sopenharmony_ci/// Plugin deregister function, all plugins must be implemented.
158fa7767c5Sopenharmony_ciusing UnregisterFunc = void (*)();
159fa7767c5Sopenharmony_ci
160fa7767c5Sopenharmony_ci#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
161fa7767c5Sopenharmony_ci#define PLUGIN_EXPORT extern "C" __declspec(dllexport)
162fa7767c5Sopenharmony_ci#else
163fa7767c5Sopenharmony_ci#if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
164fa7767c5Sopenharmony_ci#define PLUGIN_EXPORT extern "C" __attribute__((visibility("default")))
165fa7767c5Sopenharmony_ci#else
166fa7767c5Sopenharmony_ci#define PLUGIN_EXPORT
167fa7767c5Sopenharmony_ci#endif
168fa7767c5Sopenharmony_ci#endif
169fa7767c5Sopenharmony_ci
170fa7767c5Sopenharmony_ci/// Macro definition, string concatenation
171fa7767c5Sopenharmony_ci#define PLUGIN_PASTE_ARGS(str1, str2) str1##str2
172fa7767c5Sopenharmony_ci
173fa7767c5Sopenharmony_ci/// Macro definition, string concatenation
174fa7767c5Sopenharmony_ci#define PLUGIN_PASTE(str1, str2) PLUGIN_PASTE_ARGS(str1, str2)
175fa7767c5Sopenharmony_ci
176fa7767c5Sopenharmony_ci/// Macro definition, stringify
177fa7767c5Sopenharmony_ci#define PLUGIN_STRINGIFY_ARG(str) #str
178fa7767c5Sopenharmony_ci
179fa7767c5Sopenharmony_ci/// Macro definition, stringify
180fa7767c5Sopenharmony_ci#define PLUGIN_STRINGIFY(str) PLUGIN_STRINGIFY_ARG(str)
181fa7767c5Sopenharmony_ci
182fa7767c5Sopenharmony_ci/**
183fa7767c5Sopenharmony_ci * @brief Macro definition, Defines basic plugin information.
184fa7767c5Sopenharmony_ci * Which is invoked during plugin package registration. All plugin packages must be implemented.
185fa7767c5Sopenharmony_ci *
186fa7767c5Sopenharmony_ci * @param name              Package name. For details, @see PackageDef::name
187fa7767c5Sopenharmony_ci * @param license           Package License, For details, @see PackageDef::licenseType
188fa7767c5Sopenharmony_ci * @param registerFunc      Plugin registration function, MUST NOT be NULL.
189fa7767c5Sopenharmony_ci * @param unregisterFunc    Plugin deregister function,MUST NOT be NULL.
190fa7767c5Sopenharmony_ci */
191fa7767c5Sopenharmony_ci#define PLUGIN_DEFINITION(name, license, registerFunc, unregisterFunc)                                                 \
192fa7767c5Sopenharmony_ci    PLUGIN_EXPORT OHOS::Media::Plugin::Status PLUGIN_PASTE(register_, name)(                                           \
193fa7767c5Sopenharmony_ci        const std::shared_ptr<OHOS::Media::Plugin::PackageRegister>& pkgReg)                                           \
194fa7767c5Sopenharmony_ci    {                                                                                                                  \
195fa7767c5Sopenharmony_ci        pkgReg->AddPackage({PLUGIN_INTERFACE_VERSION, PLUGIN_STRINGIFY(name), license});                               \
196fa7767c5Sopenharmony_ci        std::shared_ptr<OHOS::Media::Plugin::Register> pluginReg = pkgReg;                                             \
197fa7767c5Sopenharmony_ci        return registerFunc(pluginReg);                                                                                \
198fa7767c5Sopenharmony_ci    }                                                                                                                  \
199fa7767c5Sopenharmony_ci    PLUGIN_EXPORT void PLUGIN_PASTE(unregister_, name)()                                                               \
200fa7767c5Sopenharmony_ci    {                                                                                                                  \
201fa7767c5Sopenharmony_ci        unregisterFunc();                                                                                              \
202fa7767c5Sopenharmony_ci    }
203fa7767c5Sopenharmony_ci} // namespace Plugin
204fa7767c5Sopenharmony_ci} // namespace Media
205fa7767c5Sopenharmony_ci} // namespace OHOS
206fa7767c5Sopenharmony_ci#endif // HISTREAMER_PLUGIN_INTF_PLUGIN_DEFINITION_H
207