1 /* 2 * Copyright 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_LINUX_SCREEN_CAPTURER_X11_H_ 12 #define MODULES_DESKTOP_CAPTURE_LINUX_SCREEN_CAPTURER_X11_H_ 13 14 #include <X11/X.h> 15 #include <X11/Xlib.h> 16 #include <X11/extensions/Xdamage.h> 17 #include <X11/extensions/Xfixes.h> 18 #include <X11/extensions/Xrandr.h> 19 20 #include <memory> 21 22 #include "modules/desktop_capture/desktop_capture_options.h" 23 #include "modules/desktop_capture/desktop_capturer.h" 24 #include "modules/desktop_capture/desktop_frame.h" 25 #include "modules/desktop_capture/desktop_region.h" 26 #include "modules/desktop_capture/linux/shared_x_display.h" 27 #include "modules/desktop_capture/linux/x_atom_cache.h" 28 #include "modules/desktop_capture/linux/x_server_pixel_buffer.h" 29 #include "modules/desktop_capture/screen_capture_frame_queue.h" 30 #include "modules/desktop_capture/screen_capturer_helper.h" 31 #include "modules/desktop_capture/shared_desktop_frame.h" 32 #include "rtc_base/constructor_magic.h" 33 34 namespace webrtc { 35 36 // A class to perform video frame capturing for Linux on X11. 37 // 38 // If XDamage is used, this class sets DesktopFrame::updated_region() according 39 // to the areas reported by XDamage. Otherwise this class does not detect 40 // DesktopFrame::updated_region(), the field is always set to the entire frame 41 // rectangle. ScreenCapturerDifferWrapper should be used if that functionality 42 // is necessary. 43 class ScreenCapturerX11 : public DesktopCapturer, 44 public SharedXDisplay::XEventHandler { 45 public: 46 ScreenCapturerX11(); 47 ~ScreenCapturerX11() override; 48 49 static std::unique_ptr<DesktopCapturer> CreateRawScreenCapturer( 50 const DesktopCaptureOptions& options); 51 52 // TODO(ajwong): Do we really want this to be synchronous? 53 bool Init(const DesktopCaptureOptions& options); 54 55 // DesktopCapturer interface. 56 void Start(Callback* delegate) override; 57 void CaptureFrame() override; 58 bool GetSourceList(SourceList* sources) override; 59 bool SelectSource(SourceId id) override; 60 61 private: display()62 Display* display() { return options_.x_display()->display(); } 63 64 // SharedXDisplay::XEventHandler interface. 65 bool HandleXEvent(const XEvent& event) override; 66 67 void InitXDamage(); 68 void InitXrandr(); 69 void UpdateMonitors(); 70 71 // Capture screen pixels to the current buffer in the queue. In the DAMAGE 72 // case, the ScreenCapturerHelper already holds the list of invalid rectangles 73 // from HandleXEvent(). In the non-DAMAGE case, this captures the 74 // whole screen, then calculates some invalid rectangles that include any 75 // differences between this and the previous capture. 76 std::unique_ptr<DesktopFrame> CaptureScreen(); 77 78 // Called when the screen configuration is changed. 79 void ScreenConfigurationChanged(); 80 81 // Synchronize the current buffer with |last_buffer_|, by copying pixels from 82 // the area of |last_invalid_rects|. 83 // Note this only works on the assumption that kNumBuffers == 2, as 84 // |last_invalid_rects| holds the differences from the previous buffer and 85 // the one prior to that (which will then be the current buffer). 86 void SynchronizeFrame(); 87 88 void DeinitXlib(); 89 90 DesktopCaptureOptions options_; 91 92 Callback* callback_ = nullptr; 93 94 // X11 graphics context. 95 GC gc_ = nullptr; 96 Window root_window_ = BadValue; 97 98 // XRandR 1.5 monitors. 99 bool use_randr_ = false; 100 int randr_event_base_ = 0; 101 XRRMonitorInfo* monitors_ = nullptr; 102 int num_monitors_ = 0; 103 DesktopRect selected_monitor_rect_; 104 // selected_monitor_name_ will be changed to kFullDesktopScreenId 105 // by a call to SelectSource() at the end of Init() because 106 // selected_monitor_rect_ should be updated as well. 107 // Setting it to kFullDesktopScreenId here might be misleading. 108 Atom selected_monitor_name_ = 0; 109 typedef XRRMonitorInfo* (*get_monitors_func)(Display*, Window, Bool, int*); 110 typedef void (*free_monitors_func)(XRRMonitorInfo*); 111 get_monitors_func get_monitors_ = nullptr; 112 free_monitors_func free_monitors_ = nullptr; 113 114 // XFixes. 115 bool has_xfixes_ = false; 116 int xfixes_event_base_ = -1; 117 int xfixes_error_base_ = -1; 118 119 // XDamage information. 120 bool use_damage_ = false; 121 Damage damage_handle_ = 0; 122 int damage_event_base_ = -1; 123 int damage_error_base_ = -1; 124 XserverRegion damage_region_ = 0; 125 126 // Access to the X Server's pixel buffer. 127 XServerPixelBuffer x_server_pixel_buffer_; 128 129 // A thread-safe list of invalid rectangles, and the size of the most 130 // recently captured screen. 131 ScreenCapturerHelper helper_; 132 133 // Queue of the frames buffers. 134 ScreenCaptureFrameQueue<SharedDesktopFrame> queue_; 135 136 // Invalid region from the previous capture. This is used to synchronize the 137 // current with the last buffer used. 138 DesktopRegion last_invalid_region_; 139 140 std::unique_ptr<XAtomCache> atom_cache_; 141 142 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerX11); 143 }; 144 145 } // namespace webrtc 146 147 #endif // MODULES_DESKTOP_CAPTURE_LINUX_SCREEN_CAPTURER_X11_H_ 148