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_SERVICE_SERVER_H 17 #define SCREEN_CAPTURE_SERVICE_SERVER_H 18 19 #include "screen_capture_server_base.h" 20 #include "ui_extension_ability_connection.h" 21 22 namespace OHOS { 23 namespace Media { 24 25 class ScreenCaptureServer : public std::enable_shared_from_this<ScreenCaptureServer>, 26 public IScreenCaptureService, public NoCopyable { 27 public: 28 static std::map<int32_t, std::weak_ptr<ScreenCaptureServer>> serverMap_; 29 static std::map<int32_t, std::pair<int32_t, int32_t>> saUidAppUidMap_; 30 static const int32_t maxSessionId_; 31 static const int32_t maxAppLimit_; 32 static UniqueIDGenerator gIdGenerator_; 33 static std::list<int32_t> startedSessionIDList_; 34 static const int32_t maxSessionPerUid_; 35 static const int32_t maxSCServerDataTypePerUid_; 36 static std::shared_mutex mutexServerMapRWGlobal_; 37 static std::shared_mutex mutexListRWGlobal_; 38 static std::shared_mutex mutexSaAppInfoMapGlobal_; 39 static std::atomic<int32_t> systemScreenRecorderPid_; 40 41 static std::shared_ptr<IScreenCaptureService> Create(); 42 static bool IsSAServiceCalling(); 43 static bool CanScreenCaptureInstanceBeCreate(int32_t appUid); 44 static std::shared_ptr<IScreenCaptureService> CreateScreenCaptureNewInstance(); 45 static int32_t ReportAVScreenCaptureUserChoice(int32_t sessionId, const std::string &content); 46 static int32_t GetRunningScreenCaptureInstancePid(std::list<int32_t> &pidList); 47 static void GetChoiceFromJson(Json::Value &root, const std::string &content, std::string key, std::string &value); 48 static void PrepareSelectWindow(Json::Value &root, std::shared_ptr<ScreenCaptureServer> &server); 49 static void AddScreenCaptureServerMap(int32_t sessionId, std::weak_ptr<ScreenCaptureServer> server); 50 static void RemoveScreenCaptureServerMap(int32_t sessionId); 51 static bool CheckScreenCaptureSessionIdLimit(int32_t curAppUid); 52 static bool CheckSCServerSpecifiedDataTypeNum(int32_t curAppUid, DataType dataType); 53 static void CountScreenCaptureAppNum(std::set<int32_t>& appSet); 54 static bool CheckScreenCaptureAppLimit(int32_t curAppUid); 55 static std::shared_ptr<ScreenCaptureServer> GetScreenCaptureServerById(int32_t id); 56 static std::shared_ptr<ScreenCaptureServer> GetScreenCaptureServerByIdWithLock(int32_t id); 57 static std::list<int32_t> GetStartedScreenCaptureServerPidList(); 58 static int32_t CountStartedScreenCaptureServerNumByPid(int32_t pid); 59 static void AddStartedSessionIdList(int32_t value); 60 static void RemoveStartedSessionIdList(int32_t value); 61 static std::list<int32_t> GetAllStartedSessionIdList(); 62 static void AddSaAppInfoMap(int32_t saUid, int32_t curAppUid); 63 static void RemoveSaAppInfoMap(int32_t saUid); 64 static bool CheckSaUid(int32_t saUid, int32_t appUid); 65 static bool IsSaUidValid(int32_t saUid, int32_t appUid); 66 static bool CheckPidIsScreenRecorder(int32_t pid); 67 ScreenCaptureServer(); 68 ~ScreenCaptureServer(); 69 70 int32_t SetCaptureMode(CaptureMode captureMode) override; 71 int32_t SetDataType(DataType dataType) override; 72 int32_t SetRecorderInfo(RecorderInfo recorderInfo) override; 73 int32_t SetOutputFile(int32_t outputFd) override; 74 int32_t SetAndCheckLimit() override; 75 int32_t SetAndCheckSaLimit(OHOS::AudioStandard::AppInfo &appInfo) override; 76 int32_t InitAudioEncInfo(AudioEncInfo audioEncInfo) override; 77 int32_t InitAudioCap(AudioCaptureInfo audioInfo) override; 78 int32_t InitVideoEncInfo(VideoEncInfo videoEncInfo) override; 79 int32_t InitVideoCap(VideoCaptureInfo videoInfo) override; 80 int32_t StartScreenCapture(bool isPrivacyAuthorityEnabled) override; 81 int32_t StartScreenCaptureWithSurface(sptr<Surface> surface, bool isPrivacyAuthorityEnabled) override; 82 int32_t StopScreenCapture() override; 83 int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback) override; 84 int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type) override; 85 int32_t AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence, 86 int64_t ×tamp, OHOS::Rect &damage) override; 87 int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) override; 88 int32_t ReleaseVideoBuffer() override; 89 int32_t SetMicrophoneEnabled(bool isMicrophone) override; 90 bool GetMicWorkingState(); 91 int32_t SetCanvasRotation(bool canvasRotation) override; 92 int32_t ShowCursor(bool showCursor) override; 93 int32_t ResizeCanvas(int32_t width, int32_t height) override; 94 int32_t SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec) override; 95 int32_t SetMaxVideoFrameRate(int32_t frameRate) override; 96 void Release() override; 97 int32_t ExcludeContent(ScreenCaptureContentFilter &contentFilter) override; 98 99 void SetSessionId(int32_t sessionId); 100 int32_t OnReceiveUserPrivacyAuthority(bool isAllowed); 101 int32_t StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode); 102 #ifdef SUPPORT_CALL 103 int32_t TelCallStateUpdated(bool isInTelCall); 104 int32_t TelCallAudioStateUpdated(bool isInTelCallAudio); 105 #endif 106 void UpdateMicrophoneEnabled(); 107 108 int32_t AcquireAudioBufferMix(std::shared_ptr<AudioBuffer> &innerAudioBuffer, 109 std::shared_ptr<AudioBuffer> &micAudioBuffer, AVScreenCaptureMixMode type); 110 int32_t ReleaseAudioBufferMix(AVScreenCaptureMixMode type); 111 int32_t ReleaseMicAudioBuffer(); 112 int32_t ReleaseInnerAudioBuffer(); 113 int32_t GetInnerAudioCaptureBufferSize(size_t &size); 114 int32_t GetMicAudioCaptureBufferSize(size_t &size); 115 int32_t OnVoIPStatusChanged(bool isInVoIPCall); 116 int32_t OnSpeakerAliveStatusChanged(bool speakerAliveStatus); 117 int32_t ShowCursorInner(); 118 void OnDMPrivateWindowChange(bool hasPrivate); 119 void SetMissionId(uint64_t missionId); 120 void SetDisplayId(uint64_t displayId); 121 bool IsTelInCallSkipList(); 122 int32_t GetAppPid(); 123 int32_t GetAppUid(); 124 void NotifyStateChange(AVScreenCaptureStateCode stateCode); 125 void NotifyDisplaySelected(uint64_t displayId); 126 void SetMouseChangeListener(std::shared_ptr<MouseChangeListener> listener); 127 std::shared_ptr<MouseChangeListener> GetMouseChangeListener(); 128 int32_t SetAndCheckAppInfo(OHOS::AudioStandard::AppInfo &appInfo); 129 void SetSCServerSaUid(int32_t saUid); 130 int32_t GetSCServerSaUid(); 131 DataType GetSCServerDataType(); 132 AVScreenCaptureState GetSCServerCaptureState(); 133 134 private: 135 int32_t StartScreenCaptureInner(bool isPrivacyAuthorityEnabled); 136 int32_t RegisterServerCallbacks(); 137 int32_t OnStartScreenCapture(); 138 bool IsFirstStartPidInstance(int32_t pid); 139 bool FirstPidUpdatePrivacyUsingPermissionState(int32_t pid); 140 void PostStartScreenCapture(bool isSuccess); 141 void PostStartScreenCaptureSuccessAction(); 142 int32_t InitRecorderInfo(std::shared_ptr<IRecorderService> &recorder, AudioCaptureInfo audioInfo); 143 int32_t InitRecorder(); 144 int32_t StartScreenCaptureFile(); 145 int32_t StartScreenCaptureStream(); 146 int32_t StartAudioCapture(); 147 std::string GenerateThreadNameByPrefix(std::string threadName); 148 int32_t StartStreamInnerAudioCapture(); 149 int32_t StartStreamMicAudioCapture(); 150 int32_t StartFileInnerAudioCapture(); 151 int32_t StopInnerAudioCapture(); 152 int32_t StartFileMicAudioCapture(); 153 int32_t StartMicAudioCapture(); 154 int32_t StopMicAudioCapture(); 155 int32_t StartStreamVideoCapture(); 156 int32_t StartStreamHomeVideoCapture(); 157 int32_t StopScreenCaptureInner(AVScreenCaptureStateCode stateCode); 158 bool IsLastStartedPidInstance(int32_t pid); 159 bool LastPidUpdatePrivacyUsingPermissionState(int32_t pid); 160 void PostStopScreenCapture(AVScreenCaptureStateCode stateCode); 161 int32_t StopAudioCapture(); 162 int32_t StopVideoCapture(); 163 int32_t StopScreenCaptureRecorder(); 164 int32_t CheckAllParams(); 165 int32_t CheckCaptureStreamParams(); 166 int32_t CheckCaptureFileParams(); 167 int32_t SetCanvasRotationInner(); 168 int32_t SkipPrivacyModeInner(); 169 int32_t SetScreenScaleMode(); 170 void InitAppInfo(); 171 void CloseFd(); 172 void ReleaseInner(); 173 void GetDumpFlag(); 174 int32_t SetMicrophoneOn(); 175 int32_t SetMicrophoneOff(); 176 177 VirtualScreenOption InitVirtualScreenOption(const std::string &name, sptr<OHOS::Surface> consumer); 178 int32_t GetMissionIds(std::vector<uint64_t> &missionIds); 179 int32_t MakeVirtualScreenMirrorForWindow(sptr<Rosen::Display> defaultDisplay, 180 std::vector<ScreenId> mirrorIds); 181 int32_t MakeVirtualScreenMirrorForHomeScreen(sptr<Rosen::Display> defaultDisplay, 182 std::vector<ScreenId> mirrorIds); 183 int32_t MakeVirtualScreenMirrorForSpecifiedScreen(sptr<Rosen::Display> defaultDisplay, 184 std::vector<ScreenId> mirrorIds); 185 int32_t MakeVirtualScreenMirror(); 186 int32_t CreateVirtualScreen(const std::string &name, sptr<OHOS::Surface> consumer); 187 int32_t PrepareVirtualScreenMirror(); 188 void DestroyVirtualScreen(); 189 190 bool CheckScreenCapturePermission(); 191 bool IsUserPrivacyAuthorityNeeded(); 192 bool UpdatePrivacyUsingPermissionState(VideoPermissionState state); 193 bool CheckPrivacyWindowSkipPermission(); 194 int32_t RequestUserPrivacyAuthority(); 195 int32_t StartPrivacyWindow(); 196 void SetCaptureConfig(CaptureMode captureMode, int32_t missionId = -1); // -1 invalid 197 ScreenScaleMode GetScreenScaleMode(const AVScreenCaptureFillMode &fillMode); 198 #ifdef PC_STANDARD 199 bool CheckCaptureSpecifiedWindowForSelectWindow(); 200 void SendConfigToUIParams(AAFwk::Want& want); 201 bool IsHopper(); 202 int32_t MakeVirtualScreenMirrorForHomeScreenForHopper(sptr<Rosen::Display> defaultDisplay, 203 std::vector<ScreenId> mirrorIds); 204 int32_t MakeVirtualScreenMirrorForSpecifiedScreenForHopper(sptr<Rosen::Display> defaultDisplay, 205 std::vector<ScreenId> mirrorIds); 206 #endif 207 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION 208 int32_t TryStartNotification(); 209 int32_t TryNotificationOnPostStartScreenCapture(); 210 #endif 211 int32_t StartNotification(); 212 std::shared_ptr<NotificationLocalLiveViewContent> GetLocalLiveViewContent(); 213 void UpdateLiveViewContent(); 214 std::shared_ptr<PixelMap> GetPixelMap(std::string path); 215 std::shared_ptr<PixelMap> GetPixelMapSvg(std::string path, int32_t width, int32_t height); 216 void ResSchedReportData(int64_t value, std::unordered_map<std::string, std::string> payload); 217 int64_t GetCurrentMillisecond(); 218 void SetMetaDataReport(); 219 void SetErrorInfo(int32_t errCode, const std::string &errMsg, StopReason stopReason, bool userAgree); 220 int32_t ReStartMicForVoIPStatusSwitch(); 221 void RegisterPrivateWindowListener(); 222 void RegisterScreenConnectListener(); 223 bool RegisterMMISystemAbilityListener(); 224 bool UnRegisterMMISystemAbilityListener(); 225 int32_t RegisterMouseChangeListener(std::string type); 226 int32_t UnRegisterMouseChangeListener(std::string type); 227 uint64_t GetDisplayIdOfWindows(uint64_t displayId); 228 std::string GetStringByResourceName(const char* name); 229 void RefreshResConfig(); 230 void InitResourceManager(); 231 void SetSystemScreenRecorderStatus(bool status); 232 #ifdef SUPPORT_CALL 233 int32_t OnTelCallStart(); 234 int32_t OnTelCallStop(); 235 #endif 236 bool DestroyPopWindow(); 237 void StopNotStartedScreenCapture(AVScreenCaptureStateCode stateCode); 238 239 private: 240 std::mutex mutex_; 241 std::mutex cbMutex_; 242 std::shared_ptr<ScreenCaptureObserverCallBack> screenCaptureObserverCb_ = nullptr; 243 std::shared_ptr<ScreenCaptureCallBack> screenCaptureCb_ = nullptr; 244 bool canvasRotation_ = false; 245 bool showCursor_ = true; 246 bool isMicrophoneSwitchTurnOn_ = true; 247 bool isPrivacyAuthorityEnabled_ = false; 248 std::vector<uint64_t> surfaceIdList_ = {}; 249 250 int32_t sessionId_ = 0; 251 int32_t notificationId_ = 0; 252 std::string callingLabel_; 253 std::string liveViewText_; 254 std::atomic<int32_t> micCount_{0}; 255 float density_ = 0.0f; 256 int32_t capsuleVpSize_ = 18; 257 int32_t capsulePxSize_ = 0; 258 int32_t saUid_ = -1; 259 260 /* used for both CAPTURE STREAM and CAPTURE FILE */ 261 OHOS::AudioStandard::AppInfo appInfo_; 262 bool isScreenCaptureAuthority_ = false; 263 std::string appName_ = ""; 264 AVScreenCaptureConfig captureConfig_; 265 AVScreenCaptureAvType avType_ = AVScreenCaptureAvType::INVALID_TYPE; 266 AVScreenCaptureDataMode dataMode_ = AVScreenCaptureDataMode::BUFFER_MODE; 267 StatisticalEventInfo statisticalEventInfo_; 268 sptr<OHOS::Surface> consumer_ = nullptr; 269 bool isConsumerStart_ = false; 270 bool isDump_ = false; 271 ScreenId virtualScreenId_ = SCREEN_ID_INVALID; 272 ScreenId displayScreenId_ = SCREEN_ID_INVALID; 273 std::vector<uint64_t> missionIds_; 274 ScreenCaptureContentFilter contentFilter_; 275 AVScreenCaptureState captureState_ = AVScreenCaptureState::CREATED; 276 std::shared_ptr<NotificationLocalLiveViewContent> localLiveViewContent_; 277 int64_t startTime_ = 0; 278 sptr<ISystemAbilityStatusChange> mmiListener_ = nullptr; 279 std::shared_ptr<MouseChangeListener> mouseChangeListener_ = nullptr; 280 sptr<UIExtensionAbilityConnection> connection_ = nullptr; 281 282 /* used for CAPTURE STREAM */ 283 sptr<IBufferConsumerListener> surfaceCb_ = nullptr; 284 sptr<OHOS::Surface> surface_ = nullptr; 285 bool isSurfaceMode_ = false; 286 std::shared_ptr<AudioCapturerWrapper> innerAudioCapture_; 287 std::shared_ptr<AudioCapturerWrapper> micAudioCapture_; 288 289 /* used for CAPTURE FILE */ 290 std::shared_ptr<IRecorderService> recorder_ = nullptr; 291 std::string url_; 292 OutputFormatType fileFormat_ = OutputFormatType::FORMAT_DEFAULT; 293 int32_t outputFd_ = -1; 294 int32_t audioSourceId_ = 0; 295 int32_t videoSourceId_ = 0; 296 std::shared_ptr<AudioDataSource> audioSource_ = nullptr; 297 /* used for DFX events */ 298 uint64_t instanceId_ = 0; 299 std::shared_ptr<ScreenRendererAudioStateChangeCallback> captureCallback_; 300 std::vector<uint64_t> skipPrivacyWindowIDsVec_; 301 sptr<DisplayManager::IPrivateWindowListener> displayListener_; 302 bool isCalledBySystemApp_ = false; 303 Global::Resource::ResourceManager *resourceManager_ = nullptr; 304 Global::Resource::ResConfig *resConfig_ = nullptr; 305 OHOS::sptr<Rosen::ScreenManager::IScreenListener> screenConnectListener_ = nullptr; 306 int32_t apiVersion_ = -1; 307 #ifdef SUPPORT_CALL 308 std::atomic<bool> isInTelCall_ = false; 309 std::atomic<bool> isInTelCallAudio_ = false; 310 #endif 311 312 private: 313 static int32_t CheckAudioCapParam(const AudioCaptureInfo &audioCapInfo); 314 static int32_t CheckVideoCapParam(const VideoCaptureInfo &videoCapInfo); 315 static int32_t CheckAudioEncParam(const AudioEncInfo &audioEncInfo); 316 static int32_t CheckVideoEncParam(const VideoEncInfo &videoEncInfo); 317 static int32_t CheckAudioCapInfo(AudioCaptureInfo &audioCapInfo); 318 static int32_t CheckVideoCapInfo(VideoCaptureInfo &videoCapInfo); 319 static int32_t CheckAudioEncInfo(AudioEncInfo &audioEncInfo); 320 static int32_t CheckVideoEncInfo(VideoEncInfo &videoEncInfo); 321 static int32_t CheckCaptureMode(CaptureMode captureMode); 322 static int32_t CheckDataType(DataType dataType); 323 324 private: 325 static constexpr int32_t ROOT_UID = 0; 326 static constexpr int32_t AUDIO_BITRATE_MIN = 8000; 327 static constexpr int32_t AUDIO_BITRATE_MAX = 384000; 328 static constexpr int32_t VIDEO_BITRATE_MIN = 1; 329 static constexpr int32_t VIDEO_BITRATE_MAX = 30000000; 330 static constexpr int32_t VIDEO_FRAME_RATE_MIN = 1; 331 static constexpr int32_t VIDEO_FRAME_RATE_MAX = 60; 332 static constexpr int32_t VIDEO_FRAME_WIDTH_MAX = 10240; 333 static constexpr int32_t VIDEO_FRAME_HEIGHT_MAX = 4320; 334 static constexpr int32_t SESSION_ID_INVALID = -1; 335 static constexpr int32_t AV_SCREEN_CAPTURE_SESSION_UID = 1013; 336 static constexpr const char* NOTIFICATION_SCREEN_RECORDING_TITLE_ID = "notification_screen_recording_title"; 337 static constexpr const char* QUOTATION_MARKS_STRING = "\""; 338 }; 339 } // namespace Media 340 } // namespace OHOS 341 #endif // SCREEN_CAPTURE_SERVICE_SERVER_H 342