1da853ecaSopenharmony_ci/*
2da853ecaSopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3da853ecaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4da853ecaSopenharmony_ci * you may not use this file except in compliance with the License.
5da853ecaSopenharmony_ci * You may obtain a copy of the License at
6da853ecaSopenharmony_ci *
7da853ecaSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8da853ecaSopenharmony_ci *
9da853ecaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10da853ecaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11da853ecaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12da853ecaSopenharmony_ci * See the License for the specific language governing permissions and
13da853ecaSopenharmony_ci * limitations under the License.
14da853ecaSopenharmony_ci */
15da853ecaSopenharmony_ci
16da853ecaSopenharmony_ci#ifndef HISTREAMER_PLUGIN_INTF_AUDIO_SINK_PLUGIN_H
17da853ecaSopenharmony_ci#define HISTREAMER_PLUGIN_INTF_AUDIO_SINK_PLUGIN_H
18da853ecaSopenharmony_ci
19da853ecaSopenharmony_ci#include "buffer/avbuffer.h"
20da853ecaSopenharmony_ci#include "filter/filter.h"
21da853ecaSopenharmony_ci#include "plugin/plugin_caps.h"
22da853ecaSopenharmony_ci#include "plugin/plugin_base.h"
23da853ecaSopenharmony_ci#include "plugin/plugin_definition.h"
24da853ecaSopenharmony_ci
25da853ecaSopenharmony_cinamespace OHOS {
26da853ecaSopenharmony_cinamespace Media {
27da853ecaSopenharmony_cinamespace Plugins {
28da853ecaSopenharmony_ci/**
29da853ecaSopenharmony_ci * @brief Audio Sink Plugin.
30da853ecaSopenharmony_ci *
31da853ecaSopenharmony_ci * Component that receives media streams.
32da853ecaSopenharmony_ci *
33da853ecaSopenharmony_ci * @since 1.0
34da853ecaSopenharmony_ci * @version 1.0
35da853ecaSopenharmony_ci */
36da853ecaSopenharmony_cistruct AudioSinkPlugin : public Plugins::PluginBase {
37da853ecaSopenharmony_ci    /// constructor
38da853ecaSopenharmony_ci    explicit AudioSinkPlugin(std::string name): PluginBase(std::move(name)) {}
39da853ecaSopenharmony_ci    /**
40da853ecaSopenharmony_ci     * @brief Get the mute operation set for the audio.
41da853ecaSopenharmony_ci     *
42da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
43da853ecaSopenharmony_ci     *
44da853ecaSopenharmony_ci     * @param mute  Indicates the mute operation set for the audio.
45da853ecaSopenharmony_ci     *              Value true means that the audio is muted, and false means the opposite.
46da853ecaSopenharmony_ci     * @return   Execution status return
47da853ecaSopenharmony_ci     * @retval OK: Plugin GetMute succeeded.
48da853ecaSopenharmony_ci     */
49da853ecaSopenharmony_ci    virtual Status GetMute(bool& mute) = 0;
50da853ecaSopenharmony_ci
51da853ecaSopenharmony_ci    /**
52da853ecaSopenharmony_ci     * @brief Set the mute operation for the audio.
53da853ecaSopenharmony_ci     *
54da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
55da853ecaSopenharmony_ci     *
56da853ecaSopenharmony_ci     * @param mute  Indicates the mute operation set for the audio.
57da853ecaSopenharmony_ci     *              Value true means that the audio is muted, and false means the opposite.
58da853ecaSopenharmony_ci     * @return  Execution status return
59da853ecaSopenharmony_ci     * @retval OK: Plugin SetMute succeeded.
60da853ecaSopenharmony_ci     */
61da853ecaSopenharmony_ci    virtual Status SetMute(bool mute) = 0;
62da853ecaSopenharmony_ci
63da853ecaSopenharmony_ci    /**
64da853ecaSopenharmony_ci     * @brief Get the audio volume.
65da853ecaSopenharmony_ci     *
66da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
67da853ecaSopenharmony_ci     *
68da853ecaSopenharmony_ci     * @param volume    Indicates the volume to set. The value ranges from 0.0 to 1.0.
69da853ecaSopenharmony_ci     * @return  Execution status return
70da853ecaSopenharmony_ci     * @retval OK: Plugin GetVolume succeeded.
71da853ecaSopenharmony_ci     */
72da853ecaSopenharmony_ci    virtual Status GetVolume(float& volume) = 0;
73da853ecaSopenharmony_ci
74da853ecaSopenharmony_ci    /**
75da853ecaSopenharmony_ci     * @brief Set the audio volume.
76da853ecaSopenharmony_ci     *
77da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
78da853ecaSopenharmony_ci     *
79da853ecaSopenharmony_ci     * @param volume    Indicates the volume to set. The value ranges from 0.0 to 1.0.
80da853ecaSopenharmony_ci     * @return  Execution status return
81da853ecaSopenharmony_ci     * @retval OK: Plugin SetVolume succeeded.
82da853ecaSopenharmony_ci     * @retval ERROR_INVALID_DATA: The value is not in the valid range.
83da853ecaSopenharmony_ci     */
84da853ecaSopenharmony_ci    virtual Status SetVolume(float volume) = 0;
85da853ecaSopenharmony_ci
86da853ecaSopenharmony_ci    /**
87da853ecaSopenharmony_ci     * @brief Get the current audio rendering speed.
88da853ecaSopenharmony_ci     *
89da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
90da853ecaSopenharmony_ci     *
91da853ecaSopenharmony_ci     * @param speed Indicates the pointer to the current rendering speed to obtain.
92da853ecaSopenharmony_ci     * @return  Execution status return
93da853ecaSopenharmony_ci     * @retval OK: Plugin GetSpeed succeeded.
94da853ecaSopenharmony_ci     */
95da853ecaSopenharmony_ci    virtual Status GetSpeed(float& speed) = 0;
96da853ecaSopenharmony_ci
97da853ecaSopenharmony_ci    /**
98da853ecaSopenharmony_ci     * @brief Set the audio rendering speed.
99da853ecaSopenharmony_ci     *
100da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
101da853ecaSopenharmony_ci     *
102da853ecaSopenharmony_ci     * @param speed speed Indicates the pointer to the current rendering speed to obtain.
103da853ecaSopenharmony_ci     * @return  Execution status return
104da853ecaSopenharmony_ci     * @retval OK: Plugin SetSpeed succeeded.
105da853ecaSopenharmony_ci     * @retval ERROR_INVALID_DATA: The value is not in the valid range.
106da853ecaSopenharmony_ci     */
107da853ecaSopenharmony_ci    virtual Status SetSpeed(float speed) = 0;
108da853ecaSopenharmony_ci
109da853ecaSopenharmony_ci    /**
110da853ecaSopenharmony_ci     * @brief Get the audioeffect mode.
111da853ecaSopenharmony_ci     *
112da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
113da853ecaSopenharmony_ci     *
114da853ecaSopenharmony_ci     * @param effectMode speed Indicates the pointer to the current audioeffect mode to obtain.
115da853ecaSopenharmony_ci     * @return  Execution status return
116da853ecaSopenharmony_ci     * @retval OK: Plugin GetAudioEffectMode succeeded.
117da853ecaSopenharmony_ci     * @retval ERROR_INVALID_DATA: The value is not in the valid range.
118da853ecaSopenharmony_ci     */
119da853ecaSopenharmony_ci    virtual Status GetAudioEffectMode(int32_t &effectMode) = 0;
120da853ecaSopenharmony_ci
121da853ecaSopenharmony_ci    /**
122da853ecaSopenharmony_ci     * @brief Set the audio audioeffect mode.
123da853ecaSopenharmony_ci     *
124da853ecaSopenharmony_ci     * This function can be called in any state except DESTROYED and INVALID.
125da853ecaSopenharmony_ci     *
126da853ecaSopenharmony_ci     * @param effectMode speed Indicates the pointer to the current audioeffect mode to obtain.
127da853ecaSopenharmony_ci     * @return  Execution status return
128da853ecaSopenharmony_ci     * @retval OK: Plugin SetAudioEffectMode succeeded.
129da853ecaSopenharmony_ci     * @retval ERROR_INVALID_DATA: The value is not in the valid range.
130da853ecaSopenharmony_ci     */
131da853ecaSopenharmony_ci    virtual Status SetAudioEffectMode(int32_t effectMode) = 0;
132da853ecaSopenharmony_ci
133da853ecaSopenharmony_ci    /**
134da853ecaSopenharmony_ci     * @brief Pauses audio rendering
135da853ecaSopenharmony_ci     *
136da853ecaSopenharmony_ci     * The function is valid only in the RUNNING state. If the pause is successful,
137da853ecaSopenharmony_ci     * the plugin enters the PAUSED state.
138da853ecaSopenharmony_ci     *
139da853ecaSopenharmony_ci     * @return  Execution status return
140da853ecaSopenharmony_ci     * @retval OK: Plugin Pause succeeded.
141da853ecaSopenharmony_ci     */
142da853ecaSopenharmony_ci    virtual Status Pause() = 0;
143da853ecaSopenharmony_ci
144da853ecaSopenharmony_ci    /**
145da853ecaSopenharmony_ci     * @brief Pauses audio rendering without clear device lists
146da853ecaSopenharmony_ci     *
147da853ecaSopenharmony_ci     * The function is valid only in the RUNNING state. If the pause is successful,
148da853ecaSopenharmony_ci     * the plugin enters the PAUSED state.
149da853ecaSopenharmony_ci     *
150da853ecaSopenharmony_ci     * @return  Execution status return
151da853ecaSopenharmony_ci     * @retval OK: Plugin Pause succeeded.
152da853ecaSopenharmony_ci     */
153da853ecaSopenharmony_ci    virtual Status PauseTransitent() = 0;
154da853ecaSopenharmony_ci
155da853ecaSopenharmony_ci    /**
156da853ecaSopenharmony_ci     * @brief Resumes audio rendering
157da853ecaSopenharmony_ci     *
158da853ecaSopenharmony_ci     * The function is valid only in the PAUSED state. If the resume is successful,
159da853ecaSopenharmony_ci     * the plugin enters the RUNNING state.
160da853ecaSopenharmony_ci     *
161da853ecaSopenharmony_ci     * @return  Execution status return
162da853ecaSopenharmony_ci     * @retval OK: Plugin Resume succeeded.
163da853ecaSopenharmony_ci     */
164da853ecaSopenharmony_ci    virtual Status Resume() = 0;
165da853ecaSopenharmony_ci
166da853ecaSopenharmony_ci    /**
167da853ecaSopenharmony_ci     * @brief Get the estimated latency of the audio device driver.
168da853ecaSopenharmony_ci     *
169da853ecaSopenharmony_ci     * The function is valid only in the after PREPARED state.
170da853ecaSopenharmony_ci     *
171da853ecaSopenharmony_ci     * @param hstTime  latency times based on {@link HST_TIME_BASE}.
172da853ecaSopenharmony_ci     * @return  Execution status return
173da853ecaSopenharmony_ci     * @retval OK: Plugin GetLatency succeeded.
174da853ecaSopenharmony_ci     */
175da853ecaSopenharmony_ci    virtual Status GetLatency(uint64_t& hstTime) = 0;
176da853ecaSopenharmony_ci
177da853ecaSopenharmony_ci    /**
178da853ecaSopenharmony_ci     * @brief Get the audio frame size, that is, the length (in bytes) of a frame.
179da853ecaSopenharmony_ci     *
180da853ecaSopenharmony_ci     * The function is valid only in the RUNNING state.
181da853ecaSopenharmony_ci     *
182da853ecaSopenharmony_ci     * @param size  size Indicates the pointer to the audio frame size (in bytes).
183da853ecaSopenharmony_ci     * @return  Execution status return
184da853ecaSopenharmony_ci     * @retval OK: Plugin GetFrameSize succeeded.
185da853ecaSopenharmony_ci     */
186da853ecaSopenharmony_ci    virtual Status GetFrameSize(size_t& size) = 0;
187da853ecaSopenharmony_ci
188da853ecaSopenharmony_ci    /**
189da853ecaSopenharmony_ci     * @brief Get the number of audio frames in the audio buffer.
190da853ecaSopenharmony_ci     *
191da853ecaSopenharmony_ci     * The function is valid only in the RUNNING state.
192da853ecaSopenharmony_ci     *
193da853ecaSopenharmony_ci     * @param count Indicates the pointer to the number of audio frames in the audio buffer.
194da853ecaSopenharmony_ci     * @return  Execution status return
195da853ecaSopenharmony_ci     * @retval OK: Plugin GetFrameCount succeeded.
196da853ecaSopenharmony_ci     */
197da853ecaSopenharmony_ci    virtual Status GetFrameCount(uint32_t& count) = 0;
198da853ecaSopenharmony_ci
199da853ecaSopenharmony_ci    /**
200da853ecaSopenharmony_ci     * @brief Writes a frame of output data into the audio driver for rendering.
201da853ecaSopenharmony_ci     *
202da853ecaSopenharmony_ci     * The function is valid only in the RUNNING state.
203da853ecaSopenharmony_ci     *
204da853ecaSopenharmony_ci     * @param input Indicates the pointer to the frame to write.
205da853ecaSopenharmony_ci     * @return  Execution status return
206da853ecaSopenharmony_ci     * @retval OK: Plugin Write succeeded.
207da853ecaSopenharmony_ci     */
208da853ecaSopenharmony_ci    virtual Status Write(const std::shared_ptr<AVBuffer>& input) = 0;
209da853ecaSopenharmony_ci
210da853ecaSopenharmony_ci    /**
211da853ecaSopenharmony_ci     * @brief Flushes data in the audio buffer.
212da853ecaSopenharmony_ci     *
213da853ecaSopenharmony_ci     * The function is valid only in after RUNNING state.
214da853ecaSopenharmony_ci     *
215da853ecaSopenharmony_ci     * @return  Execution status return
216da853ecaSopenharmony_ci     * @retval OK: Plugin Flush succeeded.
217da853ecaSopenharmony_ci     */
218da853ecaSopenharmony_ci    virtual Status Flush() = 0;
219da853ecaSopenharmony_ci
220da853ecaSopenharmony_ci    /**
221da853ecaSopenharmony_ci     * @brief Drain data to make sure all the data processed.
222da853ecaSopenharmony_ci     *
223da853ecaSopenharmony_ci     * The function is valid only in RUNNING state.
224da853ecaSopenharmony_ci     *
225da853ecaSopenharmony_ci     * @return  Execution status return
226da853ecaSopenharmony_ci     * @retval OK: Plugin Drain succeeded.
227da853ecaSopenharmony_ci     */
228da853ecaSopenharmony_ci    virtual Status Drain() = 0;
229da853ecaSopenharmony_ci
230da853ecaSopenharmony_ci    virtual int64_t GetPlayedOutDurationUs(int64_t nowUs) = 0;
231da853ecaSopenharmony_ci
232da853ecaSopenharmony_ci    virtual Status GetFramePosition(int32_t &framePosition) = 0;
233da853ecaSopenharmony_ci
234da853ecaSopenharmony_ci    virtual void SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver>& receiver) = 0;
235da853ecaSopenharmony_ci
236da853ecaSopenharmony_ci    virtual int32_t SetVolumeWithRamp(float targetVolume, int32_t duration) = 0;
237da853ecaSopenharmony_ci
238da853ecaSopenharmony_ci    virtual Status SetMuted(bool isMuted) = 0;
239da853ecaSopenharmony_ci    virtual AudioSampleFormat GetSampleFormat()
240da853ecaSopenharmony_ci    {
241da853ecaSopenharmony_ci        return INVALID_WIDTH;
242da853ecaSopenharmony_ci    }
243da853ecaSopenharmony_ci};
244da853ecaSopenharmony_ci
245da853ecaSopenharmony_ci/// Audio sink plugin api major number.
246da853ecaSopenharmony_ci#define AUDIO_SINK_API_VERSION_MAJOR (1)
247da853ecaSopenharmony_ci
248da853ecaSopenharmony_ci/// Audio sink plugin api minor number
249da853ecaSopenharmony_ci#define AUDIO_SINK_API_VERSION_MINOR (0)
250da853ecaSopenharmony_ci
251da853ecaSopenharmony_ci/// Audio sink plugin version
252da853ecaSopenharmony_ci#define AUDIO_SINK_API_VERSION MAKE_VERSION(AUDIO_SINK_API_VERSION_MAJOR, AUDIO_SINK_API_VERSION_MINOR)
253da853ecaSopenharmony_ci
254da853ecaSopenharmony_ci/**
255da853ecaSopenharmony_ci * @brief Describes the audio sink plugin information.
256da853ecaSopenharmony_ci *
257da853ecaSopenharmony_ci * @since 1.0
258da853ecaSopenharmony_ci * @version 1.0
259da853ecaSopenharmony_ci */
260da853ecaSopenharmony_cistruct AudioSinkPluginDef : public Plugins::PluginDefBase {
261da853ecaSopenharmony_ci    AudioSinkPluginDef()
262da853ecaSopenharmony_ci    {
263da853ecaSopenharmony_ci        apiVersion = AUDIO_SINK_API_VERSION; ///< Audio sink plugin version.
264da853ecaSopenharmony_ci        pluginType = Plugins::PluginType::AUDIO_SINK; ///< Plugin type, MUST be AUDIO_SINK.
265da853ecaSopenharmony_ci    }
266da853ecaSopenharmony_ci};
267da853ecaSopenharmony_ci} // namespace Plugin
268da853ecaSopenharmony_ci} // namespace Media
269da853ecaSopenharmony_ci} // namespace OHOS
270da853ecaSopenharmony_ci#endif // HISTREAMER_PLUGIN_INTF_AUDIO_SINK_PLUGIN_H
271