| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/desktop_capture/window_capturer.h" | 11 #include "webrtc/modules/desktop_capture/window_capturer.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 | 14 |
| 15 #include <memory> | 15 #include <memory> |
| 16 | 16 |
| 17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 18 #include "webrtc/base/constructormagic.h" | 18 #include "webrtc/base/constructormagic.h" |
| 19 #include "webrtc/base/win32.h" | 19 #include "webrtc/base/win32.h" |
| 20 #include "webrtc/modules/desktop_capture/desktop_frame_win.h" | 20 #include "webrtc/modules/desktop_capture/desktop_frame_win.h" |
| 21 #include "webrtc/modules/desktop_capture/win/window_capture_utils.h" | 21 #include "webrtc/modules/desktop_capture/win/window_capture_utils.h" |
| 22 #include "webrtc/system_wrappers/include/logging.h" | 22 #include "webrtc/system_wrappers/include/logging.h" |
| 23 | 23 |
| 24 namespace webrtc { | 24 namespace webrtc { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param) { | 28 BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param) { |
| 29 WindowCapturer::WindowList* list = | 29 DesktopCapturer::SourceList* list = |
| 30 reinterpret_cast<WindowCapturer::WindowList*>(param); | 30 reinterpret_cast<DesktopCapturer::SourceList*>(param); |
| 31 | 31 |
| 32 // Skip windows that are invisible, minimized, have no title, or are owned, | 32 // Skip windows that are invisible, minimized, have no title, or are owned, |
| 33 // unless they have the app window style set. | 33 // unless they have the app window style set. |
| 34 int len = GetWindowTextLength(hwnd); | 34 int len = GetWindowTextLength(hwnd); |
| 35 HWND owner = GetWindow(hwnd, GW_OWNER); | 35 HWND owner = GetWindow(hwnd, GW_OWNER); |
| 36 LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE); | 36 LONG exstyle = GetWindowLong(hwnd, GWL_EXSTYLE); |
| 37 if (len == 0 || IsIconic(hwnd) || !IsWindowVisible(hwnd) || | 37 if (len == 0 || IsIconic(hwnd) || !IsWindowVisible(hwnd) || |
| 38 (owner && !(exstyle & WS_EX_APPWINDOW))) { | 38 (owner && !(exstyle & WS_EX_APPWINDOW))) { |
| 39 return TRUE; | 39 return TRUE; |
| 40 } | 40 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 55 // Windows 8 introduced a "Modern App" identified by their class name being | 55 // Windows 8 introduced a "Modern App" identified by their class name being |
| 56 // either ApplicationFrameWindow or windows.UI.Core.coreWindow. The | 56 // either ApplicationFrameWindow or windows.UI.Core.coreWindow. The |
| 57 // associated windows cannot be captured, so we skip them. | 57 // associated windows cannot be captured, so we skip them. |
| 58 // http://crbug.com/526883. | 58 // http://crbug.com/526883. |
| 59 if (rtc::IsWindows8OrLater() && | 59 if (rtc::IsWindows8OrLater() && |
| 60 (wcscmp(class_name, L"ApplicationFrameWindow") == 0 || | 60 (wcscmp(class_name, L"ApplicationFrameWindow") == 0 || |
| 61 wcscmp(class_name, L"Windows.UI.Core.CoreWindow") == 0)) { | 61 wcscmp(class_name, L"Windows.UI.Core.CoreWindow") == 0)) { |
| 62 return TRUE; | 62 return TRUE; |
| 63 } | 63 } |
| 64 | 64 |
| 65 WindowCapturer::Window window; | 65 DesktopCapturer::Source window; |
| 66 window.id = reinterpret_cast<WindowId>(hwnd); | 66 window.id = reinterpret_cast<WindowId>(hwnd); |
| 67 | 67 |
| 68 const size_t kTitleLength = 500; | 68 const size_t kTitleLength = 500; |
| 69 WCHAR window_title[kTitleLength]; | 69 WCHAR window_title[kTitleLength]; |
| 70 // Truncate the title if it's longer than kTitleLength. | 70 // Truncate the title if it's longer than kTitleLength. |
| 71 GetWindowText(hwnd, window_title, kTitleLength); | 71 GetWindowText(hwnd, window_title, kTitleLength); |
| 72 window.title = rtc::ToUtf8(window_title); | 72 window.title = rtc::ToUtf8(window_title); |
| 73 | 73 |
| 74 // Skip windows when we failed to convert the title or it is empty. | 74 // Skip windows when we failed to convert the title or it is empty. |
| 75 if (window.title.empty()) | 75 if (window.title.empty()) |
| 76 return TRUE; | 76 return TRUE; |
| 77 | 77 |
| 78 list->push_back(window); | 78 list->push_back(window); |
| 79 | 79 |
| 80 return TRUE; | 80 return TRUE; |
| 81 } | 81 } |
| 82 | 82 |
| 83 class WindowCapturerWin : public WindowCapturer { | 83 class WindowCapturerWin : public WindowCapturer { |
| 84 public: | 84 public: |
| 85 WindowCapturerWin(); | 85 WindowCapturerWin(); |
| 86 ~WindowCapturerWin() override; | 86 ~WindowCapturerWin() override; |
| 87 | 87 |
| 88 // WindowCapturer interface. | |
| 89 bool GetWindowList(WindowList* windows) override; | |
| 90 bool SelectWindow(WindowId id) override; | |
| 91 bool BringSelectedWindowToFront() override; | |
| 92 | |
| 93 // DesktopCapturer interface. | 88 // DesktopCapturer interface. |
| 94 void Start(Callback* callback) override; | 89 void Start(Callback* callback) override; |
| 95 void CaptureFrame() override; | 90 void CaptureFrame() override; |
| 91 bool GetSourceList(SourceList* sources) override; |
| 92 bool SelectSource(SourceId id) override; |
| 93 bool FocusOnSelectedSource() override; |
| 96 | 94 |
| 97 private: | 95 private: |
| 98 Callback* callback_ = nullptr; | 96 Callback* callback_ = nullptr; |
| 99 | 97 |
| 100 // HWND and HDC for the currently selected window or nullptr if window is not | 98 // HWND and HDC for the currently selected window or nullptr if window is not |
| 101 // selected. | 99 // selected. |
| 102 HWND window_ = nullptr; | 100 HWND window_ = nullptr; |
| 103 | 101 |
| 104 DesktopSize previous_size_; | 102 DesktopSize previous_size_; |
| 105 | 103 |
| 106 AeroChecker aero_checker_; | 104 AeroChecker aero_checker_; |
| 107 | 105 |
| 108 // This map is used to avoid flickering for the case when SelectWindow() calls | 106 // This map is used to avoid flickering for the case when SelectWindow() calls |
| 109 // are interleaved with Capture() calls. | 107 // are interleaved with Capture() calls. |
| 110 std::map<HWND, DesktopSize> window_size_map_; | 108 std::map<HWND, DesktopSize> window_size_map_; |
| 111 | 109 |
| 112 RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerWin); | 110 RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerWin); |
| 113 }; | 111 }; |
| 114 | 112 |
| 115 WindowCapturerWin::WindowCapturerWin() {} | 113 WindowCapturerWin::WindowCapturerWin() {} |
| 116 WindowCapturerWin::~WindowCapturerWin() {} | 114 WindowCapturerWin::~WindowCapturerWin() {} |
| 117 | 115 |
| 118 bool WindowCapturerWin::GetWindowList(WindowList* windows) { | 116 bool WindowCapturerWin::GetSourceList(SourceList* sources) { |
| 119 WindowList result; | 117 SourceList result; |
| 120 LPARAM param = reinterpret_cast<LPARAM>(&result); | 118 LPARAM param = reinterpret_cast<LPARAM>(&result); |
| 121 if (!EnumWindows(&WindowsEnumerationHandler, param)) | 119 if (!EnumWindows(&WindowsEnumerationHandler, param)) |
| 122 return false; | 120 return false; |
| 123 windows->swap(result); | 121 sources->swap(result); |
| 124 | 122 |
| 125 std::map<HWND, DesktopSize> new_map; | 123 std::map<HWND, DesktopSize> new_map; |
| 126 for (const auto& item : *windows) { | 124 for (const auto& item : *sources) { |
| 127 HWND hwnd = reinterpret_cast<HWND>(item.id); | 125 HWND hwnd = reinterpret_cast<HWND>(item.id); |
| 128 new_map[hwnd] = window_size_map_[hwnd]; | 126 new_map[hwnd] = window_size_map_[hwnd]; |
| 129 } | 127 } |
| 130 window_size_map_.swap(new_map); | 128 window_size_map_.swap(new_map); |
| 131 | 129 |
| 132 return true; | 130 return true; |
| 133 } | 131 } |
| 134 | 132 |
| 135 bool WindowCapturerWin::SelectWindow(WindowId id) { | 133 bool WindowCapturerWin::SelectSource(SourceId id) { |
| 136 HWND window = reinterpret_cast<HWND>(id); | 134 HWND window = reinterpret_cast<HWND>(id); |
| 137 if (!IsWindow(window) || !IsWindowVisible(window) || IsIconic(window)) | 135 if (!IsWindow(window) || !IsWindowVisible(window) || IsIconic(window)) |
| 138 return false; | 136 return false; |
| 139 window_ = window; | 137 window_ = window; |
| 140 // When a window is not in the map, window_size_map_[window] will create an | 138 // When a window is not in the map, window_size_map_[window] will create an |
| 141 // item with DesktopSize (0, 0). | 139 // item with DesktopSize (0, 0). |
| 142 previous_size_ = window_size_map_[window]; | 140 previous_size_ = window_size_map_[window]; |
| 143 return true; | 141 return true; |
| 144 } | 142 } |
| 145 | 143 |
| 146 bool WindowCapturerWin::BringSelectedWindowToFront() { | 144 bool WindowCapturerWin::FocusOnSelectedSource() { |
| 147 if (!window_) | 145 if (!window_) |
| 148 return false; | 146 return false; |
| 149 | 147 |
| 150 if (!IsWindow(window_) || !IsWindowVisible(window_) || IsIconic(window_)) | 148 if (!IsWindow(window_) || !IsWindowVisible(window_) || IsIconic(window_)) |
| 151 return false; | 149 return false; |
| 152 | 150 |
| 153 return BringWindowToTop(window_) != FALSE && | 151 return BringWindowToTop(window_) != FALSE && |
| 154 SetForegroundWindow(window_) != FALSE; | 152 SetForegroundWindow(window_) != FALSE; |
| 155 } | 153 } |
| 156 | 154 |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 return new WindowCapturerWin(); | 268 return new WindowCapturerWin(); |
| 271 } | 269 } |
| 272 | 270 |
| 273 // static | 271 // static |
| 274 std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawWindowCapturer( | 272 std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateRawWindowCapturer( |
| 275 const DesktopCaptureOptions& options) { | 273 const DesktopCaptureOptions& options) { |
| 276 return std::unique_ptr<DesktopCapturer>(new WindowCapturerWin()); | 274 return std::unique_ptr<DesktopCapturer>(new WindowCapturerWin()); |
| 277 } | 275 } |
| 278 | 276 |
| 279 } // namespace webrtc | 277 } // namespace webrtc |
| OLD | NEW |