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