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 | |
| 11 | #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_ |
| 12 | #define WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_ |
| 13 | |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 16 | #include <windows.h> |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 17 | #include <magnification.h> |
| 18 | #include <wincodec.h> |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 19 | |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 20 | #include "webrtc/base/constructormagic.h" |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 21 | #include "webrtc/base/scoped_ptr.h" |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 22 | #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h" |
mcasas@webrtc.org | 2fa7f79 | 2014-05-21 11:07:29 +0000 | [diff] [blame] | 23 | #include "webrtc/modules/desktop_capture/screen_capturer.h" |
henrike@webrtc.org | 88fbb2d | 2014-05-21 21:18:46 +0000 | [diff] [blame] | 24 | #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame^] | 25 | #include "webrtc/modules/desktop_capture/shared_desktop_frame.h" |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 26 | #include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 27 | #include "webrtc/system_wrappers/include/atomic32.h" |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
| 31 | class DesktopFrame; |
| 32 | class DesktopRect; |
| 33 | class Differ; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 34 | |
| 35 | // Captures the screen using the Magnification API to support window exclusion. |
| 36 | // Each capturer must run on a dedicated thread because it uses thread local |
| 37 | // storage for redirecting the library callback. Also the thread must have a UI |
| 38 | // message loop to handle the window messages for the magnifier window. |
| 39 | class ScreenCapturerWinMagnifier : public ScreenCapturer { |
| 40 | public: |
| 41 | // |fallback_capturer| will be used to capture the screen if a non-primary |
| 42 | // screen is being captured, or the OS does not support Magnification API, or |
| 43 | // the magnifier capturer fails (e.g. in Windows8 Metro mode). |
| 44 | explicit ScreenCapturerWinMagnifier( |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 45 | std::unique_ptr<ScreenCapturer> fallback_capturer); |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 46 | virtual ~ScreenCapturerWinMagnifier(); |
| 47 | |
| 48 | // Overridden from ScreenCapturer: |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 49 | void Start(Callback* callback) override; |
sergeyu | cc9669c | 2016-02-09 15:13:26 -0800 | [diff] [blame] | 50 | void SetSharedMemoryFactory( |
| 51 | rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) override; |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 52 | void Capture(const DesktopRegion& region) override; |
| 53 | bool GetScreenList(ScreenList* screens) override; |
| 54 | bool SelectScreen(ScreenId id) override; |
| 55 | void SetExcludedWindow(WindowId window) override; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 56 | |
| 57 | private: |
| 58 | typedef BOOL(WINAPI* MagImageScalingCallback)(HWND hwnd, |
| 59 | void* srcdata, |
| 60 | MAGIMAGEHEADER srcheader, |
| 61 | void* destdata, |
| 62 | MAGIMAGEHEADER destheader, |
| 63 | RECT unclipped, |
| 64 | RECT clipped, |
| 65 | HRGN dirty); |
| 66 | typedef BOOL(WINAPI* MagInitializeFunc)(void); |
| 67 | typedef BOOL(WINAPI* MagUninitializeFunc)(void); |
| 68 | typedef BOOL(WINAPI* MagSetWindowSourceFunc)(HWND hwnd, RECT rect); |
| 69 | typedef BOOL(WINAPI* MagSetWindowFilterListFunc)(HWND hwnd, |
| 70 | DWORD dwFilterMode, |
| 71 | int count, |
| 72 | HWND* pHWND); |
| 73 | typedef BOOL(WINAPI* MagSetImageScalingCallbackFunc)( |
| 74 | HWND hwnd, |
| 75 | MagImageScalingCallback callback); |
| 76 | |
| 77 | static BOOL WINAPI OnMagImageScalingCallback(HWND hwnd, |
| 78 | void* srcdata, |
| 79 | MAGIMAGEHEADER srcheader, |
| 80 | void* destdata, |
| 81 | MAGIMAGEHEADER destheader, |
| 82 | RECT unclipped, |
| 83 | RECT clipped, |
| 84 | HRGN dirty); |
| 85 | |
| 86 | // Captures the screen within |rect| in the desktop coordinates. Returns true |
| 87 | // if succeeded. |
| 88 | // It can only capture the primary screen for now. The magnification library |
| 89 | // crashes under some screen configurations (e.g. secondary screen on top of |
| 90 | // primary screen) if it tries to capture a non-primary screen. The caller |
| 91 | // must make sure not calling it on non-primary screens. |
| 92 | bool CaptureImage(const DesktopRect& rect); |
| 93 | |
| 94 | // Helper method for setting up the magnifier control. Returns true if |
| 95 | // succeeded. |
| 96 | bool InitializeMagnifier(); |
| 97 | |
| 98 | // Called by OnMagImageScalingCallback to output captured data. |
| 99 | void OnCaptured(void* data, const MAGIMAGEHEADER& header); |
| 100 | |
| 101 | // Makes sure the current frame exists and matches |size|. |
| 102 | void CreateCurrentFrameIfNecessary(const DesktopSize& size); |
| 103 | |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 104 | // Start the fallback capturer and select the screen. |
| 105 | void StartFallbackCapturer(); |
| 106 | |
| 107 | static Atomic32 tls_index_; |
| 108 | |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 109 | std::unique_ptr<ScreenCapturer> fallback_capturer_; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 110 | bool fallback_capturer_started_; |
| 111 | Callback* callback_; |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 112 | std::unique_ptr<SharedMemoryFactory> shared_memory_factory_; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 113 | ScreenId current_screen_id_; |
| 114 | std::wstring current_device_key_; |
| 115 | HWND excluded_window_; |
| 116 | |
| 117 | // A thread-safe list of invalid rectangles, and the size of the most |
| 118 | // recently captured screen. |
| 119 | ScreenCapturerHelper helper_; |
| 120 | |
| 121 | // Queue of the frames buffers. |
zijiehe | 809dcb4 | 2016-04-22 16:08:39 -0700 | [diff] [blame^] | 122 | ScreenCaptureFrameQueue<SharedDesktopFrame> queue_; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 123 | |
| 124 | // Class to calculate the difference between two screen bitmaps. |
kwiberg | 2bb3afa | 2016-03-16 15:58:08 -0700 | [diff] [blame] | 125 | std::unique_ptr<Differ> differ_; |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 126 | |
| 127 | // Used to suppress duplicate logging of SetThreadExecutionState errors. |
| 128 | bool set_thread_execution_state_failed_; |
| 129 | |
| 130 | ScopedThreadDesktop desktop_; |
| 131 | |
| 132 | // Used for getting the screen dpi. |
| 133 | HDC desktop_dc_; |
| 134 | |
| 135 | HMODULE mag_lib_handle_; |
| 136 | MagInitializeFunc mag_initialize_func_; |
| 137 | MagUninitializeFunc mag_uninitialize_func_; |
| 138 | MagSetWindowSourceFunc set_window_source_func_; |
| 139 | MagSetWindowFilterListFunc set_window_filter_list_func_; |
| 140 | MagSetImageScalingCallbackFunc set_image_scaling_callback_func_; |
| 141 | |
| 142 | // The hidden window hosting the magnifier control. |
| 143 | HWND host_window_; |
| 144 | // The magnifier control that captures the screen. |
| 145 | HWND magnifier_window_; |
| 146 | |
| 147 | // True if the magnifier control has been successfully initialized. |
| 148 | bool magnifier_initialized_; |
| 149 | |
| 150 | // True if the last OnMagImageScalingCallback was called and handled |
| 151 | // successfully. Reset at the beginning of each CaptureImage call. |
| 152 | bool magnifier_capture_succeeded_; |
| 153 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 154 | RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinMagnifier); |
jiayl@webrtc.org | 4220434 | 2014-05-05 16:08:47 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | } // namespace webrtc |
| 158 | |
| 159 | #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_ |