• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_tags.h"
21 #include "plugin_buffer.h"
22 #include "plugin_definition.h"
23 #include "plugin_event.h"
24 
25 namespace OHOS {
26 namespace MediaAVCodec {
27 namespace Plugin {
28 enum class ErrorType {
29     PLUGIN_ERROR,
30     ALGO_ERROR,
31     CLIENT_ERROR,
32     SERVER_ERROR,
33 };
34 
35 /**
36  * @brief Plugin status callback interface.
37  * @since 1.0
38  * @version 1.0
39  */
40 struct Callback {
41     virtual ~Callback() = default;
42     virtual void OnEvent(const PluginEvent &event) = 0;
43 };
44 
45 /**
46  * @brief Base class of a plugin. All plugins of different types inherit this interface.
47  * @details The base class contains only common operation methods and defines basic operation processes.
48  * Different operations are valid only in the corresponding states. The timing of calls is guaranteed by
49  * the plugin framework. Some operations also change the plugin status.
50  * For details, see the description of each function.
51  * @since 1.0
52  * @version 1.0
53  */
54 struct PluginBase {
PluginBasePluginBase55     explicit PluginBase(std::string name): pluginName_(std::move(name)) {}
56     virtual ~PluginBase() = default;
57 
58     /**
59      * @brief Get plugin name
60      * @return plugin name
61      */
GetNamePluginBase62     std::string GetName() const
63     {
64         return pluginName_;
65     }
66 
67     /**
68      * @brief Plugin initialization, which is used to load external resources or plugin common resources.
69      * The function is valid only in the CREATED state. If the initialization is successful,
70      * the plugin enters the INITIALIZED state.
71      * @return  Execution status return
72      *  @retval OK: Plugin Init succeeded.
73      *  @retval ERROR_NO_MEMORY: Memory allocation or external resource loading error caused by insufficient memory.
74      */
InitPluginBase75     virtual Status Init()
76     {
77         return Status::OK;
78     }
79 
80     /**
81      * @brief Plugin deinitialize to release resources.
82      * This function can be invoked in any state.
83      * After the function is invoked, the plugin will no longer be available.
84      * @return Execution status return
85      *  @retval OK: Plugin Deinit succeeded.
86      */
DeinitPluginBase87     virtual Status Deinit()
88     {
89         return Status::OK;
90     }
91 
92     /**
93      * @brief Preparing parameters required or allocate the memory for plugin running.
94      * The function is valid only in the INITIALIZED state. If the prepare is successful,
95      * the plugin enters the PREPARED state.
96      * @return Execution status return
97      *  @retval OK: Plugin Prepare succeeded.
98      *  @retval ERROR_NO_MEMORY: Memory allocation error caused by insufficient memory.
99      */
PreparePluginBase100     virtual Status Prepare()
101     {
102         return Status::OK;
103     }
104 
105     /**
106      * @brief Reset the plugin, reset the plugin running status and parameters before Prepare.
107      * The function is valid only in the PREPARED/RUNNING/PAUSED state. If the reset is successful,
108      * the plugin enters the INITIALIZED state.
109      * @return Execution status return
110      *  @retval OK: Plugin Reset succeeded.
111      *  @retval ERROR_UNIMPLEMENTED: This method is not implemented and cannot respond to reset.
112      */
ResetPluginBase113     virtual Status Reset()
114     {
115         return Status::OK;
116     }
117 
118     /**
119      * @brief The plugin enters the running state and can process data.
120      * The function is valid only in the PREPARED state. If the start is successful,
121      * the plugin enters the RUNNING state. If an error occurs during the running,
122      * the plu-in status can be changed through asynchronous callback.
123      * @return Execution status return
124      *  @retval OK: Plugin Start succeeded.
125      */
StartPluginBase126     virtual Status Start()
127     {
128         return Status::OK;
129     }
130 
131     /**
132      * @brief The plugin enters the stopped state and stops processing data.
133      * The function is valid only in the RUNNING state. If the stop is successful,
134      * the plugin enters the PREPARED state. Temporary data generated during the operation will be cleared.
135      * @return Execution status return
136      *  @retval OK: Plugin Stop succeeded.
137      */
StopPluginBase138     virtual Status Stop()
139     {
140         return Status::OK;
141     }
142 
143     /**
144      * @brief Get the value of a specified parameter.
145      * This function can be called in any state except DESTROYED and INVALID.
146      * @param tag   Plugin parameter type, which is described by tag.
147      * @param value Plugin parameter value. which is described by Any type. Need check the real type in tag.
148      * @return Execution status return
149      *  @retval OK: Plugin GetParameter succeeded.
150      *  @retval ERROR_INVALID_PARAMETER: The plugin does not support this parameter.
151      */
GetParameterPluginBase152     virtual Status GetParameter(Tag tag, ValueType &value)
153     {
154         (void)tag;
155         (void)value;
156         return Status::ERROR_UNIMPLEMENTED;
157     }
158 
159     /**
160      * @brief Set the specified parameter. The value must be within the valid range of the parameter.
161      * This function can be called in any state except DESTROYED and INVALID.
162      * @param tag   Plugin parameter type, which is described by tag.
163      * @param value Plugin parameter value. which is described by Any type. Need check the real type in tag.
164      * @return Execution status return
165      *  @retval OK: Plugin SetParameter succeeded.
166      *  @retval ERROR_INVALID_PARAMETER: The plugin does not support this parameter.
167      *  @retval ERROR_INVALID_DATA: The value is not in the valid range.
168      *  @retval ERROR_MISMATCHED_TYPE: The data type is mismatched.
169      */
SetParameterPluginBase170     virtual Status SetParameter(Tag tag, const ValueType &value)
171     {
172         (void)tag;
173         (void)value;
174         return Status::ERROR_UNIMPLEMENTED;
175     }
176 
177     /**
178      * @brief Get the allocator specified by the plugin.
179      * The allocator can allocate memory types that meet the plugin requirements.
180      * @return Obtains the allocator object or NULL if the plugin does not have requirements for memory.
181      */
GetAllocatorPluginBase182     virtual std::shared_ptr<Allocator> GetAllocator()
183     {
184         return nullptr;
185     }
186 
187     /**
188      * @brief Sets the plugin callback message to notify the plugin user.
189      * This function can be called in any state except DESTROYED and INVALID.
190      * @param cb   Message callback, NULL callback listening is canceled.
191      * @return Execution status return
192      *  @retval OK: Plugin SetCallback succeeded.
193      */
194     virtual Status SetCallback(Callback* cb) = 0;
195 
196 protected:
197     const std::string pluginName_;
198 };
199 } // namespace Plugin
200 } // namespace MediaAVCodec
201 } // namespace OHOS
202 #endif // HISTREAMER_PLUGIN_INTF_PLUGIN_BASE_H
203