1/*
2 * Copyright (C) 2024 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 TRANSCODER_H
17#define TRANSCODER_H
18
19#include <cstdint>
20#include <string>
21#include <map>
22#include <set>
23#include <parcel.h>
24#include "meta/format.h"
25#include "surface.h"
26#include "recorder.h"
27#include "av_common.h"
28#include "codec_capability.h"
29#include "media_core.h"
30
31namespace OHOS {
32namespace Media {
33using ConfigMap = std::map<std::string, int32_t>;
34
35/**
36 * @brief Enumerates transcodering error types.
37 *
38 * @since 5.0
39 * @version 5.0
40 */
41enum TransCoderErrorType : int32_t {
42    TRANSCODER_ERROR_INTERNAL
43};
44
45enum TransCoderOnInfoType : int32_t {
46    /* return the current progress of transcoder automatically. */
47    INFO_TYPE_TRANSCODER_COMPLETED = 0,
48    /* return the current progress of transcoder automatically. */
49    INFO_TYPE_PROGRESS_UPDATE = 1,
50};
51
52/**
53 * @brief Provides listeners for transcodering errors and information events.
54 *
55 * @since 5.0
56 * @version 5.0
57 */
58class TransCoderCallback {
59public:
60    virtual ~TransCoderCallback() = default;
61
62    /**
63     * @brief Called when an error occurs during transcodering. This callback is used to report transcodering errors.
64     *
65     * @param errorType Indicates the error type. For details, see {@link TransCoderErrorType}.
66     * @param errorCode Indicates the error code.
67     * @since 1.0
68     * @version 1.0
69     */
70    virtual void OnError(int32_t errorCode, const std::string &errorMsg) = 0;
71
72    /**
73     * @brief Called when an information event occurs during transcodering. This callback is used to report
74     * transcodering information.
75     *
76     * @param type Indicates the information type. For details, see {@link TransCoderInfoType}.
77     * @param extra Indicates other information, for example, the start time position of a transcodering file.
78     * @since 1.0
79     * @version 1.0
80     */
81    virtual void OnInfo(int32_t type, int32_t extra) = 0;
82};
83
84/**
85 * @brief Provides functions for audio and video transcodering.
86 *
87 * @since 1.0
88 * @version 1.0
89 */
90class TransCoder {
91public:
92    virtual ~TransCoder() = default;
93
94    /**
95     * @brief Sets the output file format.
96     *
97     * This function must be called before {@link Prepare} and after after all required sources have been set. After
98     * this function called, no more source settings allowed.
99     *
100     * @param format Indicates the output file format. For details, see {@link OutputFormatType}.
101     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
102     * @since 1.0
103     * @version 1.0
104     */
105    virtual int32_t SetOutputFormat(OutputFormatType format) = 0;
106
107    /**
108     * @brief Sets the encoder of the video to transcoder.
109     *
110     * If this function is not called, the output file does not contain the video track when the video source is
111     * YUV or RGB.
112     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
113     *
114     * @param encoder Indicates the video encoder to set. For details, see {@link VideoCodecFormat}.
115     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
116     * @since 1.0
117     * @version 1.0
118     */
119    virtual int32_t SetVideoEncoder(VideoCodecFormat encoder) = 0;
120
121    /**
122     * @brief Sets the encoding bit rate of the video to transcoder.
123     *
124     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
125     *
126     * @param rate Indicates the encoding bit rate to set.
127     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
128     * @since 1.0
129     * @version 1.0
130     */
131    virtual int32_t SetVideoEncodingBitRate(int32_t rate) = 0;
132
133    /**
134     * @brief Sets the encoding video size of the video to transcoder.
135     *
136     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
137     *
138     * @param rate Indicates the encoding bit rate to set.
139     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
140     * @since 1.0
141     * @version 1.0
142     */
143    virtual int32_t SetVideoSize(int32_t videoFrameWidth, int32_t videoFrameHeight) = 0;
144
145    /**
146     * @brief Sets the encoder of the audio to transcoder.
147     *
148     * If this function is not called, the output file does not contain the audio track.
149     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
150     *
151     * @param encoder Indicates the audio encoder to set.
152     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
153     * @since 1.0
154     * @version 1.0
155     */
156    virtual int32_t SetAudioEncoder(AudioCodecFormat encoder) = 0;
157
158    /**
159     * @brief Sets the encoding bit rate of the audio to transcoder.
160     *
161     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
162     *
163     * @param bitRate Indicates the audio encoding bit rate, in bit/s.
164     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
165     * @since 1.0
166     * @version 1.0
167     */
168    virtual int32_t SetAudioEncodingBitRate(int32_t bitRate) = 0;
169
170    /**
171     * @brief Sets the file descriptor (FD) of the input file.
172     *
173     * @param fd Indicates the FD of the file.
174     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
175     * @since 1.0
176     * @version 1.0
177     */
178    virtual int32_t SetInputFile(int32_t fd, int64_t offset, int64_t size) = 0;
179
180    /**
181     * @brief Sets the file descriptor (FD) of the output file.
182     *
183     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}
184     *
185     * @param fd Indicates the FD of the file.
186     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
187     * @since 1.0
188     * @version 1.0
189     */
190    virtual int32_t SetOutputFile(int32_t fd) = 0;
191
192    /**
193     * @brief Registers a transcodering listener.
194     *
195     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}
196     *
197     * @param callback Indicates the transcodering listener to register. For details, see {@link TransCoderCallback}.
198     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
199     * @since 1.0
200     * @version 1.0
201     */
202    virtual int32_t SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback) = 0;
203
204    /**
205     * @brief Prepares for transcodering.
206     *
207     * This function must be called before {@link Start}.
208     *
209     * @return Returns {@link MSERR_OK} if the preparation is successful; returns an error code otherwise.
210     * @since 1.0
211     * @version 1.0
212     */
213    virtual int32_t Prepare() = 0;
214
215    /**
216     * @brief Starts transcodering.
217     *
218     * This function must be called after {@link Prepare}.
219     *
220     * @return Returns {@link MSERR_OK} if the transcodering is started; returns an error code otherwise.
221     * @since 1.0
222     * @version 1.0
223     */
224    virtual int32_t Start() = 0;
225
226    /**
227     * @brief Pauses transcodering.
228     *
229     * After {@link Start} is called, you can call this function to pause transcodering.
230     *
231     * @return Returns {@link MSERR_OK} if the transcodering is paused; returns an error code otherwise.
232     * @since 1.0
233     * @version 1.0
234     */
235    virtual int32_t Pause() = 0;
236
237    /**
238    * @brief Resumes transcodering.
239    *
240    * You can call this function to resume transcodering after {@link Pause} is called.
241     *
242     * @return Returns {@link MSERR_OK} if the transcodering is resumed; returns an error code otherwise.
243     * @since 1.0
244     * @version 1.0
245     */
246    virtual int32_t Resume() = 0;
247
248    /**
249     * @brief Cancel transcodering.
250     *
251     * @param block Indicates the stop mode. The value <b>true</b> indicates that the processing stops after all caches
252     * are processed, and <b>false</b> indicates that the processing stops immediately and all caches are discarded.
253     * After the transcodering stopped, all sources and parameters must be set again to restore transcodering.
254     * The function is like to {@link Reset}, except that the block parameter is allowed to be specified.
255     * @return Returns {@link MSERR_OK} if the transcodering is stopped; returns an error code otherwise.
256     * @since 1.0
257     * @version 1.0
258     */
259    virtual int32_t Cancel() = 0;
260
261    /**
262     * @brief Releases transcodering resources. After this function called, none of interfaces of {@link Transcoder}
263     * can be used.
264     *
265     * @return Returns {@link MSERR_OK} if the transcodering is stopped; returns an error code otherwise.
266     * @since 1.0
267     * @version 1.0
268     */
269    virtual int32_t Release() = 0;
270};
271
272class __attribute__((visibility("default"))) TransCoderFactory {
273public:
274#ifdef UNSUPPORT_TRANSCODER
275    static std::shared_ptr<TransCoder> CreateTransCoder()
276    {
277        return nullptr;
278    }
279#else
280    static std::shared_ptr<TransCoder> CreateTransCoder();
281#endif
282private:
283    TransCoderFactory() = default;
284    ~TransCoderFactory() = default;
285};
286} // namespace Media
287} // namespace OHOS
288#endif // TRANSCODER_H
289