* Copyright (c) 2021-2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HISTREAMER_PLUGIN_INTF_PLUGIN_DEFINITION_H
#define HISTREAMER_PLUGIN_INTF_PLUGIN_DEFINITION_H
#include <functional>
#include <string>
#include <memory>
#include "plugin/common/plugin_types.h"
namespace OHOS {
namespace Media {
namespace Plugin {
* @brief Macro definition, creating the version information.
*
* @details The versioning is the process of assigning a unique version number to a unique state
* of plugin interface. Within a given version number category (major, minor), these numbers are
* usually assigned in ascending order and correspond to new developments in the plugin.
*
* Given a version number MAJOR.MINOR:
* - MAJOR: When you make incompatible API changes.
* - MINOR: When you add features in a backwards-compatible manner or do backwards-compatible bug fixes.
*/
#define MAKE_VERSION(MAJOR, MINOR) ((((MAJOR)&0xFFFF) << 16) | ((MINOR)&0xFFFF))
#define PLUGIN_INTERFACE_VERSION_MAJOR (1)
#define PLUGIN_INTERFACE_VERSION_MINOR (0)
#define PLUGIN_INTERFACE_VERSION MAKE_VERSION(PLUGIN_INTERFACE_VERSION_MAJOR, PLUGIN_INTERFACE_VERSION_MINOR)
* @enum License Type.
* an official permission or permit.
*
* @since 1.0
* @version 1.0
*/
enum struct LicenseType : uint8_t {
APACHE_V2,
LGPL,
GPL,
CC0,
UNKNOWN,
};
* @brief Definition of plugin packaging information.
*
* @since 1.0
* @version 1.0
*/
struct PackageDef {
uint32_t pkgVersion;
std::string name;
LicenseType
licenseType;
};
template <typename T>
using PluginCreatorFunc = std::function<std::shared_ptr<T>(const std::string& name)>;
* @brief Describes the basic information about the plugin.
*
* @since 1.0
* @version 1.0
*/
struct PluginDefBase {
uint32_t apiVersion;
PluginType pluginType = PluginType::INVALID_TYPE;
std::string name;
std::string description;
uint32_t rank;
};
* @brief The plugin registration interface.
* The plugin framework will provide the implementation.
* Developers only need to invoke the API to register the plugin.
*
* @since 1.0
* @version 1.0
*/
struct Register {
virtual ~Register() = default;
* @brief Register the plugin.
*
* @param def Basic information about the plugin
* @return Registration status return
* @retval OK: The plugin is registered succeed.
* @retval ERROR_PLUGIN_ALREADY_EXISTS: The plugin already exists in plugin registered.
* @retval ERROR_INCOMPATIBLE_VERSION: Incompatible version during plugin registration.
*/
virtual Status AddPlugin(const PluginDefBase& def) = 0;
};
* @brief The package registration interface.
* The plugin framework will provide the implementation and auto invoke the API to
* finish the package registration when plugin framework first time be initialized.
*
* @since 1.0
* @version 1.0
*/
struct PackageRegister : Register {
~PackageRegister() override = default;
* @brief Register the package.
* During package registration, all plugins in the package are automatically registered.
*
* @param def plugin packaging information.
* @return Registration status return
* @retval OK: The package is registered succeed without any errors.
* @retval ERROR_PLUGIN_ALREADY_EXISTS: The package or plugins already exists.
* @retval ERROR_INCOMPATIBLE_VERSION: Incompatible plugin interface version or api version.
*/
virtual Status AddPackage(const PackageDef& def) = 0;
};
using RegisterFunc = Status (*)(const std::shared_ptr<PackageRegister>& reg);
using UnregisterFunc = void (*)();
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#define PLUGIN_EXPORT extern "C" __declspec(dllexport)
#else
#if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
#define PLUGIN_EXPORT extern "C" __attribute__((visibility("default")))
#else
#define PLUGIN_EXPORT
#endif
#endif
#define PLUGIN_PASTE_ARGS(str1, str2) str1##str2
#define PLUGIN_PASTE(str1, str2) PLUGIN_PASTE_ARGS(str1, str2)
#define PLUGIN_STRINGIFY_ARG(str) #str
#define PLUGIN_STRINGIFY(str) PLUGIN_STRINGIFY_ARG(str)
* @brief Macro definition, Defines basic plugin information.
* Which is invoked during plugin package registration. All plugin packages must be implemented.
*
* @param name Package name. For details, @see PackageDef::name
* @param license Package License, For details, @see PackageDef::licenseType
* @param registerFunc Plugin registration function, MUST NOT be NULL.
* @param unregisterFunc Plugin deregister function,MUST NOT be NULL.
*/
#define PLUGIN_DEFINITION(name, license, registerFunc, unregisterFunc) \
PLUGIN_EXPORT OHOS::Media::Plugin::Status PLUGIN_PASTE(register_, name)( \
const std::shared_ptr<OHOS::Media::Plugin::PackageRegister>& pkgReg) \
{ \
pkgReg->AddPackage({PLUGIN_INTERFACE_VERSION, PLUGIN_STRINGIFY(name), license}); \
std::shared_ptr<OHOS::Media::Plugin::Register> pluginReg = pkgReg; \
return registerFunc(pluginReg); \
} \
PLUGIN_EXPORT void PLUGIN_PASTE(unregister_, name)() \
{ \
unregisterFunc(); \
}
}
}
}
#endif