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