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