• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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_PLUGINS_INTF_PLUGIN_BASE_H
17 #define HISTREAMER_PLUGINS_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 
OnDfxEventCallback55     virtual void OnDfxEvent(const PluginDfxEvent &event) {}
56 
SetSelectBitRateFlagCallback57     virtual void SetSelectBitRateFlag(bool flag, uint32_t desBitRate)
58     {
59         (void)flag;
60         (void)desBitRate;
61     }
62 
CanAutoSelectBitRateCallback63     virtual bool CanAutoSelectBitRate()
64     {
65         return false;
66     }
67 };
68 
69 /**
70  * @brief Base class of a plugin. All plugins of different types inherit this interface.
71  *
72  * @details The base class contains only common operation methods and defines basic operation processes.
73  * Different operations are valid only in the corresponding states. The timing of calls is guaranteed by
74  * the plugin framework. Some operations also change the plugin status.
75  * For details, see the description of each function.
76  *
77  * @since 1.0
78  * @version 1.0
79  */
80     /// Constructor
81 struct PluginBase {
PluginBasePluginBase82     explicit PluginBase(std::string name): pluginName_(std::move(name)) {}
83 
84     /// Destructor
85     virtual ~PluginBase() = default;
86 
87     /**
88      * @brief Get plugin name
89      *
90      * @return plugin name
91      */
GetNamePluginBase92     std::string GetName() const
93     {
94         return pluginName_;
95     }
96 
97     /**
98      * @brief Plugin initialization, which is used to load external resources or plugin common resources.
99      *
100      * The function is valid only in the CREATED state. If the initialization is successful,
101      * the plugin enters the INITIALIZED state.
102      *
103      * @return  Execution status return
104      *  @retval OK: Plugin Init succeeded.
105      *  @retval ERROR_NO_MEMORY: Memory allocation or external resource loading error caused by insufficient memory.
106      */
InitPluginBase107     virtual Status Init()
108     {
109         return Status::OK;
110     }
111 
112     /**
113      * @brief Plugin deinitialize to release resources.
114      *
115      * This function can be invoked in any state.
116      * After the function is invoked, the plugin will no longer be available.
117      *
118      * @return Execution status return
119      *  @retval OK: Plugin Deinit succeeded.
120      */
DeinitPluginBase121     virtual Status Deinit()
122     {
123         return Status::OK;
124     }
125 
126     /**
127      * @brief Preparing parameters required or allocate the memory for plugin running.
128      *
129      * The function is valid only in the INITIALIZED state. If the prepare is successful,
130      * the plugin enters the PREPARED state.
131      *
132      * @return Execution status return
133      *  @retval OK: Plugin Prepare succeeded.
134      *  @retval ERROR_NO_MEMORY: Memory allocation error caused by insufficient memory.
135      */
PreparePluginBase136     virtual Status Prepare()
137     {
138         return Status::OK;
139     }
140 
141     /**
142      * @brief Reset the plugin, reset the plugin running status and parameters before Prepare.
143      *
144      * The function is valid only in the PREPARED/RUNNING/PAUSED state. If the reset is successful,
145      * the plugin enters the INITIALIZED state.
146      *
147      * @return Execution status return
148      *  @retval OK: Plugin Reset succeeded.
149      *  @retval ERROR_UNIMPLEMENTED: This method is not implemented and cannot respond to reset.
150      */
ResetPluginBase151     virtual Status Reset()
152     {
153         return Status::OK;
154     }
155 
156     /**
157      * @brief The plugin enters the running state and can process data.
158      *
159      * The function is valid only in the PREPARED state. If the start is successful,
160      * the plugin enters the RUNNING state. If an error occurs during the running,
161      * the plu-in status can be changed through asynchronous callback.
162      *
163      * @return Execution status return
164      *  @retval OK: Plugin Start succeeded.
165      */
StartPluginBase166     virtual Status Start()
167     {
168         return Status::OK;
169     }
170 
171     /**
172      * @brief The plugin enters the stopped state and stops processing data.
173      *
174      * The function is valid only in the RUNNING state. If the stop is successful,
175      * the plugin enters the PREPARED state. Temporary data generated during the operation will be cleared.
176      *
177      * @return Execution status return
178      *  @retval OK: Plugin Stop succeeded.
179      */
StopPluginBase180     virtual Status Stop()
181     {
182         return Status::OK;
183     }
184 
185     /**
186      * @brief Get the value of a specified parameter.
187      *
188      * This function can be called in any state except DESTROYED and INVALID.
189      *
190      * @param tag   Plugin parameter type, which is described by tag.
191      * @param value Plugin parameter value. which is described by Any type. Need check the real type in tag.
192      * @return Execution status return
193      *  @retval OK: Plugin GetParameter succeeded.
194      *  @retval ERROR_INVALID_PARAMETER: The plugin does not support this parameter.
195      */
GetParameterPluginBase196     virtual Status GetParameter(std::shared_ptr<Meta> &meta)
197     {
198         (void)meta;
199         return Status::ERROR_UNIMPLEMENTED;
200     }
201 
202     /**
203      * @brief Set the specified parameter. The value must be within the valid range of the parameter.
204      *
205      * This function can be called in any state except DESTROYED and INVALID.
206      *
207      * @param tag   Plugin parameter type, which is described by tag.
208      * @param value Plugin parameter value. which is described by Any type. Need check the real type in tag.
209      * @return Execution status return
210      *  @retval OK: Plugin SetParameter succeeded.
211      *  @retval ERROR_INVALID_PARAMETER: The plugin does not support this parameter.
212      *  @retval ERROR_INVALID_DATA: The value is not in the valid range.
213      *  @retval ERROR_MISMATCHED_TYPE: The data type is mismatched.
214      */
SetParameterPluginBase215     virtual Status SetParameter(const std::shared_ptr<Meta> &meta)
216     {
217         (void)meta;
218         return Status::ERROR_UNIMPLEMENTED;
219     }
220 
221     /**
222      * @brief Sets the plugin callback message to notify the plugin user.
223      *
224      * This function can be called in any state except DESTROYED and INVALID.
225      *
226      * @param cb   Message callback, NULL callback listening is canceled.
227      * @return Execution status return
228      *  @retval OK: Plugin SetCallback succeeded.
229      */
SetCallbackPluginBase230     virtual Status SetCallback(Callback* cb)
231     {
232         (void)cb;
233         return Status::OK;
234     }
235 
236 protected:
237     const std::string pluginName_;
238 };
239 } // namespace Plugins
240 } // namespace Media
241 } // namespace OHOS
242 #endif // HISTREAMER_PLUGINS_INTF_PLUGIN_BASE_H
243