• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 CJ_AVTRANSCODER_H
17 #define CJ_AVTRANSCODER_H
18 
19 #include "ffi_remote_data.h"
20 #include "transcoder.h"
21 #include "recorder.h"
22 #include "avcodec_info.h"
23 #include "media_log.h"
24 #include "avtranscoder_ffi.h"
25 
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "CJAVTranscoder"};
28 }
29 
30 namespace OHOS {
31 namespace Media {
32 
33 struct EnumClassHash {
34     template <typename T>
operatorEnumClassHash35     std::size_t operator()(T t) const
36     {
37         return static_cast<std::size_t>(t);
38     }
39 };
40 
41 enum class CJAVTranscoderEvent {
42     EVENT_PROGRESS_UPDATE,
43     EVENT_COMPLETE,
44     EVENT_ERROR
45 };
46 
47 enum class CjAVTransCoderState {
48     STATE_IDLE,
49     STATE_PREPARED,
50     STATE_STARTED,
51     STATE_PAUSED,
52     STATE_CANCELLED,
53     STATE_COMPLETED,
54     STATE_RELEASED,
55     STATE_ERROR
56 };
57 
58 enum class CjAVTransCoderOpt {
59     PREPARE,
60     START,
61     PAUSE,
62     RESUME,
63     CANCEL,
64     RELEASE,
65     SET_FD
66 };
67 
68 constexpr int32_t CJAVTRANSCODER_DEFAULT_AUDIO_BIT_RATE = 48000;
69 constexpr int32_t CJAVTRANSCODER_DEFAULT_VIDEO_BIT_RATE = -1;
70 constexpr int32_t CJAVTRANSCODER_DEFAULT_FRAME_HEIGHT = -1;
71 constexpr int32_t CJAVTRANSCODER_DEFAULT_FRAME_WIDTH = -1;
72 
73 struct CjAVTransCoderConfig {
74     int32_t audioBitrate = CJAVTRANSCODER_DEFAULT_AUDIO_BIT_RATE;
75     AudioCodecFormat audioCodecFormat = AudioCodecFormat::AUDIO_DEFAULT;
76     OutputFormatType fileFormat = OutputFormatType::FORMAT_DEFAULT;
77     int32_t videoBitrate = CJAVTRANSCODER_DEFAULT_VIDEO_BIT_RATE;
78     VideoCodecFormat videoCodecFormat = VideoCodecFormat::VIDEO_DEFAULT;
79     int32_t videoFrameWidth = CJAVTRANSCODER_DEFAULT_FRAME_HEIGHT;
80     int32_t videoFrameHeight = CJAVTRANSCODER_DEFAULT_FRAME_WIDTH;
81 };
82 
83 class FFI_EXPORT CJAVTranscoder : public OHOS::FFI::FFIData {
84     DECL_TYPE(CJAVTranscoder, OHOS::FFI::FFIData)
85 public:
86     static int64_t CreateAVTranscoder(int32_t* errorcode);
87 
88     int32_t Prepare(std::shared_ptr<TransCoder> transCoder, const CAVTransCoderConfig &cconfig);
89     int32_t Start(std::shared_ptr<TransCoder> transCoder);
90     int32_t Pause(std::shared_ptr<TransCoder> transCoder);
91     int32_t Resume(std::shared_ptr<TransCoder> transCoder);
92     int32_t Cancel(std::shared_ptr<TransCoder> transCoder);
93     int32_t Release(std::shared_ptr<TransCoder> transCoder);
94 
95     CAVFileDescriptor GetInputFile();
96     int32_t SetInputFile(std::shared_ptr<TransCoder> transCoder, CAVFileDescriptor fdSrc);
97 
98     int32_t GetOutputFile();
99     int32_t SetOutputFile(std::shared_ptr<TransCoder> transCoder, int32_t fdDst);
100 
101     int32_t OnProgressUpdate(int64_t callbackId);
102     int32_t OffProgressUpdate();
103     int32_t OnComplete(int64_t callbackId);
104     int32_t OffComplete();
105     int32_t OnError(int64_t callbackId);
106     int32_t OffError();
107 
108     std::shared_ptr<TransCoder> transCoder_ = nullptr;
109     std::shared_ptr<TransCoderCallback> transCoderCb_ = nullptr;
110 
111 private:
112     bool hasConfiged_ = false;
113 
114     struct AVFileDescriptor fdSrc_;
115     int32_t fdDst_;
116 
117     int32_t GetReturnRet(int32_t errCode);
118     int32_t GetAudioCodecFormat(const std::string &mime, AudioCodecFormat &codecFormat);
119     int32_t GetVideoCodecFormat(const std::string &mime, VideoCodecFormat &codecFormat);
120     int32_t GetOutputFormat(const std::string &extension, OutputFormatType &type);
121     int32_t GetConfig(const CAVTransCoderConfig &cconfig, CjAVTransCoderConfig &config);
122 
123     void StateCallback(CjAVTransCoderState state);
124     void CancelCallback();
125     int32_t CheckStateMachine(CjAVTransCoderOpt opt);
126     int32_t CheckRepeatOperation(CjAVTransCoderOpt opt);
127 };
128 }
129 }
130 
131 #endif // CJ_AVTRANSCODER_H