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 OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event, ScreenCaptureRect* area) = 0; 51 virtual void OnDisplaySelected(uint64_t displayId) = 0; OnError(int32_t errorCode,void * userData)52 virtual void OnError(int32_t errorCode, void *userData) 53 { 54 (void)errorCode; 55 (void)userData; 56 } 57 virtual void OnBufferAvailable(const std::shared_ptr<AVBuffer> buffer, 58 AVScreenCaptureBufferType bufferType, int64_t timestamp) = 0; 59 }; 60 61 class ScreenCaptureMonitorListenerMock : public ScreenCaptureMonitor::ScreenCaptureMonitorListener { 62 public: ScreenCaptureMonitorListenerMock(std::string name)63 ScreenCaptureMonitorListenerMock(std::string name):name_(name) {} 64 ~ScreenCaptureMonitorListenerMock() = default; 65 void OnScreenCaptureStarted(int32_t pid) override; 66 void OnScreenCaptureFinished(int32_t pid) override; 67 int stateFlag_ = 0; 68 std::string name_ = "ScreenCaptureMonitor"; 69 }; 70 71 class ScreenCaptureMock { 72 public: 73 virtual ~ScreenCaptureMock() = default; 74 virtual int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallbackMock>& callback, 75 const bool isErrorCallbackEnabled = false, const bool isDataCallbackEnabled = false, 76 const bool isStateChangeCallbackEnabled = false, const bool isCaptureContentChangeCallbackEnabled = false) = 0; 77 virtual int32_t Init(AVScreenCaptureConfig config) = 0; 78 virtual int32_t StartScreenCapture() = 0; 79 virtual int32_t StartScreenCaptureWithSurface(const std::any& value) = 0; 80 virtual int32_t StopScreenCapture() = 0; 81 virtual int32_t StartScreenRecording() = 0; 82 virtual int32_t StopScreenRecording() = 0; 83 virtual int32_t Release() = 0; 84 virtual int32_t SetMicrophoneEnabled(bool isMicrophone) = 0; 85 virtual int32_t SetCanvasRotation(bool canvasRotation) = 0; 86 virtual int32_t SkipPrivacyMode(int32_t *windowIDs, int32_t windowCount) = 0; 87 virtual int32_t ResizeCanvas(int32_t width, int32_t height) = 0; 88 virtual int32_t UpdateSurface(const std::any& surface) = 0; 89 virtual int32_t SetMaxVideoFrameRate(int32_t frameRate) = 0; 90 virtual int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type) = 0; 91 virtual sptr<OHOS::SurfaceBuffer> AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, OHOS::Rect &damage) = 0; 92 virtual int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) = 0; 93 virtual int32_t ReleaseVideoBuffer() = 0; 94 virtual int32_t ExcludeWindowContent(int32_t *windowIDs, int32_t windowCount) = 0; 95 virtual int32_t ExcludeAudioContent(AVScreenCaptureFilterableAudioContent audioType) = 0; IsErrorCallbackEnabled()96 virtual bool IsErrorCallbackEnabled() 97 { 98 return false; 99 } IsDataCallbackEnabled()100 virtual bool IsDataCallbackEnabled() 101 { 102 return false; 103 } IsStateChangeCallbackEnabled()104 virtual bool IsStateChangeCallbackEnabled() 105 { 106 return false; 107 } IsCaptureContentChangeCallbackEnabled()108 virtual bool IsCaptureContentChangeCallbackEnabled() 109 { 110 return false; 111 } 112 virtual int32_t CreateCaptureStrategy(); 113 virtual int32_t StrategyForKeepCaptureDuringCall(bool value); 114 virtual int32_t SetCaptureStrategy(); 115 virtual int32_t ReleaseCaptureStrategy(); 116 virtual int32_t SetCanvasFollowRotationStrategy(bool value); 117 virtual int32_t StrategyForBFramesEncoding(bool value); 118 virtual int32_t StrategyForPrivacyMaskMode(int32_t value); 119 virtual int32_t StrategyForPickerPopUp(bool value); 120 virtual int32_t StrategyForFillMode(AVScreenCaptureFillMode value); 121 }; 122 123 class __attribute__((visibility("default"))) ScreenCaptureMockFactory { 124 public: 125 static std::shared_ptr<ScreenCaptureMock> CreateScreenCapture(); 126 private: 127 ScreenCaptureMockFactory() = delete; 128 ~ScreenCaptureMockFactory() = delete; 129 }; 130 } // namespace Media 131 } // namespace OHOS 132 #endif