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