1 /*
2 * Copyright (c) 2022 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 "image_source_encoder.h"
17
18 #include <securec.h>
19 #include <stddef.h>
20
21 #include "dscreen_constants.h"
22 #include "dscreen_errcode.h"
23 #include "dscreen_hisysevent.h"
24 #include "dscreen_log.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
ConfigureEncoder(const VideoParam & configParam)28 int32_t ImageSourceEncoder::ConfigureEncoder(const VideoParam &configParam)
29 {
30 DHLOGI("%s: ConfigureEncoder.", LOG_TAG);
31 int32_t ret = InitVideoEncoder(configParam);
32 if (ret != DH_SUCCESS) {
33 DHLOGE("%s: Init encoder failed ret:%d.", LOG_TAG, ret);
34 return ret;
35 }
36
37 ret = SetEncoderFormat(configParam);
38 if (ret != DH_SUCCESS) {
39 DHLOGE("%s: Set encoder format failed ret:%d.", LOG_TAG, ret);
40 return ret;
41 }
42
43 videoSurface_ = videoEncoder_->CreateInputSurface();
44 if (videoSurface_ == nullptr) {
45 DHLOGE("%s: Create encoder surface failed.", LOG_TAG);
46 return ERR_DH_SCREEN_CODEC_SURFACE_ERROR;
47 }
48
49 return DH_SUCCESS;
50 }
51
ReleaseEncoder()52 int32_t ImageSourceEncoder::ReleaseEncoder()
53 {
54 DHLOGI("%s: ReleaseEncoder.", LOG_TAG);
55 if (videoEncoder_ == nullptr) {
56 DHLOGE("%s: Encoder is null.", LOG_TAG);
57 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
58 }
59
60 int32_t ret = videoEncoder_->Release();
61 if (ret != Media::MSERR_OK) {
62 DHLOGE("%s: Release encoder failed.", LOG_TAG);
63 return ERR_DH_SCREEN_CODEC_RELEASE_FAILED;
64 }
65 encodeVideoCallback_ = nullptr;
66 videoEncoder_ = nullptr;
67
68 return DH_SUCCESS;
69 }
70
StartEncoder()71 int32_t ImageSourceEncoder::StartEncoder()
72 {
73 DHLOGI("%s: StartEncoder.", LOG_TAG);
74 if (videoEncoder_ == nullptr) {
75 DHLOGE("%s: Encoder is null.", LOG_TAG);
76 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
77 }
78
79 int32_t ret = videoEncoder_->Prepare();
80 if (ret != Media::MSERR_OK) {
81 DHLOGE("%s: Prepare encoder failed.", LOG_TAG);
82 return ERR_DH_SCREEN_CODEC_PREPARE_FAILED;
83 }
84
85 ret = videoEncoder_->Start();
86 if (ret != Media::MSERR_OK) {
87 DHLOGE("%s: Start encoder failed.", LOG_TAG);
88 return ERR_DH_SCREEN_CODEC_START_FAILED;
89 }
90
91 return DH_SUCCESS;
92 }
93
StopEncoder()94 int32_t ImageSourceEncoder::StopEncoder()
95 {
96 DHLOGI("%s: StopEncoder.", LOG_TAG);
97 if (videoEncoder_ == nullptr) {
98 DHLOGE("%s: Encoder is null.", LOG_TAG);
99 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
100 }
101
102 int32_t ret = videoEncoder_->Flush();
103 if (ret != Media::MSERR_OK) {
104 DHLOGE("%s: Flush encoder failed.", LOG_TAG);
105 }
106
107 ret = videoEncoder_->Stop();
108 if (ret != Media::MSERR_OK) {
109 DHLOGE("%s: Stop encoder failed.", LOG_TAG);
110 return ERR_DH_SCREEN_CODEC_STOP_FAILED;
111 }
112
113 return DH_SUCCESS;
114 }
115
InitVideoEncoder(const VideoParam & configParam)116 int32_t ImageSourceEncoder::InitVideoEncoder(const VideoParam &configParam)
117 {
118 DHLOGI("%s: InitVideoEncoder.", LOG_TAG);
119 switch (configParam.GetCodecType()) {
120 case VIDEO_CODEC_TYPE_VIDEO_H264:
121 videoEncoder_ = Media::VideoEncoderFactory::CreateByMime("video/avc");
122 break;
123 case VIDEO_CODEC_TYPE_VIDEO_H265:
124 videoEncoder_ = Media::VideoEncoderFactory::CreateByMime("video/hevc");
125 break;
126 case VIDEO_CODEC_TYPE_VIDEO_MPEG4:
127 videoEncoder_ = Media::VideoEncoderFactory::CreateByMime("video/mp4v-es");
128 break;
129 default:
130 DHLOGE("%s: codecType is invalid!", LOG_TAG);
131 videoEncoder_ = nullptr;
132 }
133
134 if (videoEncoder_ == nullptr) {
135 DHLOGE("%s: Create videoEncoder failed.", LOG_TAG);
136 return ERR_DH_SCREEN_TRANS_CREATE_CODEC_FAILED;
137 }
138
139 encodeVideoCallback_ = std::make_shared<ImageEncoderCallback>(shared_from_this());
140 int32_t ret = videoEncoder_->SetCallback(encodeVideoCallback_);
141 if (ret != Media::MSERR_OK) {
142 DHLOGE("%s: Set codec callback failed.", LOG_TAG);
143 return ERR_DH_SCREEN_CODEC_SET_CALLBACK_FAILED;
144 }
145
146 return DH_SUCCESS;
147 }
148
SetEncoderFormat(const VideoParam & configParam)149 int32_t ImageSourceEncoder::SetEncoderFormat(const VideoParam &configParam)
150 {
151 DHLOGI("%s: SetEncoderFormat.", LOG_TAG);
152 if (videoEncoder_ == nullptr) {
153 DHLOGE("%s: Encoder is null.", LOG_TAG);
154 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
155 }
156
157 switch (configParam.GetCodecType()) {
158 case VIDEO_CODEC_TYPE_VIDEO_H264:
159 imageFormat_.PutStringValue("codec_mime", "video/avc");
160 break;
161 case VIDEO_CODEC_TYPE_VIDEO_H265:
162 imageFormat_.PutStringValue("codec_mime", "video/hevc");
163 break;
164 case VIDEO_CODEC_TYPE_VIDEO_MPEG4:
165 imageFormat_.PutStringValue("codec_mime", "video/mp4v-es");
166 break;
167 default:
168 DHLOGE("%s: Codec type is invalid.", LOG_TAG);
169 return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM;
170 }
171 switch (configParam.GetVideoFormat()) {
172 case VIDEO_DATA_FORMAT_YUVI420:
173 imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::YUVI420);
174 break;
175 case VIDEO_DATA_FORMAT_NV12:
176 imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::NV12);
177 break;
178 case VIDEO_DATA_FORMAT_NV21:
179 imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::NV21);
180 break;
181 case VIDEO_DATA_FORMAT_RGBA8888:
182 imageFormat_.PutIntValue("pixel_format", Media::VideoPixelFormat::RGBA);
183 break;
184 default:
185 DHLOGE("%s: Video format is invalid.", LOG_TAG);
186 return ERR_DH_SCREEN_TRANS_ILLEGAL_PARAM;
187 }
188 imageFormat_.PutLongValue("max_input_size", MAX_YUV420_BUFFER_SIZE);
189 imageFormat_.PutIntValue("width", configParam.GetVideoWidth());
190 imageFormat_.PutIntValue("height", configParam.GetVideoHeight());
191 imageFormat_.PutIntValue("frame_rate", configParam.GetFps());
192
193 int32_t ret = videoEncoder_->Configure(imageFormat_);
194 if (ret != Media::MSERR_OK) {
195 DHLOGE("%s: Configure encoder failed.", LOG_TAG);
196 return ERR_DH_SCREEN_CODEC_CONFIGURE_FAILED;
197 }
198 return DH_SUCCESS;
199 }
200
OnError(Media::AVCodecErrorType errorType,int32_t errorCode)201 void ImageSourceEncoder::OnError(Media::AVCodecErrorType errorType, int32_t errorCode)
202 {
203 DHLOGI("%s: Encoder error, errorType(%d), errorCode(%d)", LOG_TAG, errorType, errorCode);
204 std::shared_ptr<IImageSourceProcessorListener> listener = imageProcessorListener_.lock();
205 if (listener == nullptr) {
206 DHLOGE("%s: Processor listener is null", LOG_TAG);
207 return;
208 }
209 listener->OnProcessorStateNotify(errorCode);
210 }
211
OnOutputBufferAvailable(uint32_t index,Media::AVCodecBufferInfo info,Media::AVCodecBufferFlag flag)212 void ImageSourceEncoder::OnOutputBufferAvailable(uint32_t index, Media::AVCodecBufferInfo info,
213 Media::AVCodecBufferFlag flag)
214 {
215 DHLOGD("%s: OnOutputBufferAvailable.", LOG_TAG);
216 std::shared_ptr<IImageSourceProcessorListener> listener = imageProcessorListener_.lock();
217 if (listener == nullptr) {
218 DHLOGE("%s: Processor listener is null", LOG_TAG);
219 return;
220 }
221 if (videoEncoder_ == nullptr) {
222 DHLOGE("%s: Encoder is null.", LOG_TAG);
223 return;
224 }
225
226 encoderBufferInfo_ = info;
227 videoSharedMemory_ = videoEncoder_->GetOutputBuffer(index);
228 if (videoSharedMemory_ == nullptr) {
229 DHLOGE("%s: GetOutputBuffer failed.", LOG_TAG);
230 return;
231 }
232
233 size_t dataSize = static_cast<size_t>(info.size);
234 if (dataSize == 0 || dataSize > DATA_BUFFER_MAX_SIZE) {
235 DHLOGE("%s:OnOutputBufferAvailable params invalid, size: %d.", LOG_TAG, dataSize);
236 return;
237 }
238 auto dataBuf = std::make_shared<DataBuffer>(dataSize);
239 int32_t ret = memcpy_s(dataBuf->Data(), dataBuf->Capacity(), videoSharedMemory_->GetBase(), dataSize);
240 if (ret != EOK) {
241 DHLOGE("%s: Copy data failed.", LOG_TAG);
242 return;
243 }
244 listener->OnImageProcessDone(dataBuf);
245
246 ret = videoEncoder_->ReleaseOutputBuffer(index);
247 if (ret != Media::MSERR_OK) {
248 DHLOGE("%s: videoEncoder ReleaseOutputBuffer failed.", LOG_TAG);
249 }
250 }
251
OnInputBufferAvailable(uint32_t index)252 void ImageSourceEncoder::OnInputBufferAvailable(uint32_t index)
253 {
254 (void) index;
255 }
256
OnOutputFormatChanged(const Media::Format & format)257 void ImageSourceEncoder::OnOutputFormatChanged(const Media::Format &format)
258 {
259 (void) format;
260 }
261 } // namespace DistributedHardware
262 } // namespace OHOS