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