1 /*
2 * Copyright (C) 2023 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 #include "avcodec_video_encoder_impl.h"
17 #include "i_avcodec_service.h"
18 #include "avcodec_log.h"
19 #include "avcodec_errors.h"
20 #include "avcodec_dfx.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecVideoEncoderImpl"};
24 }
25
26 namespace OHOS {
27 namespace MediaAVCodec {
CreateByMime(const std::string & mime)28 std::shared_ptr<AVCodecVideoEncoder> VideoEncoderFactory::CreateByMime(const std::string &mime)
29 {
30 AVCODEC_SYNC_TRACE;
31
32 std::shared_ptr<AVCodecVideoEncoderImpl> impl = std::make_shared<AVCodecVideoEncoderImpl>();
33
34 int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_ENCODER, true, mime);
35 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr,
36 "AVCodec video encoder impl init failed");
37
38 return impl;
39 }
40
CreateByName(const std::string & name)41 std::shared_ptr<AVCodecVideoEncoder> VideoEncoderFactory::CreateByName(const std::string &name)
42 {
43 AVCODEC_SYNC_TRACE;
44
45 std::shared_ptr<AVCodecVideoEncoderImpl> impl = std::make_shared<AVCodecVideoEncoderImpl>();
46
47 int32_t ret = impl->Init(AVCODEC_TYPE_VIDEO_ENCODER, false, name);
48 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr,
49 "AVCodec video encoder impl init failed");
50
51 return impl;
52 }
53
Init(AVCodecType type,bool isMimeType,const std::string & name)54 int32_t AVCodecVideoEncoderImpl::Init(AVCodecType type, bool isMimeType, const std::string &name)
55 {
56 AVCODEC_SYNC_TRACE;
57 codecService_ = AVCodecServiceFactory::GetInstance().CreateCodecService();
58 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
59 AVCS_ERR_UNKNOWN, "Codec service create failed");
60
61 return codecService_->Init(type, isMimeType, name);
62 }
63
AVCodecVideoEncoderImpl()64 AVCodecVideoEncoderImpl::AVCodecVideoEncoderImpl()
65 {
66 AVCODEC_LOGD("AVCodecVideoEncoderImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
67 }
68
~AVCodecVideoEncoderImpl()69 AVCodecVideoEncoderImpl::~AVCodecVideoEncoderImpl()
70 {
71 if (codecService_ != nullptr) {
72 (void)AVCodecServiceFactory::GetInstance().DestroyCodecService(codecService_);
73 codecService_ = nullptr;
74 }
75 AVCODEC_LOGD("AVCodecVideoEncoderImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
76 }
77
Configure(const Format & format)78 int32_t AVCodecVideoEncoderImpl::Configure(const Format &format)
79 {
80 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
81 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
82
83 AVCODEC_SYNC_TRACE;
84 return codecService_->Configure(format);
85 }
86
Prepare()87 int32_t AVCodecVideoEncoderImpl::Prepare()
88 {
89 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
90 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
91
92 AVCODEC_SYNC_TRACE;
93 return AVCS_ERR_OK;
94 }
95
Start()96 int32_t AVCodecVideoEncoderImpl::Start()
97 {
98 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
99 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
100
101 AVCODEC_SYNC_TRACE;
102 return codecService_->Start();
103 }
104
Stop()105 int32_t AVCodecVideoEncoderImpl::Stop()
106 {
107 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
108 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
109
110 AVCODEC_SYNC_TRACE;
111 return codecService_->Stop();
112 }
113
Flush()114 int32_t AVCodecVideoEncoderImpl::Flush()
115 {
116 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
117 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
118
119 AVCODEC_SYNC_TRACE;
120 return codecService_->Flush();
121 }
122
NotifyEos()123 int32_t AVCodecVideoEncoderImpl::NotifyEos()
124 {
125 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
126 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
127
128 AVCODEC_SYNC_TRACE;
129 return codecService_->NotifyEos();
130 }
131
Reset()132 int32_t AVCodecVideoEncoderImpl::Reset()
133 {
134 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
135 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
136
137 AVCODEC_SYNC_TRACE;
138 return codecService_->Reset();
139 }
140
Release()141 int32_t AVCodecVideoEncoderImpl::Release()
142 {
143 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
144 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
145
146 AVCODEC_SYNC_TRACE;
147 return codecService_->Release();
148 }
149
CreateInputSurface()150 sptr<Surface> AVCodecVideoEncoderImpl::CreateInputSurface()
151 {
152 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
153 nullptr, "Codec service is nullptr");
154
155 AVCODEC_SYNC_TRACE;
156 surface_ = codecService_->CreateInputSurface();
157 return surface_;
158 }
159
QueueInputBuffer(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag)160 int32_t AVCodecVideoEncoderImpl::QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag)
161 {
162 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
163 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
164
165 AVCODEC_SYNC_TRACE;
166 return codecService_->QueueInputBuffer(index, info, flag);
167 }
168
GetOutputFormat(Format & format)169 int32_t AVCodecVideoEncoderImpl::GetOutputFormat(Format &format)
170 {
171 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
172 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
173
174 AVCODEC_SYNC_TRACE;
175 return codecService_->GetOutputFormat(format);
176 }
177
ReleaseOutputBuffer(uint32_t index)178 int32_t AVCodecVideoEncoderImpl::ReleaseOutputBuffer(uint32_t index)
179 {
180 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
181 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
182
183 AVCODEC_SYNC_TRACE;
184 return codecService_->ReleaseOutputBuffer(index);
185 }
186
SetParameter(const Format & format)187 int32_t AVCodecVideoEncoderImpl::SetParameter(const Format &format)
188 {
189 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
190 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
191
192 AVCODEC_SYNC_TRACE;
193 return codecService_->SetParameter(format);
194 }
195
SetCallback(const std::shared_ptr<AVCodecCallback> & callback)196 int32_t AVCodecVideoEncoderImpl::SetCallback(const std::shared_ptr<AVCodecCallback> &callback)
197 {
198 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
199 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
200 CHECK_AND_RETURN_RET_LOG(callback != nullptr,
201 AVCS_ERR_INVALID_VAL, "Callback is nullptr");
202
203 AVCODEC_SYNC_TRACE;
204 return codecService_->SetCallback(callback);
205 }
206
GetInputFormat(Format & format)207 int32_t AVCodecVideoEncoderImpl::GetInputFormat(Format &format)
208 {
209 CHECK_AND_RETURN_RET_LOG(codecService_ != nullptr,
210 AVCS_ERR_INVALID_OPERATION, "Codec service is nullptr");
211
212 AVCODEC_SYNC_TRACE;
213 return codecService_->GetInputFormat(format);
214 }
215 } // namespace MediaAVCodec
216 } // namespace OHOS
217