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