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 HISTREAMER_PLUGIN_INTF_PLUGIN_BASE_H 17 #define HISTREAMER_PLUGIN_INTF_PLUGIN_BASE_H 18 19 #include <memory> 20 #include "plugin_event.h" 21 #include "meta/meta_key.h" 22 #include "meta/media_types.h" 23 #include "../meta/meta.h" 24 #include "common/status.h" 25 26 namespace OHOS { 27 namespace Media { 28 namespace Plugins { 29 enum class ErrorType { 30 PLUGIN_ERROR, 31 ALGO_ERROR, 32 CLIENT_ERROR, 33 SERVER_ERROR, 34 }; 35 /** 36 * @brief Plugin status callback interface. 37 * 38 * @since 1.0 39 * @version 1.0 40 */ 41 struct Callback { 42 /// Destructor 43 virtual ~Callback() = default; 44 45 /** 46 * @brief When asynchronous time occurs during plugin running, 47 * the plugin implementer invokes this interface to notify the plugin user. 48 * 49 * @note Reserved Interface, Not used yet. 50 * 51 * @param event Event ID. 52 */ 53 virtual void OnEvent(const PluginEvent &event) = 0; 54 }; 55 56 /** 57 * @brief Base class of a plugin. All plugins of different types inherit this interface. 58 * 59 * @details The base class contains only common operation methods and defines basic operation processes. 60 * Different operations are valid only in the corresponding states. The timing of calls is guaranteed by 61 * the plugin framework. Some operations also change the plugin status. 62 * For details, see the description of each function. 63 * 64 * @since 1.0 65 * @version 1.0 66 */ 67 /// Constructor 68 struct PluginBase { PluginBasePluginBase69 explicit PluginBase(std::string name): pluginName_(std::move(name)) {} 70 71 /// Destructor 72 virtual ~PluginBase() = default; 73 74 /** 75 * @brief Get plugin name 76 * 77 * @return plugin name 78 */ GetNamePluginBase79 std::string GetName() const 80 { 81 return pluginName_; 82 } 83 84 /** 85 * @brief Plugin initialization, which is used to load external resources or plugin common resources. 86 * 87 * The function is valid only in the CREATED state. If the initialization is successful, 88 * the plugin enters the INITIALIZED state. 89 * 90 * @return Execution status return 91 * @retval OK: Plugin Init succeeded. 92 * @retval ERROR_NO_MEMORY: Memory allocation or external resource loading error caused by insufficient memory. 93 */ InitPluginBase94 virtual Status Init() 95 { 96 return Status::OK; 97 } 98 99 /** 100 * @brief Plugin deinitialize to release resources. 101 * 102 * This function can be invoked in any state. 103 * After the function is invoked, the plugin will no longer be available. 104 * 105 * @return Execution status return 106 * @retval OK: Plugin Deinit succeeded. 107 */ DeinitPluginBase108 virtual Status Deinit() 109 { 110 return Status::OK; 111 } 112 113 /** 114 * @brief Preparing parameters required or allocate the memory for plugin running. 115 * 116 * The function is valid only in the INITIALIZED state. If the prepare is successful, 117 * the plugin enters the PREPARED state. 118 * 119 * @return Execution status return 120 * @retval OK: Plugin Prepare succeeded. 121 * @retval ERROR_NO_MEMORY: Memory allocation error caused by insufficient memory. 122 */ PreparePluginBase123 virtual Status Prepare() 124 { 125 return Status::OK; 126 } 127 128 /** 129 * @brief Reset the plugin, reset the plugin running status and parameters before Prepare. 130 * 131 * The function is valid only in the PREPARED/RUNNING/PAUSED state. If the reset is successful, 132 * the plugin enters the INITIALIZED state. 133 * 134 * @return Execution status return 135 * @retval OK: Plugin Reset succeeded. 136 * @retval ERROR_UNIMPLEMENTED: This method is not implemented and cannot respond to reset. 137 */ ResetPluginBase138 virtual Status Reset() 139 { 140 return Status::OK; 141 } 142 143 /** 144 * @brief The plugin enters the running state and can process data. 145 * 146 * The function is valid only in the PREPARED state. If the start is successful, 147 * the plugin enters the RUNNING state. If an error occurs during the running, 148 * the plu-in status can be changed through asynchronous callback. 149 * 150 * @return Execution status return 151 * @retval OK: Plugin Start succeeded. 152 */ StartPluginBase153 virtual Status Start() 154 { 155 return Status::OK; 156 } 157 158 /** 159 * @brief The plugin enters the stopped state and stops processing data. 160 * 161 * The function is valid only in the RUNNING state. If the stop is successful, 162 * the plugin enters the PREPARED state. Temporary data generated during the operation will be cleared. 163 * 164 * @return Execution status return 165 * @retval OK: Plugin Stop succeeded. 166 */ StopPluginBase167 virtual Status Stop() 168 { 169 return Status::OK; 170 } 171 172 /** 173 * @brief Get the value of a specified parameter. 174 * 175 * This function can be called in any state except DESTROYED and INVALID. 176 * 177 * @param tag Plugin parameter type, which is described by tag. 178 * @param value Plugin parameter value. which is described by Any type. Need check the real type in tag. 179 * @return Execution status return 180 * @retval OK: Plugin GetParameter succeeded. 181 * @retval ERROR_INVALID_PARAMETER: The plugin does not support this parameter. 182 */ GetParameterPluginBase183 virtual Status GetParameter(std::shared_ptr<Meta> &meta) 184 { 185 (void)meta; 186 return Status::ERROR_UNIMPLEMENTED; 187 } 188 189 /** 190 * @brief Set the specified parameter. The value must be within the valid range of the parameter. 191 * 192 * This function can be called in any state except DESTROYED and INVALID. 193 * 194 * @param tag Plugin parameter type, which is described by tag. 195 * @param value Plugin parameter value. which is described by Any type. Need check the real type in tag. 196 * @return Execution status return 197 * @retval OK: Plugin SetParameter succeeded. 198 * @retval ERROR_INVALID_PARAMETER: The plugin does not support this parameter. 199 * @retval ERROR_INVALID_DATA: The value is not in the valid range. 200 * @retval ERROR_MISMATCHED_TYPE: The data type is mismatched. 201 */ SetParameterPluginBase202 virtual Status SetParameter(const std::shared_ptr<Meta> &meta) 203 { 204 (void)meta; 205 return Status::ERROR_UNIMPLEMENTED; 206 } 207 208 /** 209 * @brief Sets the plugin callback message to notify the plugin user. 210 * 211 * This function can be called in any state except DESTROYED and INVALID. 212 * 213 * @param cb Message callback, NULL callback listening is canceled. 214 * @return Execution status return 215 * @retval OK: Plugin SetCallback succeeded. 216 */ SetCallbackPluginBase217 virtual Status SetCallback(Callback* cb) 218 { 219 (void)cb; 220 return Status::OK; 221 } 222 223 protected: 224 const std::string pluginName_; 225 }; 226 } // namespace Plugins 227 } // namespace Media 228 } // namespace OHOS 229 #endif // HISTREAMER_PLUGIN_INTF_PLUGIN_BASE_H 230