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 <mutex> 20 #include <cstdlib> 21 #include <thread> 22 #include <string> 23 #include <memory> 24 #include <atomic> 25 #include <queue> 26 #include <vector> 27 #include <chrono> 28 29 #include "audio_capturer_wrapper.h" 30 #include "i_screen_capture_service.h" 31 #include "nocopyable.h" 32 #include "uri_helper.h" 33 #include "task_queue.h" 34 #include "accesstoken_kit.h" 35 #include "privacy_kit.h" 36 #include "ipc_skeleton.h" 37 #include "screen_capture.h" 38 #include "audio_capturer.h" 39 #include "nativetoken_kit.h" 40 #include "token_setproc.h" 41 #include "audio_info.h" 42 #include "surface.h" 43 #include "display_manager.h" 44 #include "screen_manager.h" 45 #include "i_recorder_service.h" 46 #include "recorder_server.h" 47 #include "notification_content.h" 48 #include "notification_helper.h" 49 #include "notification_request.h" 50 #include "notification_constant.h" 51 #include "notification_slot.h" 52 #include "incall_observer.h" 53 #include "account_observer.h" 54 #include "media_data_source.h" 55 #include "meta/meta.h" 56 #include "audio_stream_manager.h" 57 #include "screen_capture_monitor_server.h" 58 #include "json/json.h" 59 #include "tokenid_kit.h" 60 61 namespace OHOS { 62 namespace Media { 63 using namespace Rosen; 64 using namespace AudioStandard; 65 using namespace OHOS::Notification; 66 using OHOS::Security::AccessToken::PrivacyKit; 67 68 class ScreenCaptureServer; 69 70 class NotificationSubscriber : public OHOS::Notification::NotificationLocalLiveViewSubscriber { 71 public: 72 void OnConnected() override; 73 void OnDisconnected() override; 74 void OnResponse(int32_t notificationId, 75 OHOS::sptr<OHOS::Notification::NotificationButtonOption> buttonOption) override; 76 void OnDied() override; 77 }; 78 79 enum VideoPermissionState : int32_t { 80 START_VIDEO = 0, 81 STOP_VIDEO = 1 82 }; 83 84 enum AVScreenCaptureState : int32_t { 85 CREATED = 0, 86 STARTING = 1, 87 STARTED = 2, 88 STOPPED = 3 89 }; 90 91 enum AVScreenCaptureMixMode : int32_t { 92 MIX_MODE = 0, 93 MIC_MODE = 1, 94 INNER_MODE = 2, 95 INVAILD_MODE = 3 96 }; 97 98 enum AVScreenCaptureAvType : int8_t { 99 INVALID_TYPE = -1, 100 AUDIO_TYPE = 0, 101 VIDEO_TYPE = 1, 102 AV_TYPE = 2 103 }; 104 105 enum AVScreenCaptureDataMode : int8_t { 106 BUFFER_MODE = 0, 107 SUFFACE_MODE = 1, 108 FILE_MODE = 2 109 }; 110 111 enum StopReason: int8_t { 112 NORMAL_STOPPED = 0, 113 RECEIVE_USER_PRIVACY_AUTHORITY_FAILED = 1, 114 POST_START_SCREENCAPTURE_HANDLE_FAILURE = 2, 115 REQUEST_USER_PRIVACY_AUTHORITY_FAILED = 3, 116 STOP_REASON_INVALID = 4 117 }; 118 119 struct SurfaceBufferEntry { SurfaceBufferEntrySurfaceBufferEntry120 SurfaceBufferEntry(sptr<OHOS::SurfaceBuffer> buf, int32_t fence, int64_t timeStamp, OHOS::Rect& damage) 121 : buffer(std::move(buf)), flushFence(fence), timeStamp(timeStamp), damageRect(damage) {} 122 ~SurfaceBufferEntry() noexcept = default; 123 124 sptr<OHOS::SurfaceBuffer> buffer; 125 int32_t flushFence; 126 int64_t timeStamp = 0; 127 OHOS::Rect damageRect = {0, 0, 0, 0}; 128 }; 129 130 struct StatisticalEventInfo { 131 int32_t errCode = 0; 132 std::string errMsg; 133 int32_t captureDuration = -1; 134 bool userAgree = false; 135 bool requireMic = false; 136 bool enableMic = false; 137 std::string videoResolution; 138 StopReason stopReason = StopReason::STOP_REASON_INVALID; 139 int32_t startLatency = -1; 140 }; 141 142 class ScreenCapBufferConsumerListener : public IBufferConsumerListener { 143 public: ScreenCapBufferConsumerListener(sptr<Surface> consumer,const std::shared_ptr<ScreenCaptureCallBack> & screenCaptureCb)144 ScreenCapBufferConsumerListener( 145 sptr<Surface> consumer, const std::shared_ptr<ScreenCaptureCallBack> &screenCaptureCb) 146 : consumer_(consumer), screenCaptureCb_(screenCaptureCb) {} 147 ~ScreenCapBufferConsumerListener(); 148 149 void OnBufferAvailable() override; 150 int32_t AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence, int64_t ×tamp, 151 OHOS::Rect &damage); 152 int32_t ReleaseVideoBuffer(); 153 int32_t Release(); 154 155 private: 156 int32_t ReleaseBuffer(); 157 void ProcessVideoBufferCallBack(); 158 159 private: 160 std::mutex mutex_; 161 sptr<OHOS::Surface> consumer_ = nullptr; 162 std::shared_ptr<ScreenCaptureCallBack> screenCaptureCb_ = nullptr; 163 164 std::mutex bufferMutex_; 165 std::condition_variable bufferCond_; 166 std::queue<std::unique_ptr<SurfaceBufferEntry>> availBuffers_; 167 168 static constexpr uint32_t MAX_BUFFER_SIZE = 3; 169 static constexpr uint32_t OPERATION_TIMEOUT_IN_MS = 1000; // 1000ms 170 }; 171 172 class ScreenCaptureObserverCallBack : public InCallObserverCallBack, public AccountObserverCallBack { 173 public: 174 explicit ScreenCaptureObserverCallBack(std::weak_ptr<ScreenCaptureServer> screenCaptureServer); 175 ~ScreenCaptureObserverCallBack() = default; 176 bool StopAndRelease(AVScreenCaptureStateCode state) override; 177 178 private: 179 std::weak_ptr<ScreenCaptureServer> screenCaptureServer_; 180 }; 181 182 class AudioDataSource : public IAudioDataSource { 183 public: AudioDataSource(AVScreenCaptureMixMode type,ScreenCaptureServer * screenCaptureServer)184 AudioDataSource(AVScreenCaptureMixMode type, ScreenCaptureServer* screenCaptureServer) : type_(type), 185 screenCaptureServer_(screenCaptureServer) {} 186 187 int32_t ReadAt(std::shared_ptr<AVBuffer> buffer, uint32_t length) override; 188 int32_t GetSize(int64_t &size) override; 189 int32_t RegisterAudioRendererEventListener(const int32_t clientPid, 190 const std::shared_ptr<AudioRendererStateChangeCallback> &callback); 191 int32_t UnregisterAudioRendererEventListener(const int32_t clientPid); 192 void SpeakerStateUpdate( 193 const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 194 bool HasSpeakerStream( 195 const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 196 void VoIPStateUpdate( 197 const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 198 void SetAppPid(int32_t appid); 199 void SetAppName(std::string appName); 200 int32_t GetAppPid(); 201 bool GetIsInVoIPCall(); 202 bool GetSpeakerAliveStatus(); 203 204 private: 205 int32_t MixModeBufferWrite(std::shared_ptr<AudioBuffer> &innerAudioBuffer, 206 std::shared_ptr<AudioBuffer> &micAudioBuffer, std::shared_ptr<AVMemory> &bufferMem); 207 int32_t appPid_ { 0 }; 208 std::string appName_; 209 bool speakerAliveStatus_ = true; 210 std::atomic<bool> isInVoIPCall_ = false; 211 std::mutex voipStatusChangeMutex_; 212 213 void MixAudio(char** srcData, char* mixData, int channels, int bufferSize); 214 215 AVScreenCaptureMixMode type_; 216 ScreenCaptureServer* screenCaptureServer_; 217 218 static constexpr int32_t ADS_LOG_SKIP_NUM = 1000; 219 }; 220 221 class PrivateWindowListenerInScreenCapture : public DisplayManager::IPrivateWindowListener { 222 public: 223 explicit PrivateWindowListenerInScreenCapture(std::weak_ptr<ScreenCaptureServer> screenCaptureServer); 224 ~PrivateWindowListenerInScreenCapture() = default; 225 void OnPrivateWindow(bool hasPrivate) override; 226 227 private: 228 std::weak_ptr<ScreenCaptureServer> screenCaptureServer_; 229 }; 230 231 class ScreenRendererAudioStateChangeCallback : public AudioRendererStateChangeCallback { 232 public: 233 void OnRendererStateChange(const std::vector<std::unique_ptr<AudioRendererChangeInfo>> &audioRendererChangeInfos); 234 void SetAudioSource(std::shared_ptr<AudioDataSource> audioSource); 235 void SetAppName(std::string appName); 236 private: 237 std::shared_ptr<AudioDataSource> audioSource_ = nullptr; 238 std::string appName_; 239 }; 240 241 class ScreenCaptureServer : public std::enable_shared_from_this<ScreenCaptureServer>, 242 public IScreenCaptureService, public NoCopyable { 243 public: 244 static std::shared_ptr<IScreenCaptureService> Create(); 245 static int32_t ReportAVScreenCaptureUserChoice(int32_t sessionId, const std::string &content); 246 static int32_t GetRunningScreenCaptureInstancePid(int32_t &pid); 247 static int32_t GetSpecificServer(int32_t sessionId, std::shared_ptr<ScreenCaptureServer> &server); 248 static void GetChoiceFromJson(Json::Value &root, const std::string &content, std::string key, std::string &value); 249 static void PrepareSelectWindow(Json::Value &root, std::shared_ptr<ScreenCaptureServer> &server); 250 ScreenCaptureServer(); 251 ~ScreenCaptureServer(); 252 253 int32_t SetCaptureMode(CaptureMode captureMode) override; 254 int32_t SetDataType(DataType dataType) override; 255 int32_t SetRecorderInfo(RecorderInfo recorderInfo) override; 256 int32_t SetOutputFile(int32_t outputFd) override; 257 int32_t InitAudioEncInfo(AudioEncInfo audioEncInfo) override; 258 int32_t InitAudioCap(AudioCaptureInfo audioInfo) override; 259 int32_t InitVideoEncInfo(VideoEncInfo videoEncInfo) override; 260 int32_t InitVideoCap(VideoCaptureInfo videoInfo) override; 261 int32_t StartScreenCapture(bool isPrivacyAuthorityEnabled) override; 262 int32_t StartScreenCaptureWithSurface(sptr<Surface> surface, bool isPrivacyAuthorityEnabled) override; 263 int32_t StopScreenCapture() override; 264 int32_t SetScreenCaptureCallback(const std::shared_ptr<ScreenCaptureCallBack> &callback) override; 265 int32_t AcquireAudioBuffer(std::shared_ptr<AudioBuffer> &audioBuffer, AudioCaptureSourceType type) override; 266 int32_t AcquireVideoBuffer(sptr<OHOS::SurfaceBuffer> &surfaceBuffer, int32_t &fence, 267 int64_t ×tamp, OHOS::Rect &damage) override; 268 int32_t ReleaseAudioBuffer(AudioCaptureSourceType type) override; 269 int32_t ReleaseVideoBuffer() override; 270 int32_t SetMicrophoneEnabled(bool isMicrophone) override; 271 bool GetMicWorkingState(); 272 int32_t SetCanvasRotation(bool canvasRotation) override; 273 int32_t ResizeCanvas(int32_t width, int32_t height) override; 274 int32_t SkipPrivacyMode(std::vector<uint64_t> &windowIDsVec) override; 275 int32_t SetMaxVideoFrameRate(int32_t frameRate) override; 276 void Release() override; 277 int32_t ExcludeContent(ScreenCaptureContentFilter &contentFilter) override; 278 279 void SetSessionId(int32_t sessionId); 280 int32_t OnReceiveUserPrivacyAuthority(bool isAllowed); 281 int32_t StopScreenCaptureByEvent(AVScreenCaptureStateCode stateCode); 282 void UpdateMicrophoneEnabled(); 283 284 int32_t AcquireAudioBufferMix(std::shared_ptr<AudioBuffer> &innerAudioBuffer, 285 std::shared_ptr<AudioBuffer> &micAudioBuffer, AVScreenCaptureMixMode type); 286 int32_t ReleaseAudioBufferMix(AVScreenCaptureMixMode type); 287 int32_t ReleaseMicAudioBuffer(); 288 int32_t ReleaseInnerAudioBuffer(); 289 int32_t GetInnerAudioCaptureBufferSize(size_t &size); 290 int32_t GetMicAudioCaptureBufferSize(size_t &size); 291 int32_t OnVoIPStatusChanged(bool isInVoIPCall); 292 int32_t OnSpeakerAliveStatusChanged(bool speakerAliveStatus); 293 void OnDMPrivateWindowChange(bool hasPrivate); 294 void SetMissionId(uint64_t missionId); 295 void SetDisplayId(uint64_t displayId); 296 bool IsTelInCallSkipList(); 297 298 private: 299 int32_t StartScreenCaptureInner(bool isPrivacyAuthorityEnabled); 300 int32_t RegisterServerCallbacks(); 301 int32_t OnStartScreenCapture(); 302 void PostStartScreenCapture(bool isSuccess); 303 void PostStartScreenCaptureSuccessAction(); 304 int32_t InitRecorderInfo(std::shared_ptr<IRecorderService> &recorder, AudioCaptureInfo audioInfo); 305 int32_t InitRecorder(); 306 int32_t StartScreenCaptureFile(); 307 int32_t StartScreenCaptureStream(); 308 int32_t StartAudioCapture(); 309 int32_t StartStreamInnerAudioCapture(); 310 int32_t StartStreamMicAudioCapture(); 311 int32_t StartFileInnerAudioCapture(); 312 int32_t StartFileMicAudioCapture(); 313 int32_t StopMicAudioCapture(); 314 int32_t StartVideoCapture(); 315 int32_t StartHomeVideoCapture(); 316 int32_t StopScreenCaptureInner(AVScreenCaptureStateCode stateCode); 317 void PostStopScreenCapture(AVScreenCaptureStateCode stateCode); 318 int32_t StopAudioCapture(); 319 int32_t StopVideoCapture(); 320 int32_t StopScreenCaptureRecorder(); 321 int32_t CheckAllParams(); 322 int32_t CheckCaptureStreamParams(); 323 int32_t CheckCaptureFileParams(); 324 int32_t SetCanvasRotationInner(); 325 int32_t SkipPrivacyModeInner(); 326 int32_t SetScreenScaleMode(); 327 void InitAppInfo(); 328 void CloseFd(); 329 void ReleaseInner(); 330 void GetDumpFlag(); 331 int32_t SetMicrophoneOn(); 332 int32_t SetMicrophoneOff(); 333 334 VirtualScreenOption InitVirtualScreenOption(const std::string &name, sptr<OHOS::Surface> consumer); 335 int32_t GetMissionIds(std::vector<uint64_t> &missionIds); 336 int32_t MakeVirtualScreenMirror(); 337 int32_t CreateVirtualScreen(const std::string &name, sptr<OHOS::Surface> consumer); 338 int32_t PrepareVirtualScreenMirror(); 339 void DestroyVirtualScreen(); 340 341 bool CheckScreenCapturePermission(); 342 bool IsUserPrivacyAuthorityNeeded(); 343 bool UpdatePrivacyUsingPermissionState(VideoPermissionState state); 344 int32_t RequestUserPrivacyAuthority(); 345 int32_t StartPrivacyWindow(); 346 #ifdef SUPPORT_SCREEN_CAPTURE_WINDOW_NOTIFICATION 347 int32_t TryStartNotification(); 348 #endif 349 int32_t StartNotification(); 350 std::shared_ptr<NotificationLocalLiveViewContent> GetLocalLiveViewContent(); 351 void UpdateLiveViewContent(); 352 std::shared_ptr<PixelMap> GetPixelMap(std::string path); 353 std::shared_ptr<PixelMap> GetPixelMapSvg(std::string path, int32_t width, int32_t height); 354 void ResSchedReportData(int64_t value, std::unordered_map<std::string, std::string> payload); 355 int64_t GetCurrentMillisecond(); 356 void SetMetaDataReport(); 357 void SetErrorInfo(int32_t errCode, const std::string &errMsg, StopReason stopReason, bool userAgree); 358 void SystemRecorderInterruptLatestRecorder(); 359 int32_t ReStartMicForVoIPStatusSwitch(); 360 void RegisterPrivateWindowListener(); 361 362 private: 363 std::mutex mutex_; 364 std::mutex cbMutex_; 365 std::shared_ptr<ScreenCaptureObserverCallBack> screenCaptureObserverCb_ = nullptr; 366 std::shared_ptr<ScreenCaptureCallBack> screenCaptureCb_ = nullptr; 367 bool canvasRotation_ = false; 368 bool isMicrophoneOn_ = true; 369 bool isPrivacyAuthorityEnabled_ = false; 370 371 int32_t sessionId_ = 0; 372 int32_t notificationId_ = 0; 373 std::string callingLabel_; 374 std::string liveViewText_; 375 std::atomic<int32_t> micCount_{0}; 376 int32_t density_ = 0; 377 int32_t capsuleVpSize_ = 18; 378 int32_t capsulePxSize_ = 0; 379 380 /* used for both CAPTURE STREAM and CAPTURE FILE */ 381 OHOS::AudioStandard::AppInfo appInfo_; 382 std::string appName_ = ""; 383 AVScreenCaptureConfig captureConfig_; 384 AVScreenCaptureAvType avType_ = AVScreenCaptureAvType::INVALID_TYPE; 385 AVScreenCaptureDataMode dataMode_ = AVScreenCaptureDataMode::BUFFER_MODE; 386 StatisticalEventInfo statisticalEventInfo_; 387 sptr<OHOS::Surface> consumer_ = nullptr; 388 bool isConsumerStart_ = false; 389 bool isDump_ = false; 390 ScreenId screenId_ = SCREEN_ID_INVALID; 391 std::vector<uint64_t> missionIds_; 392 ScreenCaptureContentFilter contentFilter_; 393 AVScreenCaptureState captureState_ = AVScreenCaptureState::CREATED; 394 std::shared_ptr<NotificationLocalLiveViewContent> localLiveViewContent_; 395 int64_t startTime_ = 0; 396 397 /* used for CAPTURE STREAM */ 398 sptr<IBufferConsumerListener> surfaceCb_ = nullptr; 399 sptr<OHOS::Surface> surface_ = nullptr; 400 bool isSurfaceMode_ = false; 401 std::shared_ptr<AudioCapturerWrapper> innerAudioCapture_; 402 std::shared_ptr<AudioCapturerWrapper> micAudioCapture_; 403 404 /* used for CAPTURE FILE */ 405 std::shared_ptr<IRecorderService> recorder_ = nullptr; 406 std::string url_; 407 OutputFormatType fileFormat_ = OutputFormatType::FORMAT_DEFAULT; 408 int32_t outputFd_ = -1; 409 int32_t audioSourceId_ = 0; 410 int32_t videoSourceId_ = 0; 411 std::shared_ptr<AudioDataSource> audioSource_ = nullptr; 412 /* used for DFX events */ 413 uint64_t instanceId_ = 0; 414 std::shared_ptr<ScreenRendererAudioStateChangeCallback> captureCallback_; 415 std::vector<uint64_t> skipPrivacyWindowIDsVec_; 416 sptr<DisplayManager::IPrivateWindowListener> displayListener_; 417 bool isCalledBySystemApp_ = false; 418 private: 419 static int32_t CheckAudioCapParam(const AudioCaptureInfo &audioCapInfo); 420 static int32_t CheckVideoCapParam(const VideoCaptureInfo &videoCapInfo); 421 static int32_t CheckAudioEncParam(const AudioEncInfo &audioEncInfo); 422 static int32_t CheckVideoEncParam(const VideoEncInfo &videoEncInfo); 423 static int32_t CheckAudioCapInfo(AudioCaptureInfo &audioCapInfo); 424 static int32_t CheckVideoCapInfo(VideoCaptureInfo &videoCapInfo); 425 static int32_t CheckAudioEncInfo(AudioEncInfo &audioEncInfo); 426 static int32_t CheckVideoEncInfo(VideoEncInfo &videoEncInfo); 427 static int32_t CheckCaptureMode(CaptureMode captureMode); 428 static int32_t CheckDataType(DataType dataType); 429 430 private: 431 static constexpr int32_t ROOT_UID = 0; 432 static constexpr int32_t AUDIO_BITRATE_MIN = 8000; 433 static constexpr int32_t AUDIO_BITRATE_MAX = 384000; 434 static constexpr int32_t VIDEO_BITRATE_MIN = 1; 435 static constexpr int32_t VIDEO_BITRATE_MAX = 30000000; 436 static constexpr int32_t VIDEO_FRAME_RATE_MIN = 1; 437 static constexpr int32_t VIDEO_FRAME_RATE_MAX = 60; 438 static constexpr int32_t VIDEO_FRAME_WIDTH_MAX = 10240; 439 static constexpr int32_t VIDEO_FRAME_HEIGHT_MAX = 4320; 440 static constexpr int32_t SESSION_ID_INVALID = -1; 441 static constexpr int32_t AV_SCREEN_CAPTURE_SESSION_UID = 1013; 442 }; 443 } // namespace Media 444 } // namespace OHOS 445 #endif // SCREEN_CAPTURE_SERVICE_SERVER_H 446