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 #ifndef SCREEN_CAPTURE_MOCK_H 16 #define SCREEN_CAPTURE_MOCK_H 17 18 #include <fcntl.h> 19 #include <stdio.h> 20 #include <string.h> 21 #include <securec.h> 22 #include <string> 23 #include <thread> 24 #include <cstdio> 25 #include "avbuffer.h" 26 #include "gtest/gtest.h" 27 #include "screen_capture.h" 28 #include "screen_capture_controller.h" 29 #include "unittest_log.h" 30 #include "media_errors.h" 31 #include "display_manager.h" 32 #include "screen_manager.h" 33 #include "external_window.h" 34 #include "screen_capture_monitor.h" 35 36 namespace OHOS { 37 namespace Media { 38 namespace ScreenCaptureTestParam { 39 constexpr uint32_t RECORDER_TIME = 2; 40 constexpr uint32_t RECORDER_TIME_5 = 5; 41 constexpr double EXCESS_RATE = 1.2; 42 } // namespace ScreenCaptureTestParam 43 44 class ScreenCaptureCallBackMock : public NoCopyable { 45 public: 46 virtual void OnError(int32_t errorCode) = 0; 47 virtual void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) = 0; 48 virtual void OnVideoBufferAvailable(bool isReady) = 0; 49 virtual void OnStateChange(AVScreenCaptureStateCode stateCode) = 0; OnError(int32_t errorCode,void * userData)50 virtual void OnError(int32_t errorCode, void *userData) 51 { 52 (void)errorCode; 53 (void)userData; 54 } 55 virtual void OnBufferAvailable(const std::shared_ptr<AVBuffer> buffer, 56 AVScreenCaptureBufferType bufferType, int64_t timestamp) = 0; 57 }; 58 59 class ScreenCaptureMonitorListenerMock : public ScreenCaptureMonitor::ScreenCaptureMonitorListener { 60 public: ScreenCaptureMonitorListenerMock(std::string name)61 ScreenCaptureMonitorListenerMock(std::string name):name_(name) {} 62 ~ScreenCaptureMonitorListenerMock() = default; 63 void OnScreenCaptureStarted(int32_t pid) override; 64 void OnScreenCaptureFinished(int32_t pid) override; 65 int stateFlag_ = 0; 66 std::string name_ = "ScreenCaptureMonitor"; 67 }; 68 69 class ScreenCaptureMock { 70 public: 71 virtual ~ScreenCaptureMock() = default; 72 virtual int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBackMock>& callback, 73 const bool isErrorCallBackEnabled = false, const bool isDataCallBackEnabled = false, 74 const bool isStateChangeCallBackEnabled = false) = 0; 75 virtual int32_t Init(AVScreenCaptureConfig config) = 0; 76 virtual int32_t StartScreenCapture() = 0; 77 virtual int32_t StartScreenCaptureWithSurface(const std::any& value) = 0; 78 virtual int32_t StopScreenCapture() = 0; 79 virtual int32_t StartScreenRecording() = 0; 80 virtual int32_t StopScreenRecording() = 0; 81 virtual int32_t Release() = 0; 82 virtual int32_t SetMicrophoneEnabled(bool isMicrophone) = 0; 83 virtual int32_t SetCanvasRotation(bool canvasRotation) = 0; 84 virtual int32_t SkipPrivacyMode(int32_t *windowIDs, int32_t windowCount) = 0; 85 virtual int32_t ResizeCanvas(int32_t width, int32_t height) = 0; 86 virtual int32_t SetMaxVideoFrameRate(int32_t frameRate) = 0; 87 virtual int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type) = 0; 88 virtual sptr<OHOS::SurfaceBuffer> AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, OHOS::Rect &damage) = 0; 89 virtual int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) = 0; 90 virtual int32_t ReleaseVideoBuffer() = 0; 91 virtual int32_t ExcludeWindowContent(int32_t *windowIDs, int32_t windowCount) = 0; 92 virtual int32_t ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType) = 0; IsErrorCallBackEnabled()93 virtual bool IsErrorCallBackEnabled() 94 { 95 return false; 96 } IsDataCallBackEnabled()97 virtual bool IsDataCallBackEnabled() 98 { 99 return false; 100 } IsStateChangeCallBackEnabled()101 virtual bool IsStateChangeCallBackEnabled() 102 { 103 return false; 104 } 105 }; 106 107 class __attribute__((visibility("default"))) ScreenCaptureMockFactory { 108 public: 109 static std::shared_ptr<ScreenCaptureMock> CreateScreenCapture(); 110 private: 111 ScreenCaptureMockFactory() = delete; 112 ~ScreenCaptureMockFactory() = delete; 113 }; 114 } // namespace Media 115 } // namespace OHOS 116 #endif