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 
OnRelease()59 void ScreenCaptureNativeCallbackMock::OnRelease()
60 {
61     std::unique_lock<std::mutex> lock(mutex_);
62     mockCb_ = nullptr;
63 }
64 
~ScreenCaptureNativeMock()65 ScreenCaptureNativeMock::~ScreenCaptureNativeMock()
66 {
67     MEDIA_LOGI("ScreenCaptureNativeMock::~ScreenCaptureNativeMock");
68 }
69 
SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBackMock> & callback,const bool isErrorCallBackEnabled,const bool isDataCallBackEnabled,const bool isStateChangeCallBackEnabled)70 int32_t ScreenCaptureNativeMock::SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBackMock>& callback,
71     const bool isErrorCallBackEnabled, const bool isDataCallBackEnabled, const bool isStateChangeCallBackEnabled)
72 {
73     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
74     (void)isErrorCallBackEnabled; // Inner is not support to set New ErrorCallBack
75     (void)isDataCallBackEnabled; // Inner is not support to set New DataCallBack
76     isStateChangeCallBackEnabled_ = isStateChangeCallBackEnabled;
77     if (callback != nullptr) {
78         cb_ = std::make_shared<ScreenCaptureNativeCallbackMock>(callback, screenCapture_);
79         return screenCapture_->SetScreenCaptureCallback(cb_);
80     }
81     return MSERR_INVALID_OPERATION;
82 }
83 
StartScreenCapture()84 int32_t ScreenCaptureNativeMock::StartScreenCapture()
85 {
86     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
87     if (isStateChangeCallBackEnabled_) {
88         int32_t ret = screenCapture_->SetPrivacyAuthorityEnabled();
89         UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetPrivacyAuthorityEnabled failed");
90     }
91     return screenCapture_->StartScreenCapture();
92 }
93 
StartScreenCaptureWithSurface(const std::any & value)94 int32_t ScreenCaptureNativeMock::StartScreenCaptureWithSurface(const std::any& value)
95 {
96     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
97     if (isStateChangeCallBackEnabled_) {
98         int32_t ret = screenCapture_->SetPrivacyAuthorityEnabled();
99         UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetPrivacyAuthorityEnabled failed");
100     }
101     sptr<Surface> surface = std::any_cast<sptr<Surface>>(value);
102     return screenCapture_->StartScreenCaptureWithSurface(surface);
103 }
104 
Init(AVScreenCaptureConfig config)105 int32_t ScreenCaptureNativeMock::Init(AVScreenCaptureConfig config)
106 {
107     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
108     return screenCapture_->Init(config);
109 }
110 
StopScreenCapture()111 int32_t ScreenCaptureNativeMock::StopScreenCapture()
112 {
113     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
114     return screenCapture_->StopScreenCapture();
115 }
116 
StartScreenRecording()117 int32_t ScreenCaptureNativeMock::StartScreenRecording()
118 {
119     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
120     if (isStateChangeCallBackEnabled_) {
121         int32_t ret = screenCapture_->SetPrivacyAuthorityEnabled();
122         UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "SetPrivacyAuthorityEnabled failed");
123     }
124     return screenCapture_->StartScreenRecording();
125 }
126 
StopScreenRecording()127 int32_t ScreenCaptureNativeMock::StopScreenRecording()
128 {
129     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
130     return screenCapture_->StopScreenRecording();
131 }
132 
Release()133 int32_t ScreenCaptureNativeMock::Release()
134 {
135     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
136     std::shared_ptr<ScreenCaptureCallBack> cb = cb_;
137     ScreenCaptureNativeCallbackMock *callback = static_cast<ScreenCaptureNativeCallbackMock *>(cb.get());
138     if (callback != nullptr) {
139         callback->OnRelease();
140         cb_ = nullptr;
141     }
142     return screenCapture_->Release();
143 }
144 
SetMicrophoneEnabled(bool isMicrophone)145 int32_t ScreenCaptureNativeMock::SetMicrophoneEnabled(bool isMicrophone)
146 {
147     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
148     return screenCapture_->SetMicrophoneEnabled(isMicrophone);
149 }
150 
SetCanvasRotation(bool canvasRotation)151 int32_t ScreenCaptureNativeMock::SetCanvasRotation(bool canvasRotation)
152 {
153     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
154     return screenCapture_->SetCanvasRotation(canvasRotation);
155 }
156 
ResizeCanvas(int32_t width,int32_t height)157 int32_t ScreenCaptureNativeMock::ResizeCanvas(int32_t width, int32_t height)
158 {
159     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
160     return screenCapture_->ResizeCanvas(width, height);
161 }
162 
SkipPrivacyMode(int32_t * windowIDs,int32_t windowCount)163 int32_t ScreenCaptureNativeMock::SkipPrivacyMode(int32_t *windowIDs, int32_t windowCount)
164 {
165     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
166     std::vector<uint64_t> vec;
167     for (int32_t i = 0; i < windowCount; i++) {
168         vec.push_back(static_cast<uint64_t>(*(windowIDs + i)));
169     }
170     return screenCapture_->SkipPrivacyMode(vec);
171 }
172 
SetMaxVideoFrameRate(int32_t frameRate)173 int32_t ScreenCaptureNativeMock::SetMaxVideoFrameRate(int32_t frameRate)
174 {
175     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
176     return screenCapture_->SetMaxVideoFrameRate(frameRate);
177 }
178 
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)179 int32_t ScreenCaptureNativeMock::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer,
180     AudioCaptureSourceType type)
181 {
182     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
183     return screenCapture_->AcquireAudioBuffer(audioBuffer, type);
184 }
185 
AcquireVideoBuffer(int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)186 sptr<OHOS::SurfaceBuffer> ScreenCaptureNativeMock::AcquireVideoBuffer(int32_t &fence, int64_t &timestamp,
187     OHOS::Rect &damage)
188 {
189     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, nullptr, "screenCapture_ == nullptr");
190     return screenCapture_->AcquireVideoBuffer(fence, timestamp, damage);
191 }
192 
ReleaseAudioBuffer(AudioCaptureSourceType type)193 int32_t ScreenCaptureNativeMock::ReleaseAudioBuffer(AudioCaptureSourceType type)
194 {
195     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
196     return screenCapture_->ReleaseAudioBuffer(type);
197 }
198 
ReleaseVideoBuffer()199 int32_t ScreenCaptureNativeMock::ReleaseVideoBuffer()
200 {
201     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
202     return screenCapture_->ReleaseVideoBuffer();
203 }
204 
ExcludeWindowContent(int32_t * windowIDs,int32_t windowCount)205 int32_t ScreenCaptureNativeMock::ExcludeWindowContent(int32_t *windowIDs, int32_t windowCount)
206 {
207     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
208     ScreenCaptureContentFilter filter;
209     std::vector<uint64_t> vec;
210     for (int32_t i = 0; i < windowCount; i++) {
211         vec.push_back(static_cast<uint64_t>(*(windowIDs + i)));
212     }
213     filter.windowIDsVec = vec;
214     return screenCapture_->ExcludeContent(filter);
215 }
216 
ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType)217 int32_t ScreenCaptureNativeMock::ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType)
218 {
219     UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
220     ScreenCaptureContentFilter filter;
221     filter.filteredAudioContents.insert(audioType);
222     return screenCapture_->ExcludeContent(filter);
223 }
224 } // namespace Media
225 } // namespace OHOS