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 AVScreenCaptureContentChangedEvent { 131 /* Content is hidden */ 132 SCREEN_CAPTURE_CONTENT_HIDE = 0, 133 /* Content is visible */ 134 SCREEN_CAPTURE_CONTENT_VISIBLE = 1, 135 /* ScreenCapture stopped by user */ 136 SCREEN_CAPTURE_CONTENT_UNAVAILABLE = 2, 137 }; 138 139 enum AVScreenCaptureFilterableAudioContent { 140 /* Audio content of notification sound */ 141 SCREEN_CAPTURE_NOTIFICATION_AUDIO = 0, 142 /* Audio content of the sound of the app itself */ 143 SCREEN_CAPTURE_CURRENT_APP_AUDIO = 1, 144 }; 145 146 enum AVScreenCaptureParamValidationState : int32_t { 147 VALIDATION_IGNORE, 148 VALIDATION_VALID, 149 VALIDATION_INVALID, 150 }; 151 152 enum AVScreenCaptureFillMode : int32_t { 153 PRESERVE_ASPECT_RATIO = 0, 154 SCALE_TO_FILL = 1, 155 }; 156 157 enum class AVScreenCapturePickerPopUp : int32_t { 158 SCREEN_CAPTURE_PICKER_POPUP_DEFAULT = -1, 159 SCREEN_CAPTURE_PICKER_POPUP_DISABLE = 0, 160 SCREEN_CAPTURE_PICKER_POPUP_ENABLE = 1, 161 }; 162 163 struct ScreenCaptureContentFilter { 164 std::set<AVScreenCaptureFilterableAudioContent> filteredAudioContents; 165 std::vector<uint64_t> windowIDsVec; 166 }; 167 168 struct AudioCaptureInfo { 169 int32_t audioSampleRate = 0; 170 int32_t audioChannels = 0; 171 AudioCaptureSourceType audioSource = AudioCaptureSourceType::SOURCE_DEFAULT; 172 AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE; 173 }; 174 175 struct AudioEncInfo { 176 int32_t audioBitrate = 0; 177 AudioCodecFormat audioCodecformat = AudioCodecFormat::AUDIO_CODEC_FORMAT_BUTT; 178 AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE; 179 }; 180 181 struct AudioInfo { 182 AudioCaptureInfo micCapInfo; 183 AudioCaptureInfo innerCapInfo; 184 AudioEncInfo audioEncInfo; 185 }; 186 187 struct ScreenCaptureStrategy { 188 bool enableDeviceLevelCapture = false; 189 bool keepCaptureDuringCall = false; 190 int32_t strategyForPrivacyMaskMode = 0; 191 bool canvasFollowRotation = false; 192 bool enableBFrame = false; 193 bool setByUser = false; 194 AVScreenCapturePickerPopUp pickerPopUp = AVScreenCapturePickerPopUp::SCREEN_CAPTURE_PICKER_POPUP_DEFAULT; 195 AVScreenCaptureFillMode fillMode = AVScreenCaptureFillMode::PRESERVE_ASPECT_RATIO; 196 }; 197 198 struct VideoCaptureInfo { 199 uint64_t displayId = 0; 200 std::list<int32_t> taskIDs; 201 int32_t videoFrameWidth = 0; 202 int32_t videoFrameHeight = 0; 203 VideoSourceType videoSource = VideoSourceType::VIDEO_SOURCE_BUTT; 204 AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE; 205 }; 206 207 struct VideoEncInfo { 208 VideoCodecFormat videoCodec = VideoCodecFormat::VIDEO_CODEC_FORMAT_BUTT; 209 int32_t videoBitrate = 0; 210 int32_t videoFrameRate = 0; 211 AVScreenCaptureParamValidationState state = AVScreenCaptureParamValidationState::VALIDATION_IGNORE; 212 }; 213 214 struct VideoInfo { 215 VideoCaptureInfo videoCapInfo; 216 VideoEncInfo videoEncInfo; 217 }; 218 219 struct RecorderInfo { 220 std::string url; 221 std::string fileFormat; 222 }; 223 224 struct AVScreenCaptureConfig { 225 CaptureMode captureMode = CaptureMode::CAPTURE_INVAILD; 226 DataType dataType = DataType::INVAILD; 227 AudioInfo audioInfo; 228 VideoInfo videoInfo; 229 RecorderInfo recorderInfo; 230 ScreenCaptureStrategy strategy; 231 }; 232 233 struct AudioBuffer { AudioBufferAudioBuffer234 AudioBuffer(uint8_t *buf, int32_t size, int64_t timestamp, AudioCaptureSourceType type) 235 : buffer(std::move(buf)), length(size), timestamp(timestamp), sourcetype(type) 236 { 237 } ~AudioBufferAudioBuffer238 ~AudioBuffer() 239 { 240 if (buffer != nullptr) { 241 free(buffer); 242 buffer = nullptr; 243 } 244 length = 0; 245 timestamp = 0; 246 } 247 uint8_t *buffer; 248 int32_t length; 249 int64_t timestamp; 250 AudioCaptureSourceType sourcetype; 251 }; 252 253 typedef struct ScreenCaptureRect { 254 /* X-coordinate of screen recording */ 255 int32_t x; 256 /* y-coordinate of screen recording */ 257 int32_t y; 258 /* Width of screen recording */ 259 int32_t width; 260 /* Height of screen recording */ 261 int32_t height; 262 } ScreenCaptureRect; 263 264 struct ScreenCaptureUserSelectionInfo { 265 int32_t selectType; 266 uint64_t displayId; 267 }; 268 269 class ScreenCaptureCallBack { 270 public: 271 virtual ~ScreenCaptureCallBack() = default; 272 273 /** 274 * @brief Called when an error occurs during screen capture. This callback is used to report errors. 275 * 276 * @param errorType Indicates the error type. For details, see {@link ScreenCaptureErrorType}. 277 * @param errorCode Indicates the error code. 278 * @since 1.0 279 * @version 1.0 280 */ 281 virtual void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) = 0; 282 283 virtual void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) = 0; 284 285 virtual void OnVideoBufferAvailable(bool isReady) = 0; 286 OnStateChange(AVScreenCaptureStateCode stateCode)287 virtual void OnStateChange(AVScreenCaptureStateCode stateCode) 288 { 289 (void)stateCode; 290 return; 291 } 292 OnDisplaySelected(uint64_t displayId)293 virtual void OnDisplaySelected(uint64_t displayId) 294 { 295 (void)displayId; 296 return; 297 } 298 OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event,ScreenCaptureRect * area)299 virtual void OnCaptureContentChanged(AVScreenCaptureContentChangedEvent event, ScreenCaptureRect* area) 300 { 301 (void)event; 302 (void)area; 303 return; 304 } 305 OnUserSelected(ScreenCaptureUserSelectionInfo selectionInfo)306 virtual void OnUserSelected(ScreenCaptureUserSelectionInfo selectionInfo) 307 { 308 (void)selectionInfo; 309 return; 310 } 311 }; 312 313 class ScreenCapture { 314 public: 315 virtual ~ScreenCapture() = default; 316 virtual int32_t Init(AVScreenCaptureConfig config) = 0; 317 virtual int32_t Init(OHOS::AudioStandard::AppInfo &appInfo) = 0; 318 virtual int32_t SetMicrophoneEnabled(bool isMicrophone) = 0; 319 virtual int32_t SetCanvasRotation(bool canvasRotation) = 0; 320 virtual int32_t ShowCursor(bool showCursor) = 0; 321 virtual int32_t ResizeCanvas(int32_t width, int32_t height) = 0; 322 virtual int32_t SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec) = 0; 323 virtual int32_t SetMaxVideoFrameRate(int32_t frameRate) = 0; 324 virtual int32_t StartScreenCapture() = 0; 325 virtual int32_t StartScreenCaptureWithSurface(sptr<Surface> surface) = 0; 326 virtual int32_t StopScreenCapture() = 0; 327 virtual int32_t StartScreenRecording() = 0; 328 virtual int32_t StopScreenRecording() = 0; 329 virtual int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audiobuffer, AudioCaptureSourceType type) = 0; 330 virtual sptr<OHOS::SurfaceBuffer> AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, Rect &damage) = 0; 331 virtual int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) = 0; 332 virtual int32_t ReleaseVideoBuffer() = 0; 333 virtual int32_t Release() = 0; 334 virtual int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback) = 0; 335 virtual int32_t ExcludeContent(ScreenCaptureContentFilter &contentFilter) = 0; 336 virtual int32_t SetPrivacyAuthorityEnabled() = 0; 337 virtual int32_t SetScreenCaptureStrategy(ScreenCaptureStrategy strategy) = 0; 338 virtual int32_t UpdateSurface(sptr<Surface> surface) = 0; 339 virtual int32_t SetCaptureArea(uint64_t displayId, Rect area) = 0; 340 }; 341 342 class __attribute__((visibility("default"))) ScreenCaptureFactory { 343 public: 344 #ifdef UNSUPPORT_SCREEN_CAPTURE CreateScreenCapture()345 static std::shared_ptr<ScreenCapture> CreateScreenCapture() 346 { 347 return nullptr; 348 } CreateScreenCapture(OHOS::AudioStandard::AppInfo & appInfo)349 static std::shared_ptr<ScreenCapture> CreateScreenCapture(OHOS::AudioStandard::AppInfo &appInfo) 350 { 351 return nullptr; 352 } 353 #else 354 static std::shared_ptr<ScreenCapture> CreateScreenCapture(); 355 static std::shared_ptr<ScreenCapture> CreateScreenCapture(OHOS::AudioStandard::AppInfo &appInfo); 356 #endif 357 358 private: 359 ScreenCaptureFactory() = default; 360 ~ScreenCaptureFactory() = default; 361 }; 362 } // namespace Media 363 } // namespace OHOS 364 #endif // SCREEN_CAPTURE_H