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_capi_mock.h"
17 #include "native_mfmagic.h"
18 #include "native_window.h"
19
20 using namespace std;
21 using namespace OHOS;
22 using namespace OHOS::Media;
23 using namespace testing::ext;
24 using namespace OHOS::Media::ScreenCaptureTestParam;
25 std::mutex ScreenCaptureCapiMock::mutex_;
26 std::map<OH_AVScreenCapture *, std::shared_ptr<ScreenCaptureCallBackMock>> ScreenCaptureCapiMock::mockCbMap_;
27
28 typedef struct NativeWindow OHNativeWindow;
29
OnError(OH_AVScreenCapture * screenCapture,int32_t errorCode)30 void ScreenCaptureCapiMock::OnError(OH_AVScreenCapture *screenCapture, int32_t errorCode)
31 {
32 std::shared_ptr<ScreenCaptureCallBackMock> mockCb = GetCallback(screenCapture);
33 if (mockCb != nullptr) {
34 mockCb->OnError(errorCode);
35 }
36 }
37
OnAudioBufferAvailable(OH_AVScreenCapture * screenCapture,bool isReady,OH_AudioCaptureSourceType type)38 void ScreenCaptureCapiMock::OnAudioBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady,
39 OH_AudioCaptureSourceType type)
40 {
41 std::shared_ptr<ScreenCaptureCallBackMock> mockCb = GetCallback(screenCapture);
42 if (mockCb != nullptr) {
43 mockCb->OnAudioBufferAvailable(isReady, static_cast<AudioCaptureSourceType>(type));
44 }
45 }
46
OnVideoBufferAvailable(OH_AVScreenCapture * screenCapture,bool isReady)47 void ScreenCaptureCapiMock::OnVideoBufferAvailable(OH_AVScreenCapture *screenCapture, bool isReady)
48 {
49 std::shared_ptr<ScreenCaptureCallBackMock> mockCb = GetCallback(screenCapture);
50 if (mockCb != nullptr) {
51 mockCb->OnVideoBufferAvailable(isReady);
52 }
53 }
54
OnErrorNew(OH_AVScreenCapture * screenCapture,int32_t errorCode,void * userData)55 void ScreenCaptureCapiMock::OnErrorNew(OH_AVScreenCapture *screenCapture, int32_t errorCode, void *userData)
56 {
57 std::shared_ptr<ScreenCaptureCallBackMock> mockCb = GetCallback(screenCapture);
58 if (mockCb != nullptr) {
59 mockCb->OnError(errorCode, userData);
60 }
61 }
62
OnBufferAvailable(OH_AVScreenCapture * screenCapture,OH_AVBuffer * buffer,OH_AVScreenCaptureBufferType bufferType,int64_t timestamp,void * userData)63 void ScreenCaptureCapiMock::OnBufferAvailable(OH_AVScreenCapture *screenCapture, OH_AVBuffer *buffer,
64 OH_AVScreenCaptureBufferType bufferType, int64_t timestamp, void *userData)
65 {
66 UNITTEST_CHECK_AND_RETURN_LOG(buffer != nullptr, "OH_AVBuffer buffer == nullptr");
67 std::shared_ptr<ScreenCaptureCallBackMock> mockCb = GetCallback(screenCapture);
68 if (mockCb != nullptr) {
69 mockCb->OnBufferAvailable(buffer->buffer_, static_cast<AVScreenCaptureBufferType>(bufferType), timestamp);
70 }
71 }
72
OnStateChange(OH_AVScreenCapture * screenCapture,OH_AVScreenCaptureStateCode stateCode,void * userData)73 void ScreenCaptureCapiMock::OnStateChange(OH_AVScreenCapture *screenCapture,
74 OH_AVScreenCaptureStateCode stateCode, void *userData)
75 {
76 std::shared_ptr<ScreenCaptureCallBackMock> mockCb = GetCallback(screenCapture);
77 if (mockCb != nullptr) {
78 mockCb->OnStateChange(static_cast<AVScreenCaptureStateCode>(stateCode));
79 }
80 }
81
OnDisplaySelected(OH_AVScreenCapture * screenCapture,uint64_t displayId,void * userData)82 void ScreenCaptureCapiMock::OnDisplaySelected(OH_AVScreenCapture *screenCapture, uint64_t displayId, void *userData)
83 {
84 std::shared_ptr<ScreenCaptureCallBackMock> mockCb = GetCallback(screenCapture);
85 if (mockCb != nullptr) {
86 mockCb->OnDisplaySelected(displayId);
87 }
88 }
89
Convert(AVScreenCaptureConfig config)90 OH_AVScreenCaptureConfig ScreenCaptureCapiMock::Convert(AVScreenCaptureConfig config)
91 {
92 OH_AVScreenCaptureConfig tempConfig;
93 tempConfig.captureMode = static_cast<OH_CaptureMode>(config.captureMode);
94 tempConfig.dataType = static_cast<OH_DataType>(config.dataType);
95 tempConfig.audioInfo.micCapInfo = {
96 .audioSampleRate = config.audioInfo.micCapInfo.audioSampleRate,
97 .audioChannels = config.audioInfo.micCapInfo.audioChannels,
98 .audioSource = static_cast<OH_AudioCaptureSourceType>(config.audioInfo.micCapInfo.audioSource)
99 };
100 tempConfig.audioInfo.innerCapInfo = {
101 .audioSampleRate = config.audioInfo.innerCapInfo.audioSampleRate,
102 .audioChannels = config.audioInfo.innerCapInfo.audioChannels,
103 .audioSource = static_cast<OH_AudioCaptureSourceType>(config.audioInfo.innerCapInfo.audioSource)
104 };
105 tempConfig.audioInfo.audioEncInfo.audioBitrate = config.audioInfo.audioEncInfo.audioBitrate;
106 tempConfig.audioInfo.audioEncInfo.audioCodecformat =
107 static_cast<OH_AudioCodecFormat>(config.audioInfo.audioEncInfo.audioCodecformat);
108 tempConfig.videoInfo.videoCapInfo.displayId = config.videoInfo.videoCapInfo.displayId;
109 std::list<int32_t> taskIds = config.videoInfo.videoCapInfo.taskIDs;
110 if (taskIds.size() > 0) {
111 int32_t *taskIds_temp = static_cast<int*>(malloc(sizeof(int)));
112 for (std::list<int32_t>::iterator its = taskIds.begin(); its != taskIds.end(); ++its) {
113 *taskIds_temp = *its;
114 taskIds_temp++;
115 }
116 tempConfig.videoInfo.videoCapInfo.missionIDs = taskIds_temp;
117 }
118 tempConfig.videoInfo.videoCapInfo.missionIDsLen = taskIds.size();
119 tempConfig.videoInfo.videoCapInfo.videoFrameWidth = config.videoInfo.videoCapInfo.videoFrameWidth;
120 tempConfig.videoInfo.videoCapInfo.videoFrameHeight = config.videoInfo.videoCapInfo.videoFrameHeight;
121 tempConfig.videoInfo.videoCapInfo.videoSource =
122 static_cast<OH_VideoSourceType>(config.videoInfo.videoCapInfo.videoSource);
123 tempConfig.videoInfo.videoEncInfo = {
124 .videoCodec = static_cast<OH_VideoCodecFormat>(config.videoInfo.videoEncInfo.videoCodec),
125 .videoBitrate = static_cast<OH_VideoCodecFormat>(config.videoInfo.videoEncInfo.videoBitrate),
126 .videoFrameRate = static_cast<OH_VideoCodecFormat>(config.videoInfo.videoEncInfo.videoFrameRate)
127 };
128 std::string url = config.recorderInfo.url;
129 if (!(url.empty())) {
130 tempConfig.recorderInfo.url = const_cast<char *>(config.recorderInfo.url.c_str());
131 tempConfig.recorderInfo.urlLen = config.recorderInfo.url.size();
132 }
133 if (config.recorderInfo.fileFormat == ContainerFormatType::CFT_MPEG_4A) {
134 tempConfig.recorderInfo.fileFormat = OH_ContainerFormatType::CFT_MPEG_4A;
135 } else if (config.recorderInfo.fileFormat == ContainerFormatType::CFT_MPEG_4) {
136 tempConfig.recorderInfo.fileFormat = OH_ContainerFormatType::CFT_MPEG_4;
137 }
138 return tempConfig;
139 }
140
GetCallback(OH_AVScreenCapture * screenCapture)141 std::shared_ptr<ScreenCaptureCallBackMock> ScreenCaptureCapiMock::GetCallback(OH_AVScreenCapture *screenCapture)
142 {
143 std::lock_guard<std::mutex> lock(mutex_);
144 if (mockCbMap_.empty()) {
145 return nullptr;
146 }
147 if (mockCbMap_.find(screenCapture) != mockCbMap_.end()) {
148 return mockCbMap_.at(screenCapture);
149 }
150 return nullptr;
151 }
152
DelCallback(OH_AVScreenCapture * screenCapture)153 void ScreenCaptureCapiMock::DelCallback(OH_AVScreenCapture *screenCapture)
154 {
155 std::lock_guard<std::mutex> lock(mutex_);
156 if (mockCbMap_.empty()) {
157 return;
158 }
159 auto it = mockCbMap_.find(screenCapture);
160 if (it != mockCbMap_.end()) {
161 mockCbMap_.erase(it);
162 }
163 }
164
SetScreenCaptureCallback(OH_AVScreenCapture * screencapture,std::shared_ptr<ScreenCaptureCallBackMock> cb)165 void ScreenCaptureCapiMock::SetScreenCaptureCallback(OH_AVScreenCapture *screencapture,
166 std::shared_ptr<ScreenCaptureCallBackMock> cb)
167 {
168 std::lock_guard<std::mutex> lock(mutex_);
169 mockCbMap_[screencapture] = cb;
170 }
171
SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBackMock> & callback,const bool isErrorCallBackEnabled,const bool isDataCallBackEnabled,const bool isStateChangeCallBackEnabled)172 int32_t ScreenCaptureCapiMock::SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBackMock>& callback,
173 const bool isErrorCallBackEnabled, const bool isDataCallBackEnabled, const bool isStateChangeCallBackEnabled)
174 {
175 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
176 if (callback == nullptr) {
177 return MSERR_INVALID_OPERATION;
178 }
179 MEDIA_LOGD("ScreenCaptureCapiMock SetScreenCaptureCallback");
180 SetScreenCaptureCallback(screenCapture_, callback);
181 struct OH_AVScreenCaptureCallback ohCallback;
182 ohCallback.onError = ScreenCaptureCapiMock::OnError;
183 ohCallback.onAudioBufferAvailable = ScreenCaptureCapiMock::OnAudioBufferAvailable;
184 ohCallback.onVideoBufferAvailable = ScreenCaptureCapiMock::OnVideoBufferAvailable;
185 int ret = OH_AVScreenCapture_SetCallback(screenCapture_, ohCallback);
186 if (ret != AV_SCREEN_CAPTURE_ERR_OK) {
187 MEDIA_LOGE("ScreenCaptureCapiMock SetCallback failed, ret: %{public}d", ret);
188 return MSERR_UNKNOWN;
189 }
190 if (isErrorCallBackEnabled) {
191 MEDIA_LOGD("ScreenCaptureCapiMock SetErrorCallback");
192 isErrorCallBackEnabled_ = isErrorCallBackEnabled;
193 ret = OH_AVScreenCapture_SetErrorCallback(screenCapture_, ScreenCaptureCapiMock::OnErrorNew, this);
194 if (ret != AV_SCREEN_CAPTURE_ERR_OK) {
195 MEDIA_LOGE("ScreenCaptureCapiMock SetErrorCallback failed, ret: %{public}d", ret);
196 return MSERR_UNKNOWN;
197 }
198 }
199 if (isDataCallBackEnabled) {
200 MEDIA_LOGD("ScreenCaptureCapiMock SetDataCallback");
201 isDataCallBackEnabled_ = isDataCallBackEnabled;
202 ret = OH_AVScreenCapture_SetDataCallback(screenCapture_, ScreenCaptureCapiMock::OnBufferAvailable, this);
203 if (ret != AV_SCREEN_CAPTURE_ERR_OK) {
204 MEDIA_LOGE("ScreenCaptureCapiMock SetDataCallback failed, ret: %{public}d", ret);
205 return MSERR_UNKNOWN;
206 }
207 }
208 if (isStateChangeCallBackEnabled) {
209 MEDIA_LOGD("ScreenCaptureCapiMock SetStateCallback");
210 isStateChangeCallBackEnabled_ = isStateChangeCallBackEnabled;
211 ret = OH_AVScreenCapture_SetStateCallback(screenCapture_, ScreenCaptureCapiMock::OnStateChange, this);
212 if (ret != AV_SCREEN_CAPTURE_ERR_OK) {
213 MEDIA_LOGE("ScreenCaptureCapiMock SetStateCallback failed, ret: %{public}d", ret);
214 return MSERR_UNKNOWN;
215 }
216 }
217 return MSERR_OK;
218 }
219
StartScreenCapture()220 int32_t ScreenCaptureCapiMock::StartScreenCapture()
221 {
222 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
223 return OH_AVScreenCapture_StartScreenCapture(screenCapture_);
224 }
225
StartScreenCaptureWithSurface(const std::any & value)226 int32_t ScreenCaptureCapiMock::StartScreenCaptureWithSurface(const std::any& value)
227 {
228 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
229 sptr<Surface> surface = std::any_cast<sptr<Surface>>(value);
230 OHNativeWindow* nativeWindow = new OHNativeWindow();
231 nativeWindow->surface = surface;
232 return OH_AVScreenCapture_StartScreenCaptureWithSurface(screenCapture_, nativeWindow);
233 }
234
Init(AVScreenCaptureConfig config)235 int32_t ScreenCaptureCapiMock::Init(AVScreenCaptureConfig config)
236 {
237 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
238 OH_AVScreenCaptureConfig tempConfig = Convert(config);
239 return OH_AVScreenCapture_Init(screenCapture_, tempConfig);
240 }
241
StopScreenCapture()242 int32_t ScreenCaptureCapiMock::StopScreenCapture()
243 {
244 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
245 return OH_AVScreenCapture_StopScreenCapture(screenCapture_);
246 }
247
StartScreenRecording()248 int32_t ScreenCaptureCapiMock::StartScreenRecording()
249 {
250 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
251 return OH_AVScreenCapture_StartScreenRecording(screenCapture_);
252 }
253
StopScreenRecording()254 int32_t ScreenCaptureCapiMock::StopScreenRecording()
255 {
256 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
257 return OH_AVScreenCapture_StopScreenRecording(screenCapture_);
258 }
259
Release()260 int32_t ScreenCaptureCapiMock::Release()
261 {
262 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
263 DelCallback(screenCapture_);
264 int32_t ret = OH_AVScreenCapture_Release(screenCapture_);
265 screenCapture_ = nullptr;
266 return ret;
267 }
268
SetMicrophoneEnabled(bool isMicrophone)269 int32_t ScreenCaptureCapiMock::SetMicrophoneEnabled(bool isMicrophone)
270 {
271 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
272 return OH_AVScreenCapture_SetMicrophoneEnabled(screenCapture_, isMicrophone);
273 }
274
SetCanvasRotation(bool canvasRotation)275 int32_t ScreenCaptureCapiMock::SetCanvasRotation(bool canvasRotation)
276 {
277 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
278 return OH_AVScreenCapture_SetCanvasRotation(screenCapture_, canvasRotation);
279 }
280
ResizeCanvas(int32_t width,int32_t height)281 int32_t ScreenCaptureCapiMock::ResizeCanvas(int32_t width, int32_t height)
282 {
283 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
284 return OH_AVScreenCapture_ResizeCanvas(screenCapture_, width, height);
285 }
286
SkipPrivacyMode(int32_t * windowIDs,int32_t windowCount)287 int32_t ScreenCaptureCapiMock::SkipPrivacyMode(int32_t *windowIDs, int32_t windowCount)
288 {
289 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
290 return OH_AVScreenCapture_SkipPrivacyMode(screenCapture_, windowIDs, windowCount);
291 }
292
SetMaxVideoFrameRate(int32_t frameRate)293 int32_t ScreenCaptureCapiMock::SetMaxVideoFrameRate(int32_t frameRate)
294 {
295 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
296 return OH_AVScreenCapture_SetMaxVideoFrameRate(screenCapture_, frameRate);
297 }
298
AcquireAudioBuffer(std::shared_ptr<AudioBuffer> & audioBuffer,AudioCaptureSourceType type)299 int32_t ScreenCaptureCapiMock::AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer,
300 AudioCaptureSourceType type)
301 {
302 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
303 OH_AudioBuffer *buffer = static_cast<OH_AudioBuffer *>(malloc(sizeof(OH_AudioBuffer)));
304 if (buffer == nullptr) {
305 return MSERR_INVALID_OPERATION;
306 }
307 auto t_type = static_cast<OH_AudioCaptureSourceType>(type);
308 int32_t ret = OH_AVScreenCapture_AcquireAudioBuffer(screenCapture_, &buffer, t_type);
309 if (ret == AV_SCREEN_CAPTURE_ERR_OK) {
310 // If ret is not AV_SCREEN_CAPTURE_ERR_OK, the object referenced by buffer is not initialized and can't be used.
311 audioBuffer =
312 std::make_shared<AudioBuffer>(buffer->buf, buffer->size, buffer->timestamp,
313 static_cast<AudioCaptureSourceType>(buffer->type));
314 }
315 free(buffer);
316 buffer = nullptr;
317 return ret;
318 }
319
AcquireVideoBuffer(int32_t & fence,int64_t & timestamp,OHOS::Rect & damage)320 sptr<OHOS::SurfaceBuffer> ScreenCaptureCapiMock::AcquireVideoBuffer(int32_t &fence, int64_t ×tamp,
321 OHOS::Rect &damage)
322 {
323 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, nullptr, "screenCapture_ == nullptr");
324 OH_Rect damage_ = {
325 .x = damage.x,
326 .y = damage.y,
327 .width = damage.w,
328 .height = damage.h,
329 };
330 OH_NativeBuffer* buffer = OH_AVScreenCapture_AcquireVideoBuffer(screenCapture_, &fence, ×tamp, &damage_);
331 sptr<OHOS::SurfaceBuffer> surfacebuffer;
332 if (buffer != nullptr) {
333 surfacebuffer = OHOS::SurfaceBuffer::NativeBufferToSurfaceBuffer(buffer);
334 }
335 return surfacebuffer;
336 }
337
ReleaseAudioBuffer(OHOS::Media::AudioCaptureSourceType type)338 int32_t ScreenCaptureCapiMock::ReleaseAudioBuffer(OHOS::Media::AudioCaptureSourceType type)
339 {
340 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
341 auto t_type = static_cast<OH_AudioCaptureSourceType>(type);
342 return OH_AVScreenCapture_ReleaseAudioBuffer(screenCapture_, t_type);
343 }
344
ReleaseVideoBuffer()345 int32_t ScreenCaptureCapiMock::ReleaseVideoBuffer()
346 {
347 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
348 return OH_AVScreenCapture_ReleaseVideoBuffer(screenCapture_);
349 }
350
ExcludeWindowContent(int32_t * windowIDs,int32_t windowCount)351 int32_t ScreenCaptureCapiMock::ExcludeWindowContent(int32_t *windowIDs, int32_t windowCount)
352 {
353 int32_t ret = MSERR_OK;
354 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
355 if (contentFilter_ != nullptr) {
356 ret = OH_AVScreenCapture_ReleaseContentFilter(contentFilter_);
357 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "release content failed");
358 }
359 contentFilter_ = OH_AVScreenCapture_CreateContentFilter();
360 OH_AVScreenCapture_ContentFilter_AddWindowContent(contentFilter_, windowIDs, windowCount);
361 return OH_AVScreenCapture_ExcludeContent(screenCapture_, contentFilter_);
362 }
363
ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType)364 int32_t ScreenCaptureCapiMock::ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType)
365 {
366 int32_t ret = MSERR_OK;
367 UNITTEST_CHECK_AND_RETURN_RET_LOG(screenCapture_ != nullptr, MSERR_INVALID_OPERATION, "screenCapture_ == nullptr");
368 if (contentFilter_ != nullptr) {
369 ret = OH_AVScreenCapture_ReleaseContentFilter(contentFilter_);
370 UNITTEST_CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, ret, "release content failed");
371 }
372 contentFilter_ = OH_AVScreenCapture_CreateContentFilter();
373 OH_AVScreenCapture_ContentFilter_AddAudioContent(contentFilter_,
374 static_cast<OH_AVScreenCaptureFilterableAudioContent>(audioType));
375 return OH_AVScreenCapture_ExcludeContent(screenCapture_, contentFilter_);
376 }