1049e185fSopenharmony_ci/*
2049e185fSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3049e185fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4049e185fSopenharmony_ci * you may not use this file except in compliance with the License.
5049e185fSopenharmony_ci * You may obtain a copy of the License at
6049e185fSopenharmony_ci *
7049e185fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8049e185fSopenharmony_ci *
9049e185fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10049e185fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11049e185fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12049e185fSopenharmony_ci * See the License for the specific language governing permissions and
13049e185fSopenharmony_ci * limitations under the License.
14049e185fSopenharmony_ci */
15049e185fSopenharmony_ci
16049e185fSopenharmony_ci#ifndef TRANSCODER_H
17049e185fSopenharmony_ci#define TRANSCODER_H
18049e185fSopenharmony_ci
19049e185fSopenharmony_ci#include <cstdint>
20049e185fSopenharmony_ci#include <string>
21049e185fSopenharmony_ci#include <map>
22049e185fSopenharmony_ci#include <set>
23049e185fSopenharmony_ci#include <parcel.h>
24049e185fSopenharmony_ci#include "meta/format.h"
25049e185fSopenharmony_ci#include "surface.h"
26049e185fSopenharmony_ci#include "recorder.h"
27049e185fSopenharmony_ci#include "av_common.h"
28049e185fSopenharmony_ci#include "codec_capability.h"
29049e185fSopenharmony_ci#include "media_core.h"
30049e185fSopenharmony_ci
31049e185fSopenharmony_cinamespace OHOS {
32049e185fSopenharmony_cinamespace Media {
33049e185fSopenharmony_ciusing ConfigMap = std::map<std::string, int32_t>;
34049e185fSopenharmony_ci
35049e185fSopenharmony_ci/**
36049e185fSopenharmony_ci * @brief Enumerates transcodering error types.
37049e185fSopenharmony_ci *
38049e185fSopenharmony_ci * @since 5.0
39049e185fSopenharmony_ci * @version 5.0
40049e185fSopenharmony_ci */
41049e185fSopenharmony_cienum TransCoderErrorType : int32_t {
42049e185fSopenharmony_ci    TRANSCODER_ERROR_INTERNAL
43049e185fSopenharmony_ci};
44049e185fSopenharmony_ci
45049e185fSopenharmony_cienum TransCoderOnInfoType : int32_t {
46049e185fSopenharmony_ci    /* return the current progress of transcoder automatically. */
47049e185fSopenharmony_ci    INFO_TYPE_TRANSCODER_COMPLETED = 0,
48049e185fSopenharmony_ci    /* return the current progress of transcoder automatically. */
49049e185fSopenharmony_ci    INFO_TYPE_PROGRESS_UPDATE = 1,
50049e185fSopenharmony_ci};
51049e185fSopenharmony_ci
52049e185fSopenharmony_ci/**
53049e185fSopenharmony_ci * @brief Provides listeners for transcodering errors and information events.
54049e185fSopenharmony_ci *
55049e185fSopenharmony_ci * @since 5.0
56049e185fSopenharmony_ci * @version 5.0
57049e185fSopenharmony_ci */
58049e185fSopenharmony_ciclass TransCoderCallback {
59049e185fSopenharmony_cipublic:
60049e185fSopenharmony_ci    virtual ~TransCoderCallback() = default;
61049e185fSopenharmony_ci
62049e185fSopenharmony_ci    /**
63049e185fSopenharmony_ci     * @brief Called when an error occurs during transcodering. This callback is used to report transcodering errors.
64049e185fSopenharmony_ci     *
65049e185fSopenharmony_ci     * @param errorType Indicates the error type. For details, see {@link TransCoderErrorType}.
66049e185fSopenharmony_ci     * @param errorCode Indicates the error code.
67049e185fSopenharmony_ci     * @since 1.0
68049e185fSopenharmony_ci     * @version 1.0
69049e185fSopenharmony_ci     */
70049e185fSopenharmony_ci    virtual void OnError(int32_t errorCode, const std::string &errorMsg) = 0;
71049e185fSopenharmony_ci
72049e185fSopenharmony_ci    /**
73049e185fSopenharmony_ci     * @brief Called when an information event occurs during transcodering. This callback is used to report
74049e185fSopenharmony_ci     * transcodering information.
75049e185fSopenharmony_ci     *
76049e185fSopenharmony_ci     * @param type Indicates the information type. For details, see {@link TransCoderInfoType}.
77049e185fSopenharmony_ci     * @param extra Indicates other information, for example, the start time position of a transcodering file.
78049e185fSopenharmony_ci     * @since 1.0
79049e185fSopenharmony_ci     * @version 1.0
80049e185fSopenharmony_ci     */
81049e185fSopenharmony_ci    virtual void OnInfo(int32_t type, int32_t extra) = 0;
82049e185fSopenharmony_ci};
83049e185fSopenharmony_ci
84049e185fSopenharmony_ci/**
85049e185fSopenharmony_ci * @brief Provides functions for audio and video transcodering.
86049e185fSopenharmony_ci *
87049e185fSopenharmony_ci * @since 1.0
88049e185fSopenharmony_ci * @version 1.0
89049e185fSopenharmony_ci */
90049e185fSopenharmony_ciclass TransCoder {
91049e185fSopenharmony_cipublic:
92049e185fSopenharmony_ci    virtual ~TransCoder() = default;
93049e185fSopenharmony_ci
94049e185fSopenharmony_ci    /**
95049e185fSopenharmony_ci     * @brief Sets the output file format.
96049e185fSopenharmony_ci     *
97049e185fSopenharmony_ci     * This function must be called before {@link Prepare} and after after all required sources have been set. After
98049e185fSopenharmony_ci     * this function called, no more source settings allowed.
99049e185fSopenharmony_ci     *
100049e185fSopenharmony_ci     * @param format Indicates the output file format. For details, see {@link OutputFormatType}.
101049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
102049e185fSopenharmony_ci     * @since 1.0
103049e185fSopenharmony_ci     * @version 1.0
104049e185fSopenharmony_ci     */
105049e185fSopenharmony_ci    virtual int32_t SetOutputFormat(OutputFormatType format) = 0;
106049e185fSopenharmony_ci
107049e185fSopenharmony_ci    /**
108049e185fSopenharmony_ci     * @brief Sets the encoder of the video to transcoder.
109049e185fSopenharmony_ci     *
110049e185fSopenharmony_ci     * If this function is not called, the output file does not contain the video track when the video source is
111049e185fSopenharmony_ci     * YUV or RGB.
112049e185fSopenharmony_ci     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
113049e185fSopenharmony_ci     *
114049e185fSopenharmony_ci     * @param encoder Indicates the video encoder to set. For details, see {@link VideoCodecFormat}.
115049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
116049e185fSopenharmony_ci     * @since 1.0
117049e185fSopenharmony_ci     * @version 1.0
118049e185fSopenharmony_ci     */
119049e185fSopenharmony_ci    virtual int32_t SetVideoEncoder(VideoCodecFormat encoder) = 0;
120049e185fSopenharmony_ci
121049e185fSopenharmony_ci    /**
122049e185fSopenharmony_ci     * @brief Sets the encoding bit rate of the video to transcoder.
123049e185fSopenharmony_ci     *
124049e185fSopenharmony_ci     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
125049e185fSopenharmony_ci     *
126049e185fSopenharmony_ci     * @param rate Indicates the encoding bit rate to set.
127049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
128049e185fSopenharmony_ci     * @since 1.0
129049e185fSopenharmony_ci     * @version 1.0
130049e185fSopenharmony_ci     */
131049e185fSopenharmony_ci    virtual int32_t SetVideoEncodingBitRate(int32_t rate) = 0;
132049e185fSopenharmony_ci
133049e185fSopenharmony_ci    /**
134049e185fSopenharmony_ci     * @brief Sets the encoding video size of the video to transcoder.
135049e185fSopenharmony_ci     *
136049e185fSopenharmony_ci     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
137049e185fSopenharmony_ci     *
138049e185fSopenharmony_ci     * @param rate Indicates the encoding bit rate to set.
139049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
140049e185fSopenharmony_ci     * @since 1.0
141049e185fSopenharmony_ci     * @version 1.0
142049e185fSopenharmony_ci     */
143049e185fSopenharmony_ci    virtual int32_t SetVideoSize(int32_t videoFrameWidth, int32_t videoFrameHeight) = 0;
144049e185fSopenharmony_ci
145049e185fSopenharmony_ci    /**
146049e185fSopenharmony_ci     * @brief Sets the encoder of the audio to transcoder.
147049e185fSopenharmony_ci     *
148049e185fSopenharmony_ci     * If this function is not called, the output file does not contain the audio track.
149049e185fSopenharmony_ci     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
150049e185fSopenharmony_ci     *
151049e185fSopenharmony_ci     * @param encoder Indicates the audio encoder to set.
152049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
153049e185fSopenharmony_ci     * @since 1.0
154049e185fSopenharmony_ci     * @version 1.0
155049e185fSopenharmony_ci     */
156049e185fSopenharmony_ci    virtual int32_t SetAudioEncoder(AudioCodecFormat encoder) = 0;
157049e185fSopenharmony_ci
158049e185fSopenharmony_ci    /**
159049e185fSopenharmony_ci     * @brief Sets the encoding bit rate of the audio to transcoder.
160049e185fSopenharmony_ci     *
161049e185fSopenharmony_ci     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}.
162049e185fSopenharmony_ci     *
163049e185fSopenharmony_ci     * @param bitRate Indicates the audio encoding bit rate, in bit/s.
164049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
165049e185fSopenharmony_ci     * @since 1.0
166049e185fSopenharmony_ci     * @version 1.0
167049e185fSopenharmony_ci     */
168049e185fSopenharmony_ci    virtual int32_t SetAudioEncodingBitRate(int32_t bitRate) = 0;
169049e185fSopenharmony_ci
170049e185fSopenharmony_ci    /**
171049e185fSopenharmony_ci     * @brief Sets the file descriptor (FD) of the input file.
172049e185fSopenharmony_ci     *
173049e185fSopenharmony_ci     * @param fd Indicates the FD of the file.
174049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
175049e185fSopenharmony_ci     * @since 1.0
176049e185fSopenharmony_ci     * @version 1.0
177049e185fSopenharmony_ci     */
178049e185fSopenharmony_ci    virtual int32_t SetInputFile(int32_t fd, int64_t offset, int64_t size) = 0;
179049e185fSopenharmony_ci
180049e185fSopenharmony_ci    /**
181049e185fSopenharmony_ci     * @brief Sets the file descriptor (FD) of the output file.
182049e185fSopenharmony_ci     *
183049e185fSopenharmony_ci     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}
184049e185fSopenharmony_ci     *
185049e185fSopenharmony_ci     * @param fd Indicates the FD of the file.
186049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
187049e185fSopenharmony_ci     * @since 1.0
188049e185fSopenharmony_ci     * @version 1.0
189049e185fSopenharmony_ci     */
190049e185fSopenharmony_ci    virtual int32_t SetOutputFile(int32_t fd) = 0;
191049e185fSopenharmony_ci
192049e185fSopenharmony_ci    /**
193049e185fSopenharmony_ci     * @brief Registers a transcodering listener.
194049e185fSopenharmony_ci     *
195049e185fSopenharmony_ci     * This function must be called after {@link SetOutputFormat} but before {@link Prepare}
196049e185fSopenharmony_ci     *
197049e185fSopenharmony_ci     * @param callback Indicates the transcodering listener to register. For details, see {@link TransCoderCallback}.
198049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the setting is successful; returns an error code otherwise.
199049e185fSopenharmony_ci     * @since 1.0
200049e185fSopenharmony_ci     * @version 1.0
201049e185fSopenharmony_ci     */
202049e185fSopenharmony_ci    virtual int32_t SetTransCoderCallback(const std::shared_ptr<TransCoderCallback> &callback) = 0;
203049e185fSopenharmony_ci
204049e185fSopenharmony_ci    /**
205049e185fSopenharmony_ci     * @brief Prepares for transcodering.
206049e185fSopenharmony_ci     *
207049e185fSopenharmony_ci     * This function must be called before {@link Start}.
208049e185fSopenharmony_ci     *
209049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the preparation is successful; returns an error code otherwise.
210049e185fSopenharmony_ci     * @since 1.0
211049e185fSopenharmony_ci     * @version 1.0
212049e185fSopenharmony_ci     */
213049e185fSopenharmony_ci    virtual int32_t Prepare() = 0;
214049e185fSopenharmony_ci
215049e185fSopenharmony_ci    /**
216049e185fSopenharmony_ci     * @brief Starts transcodering.
217049e185fSopenharmony_ci     *
218049e185fSopenharmony_ci     * This function must be called after {@link Prepare}.
219049e185fSopenharmony_ci     *
220049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the transcodering is started; returns an error code otherwise.
221049e185fSopenharmony_ci     * @since 1.0
222049e185fSopenharmony_ci     * @version 1.0
223049e185fSopenharmony_ci     */
224049e185fSopenharmony_ci    virtual int32_t Start() = 0;
225049e185fSopenharmony_ci
226049e185fSopenharmony_ci    /**
227049e185fSopenharmony_ci     * @brief Pauses transcodering.
228049e185fSopenharmony_ci     *
229049e185fSopenharmony_ci     * After {@link Start} is called, you can call this function to pause transcodering.
230049e185fSopenharmony_ci     *
231049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the transcodering is paused; returns an error code otherwise.
232049e185fSopenharmony_ci     * @since 1.0
233049e185fSopenharmony_ci     * @version 1.0
234049e185fSopenharmony_ci     */
235049e185fSopenharmony_ci    virtual int32_t Pause() = 0;
236049e185fSopenharmony_ci
237049e185fSopenharmony_ci    /**
238049e185fSopenharmony_ci    * @brief Resumes transcodering.
239049e185fSopenharmony_ci    *
240049e185fSopenharmony_ci    * You can call this function to resume transcodering after {@link Pause} is called.
241049e185fSopenharmony_ci     *
242049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the transcodering is resumed; returns an error code otherwise.
243049e185fSopenharmony_ci     * @since 1.0
244049e185fSopenharmony_ci     * @version 1.0
245049e185fSopenharmony_ci     */
246049e185fSopenharmony_ci    virtual int32_t Resume() = 0;
247049e185fSopenharmony_ci
248049e185fSopenharmony_ci    /**
249049e185fSopenharmony_ci     * @brief Cancel transcodering.
250049e185fSopenharmony_ci     *
251049e185fSopenharmony_ci     * @param block Indicates the stop mode. The value <b>true</b> indicates that the processing stops after all caches
252049e185fSopenharmony_ci     * are processed, and <b>false</b> indicates that the processing stops immediately and all caches are discarded.
253049e185fSopenharmony_ci     * After the transcodering stopped, all sources and parameters must be set again to restore transcodering.
254049e185fSopenharmony_ci     * The function is like to {@link Reset}, except that the block parameter is allowed to be specified.
255049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the transcodering is stopped; returns an error code otherwise.
256049e185fSopenharmony_ci     * @since 1.0
257049e185fSopenharmony_ci     * @version 1.0
258049e185fSopenharmony_ci     */
259049e185fSopenharmony_ci    virtual int32_t Cancel() = 0;
260049e185fSopenharmony_ci
261049e185fSopenharmony_ci    /**
262049e185fSopenharmony_ci     * @brief Releases transcodering resources. After this function called, none of interfaces of {@link Transcoder}
263049e185fSopenharmony_ci     * can be used.
264049e185fSopenharmony_ci     *
265049e185fSopenharmony_ci     * @return Returns {@link MSERR_OK} if the transcodering is stopped; returns an error code otherwise.
266049e185fSopenharmony_ci     * @since 1.0
267049e185fSopenharmony_ci     * @version 1.0
268049e185fSopenharmony_ci     */
269049e185fSopenharmony_ci    virtual int32_t Release() = 0;
270049e185fSopenharmony_ci};
271049e185fSopenharmony_ci
272049e185fSopenharmony_ciclass __attribute__((visibility("default"))) TransCoderFactory {
273049e185fSopenharmony_cipublic:
274049e185fSopenharmony_ci#ifdef UNSUPPORT_TRANSCODER
275049e185fSopenharmony_ci    static std::shared_ptr<TransCoder> CreateTransCoder()
276049e185fSopenharmony_ci    {
277049e185fSopenharmony_ci        return nullptr;
278049e185fSopenharmony_ci    }
279049e185fSopenharmony_ci#else
280049e185fSopenharmony_ci    static std::shared_ptr<TransCoder> CreateTransCoder();
281049e185fSopenharmony_ci#endif
282049e185fSopenharmony_ciprivate:
283049e185fSopenharmony_ci    TransCoderFactory() = default;
284049e185fSopenharmony_ci    ~TransCoderFactory() = default;
285049e185fSopenharmony_ci};
286049e185fSopenharmony_ci} // namespace Media
287049e185fSopenharmony_ci} // namespace OHOS
288049e185fSopenharmony_ci#endif // TRANSCODER_H
289