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 "videoenc_inner_mock.h"
17 #include "avformat_inner_mock.h"
18 #include "avmemory_inner_mock.h"
19 #include "surface_inner_mock.h"
20
21 namespace OHOS {
22 namespace MediaAVCodec {
VideoEncCallbackMock(std::shared_ptr<AVCodecCallbackMock> cb,std::weak_ptr<AVCodecVideoEncoder> ve)23 VideoEncCallbackMock::VideoEncCallbackMock(std::shared_ptr<AVCodecCallbackMock> cb,
24 std::weak_ptr<AVCodecVideoEncoder> ve)
25 : mockCb_(cb), videoEnc_(ve)
26 {
27 }
28
OnError(AVCodecErrorType errorType,int32_t errorCode)29 void VideoEncCallbackMock::OnError(AVCodecErrorType errorType, int32_t errorCode)
30 {
31 (void)errorType;
32 if (mockCb_ != nullptr) {
33 mockCb_->OnError(errorCode);
34 }
35 }
36
OnOutputFormatChanged(const Format & format)37 void VideoEncCallbackMock::OnOutputFormatChanged(const Format &format)
38 {
39 if (mockCb_ != nullptr) {
40 auto formatMock = std::make_shared<AVFormatInnerMock>(format);
41 mockCb_->OnStreamChanged(formatMock);
42 }
43 }
44
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVSharedMemory> buffer)45 void VideoEncCallbackMock::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer)
46 {
47 auto videoEnc = videoEnc_.lock();
48 if (mockCb_ != nullptr && videoEnc != nullptr) {
49 if (buffer != nullptr) {
50 std::shared_ptr<AVMemoryMock> memMock =
51 buffer == nullptr ? nullptr : std::make_shared<AVMemoryInnerMock>(buffer);
52 mockCb_->OnNeedInputData(index, memMock);
53 }
54 }
55 }
56
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag,std::shared_ptr<AVSharedMemory> buffer)57 void VideoEncCallbackMock::OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
58 std::shared_ptr<AVSharedMemory> buffer)
59 {
60 if (mockCb_ != nullptr) {
61 struct OH_AVCodecBufferAttr bufferInfo;
62 bufferInfo.pts = info.presentationTimeUs;
63 bufferInfo.size = info.size;
64 bufferInfo.offset = info.offset;
65 bufferInfo.flags = flag;
66 std::shared_ptr<AVMemoryMock> memMock =
67 buffer == nullptr ? nullptr : std::make_shared<AVMemoryInnerMock>(buffer);
68 return mockCb_->OnNewOutputData(index, memMock, bufferInfo);
69 }
70 }
71
SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)72 int32_t VideoEncInnerMock::SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)
73 {
74 if (cb != nullptr) {
75 auto callback = std::make_shared<VideoEncCallbackMock>(cb, videoEnc_);
76 if (videoEnc_ != nullptr && callback != nullptr) {
77 return videoEnc_->SetCallback(callback);
78 }
79 }
80 return AV_ERR_UNKNOWN;
81 }
82
Configure(std::shared_ptr<FormatMock> format)83 int32_t VideoEncInnerMock::Configure(std::shared_ptr<FormatMock> format)
84 {
85 if (videoEnc_ != nullptr) {
86 auto fmt = std::static_pointer_cast<AVFormatInnerMock>(format);
87 return videoEnc_->Configure(fmt->GetFormat());
88 }
89 return AV_ERR_UNKNOWN;
90 }
91
Start()92 int32_t VideoEncInnerMock::Start()
93 {
94 if (videoEnc_ != nullptr) {
95 return videoEnc_->Start();
96 }
97 return AV_ERR_UNKNOWN;
98 }
99
Stop()100 int32_t VideoEncInnerMock::Stop()
101 {
102 if (videoEnc_ != nullptr) {
103 return videoEnc_->Stop();
104 }
105 return AV_ERR_UNKNOWN;
106 }
107
Flush()108 int32_t VideoEncInnerMock::Flush()
109 {
110 if (videoEnc_ != nullptr) {
111 return videoEnc_->Flush();
112 }
113 return AV_ERR_UNKNOWN;
114 }
115
Reset()116 int32_t VideoEncInnerMock::Reset()
117 {
118 if (videoEnc_ != nullptr) {
119 return videoEnc_->Reset();
120 }
121 return AV_ERR_UNKNOWN;
122 }
123
Release()124 int32_t VideoEncInnerMock::Release()
125 {
126 if (videoEnc_ != nullptr) {
127 return videoEnc_->Release();
128 }
129 return AV_ERR_UNKNOWN;
130 }
131
NotifyEos()132 int32_t VideoEncInnerMock::NotifyEos()
133 {
134 if (videoEnc_ != nullptr) {
135 return videoEnc_->NotifyEos();
136 }
137 return AV_ERR_UNKNOWN;
138 }
139
GetOutputDescription()140 std::shared_ptr<FormatMock> VideoEncInnerMock::GetOutputDescription()
141 {
142 if (videoEnc_ != nullptr) {
143 Format format;
144 (void)videoEnc_->GetOutputFormat(format);
145 return std::make_shared<AVFormatInnerMock>(format);
146 }
147 return nullptr;
148 }
149
SetParameter(std::shared_ptr<FormatMock> format)150 int32_t VideoEncInnerMock::SetParameter(std::shared_ptr<FormatMock> format)
151 {
152 if (videoEnc_ != nullptr) {
153 auto fmt = std::static_pointer_cast<AVFormatInnerMock>(format);
154 return videoEnc_->SetParameter(fmt->GetFormat());
155 }
156 return AV_ERR_UNKNOWN;
157 }
158
FreeOutputData(uint32_t index)159 int32_t VideoEncInnerMock::FreeOutputData(uint32_t index)
160 {
161 if (videoEnc_ != nullptr) {
162 return videoEnc_->ReleaseOutputBuffer(index);
163 }
164 return AV_ERR_UNKNOWN;
165 }
166
PushInputData(uint32_t index,OH_AVCodecBufferAttr & attr)167 int32_t VideoEncInnerMock::PushInputData(uint32_t index, OH_AVCodecBufferAttr &attr)
168 {
169 if (videoEnc_ != nullptr) {
170 AVCodecBufferInfo info;
171 info.presentationTimeUs = attr.pts;
172 info.offset = attr.offset;
173 info.size = attr.size;
174 AVCodecBufferFlag flag = static_cast<AVCodecBufferFlag>(attr.flags);
175 return videoEnc_->QueueInputBuffer(index, info, flag);
176 }
177 return AV_ERR_UNKNOWN;
178 }
179
CreateInputSurface()180 std::shared_ptr<SurfaceMock> VideoEncInnerMock::CreateInputSurface()
181 {
182 if (videoEnc_ != nullptr) {
183 sptr<Surface> surface = videoEnc_->CreateInputSurface();
184 if (surface != nullptr) {
185 return std::make_shared<SurfaceInnerMock>(surface);
186 }
187 }
188 return nullptr;
189 }
190
IsValid()191 bool VideoEncInnerMock::IsValid()
192 {
193 return true;
194 }
195 } // namespace MediaAVCodec
196 } // namespace OHOS