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