jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_ |
| 12 | #define MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_ |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 13 | |
| 14 | #include <magnification.h> |
| 15 | #include <wincodec.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 16 | #include <windows.h> |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 17 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 18 | #include <memory> |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/desktop_capture/desktop_capturer.h" |
| 21 | #include "modules/desktop_capture/screen_capture_frame_queue.h" |
| 22 | #include "modules/desktop_capture/screen_capturer_helper.h" |
| 23 | #include "modules/desktop_capture/shared_desktop_frame.h" |
| 24 | #include "modules/desktop_capture/win/scoped_thread_desktop.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "rtc_base/constructor_magic.h" |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
| 29 | class DesktopFrame; |
| 30 | class DesktopRect; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 31 | |
| 32 | // Captures the screen using the Magnification API to support window exclusion. |
| 33 | // Each capturer must run on a dedicated thread because it uses thread local |
| 34 | // storage for redirecting the library callback. Also the thread must have a UI |
| 35 | // message loop to handle the window messages for the magnifier window. |
zijiehe | c59bf04 | 2016-09-23 17:54:32 -0700 | [diff] [blame] | 36 | // |
| 37 | // This class does not detect DesktopFrame::updated_region(), the field is |
| 38 | // always set to the entire frame rectangle. ScreenCapturerDifferWrapper should |
| 39 | // be used if that functionality is necessary. |
zijiehe | 98903d2 | 2016-11-10 21:57:10 -0800 | [diff] [blame] | 40 | class ScreenCapturerWinMagnifier : public DesktopCapturer { |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 41 | public: |
zijiehe | 3fa87f7 | 2017-02-22 13:47:00 -0800 | [diff] [blame] | 42 | ScreenCapturerWinMagnifier(); |
sergeyu | e183121 | 2016-10-26 13:15:42 -0700 | [diff] [blame] | 43 | ~ScreenCapturerWinMagnifier() override; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 44 | |
| 45 | // Overridden from ScreenCapturer: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 46 | void Start(Callback* callback) override; |
sergeyu | cc9669c | 2016-02-09 15:13:26 -0800 | [diff] [blame] | 47 | void SetSharedMemoryFactory( |
kwiberg | 84be511 | 2016-04-27 01:19:58 -0700 | [diff] [blame] | 48 | std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override; |
zijiehe | 91902cb | 2016-10-13 16:47:49 -0700 | [diff] [blame] | 49 | void CaptureFrame() override; |
zijiehe | fce4905 | 2016-11-07 15:25:18 -0800 | [diff] [blame] | 50 | bool GetSourceList(SourceList* screens) override; |
| 51 | bool SelectSource(SourceId id) override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 52 | void SetExcludedWindow(WindowId window) override; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | typedef BOOL(WINAPI* MagImageScalingCallback)(HWND hwnd, |
| 56 | void* srcdata, |
| 57 | MAGIMAGEHEADER srcheader, |
| 58 | void* destdata, |
| 59 | MAGIMAGEHEADER destheader, |
| 60 | RECT unclipped, |
| 61 | RECT clipped, |
| 62 | HRGN dirty); |
| 63 | typedef BOOL(WINAPI* MagInitializeFunc)(void); |
| 64 | typedef BOOL(WINAPI* MagUninitializeFunc)(void); |
| 65 | typedef BOOL(WINAPI* MagSetWindowSourceFunc)(HWND hwnd, RECT rect); |
| 66 | typedef BOOL(WINAPI* MagSetWindowFilterListFunc)(HWND hwnd, |
| 67 | DWORD dwFilterMode, |
| 68 | int count, |
| 69 | HWND* pHWND); |
| 70 | typedef BOOL(WINAPI* MagSetImageScalingCallbackFunc)( |
| 71 | HWND hwnd, |
| 72 | MagImageScalingCallback callback); |
| 73 | |
| 74 | static BOOL WINAPI OnMagImageScalingCallback(HWND hwnd, |
| 75 | void* srcdata, |
| 76 | MAGIMAGEHEADER srcheader, |
| 77 | void* destdata, |
| 78 | MAGIMAGEHEADER destheader, |
| 79 | RECT unclipped, |
| 80 | RECT clipped, |
| 81 | HRGN dirty); |
| 82 | |
| 83 | // Captures the screen within |rect| in the desktop coordinates. Returns true |
| 84 | // if succeeded. |
| 85 | // It can only capture the primary screen for now. The magnification library |
| 86 | // crashes under some screen configurations (e.g. secondary screen on top of |
| 87 | // primary screen) if it tries to capture a non-primary screen. The caller |
| 88 | // must make sure not calling it on non-primary screens. |
| 89 | bool CaptureImage(const DesktopRect& rect); |
| 90 | |
| 91 | // Helper method for setting up the magnifier control. Returns true if |
| 92 | // succeeded. |
| 93 | bool InitializeMagnifier(); |
| 94 | |
| 95 | // Called by OnMagImageScalingCallback to output captured data. |
| 96 | void OnCaptured(void* data, const MAGIMAGEHEADER& header); |
| 97 | |
| 98 | // Makes sure the current frame exists and matches |size|. |
| 99 | void CreateCurrentFrameIfNecessary(const DesktopSize& size); |
| 100 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 101 | Callback* callback_ = nullptr; |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 102 | std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 103 | ScreenId current_screen_id_ = kFullDesktopScreenId; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 104 | std::wstring current_device_key_; |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 105 | HWND excluded_window_ = NULL; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 106 | |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 107 | // Queue of the frames buffers. |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame] | 108 | ScreenCaptureFrameQueue<SharedDesktopFrame> queue_; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 109 | |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 110 | ScopedThreadDesktop desktop_; |
| 111 | |
| 112 | // Used for getting the screen dpi. |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 113 | HDC desktop_dc_ = NULL; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 114 | |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 115 | HMODULE mag_lib_handle_ = NULL; |
| 116 | MagInitializeFunc mag_initialize_func_ = nullptr; |
| 117 | MagUninitializeFunc mag_uninitialize_func_ = nullptr; |
| 118 | MagSetWindowSourceFunc set_window_source_func_ = nullptr; |
| 119 | MagSetWindowFilterListFunc set_window_filter_list_func_ = nullptr; |
| 120 | MagSetImageScalingCallbackFunc set_image_scaling_callback_func_ = nullptr; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 121 | |
| 122 | // The hidden window hosting the magnifier control. |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 123 | HWND host_window_ = NULL; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 124 | // The magnifier control that captures the screen. |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 125 | HWND magnifier_window_ = NULL; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 126 | |
| 127 | // True if the magnifier control has been successfully initialized. |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 128 | bool magnifier_initialized_ = false; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 129 | |
| 130 | // True if the last OnMagImageScalingCallback was called and handled |
| 131 | // successfully. Reset at the beginning of each CaptureImage call. |
sergeyu | 5d91028 | 2016-06-07 16:41:58 -0700 | [diff] [blame] | 132 | bool magnifier_capture_succeeded_ = true; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 133 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 134 | RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinMagnifier); |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | } // namespace webrtc |
| 138 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 139 | #endif // MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_ |