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_UNIT_TEST_H 16 #define SCREEN_CAPTURE_UNIT_TEST_H 17 18 #include "gtest/gtest.h" 19 #include "screen_capture_mock.h" 20 21 namespace OHOS { 22 namespace Media { 23 class ScreenCaptureUnitTestCallback : public ScreenCaptureCallBackMock { 24 public: ScreenCaptureUnitTestCallback(std::shared_ptr<ScreenCaptureMock> ScreenCapture,FILE * aFile,FILE * vFile,int32_t aFlag,int32_t vFlag)25 explicit ScreenCaptureUnitTestCallback(std::shared_ptr<ScreenCaptureMock> ScreenCapture, FILE *aFile, FILE *vFile, 26 int32_t aFlag, int32_t vFlag) 27 : screenCapture_(ScreenCapture), aFile_(aFile), vFile_(vFile), aFlag_(aFlag), vFlag_(vFlag) {} ScreenCaptureUnitTestCallback(std::shared_ptr<ScreenCaptureMock> ScreenCapture)28 explicit ScreenCaptureUnitTestCallback(std::shared_ptr<ScreenCaptureMock> ScreenCapture) 29 : screenCapture_(ScreenCapture) {} 30 ~ScreenCaptureUnitTestCallback() = default; 31 void OnError(int32_t errorCode) override; 32 void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) override; 33 void OnVideoBufferAvailable(bool isReady) override; 34 void OnStateChange(AVScreenCaptureStateCode stateCode) override; 35 void OnDisplaySelected(uint64_t displayId) override; 36 void OnError(int32_t errorCode, void *userData) override; 37 void OnBufferAvailable(std::shared_ptr<AVBuffer> buffer, AVScreenCaptureBufferType bufferType, 38 int64_t timestamp) override; 39 void ProcessAudioBuffer(uint8_t *buffer, int32_t size, int64_t timestamp, AVScreenCaptureBufferType bufferType); 40 void ProcessVideoBuffer(sptr<OHOS::SurfaceBuffer> surfacebuffer, int64_t timestamp); 41 void CheckDataCallbackAudio(AudioCaptureSourceType type, int32_t flag); 42 void CheckDataCallbackVideo(int32_t flag); 43 void DumpBuffer(FILE *file, uint8_t *buffer, int32_t size, int64_t timestamp, 44 AVScreenCaptureBufferType bufferType = AVScreenCaptureBufferType::SCREEN_CAPTURE_BUFFERTYPE_VIDEO); 45 void InitCaptureTrackInfo(FILE *file, int32_t flag, AVScreenCaptureBufferType bufferType); GetScreenCaptureState()46 AVScreenCaptureStateCode GetScreenCaptureState() 47 { 48 return screenCaptureState_.load(); 49 } 50 void DumpAudioBuffer(std::shared_ptr<AudioBuffer> audioBuffer); 51 void DumpVideoBuffer(sptr<OHOS::SurfaceBuffer> surfacebuffer, int64_t timestamp); 52 int32_t GetFrameNumber(); 53 54 private: 55 std::shared_ptr<ScreenCaptureMock> screenCapture_; 56 FILE *innerAudioFile_ = nullptr; 57 FILE *micAudioFile_ = nullptr; 58 FILE *videoFile_ = nullptr; 59 int32_t innerAudioFlag_ = 0; 60 int32_t micAudioFlag_ = 0; 61 int32_t videoFlag_ = 0; 62 std::atomic<AVScreenCaptureStateCode> screenCaptureState_ = AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID; 63 uint64_t screenCaptureDisplayId_ = -1; 64 65 FILE *aFile_ = nullptr; 66 FILE *vFile_ = nullptr; 67 int32_t aFlag_ = 0; 68 int32_t vFlag_ = 0; 69 int32_t frameNumber = 0; 70 }; 71 72 class ScreenCaptureUnitTest : public testing::Test { 73 public: 74 static void SetUpTestCase(void); 75 static void TearDownTestCase(void); 76 void SetUp(void); 77 void TearDown(void); 78 void AudioLoop(void); 79 void AudioLoopWithoutRelease(void); 80 void BeforeScreenCaptureSpecifiedWindowCbCase07(void); 81 int32_t SetConfig(AVScreenCaptureConfig &config); 82 int32_t SetConfigFile(AVScreenCaptureConfig &config, RecorderInfo &recorderInfo); 83 int32_t SetRecorderInfo(std::string name, RecorderInfo &recorderInfo); 84 void OpenFile(std::string name); 85 void CloseFile(void); 86 void OpenFile(std::string name, bool isInnerAudioEnabled, bool isMicAudioEnabled, bool isVideoEnabled); 87 void OpenFileFd(std::string name); 88 static void SetAccessTokenPermission(); 89 static void SetHapPermission(); 90 91 protected: 92 static const std::string SCREEN_CAPTURE_ROOT_DIR; 93 94 std::shared_ptr<ScreenCaptureMock> screenCapture_ = nullptr; 95 std::shared_ptr<ScreenCaptureUnitTestCallback> screenCaptureCb_ = nullptr; 96 std::unique_ptr<std::thread> audioLoop_ = nullptr; 97 98 AVScreenCaptureConfig config_; 99 FILE *aFile = nullptr; 100 FILE *vFile = nullptr; 101 int32_t aFlag = 0; 102 int32_t vFlag = 0; 103 char fileName[100] = {0}; 104 FILE *innerAudioFile_ = nullptr; 105 FILE *micAudioFile_ = nullptr; 106 FILE *videoFile_ = nullptr; 107 int32_t outputFd_ = -1; 108 }; 109 110 class ScreenCapBufferDemoConsumerListener : public IBufferConsumerListener { 111 public: ScreenCapBufferDemoConsumerListener(sptr<Surface> consumer)112 ScreenCapBufferDemoConsumerListener(sptr<Surface> consumer) 113 : consumer_(consumer) {} ~ScreenCapBufferDemoConsumerListener()114 ~ScreenCapBufferDemoConsumerListener() {} 115 void OnBufferAvailable() override; 116 private: 117 sptr<OHOS::Surface> consumer_ = nullptr; 118 }; 119 } // namespace Media 120 } // namespace OHOS 121 #endif