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