1 /* 2 * Copyright (c) 2018 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 MODULES_DESKTOP_CAPTURE_MAC_SCREEN_CAPTURER_MAC_H_ 12 #define MODULES_DESKTOP_CAPTURE_MAC_SCREEN_CAPTURER_MAC_H_ 13 14 #include <CoreGraphics/CoreGraphics.h> 15 16 #include <memory> 17 #include <vector> 18 19 #include "api/sequence_checker.h" 20 #include "modules/desktop_capture/desktop_capture_options.h" 21 #include "modules/desktop_capture/desktop_capturer.h" 22 #include "modules/desktop_capture/desktop_frame.h" 23 #include "modules/desktop_capture/desktop_geometry.h" 24 #include "modules/desktop_capture/desktop_region.h" 25 #include "modules/desktop_capture/mac/desktop_configuration.h" 26 #include "modules/desktop_capture/mac/desktop_configuration_monitor.h" 27 #include "modules/desktop_capture/mac/desktop_frame_provider.h" 28 #include "modules/desktop_capture/screen_capture_frame_queue.h" 29 #include "modules/desktop_capture/screen_capturer_helper.h" 30 #include "modules/desktop_capture/shared_desktop_frame.h" 31 32 namespace webrtc { 33 34 class DisplayStreamManager; 35 36 // A class to perform video frame capturing for mac. 37 class ScreenCapturerMac final : public DesktopCapturer { 38 public: 39 ScreenCapturerMac( 40 rtc::scoped_refptr<DesktopConfigurationMonitor> desktop_config_monitor, 41 bool detect_updated_region, 42 bool allow_iosurface); 43 ~ScreenCapturerMac() override; 44 45 ScreenCapturerMac(const ScreenCapturerMac&) = delete; 46 ScreenCapturerMac& operator=(const ScreenCapturerMac&) = delete; 47 48 // TODO(julien.isorce): Remove Init() or make it private. 49 bool Init(); 50 51 // DesktopCapturer interface. 52 void Start(Callback* callback) override; 53 void CaptureFrame() override; 54 void SetExcludedWindow(WindowId window) override; 55 bool GetSourceList(SourceList* screens) override; 56 bool SelectSource(SourceId id) override; 57 58 private: 59 // Returns false if the selected screen is no longer valid. 60 bool CgBlit(const DesktopFrame& frame, const DesktopRegion& region); 61 62 // Called when the screen configuration is changed. 63 void ScreenConfigurationChanged(); 64 65 bool RegisterRefreshAndMoveHandlers(); 66 void UnregisterRefreshAndMoveHandlers(); 67 68 void ScreenRefresh(CGDirectDisplayID display_id, 69 CGRectCount count, 70 const CGRect* rect_array, 71 DesktopVector display_origin, 72 IOSurfaceRef io_surface); 73 void ReleaseBuffers(); 74 75 std::unique_ptr<DesktopFrame> CreateFrame(); 76 77 const bool detect_updated_region_; 78 79 Callback* callback_ = nullptr; 80 81 // Queue of the frames buffers. 82 ScreenCaptureFrameQueue<SharedDesktopFrame> queue_; 83 84 // Current display configuration. 85 MacDesktopConfiguration desktop_config_; 86 87 // Currently selected display, or 0 if the full desktop is selected. On OS X 88 // 10.6 and before, this is always 0. 89 CGDirectDisplayID current_display_ = 0; 90 91 // The physical pixel bounds of the current screen. 92 DesktopRect screen_pixel_bounds_; 93 94 // The dip to physical pixel scale of the current screen. 95 float dip_to_pixel_scale_ = 1.0f; 96 97 // A thread-safe list of invalid rectangles, and the size of the most 98 // recently captured screen. 99 ScreenCapturerHelper helper_; 100 101 // Contains an invalid region from the previous capture. 102 DesktopRegion last_invalid_region_; 103 104 // Monitoring display reconfiguration. 105 rtc::scoped_refptr<DesktopConfigurationMonitor> desktop_config_monitor_; 106 107 CGWindowID excluded_window_ = 0; 108 109 // List of streams, one per screen. 110 std::vector<CGDisplayStreamRef> display_streams_; 111 112 // Container holding latest state of the snapshot per displays. 113 DesktopFrameProvider desktop_frame_provider_; 114 115 // Start, CaptureFrame and destructor have to called in the same thread. 116 SequenceChecker thread_checker_; 117 }; 118 119 } // namespace webrtc 120 121 #endif // MODULES_DESKTOP_CAPTURE_MAC_SCREEN_CAPTURER_MAC_H_ 122