blob: ad3ddb18fd21e92be51098b836ad94bb360feb2b [file] [log] [blame]
jiayl@webrtc.org42204342014-05-05 16:08:47 +00001/*
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
kwiberg2bb3afa2016-03-16 15:58:08 -070014#include <memory>
15
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000016#include <windows.h>
jiayl@webrtc.org42204342014-05-05 16:08:47 +000017#include <magnification.h>
18#include <wincodec.h>
jiayl@webrtc.org42204342014-05-05 16:08:47 +000019
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000020#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000021#include "webrtc/base/scoped_ptr.h"
jiayl@webrtc.org42204342014-05-05 16:08:47 +000022#include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h"
mcasas@webrtc.org2fa7f792014-05-21 11:07:29 +000023#include "webrtc/modules/desktop_capture/screen_capturer.h"
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000024#include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
zijiehe809dcb42016-04-22 16:08:39 -070025#include "webrtc/modules/desktop_capture/shared_desktop_frame.h"
jiayl@webrtc.org42204342014-05-05 16:08:47 +000026#include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010027#include "webrtc/system_wrappers/include/atomic32.h"
jiayl@webrtc.org42204342014-05-05 16:08:47 +000028
29namespace webrtc {
30
31class DesktopFrame;
32class DesktopRect;
33class Differ;
jiayl@webrtc.org42204342014-05-05 16:08:47 +000034
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.
39class 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(
kwiberg2bb3afa2016-03-16 15:58:08 -070045 std::unique_ptr<ScreenCapturer> fallback_capturer);
jiayl@webrtc.org42204342014-05-05 16:08:47 +000046 virtual ~ScreenCapturerWinMagnifier();
47
48 // Overridden from ScreenCapturer:
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000049 void Start(Callback* callback) override;
sergeyucc9669c2016-02-09 15:13:26 -080050 void SetSharedMemoryFactory(
51 rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) override;
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000052 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.org42204342014-05-05 16:08:47 +000056
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.org42204342014-05-05 16:08:47 +0000104 // Start the fallback capturer and select the screen.
105 void StartFallbackCapturer();
106
107 static Atomic32 tls_index_;
108
kwiberg2bb3afa2016-03-16 15:58:08 -0700109 std::unique_ptr<ScreenCapturer> fallback_capturer_;
jiayl@webrtc.org42204342014-05-05 16:08:47 +0000110 bool fallback_capturer_started_;
111 Callback* callback_;
kwiberg2bb3afa2016-03-16 15:58:08 -0700112 std::unique_ptr<SharedMemoryFactory> shared_memory_factory_;
jiayl@webrtc.org42204342014-05-05 16:08:47 +0000113 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.
zijiehe809dcb42016-04-22 16:08:39 -0700122 ScreenCaptureFrameQueue<SharedDesktopFrame> queue_;
jiayl@webrtc.org42204342014-05-05 16:08:47 +0000123
124 // Class to calculate the difference between two screen bitmaps.
kwiberg2bb3afa2016-03-16 15:58:08 -0700125 std::unique_ptr<Differ> differ_;
jiayl@webrtc.org42204342014-05-05 16:08:47 +0000126
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
henrikg3c089d72015-09-16 05:37:44 -0700154 RTC_DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinMagnifier);
jiayl@webrtc.org42204342014-05-05 16:08:47 +0000155};
156
157} // namespace webrtc
158
159#endif // WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_