• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event, ScreenCaptureRect* area) override;
36     void OnDisplaySelected(uint64_t displayId) override;
37     void OnError(int32_t errorCode, void *userData) override;
38     void OnBufferAvailable(std::shared_ptr<AVBuffer> buffer, AVScreenCaptureBufferType bufferType,
39         int64_t timestamp) override;
40     void ProcessAudioBuffer(uint8_t *buffer, int32_t size, int64_t timestamp, AVScreenCaptureBufferType bufferType);
41     void ProcessVideoBuffer(sptr<OHOS::SurfaceBuffer> surfacebuffer, int64_t timestamp);
42     void CheckDataCallbackAudio(AudioCaptureSourceType type, int32_t flag);
43     void CheckDataCallbackVideo(int32_t flag);
44     void DumpBuffer(FILE *file, uint8_t *buffer, int32_t size, int64_t timestamp,
45         AVScreenCaptureBufferType bufferType = AVScreenCaptureBufferType::SCREEN_CAPTURE_BUFFERTYPE_VIDEO);
46     void InitCaptureTrackInfo(FILE *file, int32_t flag, AVScreenCaptureBufferType bufferType);
GetScreenCaptureState()47     AVScreenCaptureStateCode GetScreenCaptureState()
48     {
49         return screenCaptureState_.load();
50     }
GetScreenCaptureContentChangedEvent()51     AVScreenCaptureContentChangedEvent GetScreenCaptureContentChangedEvent()
52     {
53         return screenCaptureContentChange_.load();
54     }
55     void DumpAudioBuffer(std::shared_ptr<AudioBuffer> audioBuffer);
56     void DumpVideoBuffer(sptr<OHOS::SurfaceBuffer> surfacebuffer, int64_t timestamp);
57     int32_t GetFrameNumber();
58 
59 private:
60     std::shared_ptr<ScreenCaptureMock> screenCapture_;
61     FILE *innerAudioFile_ = nullptr;
62     FILE *micAudioFile_ = nullptr;
63     FILE *videoFile_ = nullptr;
64     int32_t innerAudioFlag_ = 0;
65     int32_t micAudioFlag_ = 0;
66     int32_t videoFlag_ = 0;
67     std::atomic<AVScreenCaptureStateCode> screenCaptureState_ = AVScreenCaptureStateCode::SCREEN_CAPTURE_STATE_INVLID;
68     std::atomic<AVScreenCaptureContentChangedEvent> screenCaptureContentChange_ =
69         AVScreenCaptureContentChangedEvent::SCREEN_CAPTURE_CONTENT_VISIBLE;
70     ScreenCaptureRect* area_ = nullptr;
71     uint64_t screenCaptureDisplayId_ = -1;
72 
73     FILE *aFile_ = nullptr;
74     FILE *vFile_ = nullptr;
75     int32_t aFlag_ = 0;
76     int32_t vFlag_ = 0;
77     int32_t frameNumber = 0;
78 };
79 
80 class ScreenCaptureUnitTest : public testing::Test {
81 public:
82     static void SetUpTestCase(void);
83     static void TearDownTestCase(void);
84     void SetUp(void);
85     void TearDown(void);
86     void AudioLoop(void);
87     void AudioLoopWithoutRelease(void);
88     void BeforeScreenCaptureSpecifiedWindowCbCase07(void);
89     int32_t SetConfig(AVScreenCaptureConfig &config);
90     int32_t SetConfigFile(AVScreenCaptureConfig &config, RecorderInfo &recorderInfo);
91     int32_t SetRecorderInfo(std::string name, RecorderInfo &recorderInfo);
92     void OpenFile(std::string name);
93     void CloseFile(void);
94     void OpenFile(std::string name, bool isInnerAudioEnabled, bool isMicAudioEnabled, bool isVideoEnabled);
95     void OpenFileFd(std::string name);
96     static void SetAccessTokenPermission();
97     static void SetHapPermission();
98 
99 protected:
100     static const std::string SCREEN_CAPTURE_ROOT_DIR;
101 
102     std::shared_ptr<ScreenCaptureMock> screenCapture_ = nullptr;
103     std::shared_ptr<ScreenCaptureUnitTestCallback> screenCaptureCb_ = nullptr;
104     std::unique_ptr<std::thread> audioLoop_ = nullptr;
105 
106     AVScreenCaptureConfig config_;
107     FILE *aFile = nullptr;
108     FILE *vFile = nullptr;
109     int32_t aFlag = 0;
110     int32_t vFlag = 0;
111     char fileName[100] = {0};
112     FILE *innerAudioFile_ = nullptr;
113     FILE *micAudioFile_ = nullptr;
114     FILE *videoFile_ = nullptr;
115     int32_t outputFd_ = -1;
116 };
117 
118 class ScreenCapBufferDemoConsumerListener : public IBufferConsumerListener {
119 public:
ScreenCapBufferDemoConsumerListener(sptr<Surface> consumer)120     ScreenCapBufferDemoConsumerListener(sptr<Surface> consumer)
121         : consumer_(consumer) {}
~ScreenCapBufferDemoConsumerListener()122     ~ScreenCapBufferDemoConsumerListener() {}
123     void OnBufferAvailable() override;
124 private:
125     sptr<OHOS::Surface> consumer_ = nullptr;
126 };
127 } // namespace Media
128 } // namespace OHOS
129 #endif