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 
16 #ifndef SCREEN_CAPTURE_H
17 #define SCREEN_CAPTURE_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <list>
22 #include <set>
23 #include "avcodec_info.h"
24 #include "surface.h"
25 #include "recorder.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace ScreenCaptureState {
30 const std::string STATE_IDLE = "idle";
31 const std::string STATE_STARTED = "started";
32 const std::string STATE_STOPPED = "stopped";
33 const std::string STATE_ERROR = "error";
34 }
35 
36 namespace ScreenCaptureEvent {
37 const std::string EVENT_STATE_CHANGE = "stateChange";
38 const std::string EVENT_ERROR = "error";
39 }
40 
41 enum ScreenCaptureErrorType : int32_t {
42     SCREEN_CAPTURE_ERROR_INTERNAL,
43     SCREEN_CAPTURE_ERROR_EXTEND_START = 0X10000,
44 };
45 
46 enum AVScreenCaptureErrorCode {
47     SCREEN_CAPTURE_ERR_BASE = 0,
48     SCREEN_CAPTURE_ERR_OK = SCREEN_CAPTURE_ERR_BASE,
49     SCREEN_CAPTURE_ERR_NO_MEMORY = SCREEN_CAPTURE_ERR_BASE + 1,
50     SCREEN_CAPTURE_ERR_OPERATE_NOT_PERMIT = SCREEN_CAPTURE_ERR_BASE + 2,
51     SCREEN_CAPTURE_ERR_INVALID_VAL = SCREEN_CAPTURE_ERR_BASE + 3,
52     SCREEN_CAPTURE_ERR_IO = SCREEN_CAPTURE_ERR_BASE + 4,
53     SCREEN_CAPTURE_ERR_TIMEOUT = SCREEN_CAPTURE_ERR_BASE + 5,
54     SCREEN_CAPTURE_ERR_UNKNOWN = SCREEN_CAPTURE_ERR_BASE + 6,
55     SCREEN_CAPTURE_ERR_SERVICE_DIED = SCREEN_CAPTURE_ERR_BASE + 7,
56     SCREEN_CAPTURE_ERR_INVALID_STATE = SCREEN_CAPTURE_ERR_BASE + 8,
57     SCREEN_CAPTURE_ERR_UNSUPPORT = SCREEN_CAPTURE_ERR_BASE + 9,
58     SCREEN_CAPTURE_ERR_EXTEND_START = SCREEN_CAPTURE_ERR_BASE + 100,
59 };
60 
61 enum AudioCaptureSourceType : int32_t {
62     /** Invalid audio source */
63     SOURCE_INVALID = -1,
64     /** Default audio source */
65     SOURCE_DEFAULT = 0,
66     /** Microphone */
67     MIC = 1,
68     /** all PlayBack **/
69     ALL_PLAYBACK = 2,
70     /** app PlayBack **/
71     APP_PLAYBACK = 3
72 };
73 
74 enum DataType {
75     ORIGINAL_STREAM = 0,
76     ENCODED_STREAM = 1,
77     CAPTURE_FILE = 2,
78     INVAILD = -1
79 };
80 
81 enum CaptureMode : int32_t {
82     /* capture home screen */
83     CAPTURE_HOME_SCREEN = 0,
84     /* capture a specified screen */
85     CAPTURE_SPECIFIED_SCREEN = 1,
86     /* capture a specified window */
87     CAPTURE_SPECIFIED_WINDOW = 2,
88     CAPTURE_INVAILD = -1
89 };
90 
91 enum AVScreenCaptureStateCode {
92     /* Screen capture state INVALID */
93     SCREEN_CAPTURE_STATE_INVLID = -1,
94     /* Screen capture started by user */
95     SCREEN_CAPTURE_STATE_STARTED = 0,
96     /* Screen capture canceled by user */
97     SCREEN_CAPTURE_STATE_CANCELED = 1,
98     /* ScreenCapture stopped by user */
99     SCREEN_CAPTURE_STATE_STOPPED_BY_USER = 2,
100     /* ScreenCapture interrupted by other screen capture */
101     SCREEN_CAPTURE_STATE_INTERRUPTED_BY_OTHER = 3,
102     /* ScreenCapture stopped by SIM call */
103     SCREEN_CAPTURE_STATE_STOPPED_BY_CALL = 4,
104     /* Microphone is temporarily unavailable */
105     SCREEN_CAPTURE_STATE_MIC_UNAVAILABLE = 5,
106     /* Microphone is muted by user */
107     SCREEN_CAPTURE_STATE_MIC_MUTED_BY_USER = 6,
108     /* Microphone is unmuted by user */
109     SCREEN_CAPTURE_STATE_MIC_UNMUTED_BY_USER = 7,
110     /* Current captured screen has private window */
111     SCREEN_CAPTURE_STATE_ENTER_PRIVATE_SCENE = 8,
112     /* Private window disappeared on current captured screen*/
113     SCREEN_CAPTURE_STATE_EXIT_PRIVATE_SCENE = 9,
114     /* ScreenCapture stopped by user switches */
115     SCREEN_CAPTURE_STATE_STOPPED_BY_USER_SWITCHES = 10,
116 };
117 
118 enum AVScreenCaptureBufferType {
119     /* Buffer of video data from screen */
120     SCREEN_CAPTURE_BUFFERTYPE_INVALID = -1,
121     /* Buffer of video data from screen */
122     SCREEN_CAPTURE_BUFFERTYPE_VIDEO = 0,
123     /* Buffer of audio data from inner capture */
124     SCREEN_CAPTURE_BUFFERTYPE_AUDIO_INNER = 1,
125     /* Buffer of audio data from microphone */
126     SCREEN_CAPTURE_BUFFERTYPE_AUDIO_MIC = 2,
127 };
128 
129 enum AVScreenCaptureFilterableAudioContent {
130     /* Audio content of notification sound */
131     SCREEN_CAPTURE_NOTIFICATION_AUDIO = 0,
132     /* Audio content of the sound of the app itself */
133     SCREEN_CAPTURE_CURRENT_APP_AUDIO = 1,
134 };
135 
136 enum AVScreenCaptureParamValidationState : int32_t {
137     VALIDATION_IGNORE,
138     VALIDATION_VALID,
139     VALIDATION_INVALID,
140 };
141 
142 struct ScreenCaptureContentFilter {
143     std::set<AVScreenCaptureFilterableAudioContent> filteredAudioContents;
144     std::vector<uint64_t> windowIDsVec;
145 };
146 
147 struct AudioCaptureInfo {
148     int32_t audioSampleRate = 0;
149     int32_t audioChannels = 0;
150     AudioCaptureSourceType audioSource = AudioCaptureSourceType::SOURCE_DEFAULT;
151     AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
152 };
153 
154 struct AudioEncInfo {
155     int32_t audioBitrate = 0;
156     AudioCodecFormat audioCodecformat = AudioCodecFormat::AUDIO_CODEC_FORMAT_BUTT;
157     AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
158 };
159 
160 struct AudioInfo {
161     AudioCaptureInfo micCapInfo;
162     AudioCaptureInfo innerCapInfo;
163     AudioEncInfo audioEncInfo;
164 };
165 
166 struct VideoCaptureInfo {
167     uint64_t displayId = -1;
168     std::list<int32_t> taskIDs;
169     int32_t videoFrameWidth = 0;
170     int32_t videoFrameHeight = 0;
171     VideoSourceType videoSource = VideoSourceType::VIDEO_SOURCE_BUTT;
172     AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
173 };
174 
175 struct VideoEncInfo {
176     VideoCodecFormat videoCodec = VideoCodecFormat::VIDEO_CODEC_FORMAT_BUTT;
177     int32_t videoBitrate = 0;
178     int32_t videoFrameRate = 0;
179     AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE;
180 };
181 
182 struct VideoInfo {
183     VideoCaptureInfo videoCapInfo;
184     VideoEncInfo videoEncInfo;
185 };
186 
187 struct RecorderInfo {
188     std::string url;
189     std::string fileFormat;
190 };
191 
192 struct AVScreenCaptureConfig {
193     CaptureMode captureMode = CaptureMode::CAPTURE_INVAILD;
194     DataType dataType = DataType::INVAILD;
195     AudioInfo audioInfo;
196     VideoInfo videoInfo;
197     RecorderInfo recorderInfo;
198 };
199 
200 struct AudioBuffer {
AudioBufferAudioBuffer201     AudioBuffer(uint8_t *buf, int32_t size, int64_t timestamp, AudioCaptureSourceType type)
202         : buffer(std::move(buf)), length(size), timestamp(timestamp), sourcetype(type)
203     {
204     }
~AudioBufferAudioBuffer205     ~AudioBuffer()
206     {
207         if (buffer != nullptr) {
208             free(buffer);
209             buffer = nullptr;
210         }
211         length = 0;
212         timestamp = 0;
213     }
214     uint8_t *buffer;
215     int32_t length;
216     int64_t timestamp;
217     AudioCaptureSourceType sourcetype;
218 };
219 
220 class ScreenCaptureCallBack {
221 public:
222     virtual ~ScreenCaptureCallBack() = default;
223 
224     /**
225      * @brief Called when an error occurs during screen capture. This callback is used to report errors.
226      *
227      * @param errorType Indicates the error type. For details, see {@link ScreenCaptureErrorType}.
228      * @param errorCode Indicates the error code.
229      * @since 1.0
230      * @version 1.0
231      */
232     virtual void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) = 0;
233 
234     virtual void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) = 0;
235 
236     virtual void OnVideoBufferAvailable(bool isReady) = 0;
237 
OnStateChange(AVScreenCaptureStateCode stateCode)238     virtual void OnStateChange(AVScreenCaptureStateCode stateCode)
239     {
240         (void)stateCode;
241         return;
242     }
243 };
244 
245 class ScreenCapture {
246 public:
247     virtual ~ScreenCapture() = default;
248     virtual int32_t Init(AVScreenCaptureConfig config) = 0;
249     virtual int32_t SetMicrophoneEnabled(bool isMicrophone) = 0;
250     virtual int32_t SetCanvasRotation(bool canvasRotation) = 0;
251     virtual int32_t ResizeCanvas(int32_t width, int32_t height) = 0;
252     virtual int32_t SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec) = 0;
253     virtual int32_t SetMaxVideoFrameRate(int32_t frameRate) = 0;
254     virtual int32_t StartScreenCapture() = 0;
255     virtual int32_t StartScreenCaptureWithSurface(sptr<Surface> surface) = 0;
256     virtual int32_t StopScreenCapture() = 0;
257     virtual int32_t StartScreenRecording() = 0;
258     virtual int32_t StopScreenRecording() = 0;
259     virtual int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audiobuffer, AudioCaptureSourceType type) = 0;
260     virtual sptr<OHOS::SurfaceBuffer> AcquireVideoBuffer(int32_t &fence, int64_t &timestamp, Rect &damage) = 0;
261     virtual int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) = 0;
262     virtual int32_t ReleaseVideoBuffer() = 0;
263     virtual int32_t Release() = 0;
264     virtual int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback) = 0;
265     virtual int32_t ExcludeContent(ScreenCaptureContentFilter &contentFilter) = 0;
266     virtual int32_t SetPrivacyAuthorityEnabled() = 0;
267 };
268 
269 class __attribute__((visibility("default"))) ScreenCaptureFactory {
270 public:
271 #ifdef UNSUPPORT_SCREEN_CAPTURE
CreateScreenCapture()272     static std::shared_ptr<ScreenCapture> CreateScreenCapture()
273     {
274         return nullptr;
275     }
276 #else
277     static std::shared_ptr<ScreenCapture> CreateScreenCapture();
278 #endif
279 
280 private:
281     ScreenCaptureFactory() = default;
282     ~ScreenCaptureFactory() = default;
283 };
284 } // namespace Media
285 } // namespace OHOS
286 #endif // SCREEN_CAPTURE_H