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; 50 virtual void OnDisplaySelected(uint64_t displayId) = 0; OnError(int32_t errorCode,void * userData)51 virtual void OnError(int32_t errorCode, void *userData) 52 { 53 (void)errorCode; 54 (void)userData; 55 } 56 virtual void OnBufferAvailable(const std::shared_ptr<AVBuffer> buffer, 57 AVScreenCaptureBufferType bufferType, int64_t timestamp) = 0; 58 }; 59 60 class ScreenCaptureMonitorListenerMock : public ScreenCaptureMonitor::ScreenCaptureMonitorListener { 61 public: ScreenCaptureMonitorListenerMock(std::string name)62 ScreenCaptureMonitorListenerMock(std::string name):name_(name) {} 63 ~ScreenCaptureMonitorListenerMock() = default; 64 void OnScreenCaptureStarted(int32_t pid) override; 65 void OnScreenCaptureFinished(int32_t pid) override; 66 int stateFlag_ = 0; 67 std::string name_ = "ScreenCaptureMonitor"; 68 }; 69 70 class ScreenCaptureMock { 71 public: 72 virtual ~ScreenCaptureMock() = default; 73 virtual int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBackMock>& callback, 74 const bool isErrorCallBackEnabled = false, const bool isDataCallBackEnabled = false, 75 const bool isStateChangeCallBackEnabled = false) = 0; 76 virtual int32_t Init(AVScreenCaptureConfig config) = 0; 77 virtual int32_t StartScreenCapture() = 0; 78 virtual int32_t StartScreenCaptureWithSurface(const std::any& value) = 0; 79 virtual int32_t StopScreenCapture() = 0; 80 virtual int32_t StartScreenRecording() = 0; 81 virtual int32_t StopScreenRecording() = 0; 82 virtual int32_t Release() = 0; 83 virtual int32_t SetMicrophoneEnabled(bool isMicrophone) = 0; 84 virtual int32_t SetCanvasRotation(bool canvasRotation) = 0; 85 virtual int32_t SkipPrivacyMode(int32_t *windowIDs, int32_t windowCount) = 0; 86 virtual int32_t ResizeCanvas(int32_t width, int32_t height) = 0; 87 virtual int32_t SetMaxVideoFrameRate(int32_t frameRate) = 0; 88 virtual int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type) = 0; 89 virtual sptr<OHOS::SurfaceBuffer> AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, OHOS::Rect &damage) = 0; 90 virtual int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) = 0; 91 virtual int32_t ReleaseVideoBuffer() = 0; 92 virtual int32_t ExcludeWindowContent(int32_t *windowIDs, int32_t windowCount) = 0; 93 virtual int32_t ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType) = 0; IsErrorCallBackEnabled()94 virtual bool IsErrorCallBackEnabled() 95 { 96 return false; 97 } IsDataCallBackEnabled()98 virtual bool IsDataCallBackEnabled() 99 { 100 return false; 101 } IsStateChangeCallBackEnabled()102 virtual bool IsStateChangeCallBackEnabled() 103 { 104 return false; 105 } 106 }; 107 108 class __attribute__((visibility("default"))) ScreenCaptureMockFactory { 109 public: 110 static std::shared_ptr<ScreenCaptureMock> CreateScreenCapture(); 111 private: 112 ScreenCaptureMockFactory() = delete; 113 ~ScreenCaptureMockFactory() = delete; 114 }; 115 } // namespace Media 116 } // namespace OHOS 117 #endif