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 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_magnifier.h"
15 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
16 #include "webrtc/modules/desktop_capture/desktop_frame.h"
17 #include "webrtc/modules/desktop_capture/desktop_frame_win.h"
18 #include "webrtc/modules/desktop_capture/desktop_region.h"
19 #include "webrtc/modules/desktop_capture/differ.h"
20 #include "webrtc/modules/desktop_capture/mouse_cursor.h"
21 #include "webrtc/modules/desktop_capture/win/cursor.h"
22 #include "webrtc/modules/desktop_capture/win/desktop.h"
23 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h"
24 #include "webrtc/system_wrappers/interface/logging.h"
25 #include "webrtc/system_wrappers/interface/tick_util.h"
29 // kMagnifierWindowClass has to be "Magnifier" according to the Magnification
30 // API. The other strings can be anything.
31 static LPCTSTR kMagnifierHostClass = L"ScreenCapturerWinMagnifierHost";
32 static LPCTSTR kHostWindowName = L"MagnifierHost";
33 static LPCTSTR kMagnifierWindowClass = L"Magnifier";
34 static LPCTSTR kMagnifierWindowName = L"MagnifierWindow";
36 Atomic32 ScreenCapturerWinMagnifier::tls_index_(TLS_OUT_OF_INDEXES);
38 ScreenCapturerWinMagnifier::ScreenCapturerWinMagnifier(
39 scoped_ptr<ScreenCapturer> fallback_capturer)
40 : fallback_capturer_(fallback_capturer.Pass()),
41 fallback_capturer_started_(false),
43 current_screen_id_(kFullDesktopScreenId),
44 excluded_window_(NULL),
45 set_thread_execution_state_failed_(false),
47 mag_lib_handle_(NULL),
48 mag_initialize_func_(NULL),
49 mag_uninitialize_func_(NULL),
50 set_window_source_func_(NULL),
51 set_window_filter_list_func_(NULL),
52 set_image_scaling_callback_func_(NULL),
54 magnifier_window_(NULL),
55 magnifier_initialized_(false),
56 magnifier_capture_succeeded_(true) {
59 ScreenCapturerWinMagnifier::~ScreenCapturerWinMagnifier() {
60 // DestroyWindow must be called before MagUninitialize. magnifier_window_ is
61 // destroyed automatically when host_window_ is destroyed.
63 DestroyWindow(host_window_);
65 if (magnifier_initialized_)
66 mag_uninitialize_func_();
69 FreeLibrary(mag_lib_handle_);
72 ReleaseDC(NULL, desktop_dc_);
75 void ScreenCapturerWinMagnifier::Start(Callback* callback) {
80 InitializeMagnifier();
83 void ScreenCapturerWinMagnifier::Capture(const DesktopRegion& region) {
84 TickTime capture_start_time = TickTime::Now();
86 queue_.MoveToNextFrame();
88 // Request that the system not power-down the system, or the display hardware.
89 if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) {
90 if (!set_thread_execution_state_failed_) {
91 set_thread_execution_state_failed_ = true;
92 LOG_F(LS_WARNING) << "Failed to make system & display power assertion: "
96 // Switch to the desktop receiving user input if different from the current
98 scoped_ptr<Desktop> input_desktop(Desktop::GetInputDesktop());
99 if (input_desktop.get() != NULL && !desktop_.IsSame(*input_desktop)) {
100 // Release GDI resources otherwise SetThreadDesktop will fail.
102 ReleaseDC(NULL, desktop_dc_);
105 // If SetThreadDesktop() fails, the thread is still assigned a desktop.
106 // So we can continue capture screen bits, just from the wrong desktop.
107 desktop_.SetThreadDesktop(input_desktop.release());
110 bool succeeded = false;
112 // Do not try to use the magnfiier if it's capturing non-primary screen, or it
114 if (magnifier_initialized_ && IsCapturingPrimaryScreenOnly() &&
115 magnifier_capture_succeeded_) {
116 DesktopRect rect = GetScreenRect(current_screen_id_, current_device_key_);
117 CreateCurrentFrameIfNecessary(rect.size());
119 // CaptureImage may fail in some situations, e.g. windows8 metro mode.
120 succeeded = CaptureImage(rect);
123 // Defer to the fallback capturer if magnifier capturer did not work.
125 LOG_F(LS_WARNING) << "Switching to the fallback screen capturer.";
126 StartFallbackCapturer();
127 fallback_capturer_->Capture(region);
131 const DesktopFrame* current_frame = queue_.current_frame();
132 const DesktopFrame* last_frame = queue_.previous_frame();
133 if (last_frame && last_frame->size().equals(current_frame->size())) {
134 // Make sure the differencer is set up correctly for these previous and
136 if (!differ_.get() || (differ_->width() != current_frame->size().width()) ||
137 (differ_->height() != current_frame->size().height()) ||
138 (differ_->bytes_per_row() != current_frame->stride())) {
139 differ_.reset(new Differ(current_frame->size().width(),
140 current_frame->size().height(),
141 DesktopFrame::kBytesPerPixel,
142 current_frame->stride()));
145 // Calculate difference between the two last captured frames.
146 DesktopRegion region;
147 differ_->CalcDirtyRegion(
148 last_frame->data(), current_frame->data(), ®ion);
149 helper_.InvalidateRegion(region);
151 // No previous frame is available, or the screen is resized. Invalidate the
153 helper_.InvalidateScreen(current_frame->size());
156 helper_.set_size_most_recent(current_frame->size());
158 // Emit the current frame.
159 DesktopFrame* frame = queue_.current_frame()->Share();
160 frame->set_dpi(DesktopVector(GetDeviceCaps(desktop_dc_, LOGPIXELSX),
161 GetDeviceCaps(desktop_dc_, LOGPIXELSY)));
162 frame->mutable_updated_region()->Clear();
163 helper_.TakeInvalidRegion(frame->mutable_updated_region());
164 frame->set_capture_time_ms(
165 (TickTime::Now() - capture_start_time).Milliseconds());
166 callback_->OnCaptureCompleted(frame);
169 bool ScreenCapturerWinMagnifier::GetScreenList(ScreenList* screens) {
170 return webrtc::GetScreenList(screens);
173 bool ScreenCapturerWinMagnifier::SelectScreen(ScreenId id) {
174 bool valid = IsScreenValid(id, ¤t_device_key_);
176 // Set current_screen_id_ even if the fallback capturer is being used, so we
177 // can switch back to the magnifier when possible.
179 current_screen_id_ = id;
181 if (fallback_capturer_started_)
182 fallback_capturer_->SelectScreen(id);
187 void ScreenCapturerWinMagnifier::SetExcludedWindow(WindowId excluded_window) {
188 excluded_window_ = (HWND)excluded_window;
189 if (excluded_window_ && magnifier_initialized_) {
190 set_window_filter_list_func_(
191 magnifier_window_, MW_FILTERMODE_EXCLUDE, 1, &excluded_window_);
195 bool ScreenCapturerWinMagnifier::CaptureImage(const DesktopRect& rect) {
196 assert(magnifier_initialized_);
198 // Set the magnifier control to cover the captured rect. The content of the
199 // magnifier control will be the captured image.
200 BOOL result = SetWindowPos(magnifier_window_,
202 rect.left(), rect.top(),
203 rect.width(), rect.height(),
206 LOG_F(LS_WARNING) << "Failed to call SetWindowPos: " << GetLastError()
207 << ". Rect = {" << rect.left() << ", " << rect.top()
208 << ", " << rect.right() << ", " << rect.bottom() << "}";
212 magnifier_capture_succeeded_ = false;
214 RECT native_rect = {rect.left(), rect.top(), rect.right(), rect.bottom()};
216 // OnCaptured will be called via OnMagImageScalingCallback and fill in the
217 // frame before set_window_source_func_ returns.
218 result = set_window_source_func_(magnifier_window_, native_rect);
221 LOG_F(LS_WARNING) << "Failed to call MagSetWindowSource: " << GetLastError()
222 << ". Rect = {" << rect.left() << ", " << rect.top()
223 << ", " << rect.right() << ", " << rect.bottom() << "}";
227 return magnifier_capture_succeeded_;
230 BOOL ScreenCapturerWinMagnifier::OnMagImageScalingCallback(
233 MAGIMAGEHEADER srcheader,
235 MAGIMAGEHEADER destheader,
239 assert(tls_index_.Value() != TLS_OUT_OF_INDEXES);
241 ScreenCapturerWinMagnifier* owner =
242 reinterpret_cast<ScreenCapturerWinMagnifier*>(
243 TlsGetValue(tls_index_.Value()));
245 owner->OnCaptured(srcdata, srcheader);
250 bool ScreenCapturerWinMagnifier::InitializeMagnifier() {
251 assert(!magnifier_initialized_);
253 desktop_dc_ = GetDC(NULL);
255 mag_lib_handle_ = LoadLibrary(L"Magnification.dll");
256 if (!mag_lib_handle_)
259 // Initialize Magnification API function pointers.
260 mag_initialize_func_ = reinterpret_cast<MagInitializeFunc>(
261 GetProcAddress(mag_lib_handle_, "MagInitialize"));
262 mag_uninitialize_func_ = reinterpret_cast<MagUninitializeFunc>(
263 GetProcAddress(mag_lib_handle_, "MagUninitialize"));
264 set_window_source_func_ = reinterpret_cast<MagSetWindowSourceFunc>(
265 GetProcAddress(mag_lib_handle_, "MagSetWindowSource"));
266 set_window_filter_list_func_ = reinterpret_cast<MagSetWindowFilterListFunc>(
267 GetProcAddress(mag_lib_handle_, "MagSetWindowFilterList"));
268 set_image_scaling_callback_func_ =
269 reinterpret_cast<MagSetImageScalingCallbackFunc>(
270 GetProcAddress(mag_lib_handle_, "MagSetImageScalingCallback"));
272 if (!mag_initialize_func_ || !mag_uninitialize_func_ ||
273 !set_window_source_func_ || !set_window_filter_list_func_ ||
274 !set_image_scaling_callback_func_) {
275 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
276 << "library functions missing.";
280 BOOL result = mag_initialize_func_();
282 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
283 << "error from MagInitialize " << GetLastError();
287 HMODULE hInstance = NULL;
288 result = GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
289 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
290 reinterpret_cast<char*>(&DefWindowProc),
293 mag_uninitialize_func_();
294 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
295 << "error from GetModulehandleExA " << GetLastError();
299 // Register the host window class. See the MSDN documentation of the
300 // Magnification API for more infomation.
301 WNDCLASSEX wcex = {};
302 wcex.cbSize = sizeof(WNDCLASSEX);
303 wcex.lpfnWndProc = &DefWindowProc;
304 wcex.hInstance = hInstance;
305 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
306 wcex.lpszClassName = kMagnifierHostClass;
308 // Ignore the error which may happen when the class is already registered.
309 RegisterClassEx(&wcex);
311 // Create the host window.
312 host_window_ = CreateWindowEx(WS_EX_LAYERED,
322 mag_uninitialize_func_();
323 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
324 << "error from creating host window " << GetLastError();
328 // Create the magnifier control.
329 magnifier_window_ = CreateWindow(kMagnifierWindowClass,
330 kMagnifierWindowName,
331 WS_CHILD | WS_VISIBLE,
337 if (!magnifier_window_) {
338 mag_uninitialize_func_();
339 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
340 << "error from creating magnifier window "
345 // Hide the host window.
346 ShowWindow(host_window_, SW_HIDE);
348 // Set the scaling callback to receive captured image.
349 result = set_image_scaling_callback_func_(
351 &ScreenCapturerWinMagnifier::OnMagImageScalingCallback);
353 mag_uninitialize_func_();
354 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
355 << "error from MagSetImageScalingCallback "
360 if (excluded_window_) {
361 result = set_window_filter_list_func_(
362 magnifier_window_, MW_FILTERMODE_EXCLUDE, 1, &excluded_window_);
364 mag_uninitialize_func_();
365 LOG_F(LS_WARNING) << "Failed to initialize ScreenCapturerWinMagnifier: "
366 << "error from MagSetWindowFilterList "
372 if (tls_index_.Value() == TLS_OUT_OF_INDEXES) {
373 // More than one threads may get here at the same time, but only one will
374 // write to tls_index_ using CompareExchange.
375 DWORD new_tls_index = TlsAlloc();
376 if (!tls_index_.CompareExchange(new_tls_index, TLS_OUT_OF_INDEXES))
377 TlsFree(new_tls_index);
380 assert(tls_index_.Value() != TLS_OUT_OF_INDEXES);
381 TlsSetValue(tls_index_.Value(), this);
383 magnifier_initialized_ = true;
387 void ScreenCapturerWinMagnifier::OnCaptured(void* data,
388 const MAGIMAGEHEADER& header) {
389 DesktopFrame* current_frame = queue_.current_frame();
391 // Verify the format.
392 // TODO(jiayl): support capturing sources with pixel formats other than RGBA.
393 int captured_bytes_per_pixel = header.cbSize / header.width / header.height;
394 if (header.format != GUID_WICPixelFormat32bppRGBA ||
395 header.width != static_cast<UINT>(current_frame->size().width()) ||
396 header.height != static_cast<UINT>(current_frame->size().height()) ||
397 header.stride != static_cast<UINT>(current_frame->stride()) ||
398 captured_bytes_per_pixel != DesktopFrame::kBytesPerPixel) {
399 LOG_F(LS_WARNING) << "Output format does not match the captured format: "
400 << "width = " << header.width << ", "
401 << "height = " << header.height << ", "
402 << "stride = " << header.stride << ", "
403 << "bpp = " << captured_bytes_per_pixel << ", "
404 << "pixel format RGBA ? "
405 << (header.format == GUID_WICPixelFormat32bppRGBA) << ".";
409 // Copy the data into the frame.
410 current_frame->CopyPixelsFrom(
411 reinterpret_cast<uint8_t*>(data),
413 DesktopRect::MakeXYWH(0, 0, header.width, header.height));
415 magnifier_capture_succeeded_ = true;
418 void ScreenCapturerWinMagnifier::CreateCurrentFrameIfNecessary(
419 const DesktopSize& size) {
420 // If the current buffer is from an older generation then allocate a new one.
421 // Note that we can't reallocate other buffers at this point, since the caller
422 // may still be reading from them.
423 if (!queue_.current_frame() || !queue_.current_frame()->size().equals(size)) {
425 size.width() * size.height() * DesktopFrame::kBytesPerPixel;
426 SharedMemory* shared_memory = callback_->CreateSharedMemory(buffer_size);
428 scoped_ptr<DesktopFrame> buffer;
430 buffer.reset(new SharedMemoryDesktopFrame(
431 size, size.width() * DesktopFrame::kBytesPerPixel, shared_memory));
433 buffer.reset(new BasicDesktopFrame(size));
435 queue_.ReplaceCurrentFrame(buffer.release());
439 bool ScreenCapturerWinMagnifier::IsCapturingPrimaryScreenOnly() const {
440 if (current_screen_id_ != kFullDesktopScreenId)
441 return current_screen_id_ == 0; // the primary screen is always '0'.
443 return GetSystemMetrics(SM_CMONITORS) == 1;
446 void ScreenCapturerWinMagnifier::StartFallbackCapturer() {
447 assert(fallback_capturer_);
448 if (!fallback_capturer_started_) {
449 fallback_capturer_started_ = true;
451 fallback_capturer_->Start(callback_);
452 fallback_capturer_->SelectScreen(current_screen_id_);
456 } // namespace webrtc