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