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 "avcodec_info.h" 23 #include "surface.h" 24 #include "recorder.h" 25 26 namespace OHOS { 27 namespace Media { 28 namespace ScreenCaptureState { 29 const std::string STATE_IDLE = "idle"; 30 const std::string STATE_STARTED = "started"; 31 const std::string STATE_STOPPED = "stopped"; 32 const std::string STATE_ERROR = "error"; 33 } 34 35 namespace ScreenCaptureEvent { 36 const std::string EVENT_STATE_CHANGE = "stateChange"; 37 const std::string EVENT_ERROR = "error"; 38 } 39 40 enum ScreenCaptureErrorType : int32_t { 41 SCREEN_CAPTURE_ERROR_INTERNAL, 42 SCREEN_CAPTURE_ERROR_EXTEND_START = 0X10000, 43 }; 44 45 enum AudioCaptureSourceType : int32_t { 46 /** Invalid audio source */ 47 SOURCE_INVALID = -1, 48 /** Default audio source */ 49 SOURCE_DEFAULT = 0, 50 /** Microphone */ 51 MIC = 1, 52 /** all PlayBack **/ 53 ALL_PLAYBACK = 2, 54 /** app PlayBack **/ 55 APP_PLAYBACK = 3 56 }; 57 58 enum DataType { 59 ORIGINAL_STREAM = 0, 60 ENCODED_STREAM = 1, 61 CAPTURE_FILE = 2, 62 INVAILD = -1 63 }; 64 65 enum CaptureMode : int32_t { 66 /* capture home screen */ 67 CAPTURE_HOME_SCREEN = 0, 68 /* capture a specified screen */ 69 CAPTURE_SPECIFIED_SCREEN = 1, 70 /* capture a specified window */ 71 CAPTURE_SPECIFIED_WINDOW = 2, 72 CAPTURE_INVAILD = -1 73 }; 74 75 struct AudioCaptureInfo { 76 int32_t audioSampleRate; 77 int32_t audioChannels; 78 AudioCaptureSourceType audioSource; 79 }; 80 81 struct AudioEncInfo { 82 int32_t audioBitrate; 83 AudioCodecFormat audioCodecformat; 84 }; 85 86 struct AudioInfo { 87 AudioCaptureInfo micCapInfo; 88 AudioCaptureInfo innerCapInfo; 89 AudioEncInfo audioEncInfo; 90 }; 91 92 struct VideoCaptureInfo { 93 uint64_t displayId; 94 std::list<int32_t> taskIDs; 95 int32_t videoFrameWidth; 96 int32_t videoFrameHeight; 97 VideoSourceType videoSource; 98 }; 99 100 struct VideoEncInfo { 101 VideoCodecFormat videoCodec; 102 int32_t videoBitrate; 103 int32_t videoFrameRate; 104 }; 105 106 struct VideoInfo { 107 VideoCaptureInfo videoCapInfo; 108 VideoEncInfo videoEncInfo; 109 }; 110 111 struct RecorderInfo { 112 std::string url; 113 std::string fileFormat; 114 }; 115 116 struct AVScreenCaptureConfig { 117 CaptureMode captureMode; 118 DataType dataType; 119 AudioInfo audioInfo; 120 VideoInfo videoInfo; 121 RecorderInfo recorderInfo; 122 }; 123 124 struct AudioBuffer { AudioBufferAudioBuffer125 AudioBuffer(uint8_t *buf, int32_t size, int64_t timestamp, AudioCaptureSourceType type) 126 : buffer(std::move(buf)), length(size), timestamp(timestamp), sourcetype(type) 127 { 128 } ~AudioBufferAudioBuffer129 ~AudioBuffer() 130 { 131 if (buffer != nullptr) { 132 free(buffer); 133 buffer = nullptr; 134 } 135 length = 0; 136 timestamp = 0; 137 } 138 uint8_t *buffer; 139 int32_t length; 140 int64_t timestamp; 141 AudioCaptureSourceType sourcetype; 142 }; 143 144 class ScreenCaptureCallBack { 145 public: 146 virtual ~ScreenCaptureCallBack() = default; 147 148 /** 149 * @brief Called when an error occurs during recording. This callback is used to report recording errors. 150 * 151 * @param errorType Indicates the error type. For details, see {@link RecorderErrorType}. 152 * @param errorCode Indicates the error code. 153 * @since 1.0 154 * @version 1.0 155 */ 156 virtual void OnError(ScreenCaptureErrorType errorType, int32_t errorCode) = 0; 157 158 virtual void OnAudioBufferAvailable(bool isReady, AudioCaptureSourceType type) = 0; 159 160 virtual void OnVideoBufferAvailable(bool isReady) = 0; 161 }; 162 163 class ScreenCapture { 164 public: 165 virtual ~ScreenCapture() = default; 166 virtual int32_t Init(AVScreenCaptureConfig config) = 0; 167 virtual int32_t SetMicrophoneEnabled(bool isMicrophone) = 0; 168 virtual int32_t StartScreenCapture() = 0; 169 virtual int32_t StopScreenCapture() = 0; 170 virtual int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audiobuffer, AudioCaptureSourceType type) = 0; 171 virtual sptr<OHOS::SurfaceBuffer> AcquireVideoBuffer(int32_t &fence, int64_t ×tamp, Rect &damage) = 0; 172 virtual int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) = 0; 173 virtual int32_t ReleaseVideoBuffer() = 0; 174 virtual int32_t Release() = 0; 175 virtual int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback) = 0; 176 }; 177 178 class __attribute__((visibility("default"))) ScreenCaptureFactory { 179 public: 180 #ifdef UNSUPPORT_SCREEN_CAPTURE CreateScreenCapture()181 static std::shared_ptr<ScreenCapture> CreateScreenCapture() 182 { 183 return nullptr; 184 } 185 #else 186 static std::shared_ptr<ScreenCapture> CreateScreenCapture(); 187 #endif 188 189 private: 190 ScreenCaptureFactory() = default; 191 ~ScreenCaptureFactory() = default; 192 }; 193 } // namespace Media 194 } // namespace OHOS 195 #endif // SCREEN_CAPTURE_H