2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_
12 #define WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_
15 #include <magnification.h>
18 #include "webrtc/base/constructormagic.h"
19 #include "webrtc/modules/desktop_capture/screen_capture_frame_queue.h"
20 #include "webrtc/modules/desktop_capture/screen_capturer.h"
21 #include "webrtc/modules/desktop_capture/screen_capturer_helper.h"
22 #include "webrtc/modules/desktop_capture/win/scoped_thread_desktop.h"
23 #include "webrtc/system_wrappers/interface/atomic32.h"
24 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
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.
36 class ScreenCapturerWinMagnifier : public ScreenCapturer {
38 // |fallback_capturer| will be used to capture the screen if a non-primary
39 // screen is being captured, or the OS does not support Magnification API, or
40 // the magnifier capturer fails (e.g. in Windows8 Metro mode).
41 explicit ScreenCapturerWinMagnifier(
42 scoped_ptr<ScreenCapturer> fallback_capturer);
43 virtual ~ScreenCapturerWinMagnifier();
45 // Overridden from ScreenCapturer:
46 virtual void Start(Callback* callback) OVERRIDE;
47 virtual void Capture(const DesktopRegion& region) OVERRIDE;
48 virtual bool GetScreenList(ScreenList* screens) OVERRIDE;
49 virtual bool SelectScreen(ScreenId id) OVERRIDE;
50 virtual void SetExcludedWindow(WindowId window) OVERRIDE;
53 typedef BOOL(WINAPI* MagImageScalingCallback)(HWND hwnd,
55 MAGIMAGEHEADER srcheader,
57 MAGIMAGEHEADER destheader,
61 typedef BOOL(WINAPI* MagInitializeFunc)(void);
62 typedef BOOL(WINAPI* MagUninitializeFunc)(void);
63 typedef BOOL(WINAPI* MagSetWindowSourceFunc)(HWND hwnd, RECT rect);
64 typedef BOOL(WINAPI* MagSetWindowFilterListFunc)(HWND hwnd,
68 typedef BOOL(WINAPI* MagSetImageScalingCallbackFunc)(
70 MagImageScalingCallback callback);
72 static BOOL WINAPI OnMagImageScalingCallback(HWND hwnd,
74 MAGIMAGEHEADER srcheader,
76 MAGIMAGEHEADER destheader,
81 // Captures the screen within |rect| in the desktop coordinates. Returns true
83 // It can only capture the primary screen for now. The magnification library
84 // crashes under some screen configurations (e.g. secondary screen on top of
85 // primary screen) if it tries to capture a non-primary screen. The caller
86 // must make sure not calling it on non-primary screens.
87 bool CaptureImage(const DesktopRect& rect);
89 // Helper method for setting up the magnifier control. Returns true if
91 bool InitializeMagnifier();
93 // Called by OnMagImageScalingCallback to output captured data.
94 void OnCaptured(void* data, const MAGIMAGEHEADER& header);
96 // Makes sure the current frame exists and matches |size|.
97 void CreateCurrentFrameIfNecessary(const DesktopSize& size);
99 // Returns true if we are capturing the primary screen only.
100 bool IsCapturingPrimaryScreenOnly() const;
102 // Start the fallback capturer and select the screen.
103 void StartFallbackCapturer();
105 static Atomic32 tls_index_;
107 scoped_ptr<ScreenCapturer> fallback_capturer_;
108 bool fallback_capturer_started_;
110 ScreenId current_screen_id_;
111 std::wstring current_device_key_;
112 HWND excluded_window_;
114 // A thread-safe list of invalid rectangles, and the size of the most
115 // recently captured screen.
116 ScreenCapturerHelper helper_;
118 // Queue of the frames buffers.
119 ScreenCaptureFrameQueue queue_;
121 // Class to calculate the difference between two screen bitmaps.
122 scoped_ptr<Differ> differ_;
124 // Used to suppress duplicate logging of SetThreadExecutionState errors.
125 bool set_thread_execution_state_failed_;
127 ScopedThreadDesktop desktop_;
129 // Used for getting the screen dpi.
132 HMODULE mag_lib_handle_;
133 MagInitializeFunc mag_initialize_func_;
134 MagUninitializeFunc mag_uninitialize_func_;
135 MagSetWindowSourceFunc set_window_source_func_;
136 MagSetWindowFilterListFunc set_window_filter_list_func_;
137 MagSetImageScalingCallbackFunc set_image_scaling_callback_func_;
139 // The hidden window hosting the magnifier control.
141 // The magnifier control that captures the screen.
142 HWND magnifier_window_;
144 // True if the magnifier control has been successfully initialized.
145 bool magnifier_initialized_;
147 // True if the last OnMagImageScalingCallback was called and handled
148 // successfully. Reset at the beginning of each CaptureImage call.
149 bool magnifier_capture_succeeded_;
151 DISALLOW_COPY_AND_ASSIGN(ScreenCapturerWinMagnifier);
154 } // namespace webrtc
156 #endif // WEBRTC_MODULES_DESKTOP_CAPTURE_WIN_SCREEN_CAPTURER_WIN_MAGNIFIER_H_