desktop_capture_impl.h (5573B)
1 /* 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_MAIN_SOURCE_DESKTOP_CAPTURE_IMPL_H_ 12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_MAIN_SOURCE_DESKTOP_CAPTURE_IMPL_H_ 13 14 /* 15 * video_capture_impl.h 16 */ 17 18 #include <memory> 19 #include <set> 20 #include <string> 21 22 #include "MediaEventSource.h" 23 #include "PerformanceRecorder.h" 24 #include "api/sequence_checker.h" 25 #include "api/video/video_frame.h" 26 #include "api/video/video_sink_interface.h" 27 #include "common_video/include/video_frame_buffer_pool.h" 28 #include "modules/desktop_capture/desktop_capturer.h" 29 #include "modules/video_capture/video_capture.h" 30 #include "mozilla/DataMutex.h" 31 #include "mozilla/Maybe.h" 32 #include "mozilla/TimeStamp.h" 33 #include "nsCOMPtr.h" 34 35 class nsIThread; 36 class nsITimer; 37 38 namespace mozilla::camera { 39 enum class CaptureDeviceType; 40 } 41 42 namespace webrtc { 43 44 class VideoCaptureEncodeInterface; 45 46 // Reuses the video engine pipeline for screen sharing. 47 // As with video, DesktopCaptureImpl is a proxy for screen sharing 48 // and follows the video pipeline design 49 class DesktopCaptureImpl : public DesktopCapturer::Callback, 50 public VideoCaptureModule { 51 public: 52 /* Create a screen capture modules object 53 */ 54 static DesktopCaptureImpl* Create( 55 const int32_t aModuleId, const char* aUniqueId, 56 const mozilla::camera::CaptureDeviceType aType); 57 58 [[nodiscard]] static std::shared_ptr<VideoCaptureModule::DeviceInfo> 59 CreateDeviceInfo(const int32_t aId, 60 const mozilla::camera::CaptureDeviceType aType); 61 62 // mControlThread only. 63 void RegisterCaptureDataCallback( 64 webrtc::VideoSinkInterface<VideoFrame>* aCallback) override; 65 void RegisterCaptureDataCallback( 66 RawVideoSinkInterface* dataCallback) override {} 67 void DeRegisterCaptureDataCallback( 68 webrtc::VideoSinkInterface<VideoFrame>* aCallback) override; 69 int32_t StopCaptureIfAllClientsClose() override; 70 71 int32_t SetCaptureRotation(VideoRotation aRotation) override; 72 bool SetApplyRotation(bool aEnable) override; 73 bool GetApplyRotation() override { return true; } 74 75 const char* CurrentDeviceName() const override; 76 77 int32_t StartCapture(const VideoCaptureCapability& aCapability) override; 78 virtual bool FocusOnSelectedSource() override; 79 int32_t StopCapture() override; 80 bool CaptureStarted() override; 81 int32_t CaptureSettings(VideoCaptureCapability& aSettings) override; 82 83 void CaptureFrameOnThread(); 84 mozilla::MediaEventSource<void>* CaptureEndedEvent(); 85 86 const int32_t mModuleId; 87 const mozilla::TrackingId mTrackingId; 88 const std::string mDeviceUniqueId; 89 const mozilla::camera::CaptureDeviceType mDeviceType; 90 91 protected: 92 DesktopCaptureImpl(const int32_t aId, const char* aUniqueId, 93 const mozilla::camera::CaptureDeviceType aType); 94 virtual ~DesktopCaptureImpl(); 95 96 private: 97 // Maximum CPU usage in %. 98 static constexpr uint32_t kMaxDesktopCaptureCpuUsage = 50; 99 void InitOnThread(std::unique_ptr<DesktopCapturer> aCapturer, int aFramerate); 100 void UpdateOnThread(int aFramerate); 101 void ShutdownOnThread(); 102 // DesktopCapturer::Callback interface. 103 void OnCaptureResult(DesktopCapturer::Result aResult, 104 std::unique_ptr<DesktopFrame> aFrame) override; 105 106 // Notifies all mCallbacks of OnFrame(). mCaptureThread only. 107 void NotifyOnFrame(const VideoFrame& aFrame); 108 109 // Control thread on which the public API is called. 110 const nsCOMPtr<nsISerialEventTarget> mControlThread; 111 // Set in StartCapture. 112 mozilla::Maybe<VideoCaptureCapability> mRequestedCapability 113 RTC_GUARDED_BY(mControlThreadChecker); 114 // The DesktopCapturer is created on mControlThread but assigned and accessed 115 // only on mCaptureThread. 116 std::unique_ptr<DesktopCapturer> mCapturer 117 RTC_GUARDED_BY(mCaptureThreadChecker); 118 // Dedicated thread that does the capturing. 119 nsCOMPtr<nsIThread> mCaptureThread RTC_GUARDED_BY(mControlThreadChecker); 120 // Checks that API methods are called on mControlThread. 121 webrtc::SequenceChecker mControlThreadChecker; 122 // Checks that frame delivery only happens on mCaptureThread. 123 webrtc::SequenceChecker mCaptureThreadChecker; 124 // Timer that triggers frame captures. Only used on mCaptureThread. 125 // TODO(Bug 1806646): Drive capture with vsync instead. 126 nsCOMPtr<nsITimer> mCaptureTimer RTC_GUARDED_BY(mCaptureThreadChecker); 127 // Interval between captured frames, based on the framerate in 128 // mRequestedCapability. mCaptureThread only. 129 mozilla::Maybe<mozilla::TimeDuration> mRequestedCaptureInterval 130 RTC_GUARDED_BY(mCaptureThreadChecker); 131 // Used to make sure incoming timestamp is increasing for every frame. 132 webrtc::Timestamp mNextFrameMinimumTime RTC_GUARDED_BY(mCaptureThreadChecker); 133 // Callbacks for captured frames. Mutated on mControlThread, callbacks happen 134 // on mCaptureThread. 135 mozilla::DataMutex<std::set<webrtc::VideoSinkInterface<VideoFrame>*>> 136 mCallbacks; 137 // Subscribers to this event will be notified when the capture has ended. 138 mozilla::MediaEventProducer<void> mCaptureEndedEvent; 139 140 webrtc::VideoFrameBufferPool mBufferPool; 141 }; 142 143 } // namespace webrtc 144 145 #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_MAIN_SOURCE_DESKTOP_CAPTURE_IMPL_H_