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 "screen_capture_native_mock.h"
17 #include "media_log.h"
18
19 using namespace std;
20 using namespace OHOS;
21 using namespace testing::ext;
22 using namespace OHOS::Media::ScreenCaptureTestParam;
23
24 namespace OHOS {
25 namespace Media {
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)26 void ScreenCaptureNativeCallbackMock::OnError(ScreenCaptureErrorType errorType, int32_t errorCode)
27 {
28 std::unique_lock<std::mutex> lock(mutex_);
29 (void)errorType;
30 if (mockCb_ != nullptr) {
31 mockCb_->OnError(errorCode);
32 }
33 }
34
OnAudioBufferAvailable(bool isReady,AudioCaptureSourceType type)35 void ScreenCaptureNativeCallbackMock::OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type)
36 {
37 std::unique_lock<std::mutex> lock(mutex_);
38 if (mockCb_ != nullptr) {
39 mockCb_->OnAudioBufferAvailable(isReady, type);
40 }
41 }
42
OnVideoBufferAvailable(bool isReady)43 void ScreenCaptureNativeCallbackMock::OnVideoBufferAvailable(bool isReady)
44 {
45 std::unique_lock<std::mutex> lock(mutex_);
46 if (mockCb_ != nullptr) {
47 mockCb_->OnVideoBufferAvailable(isReady);
48 }
49 }
50
OnStateChange(AVScreenCaptureStateCode stateCode)51 void ScreenCaptureNativeCallbackMock::OnStateChange(AVScreenCaptureStateCode stateCode)
52 {
53 std::unique_lock<std::mutex> lock(mutex_);
54 if (mockCb_ != nullptr) {
55 mockCb_->OnStateChange(stateCode);
56 }
57 }
58
OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event,ScreenCaptureRect * area)59 void ScreenCaptureNativeCallbackMock::OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event,
60 ScreenCaptureRect* area)
61 {
62 std::unique_lock<std::mutex> lock(mutex_);
63 if (mockCb_ != nullptr) {
64 mockCb_->OnCaptureContentChanged(event, area);
65 }
66 }
67
OnDisplaySelected(uint64_t displayId)68 void ScreenCaptureNativeCallbackMock::OnDisplaySelected(uint64_t displayId)
69 {
70 std::unique_lock<std::mutex> lock(mutex_);
71 if (mockCb_ != nullptr) {
72 mockCb_->OnDisplaySelected(displayId);
73 }
74 }
75
OnRelease()76 void ScreenCaptureNativeCallbackMock::OnRelease()
77 {
78 std::unique_lock<std::mutex> lock(mutex_);
79 mockCb_ = nullptr;
80 }
81
~ScreenCaptureNativeMock()82 ScreenCaptureNativeMock::~ScreenCaptureNativeMock()
83 {
84 MEDIA_LOGI("ScreenCaptureNativeMock::~ScreenCaptureNativeMock");
85 }
86
SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallbackMock> & callback,const bool isErrorCallbackEnabled,const bool isDataCallbackEnabled,const bool isStateChangeCallbackEnabled,const bool isCaptureContentChangeCallbackEnabled)87 int32_t ScreenCaptureNativeMock::SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallbackMock>& callback,
88 const bool isErrorCallbackEnabled, const bool isDataCallbackEnabled, const bool isStateChangeCallbackEnabled,
89 const bool isCaptureContentChangeCallbackEnabled)
90 {
91 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
92 (void)isErrorCallbackEnabled; // Inner is not support to set New ErrorCallback
93 (void)isDataCallbackEnabled; // Inner is not support to set New DataCallback
94 isStateChangeCallbackEnabled_ = isStateChangeCallbackEnabled;
95 isCaptureContentChangeCallbackEnabled_ = isCaptureContentChangeCallbackEnabled;
96 if (callback != nullptr) {
97 cb_ = std::make_shared<ScreenCaptureNativeCallbackMock>(callback, screenCapture_);
98 return screenCapture_->SetScreenCaptureCallback(cb_);
99 }
100 return MSERR_INVALID_OPERATION;
101 }
102
StartScreenCapture()103 int32_t ScreenCaptureNativeMock::StartScreenCapture()
104 {
105 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
106 if (isStateChangeCallbackEnabled_) {
107 int32_t ret = screenCapture_->SetPrivacyAuthorityEnabled();
108 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetPrivacyAuthorityEnabled failed");
109 }
110 return screenCapture_->StartScreenCapture();
111 }
112
StartScreenCaptureWithSurface(const std::any & value)113 int32_t ScreenCaptureNativeMock::StartScreenCaptureWithSurface(const std::any& value)
114 {
115 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
116 if (isStateChangeCallbackEnabled_) {
117 int32_t ret = screenCapture_->SetPrivacyAuthorityEnabled();
118 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetPrivacyAuthorityEnabled failed");
119 }
120 sptr<Surface> surface = std::any_cast<sptr<Surface>>(value);
121 return screenCapture_->StartScreenCaptureWithSurface(surface);
122 }
123
Init(AVScreenCaptureConfig config)124 int32_t ScreenCaptureNativeMock::Init(AVScreenCaptureConfig config)
125 {
126 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
127 return screenCapture_->Init(config);
128 }
129
StopScreenCapture()130 int32_t ScreenCaptureNativeMock::StopScreenCapture()
131 {
132 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
133 return screenCapture_->StopScreenCapture();
134 }
135
StartScreenRecording()136 int32_t ScreenCaptureNativeMock::StartScreenRecording()
137 {
138 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
139 if (isStateChangeCallbackEnabled_) {
140 int32_t ret = screenCapture_->SetPrivacyAuthorityEnabled();
141 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetPrivacyAuthorityEnabled failed");
142 }
143 return screenCapture_->StartScreenRecording();
144 }
145
StopScreenRecording()146 int32_t ScreenCaptureNativeMock::StopScreenRecording()
147 {
148 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
149 return screenCapture_->StopScreenRecording();
150 }
151
Release()152 int32_t ScreenCaptureNativeMock::Release()
153 {
154 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
155 std::shared_ptr<ScreenCaptureCallBack> cb = cb_;
156 ScreenCaptureNativeCallbackMock *callback = static_cast<ScreenCaptureNativeCallbackMock *>(cb.get());
157 if (callback != nullptr) {
158 callback->OnRelease();
159 cb_ = nullptr;
160 }
161 return screenCapture_->Release();
162 }
163
SetMicrophoneEnabled(bool isMicrophone)164 int32_t ScreenCaptureNativeMock::SetMicrophoneEnabled(bool isMicrophone)
165 {
166 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
167 return screenCapture_->SetMicrophoneEnabled(isMicrophone);
168 }
169
SetCanvasRotation(bool canvasRotation)170 int32_t ScreenCaptureNativeMock::SetCanvasRotation(bool canvasRotation)
171 {
172 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
173 return screenCapture_->SetCanvasRotation(canvasRotation);
174 }
175
ResizeCanvas(int32_t width,int32_t height)176 int32_t ScreenCaptureNativeMock::ResizeCanvas(int32_t width, int32_t height)
177 {
178 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
179 return screenCapture_->ResizeCanvas(width, height);
180 }
181
UpdateSurface(const std::any & surface)182 int32_t ScreenCaptureNativeMock::UpdateSurface(const std::any& surface)
183 {
184 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
185 return screenCapture_->UpdateSurface(std::any_cast<sptr<Surface>>(surface));
186 }
187
SkipPrivacyMode(int32_t * windowIDs,int32_t windowCount)188 int32_t ScreenCaptureNativeMock::SkipPrivacyMode(int32_t *windowIDs, int32_t windowCount)
189 {
190 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
191 std::vector<uint64_t> vec;
192 for (int32_t i = 0; i < windowCount; i++) {
193 vec.push_back(static_cast<uint64_t>(*(windowIDs + i)));
194 }
195 return screenCapture_->SkipPrivacyMode(vec);
196 }
197
SetMaxVideoFrameRate(int32_t frameRate)198 int32_t ScreenCaptureNativeMock::SetMaxVideoFrameRate(int32_t frameRate)
199 {
200 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
201 return screenCapture_->SetMaxVideoFrameRate(frameRate);
202 }
203
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)204 int32_t ScreenCaptureNativeMock::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer,
205 AudioCaptureSourceType type)
206 {
207 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
208 return screenCapture_->AcquireAudioBuffer(audioBuffer, type);
209 }
210
AcquireVideoBuffer(int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)211 sptr<OHOS::SurfaceBuffer> ScreenCaptureNativeMock::AcquireVideoBuffer(int32_t &fence, int64_t ×tamp,
212 OHOS::Rect &damage)
213 {
214 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, nullptr, "screenCapture_ == nullptr");
215 return screenCapture_->AcquireVideoBuffer(fence, timestamp, damage);
216 }
217
ReleaseAudioBuffer(AudioCaptureSourceType type)218 int32_t ScreenCaptureNativeMock::ReleaseAudioBuffer(AudioCaptureSourceType type)
219 {
220 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
221 return screenCapture_->ReleaseAudioBuffer(type);
222 }
223
ReleaseVideoBuffer()224 int32_t ScreenCaptureNativeMock::ReleaseVideoBuffer()
225 {
226 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
227 return screenCapture_->ReleaseVideoBuffer();
228 }
229
ExcludeWindowContent(int32_t * windowIDs,int32_t windowCount)230 int32_t ScreenCaptureNativeMock::ExcludeWindowContent(int32_t *windowIDs, int32_t windowCount)
231 {
232 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
233 ScreenCaptureContentFilter filter;
234 std::vector<uint64_t> vec;
235 for (int32_t i = 0; i < windowCount; i++) {
236 vec.push_back(static_cast<uint64_t>(*(windowIDs + i)));
237 }
238 filter.windowIDsVec = vec;
239 return screenCapture_->ExcludeContent(filter);
240 }
241
ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType)242 int32_t ScreenCaptureNativeMock::ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType)
243 {
244 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
245 ScreenCaptureContentFilter filter;
246 filter.filteredAudioContents.insert(audioType);
247 return screenCapture_->ExcludeContent(filter);
248 }
249
CreateCaptureStrategy()250 int32_t ScreenCaptureNativeMock::CreateCaptureStrategy()
251 {
252 return MSERR_OK;
253 }
254
StrategyForKeepCaptureDuringCall(bool value)255 int32_t ScreenCaptureNativeMock::StrategyForKeepCaptureDuringCall(bool value)
256 {
257 strategy_.keepCaptureDuringCall = value;
258 return MSERR_OK;
259 }
260
SetCanvasFollowRotationStrategy(bool value)261 int32_t ScreenCaptureNativeMock::SetCanvasFollowRotationStrategy(bool value)
262 {
263 strategy_.canvasFollowRotation = value;
264 return MSERR_OK;
265 }
266
SetCaptureStrategy()267 int32_t ScreenCaptureNativeMock::SetCaptureStrategy()
268 {
269 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
270 return screenCapture_->SetScreenCaptureStrategy(strategy_);
271 }
272
ReleaseCaptureStrategy()273 int32_t ScreenCaptureNativeMock::ReleaseCaptureStrategy()
274 {
275 return MSERR_OK;
276 }
277
StrategyForBFramesEncoding(bool value)278 int32_t ScreenCaptureNativeMock::StrategyForBFramesEncoding(bool value)
279 {
280 strategy_.enableBFrame = value;
281 return MSERR_OK;
282 }
283
StrategyForPrivacyMaskMode(int32_t value)284 int32_t ScreenCaptureNativeMock::StrategyForPrivacyMaskMode(int32_t value)
285 {
286 strategy_.strategyForPrivacyMaskMode = value;
287 return MSERR_OK;
288 }
289
StrategyForPickerPopUp(bool value)290 int32_t ScreenCaptureNativeMock::StrategyForPickerPopUp(bool value)
291 {
292 strategy_.pickerPopUp = static_cast<AVScreenCapturePickerPopUp>(value);
293 return MSERR_OK;
294 }
295
StrategyForFillMode(AVScreenCaptureFillMode value)296 int32_t ScreenCaptureNativeMock::StrategyForFillMode(AVScreenCaptureFillMode value)
297 {
298 strategy_.fillMode = value;
299 return MSERR_OK;
300 }
301 } // namespace Media
302 } // namespace OHOS