OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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/win/dxgi_duplicator_controller.h" | 11 #include "webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h" |
12 | 12 |
13 #include <windows.h> | 13 #include <windows.h> |
14 | 14 |
15 #include <algorithm> | 15 #include <algorithm> |
16 #include <string> | 16 #include <string> |
17 | 17 |
18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
20 #include "webrtc/base/timeutils.h" | 20 #include "webrtc/base/timeutils.h" |
21 #include "webrtc/modules/desktop_capture/desktop_capture_types.h" | 21 #include "webrtc/modules/desktop_capture/desktop_capture_types.h" |
22 #include "webrtc/modules/desktop_capture/win/dxgi_frame.h" | 22 #include "webrtc/modules/desktop_capture/win/dxgi_frame.h" |
23 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" | 23 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
24 #include "webrtc/system_wrappers/include/sleep.h" | 24 #include "webrtc/system_wrappers/include/sleep.h" |
25 | 25 |
26 namespace webrtc { | 26 namespace webrtc { |
27 | 27 |
| 28 namespace { |
| 29 |
| 30 // TODO(zijiehe): This function should be public as |
| 31 // static bool DxgiDuplicatorController::IsSessionUnsupported() |
| 32 bool IsRunningInSession0() { |
| 33 DWORD session_id = 0; |
| 34 if (!::ProcessIdToSessionId(::GetCurrentProcessId(), &session_id)) { |
| 35 LOG(LS_WARNING) << "Failed to retrieve current session Id, current binary " |
| 36 "may not have required priviledge."; |
| 37 return true; |
| 38 } |
| 39 return session_id == 0; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
28 // static | 44 // static |
29 rtc::scoped_refptr<DxgiDuplicatorController> | 45 rtc::scoped_refptr<DxgiDuplicatorController> |
30 DxgiDuplicatorController::Instance() { | 46 DxgiDuplicatorController::Instance() { |
31 // The static instance won't be deleted to ensure it can be used by other | 47 // The static instance won't be deleted to ensure it can be used by other |
32 // threads even during program exiting. | 48 // threads even during program exiting. |
33 static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); | 49 static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); |
34 return rtc::scoped_refptr<DxgiDuplicatorController>(instance); | 50 return rtc::scoped_refptr<DxgiDuplicatorController>(instance); |
35 } | 51 } |
36 | 52 |
37 DxgiDuplicatorController::DxgiDuplicatorController() | 53 DxgiDuplicatorController::DxgiDuplicatorController() |
(...skipping 13 matching lines...) Expand all Loading... |
51 Unload(); | 67 Unload(); |
52 } | 68 } |
53 } | 69 } |
54 | 70 |
55 bool DxgiDuplicatorController::IsSupported() { | 71 bool DxgiDuplicatorController::IsSupported() { |
56 rtc::CritScope lock(&lock_); | 72 rtc::CritScope lock(&lock_); |
57 return Initialize(); | 73 return Initialize(); |
58 } | 74 } |
59 | 75 |
60 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { | 76 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { |
61 rtc::CritScope lock(&lock_); | 77 bool result = false; |
62 if (!Initialize()) { | 78 { |
63 return false; | 79 rtc::CritScope lock(&lock_); |
| 80 result = Initialize(); |
| 81 *info = d3d_info_; |
64 } | 82 } |
65 *info = d3d_info_; | 83 if (!result) { |
66 return true; | 84 LOG(LS_WARNING) << "Failed to initialize DXGI components, the D3dInfo " |
| 85 "retrieved may not accurate or out of date."; |
| 86 } |
| 87 return result; |
67 } | 88 } |
68 | 89 |
69 DxgiDuplicatorController::Result | 90 DxgiDuplicatorController::Result |
70 DxgiDuplicatorController::Duplicate(DxgiFrame* frame) { | 91 DxgiDuplicatorController::Duplicate(DxgiFrame* frame) { |
71 return DoDuplicate(frame, -1); | 92 return DoDuplicate(frame, -1); |
72 } | 93 } |
73 | 94 |
74 DxgiDuplicatorController::Result | 95 DxgiDuplicatorController::Result |
75 DxgiDuplicatorController::DuplicateMonitor(DxgiFrame* frame, int monitor_id) { | 96 DxgiDuplicatorController::DuplicateMonitor(DxgiFrame* frame, int monitor_id) { |
76 RTC_DCHECK_GE(monitor_id, 0); | 97 RTC_DCHECK_GE(monitor_id, 0); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 // size. | 131 // size. |
111 // TODO(zijiehe): Confirm whether IDXGIOutput::GetDesc() and | 132 // TODO(zijiehe): Confirm whether IDXGIOutput::GetDesc() and |
112 // IDXGIOutputDuplication::GetDesc() can detect the resolution change without | 133 // IDXGIOutputDuplication::GetDesc() can detect the resolution change without |
113 // reinitialization. | 134 // reinitialization. |
114 if (resolution_change_detector_.IsChanged( | 135 if (resolution_change_detector_.IsChanged( |
115 GetScreenRect(kFullDesktopScreenId, std::wstring()).size())) { | 136 GetScreenRect(kFullDesktopScreenId, std::wstring()).size())) { |
116 Deinitialize(); | 137 Deinitialize(); |
117 } | 138 } |
118 | 139 |
119 if (!Initialize()) { | 140 if (!Initialize()) { |
| 141 if (succeeded_duplications_ == 0 && IsRunningInSession0()) { |
| 142 LOG(LS_WARNING) << "Current binary is running in session 0. DXGI " |
| 143 "components cannot be initialized."; |
| 144 return Result::UNSUPPORTED_SESSION; |
| 145 } |
| 146 |
120 // Cannot initialize COM components now, display mode may be changing. | 147 // Cannot initialize COM components now, display mode may be changing. |
121 return Result::INITIALIZATION_FAILED; | 148 return Result::INITIALIZATION_FAILED; |
122 } | 149 } |
123 | 150 |
124 if (!frame->Prepare(SelectedDesktopSize(monitor_id), monitor_id)) { | 151 if (!frame->Prepare(SelectedDesktopSize(monitor_id), monitor_id)) { |
125 return Result::FRAME_PREPARE_FAILED; | 152 return Result::FRAME_PREPARE_FAILED; |
126 } | 153 } |
127 | 154 |
128 frame->frame()->mutable_updated_region()->Clear(); | 155 frame->frame()->mutable_updated_region()->Clear(); |
129 | 156 |
130 if (DoDuplicateUnlocked(frame->context(), monitor_id, frame->frame())) { | 157 if (DoDuplicateUnlocked(frame->context(), monitor_id, frame->frame())) { |
| 158 succeeded_duplications_++; |
131 return Result::SUCCEEDED; | 159 return Result::SUCCEEDED; |
132 } | 160 } |
133 if (monitor_id >= ScreenCountUnlocked()) { | 161 if (monitor_id >= ScreenCountUnlocked()) { |
134 // It's a user error to provide a |monitor_id| larger than screen count. We | 162 // It's a user error to provide a |monitor_id| larger than screen count. We |
135 // do not need to deinitialize. | 163 // do not need to deinitialize. |
136 return Result::INVALID_MONITOR_ID; | 164 return Result::INVALID_MONITOR_ID; |
137 } | 165 } |
138 | 166 |
139 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something | 167 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something |
140 // must be wrong from capturer APIs. We should Deinitialize(). | 168 // must be wrong from capturer APIs. We should Deinitialize(). |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 201 |
174 bool DxgiDuplicatorController::DoInitialize() { | 202 bool DxgiDuplicatorController::DoInitialize() { |
175 RTC_DCHECK(desktop_rect_.is_empty()); | 203 RTC_DCHECK(desktop_rect_.is_empty()); |
176 RTC_DCHECK(duplicators_.empty()); | 204 RTC_DCHECK(duplicators_.empty()); |
177 | 205 |
178 d3d_info_.min_feature_level = static_cast<D3D_FEATURE_LEVEL>(0); | 206 d3d_info_.min_feature_level = static_cast<D3D_FEATURE_LEVEL>(0); |
179 d3d_info_.max_feature_level = static_cast<D3D_FEATURE_LEVEL>(0); | 207 d3d_info_.max_feature_level = static_cast<D3D_FEATURE_LEVEL>(0); |
180 | 208 |
181 std::vector<D3dDevice> devices = D3dDevice::EnumDevices(); | 209 std::vector<D3dDevice> devices = D3dDevice::EnumDevices(); |
182 if (devices.empty()) { | 210 if (devices.empty()) { |
| 211 LOG(LS_WARNING) << "No D3dDevice found."; |
183 return false; | 212 return false; |
184 } | 213 } |
185 | 214 |
186 for (size_t i = 0; i < devices.size(); i++) { | 215 for (size_t i = 0; i < devices.size(); i++) { |
187 duplicators_.emplace_back(devices[i]); | |
188 if (!duplicators_.back().Initialize()) { | |
189 return false; | |
190 } | |
191 | |
192 D3D_FEATURE_LEVEL feature_level = | 216 D3D_FEATURE_LEVEL feature_level = |
193 devices[i].d3d_device()->GetFeatureLevel(); | 217 devices[i].d3d_device()->GetFeatureLevel(); |
194 if (d3d_info_.max_feature_level == 0 || | 218 if (d3d_info_.max_feature_level == 0 || |
195 feature_level > d3d_info_.max_feature_level) { | 219 feature_level > d3d_info_.max_feature_level) { |
196 d3d_info_.max_feature_level = feature_level; | 220 d3d_info_.max_feature_level = feature_level; |
197 } | 221 } |
198 if (d3d_info_.min_feature_level == 0 || | 222 if (d3d_info_.min_feature_level == 0 || |
199 feature_level < d3d_info_.min_feature_level) { | 223 feature_level < d3d_info_.min_feature_level) { |
200 d3d_info_.min_feature_level = feature_level; | 224 d3d_info_.min_feature_level = feature_level; |
201 } | 225 } |
202 | 226 |
| 227 DxgiAdapterDuplicator duplicator(devices[i]); |
| 228 // There may be several video cards on the system, some of them may not |
| 229 // support IDXGOutputDuplication. But they should not impact others from |
| 230 // taking effect, so we should continually try other adapters. This usually |
| 231 // happens when a non-official virtual adapter is installed on the system. |
| 232 if (!duplicator.Initialize()) { |
| 233 LOG(LS_WARNING) << "Failed to initialize DxgiAdapterDuplicator on " |
| 234 "adapter " |
| 235 << i; |
| 236 continue; |
| 237 } |
| 238 RTC_DCHECK(!duplicator.desktop_rect().is_empty()); |
| 239 duplicators_.push_back(std::move(duplicator)); |
| 240 |
203 desktop_rect_.UnionWith(duplicators_.back().desktop_rect()); | 241 desktop_rect_.UnionWith(duplicators_.back().desktop_rect()); |
204 } | 242 } |
205 TranslateRect(); | 243 TranslateRect(); |
206 | 244 |
207 HDC hdc = GetDC(nullptr); | 245 HDC hdc = GetDC(nullptr); |
208 // Use old DPI value if failed. | 246 // Use old DPI value if failed. |
209 if (hdc) { | 247 if (hdc) { |
210 dpi_.set(GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY)); | 248 dpi_.set(GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY)); |
211 ReleaseDC(nullptr, hdc); | 249 ReleaseDC(nullptr, hdc); |
212 } | 250 } |
213 | 251 |
214 identity_++; | 252 identity_++; |
215 return true; | 253 |
| 254 if (duplicators_.empty()) { |
| 255 LOG(LS_WARNING) << "Cannot initialize any DxgiAdapterDuplicator instance."; |
| 256 } |
| 257 |
| 258 return !duplicators_.empty(); |
216 } | 259 } |
217 | 260 |
218 void DxgiDuplicatorController::Deinitialize() { | 261 void DxgiDuplicatorController::Deinitialize() { |
219 desktop_rect_ = DesktopRect(); | 262 desktop_rect_ = DesktopRect(); |
220 duplicators_.clear(); | 263 duplicators_.clear(); |
221 resolution_change_detector_.Reset(); | 264 resolution_change_detector_.Reset(); |
222 } | 265 } |
223 | 266 |
224 bool DxgiDuplicatorController::ContextExpired( | 267 bool DxgiDuplicatorController::ContextExpired( |
225 const Context* const context) const { | 268 const Context* const context) const { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 void DxgiDuplicatorController::TranslateRect() { | 433 void DxgiDuplicatorController::TranslateRect() { |
391 const DesktopVector position = | 434 const DesktopVector position = |
392 DesktopVector().subtract(desktop_rect_.top_left()); | 435 DesktopVector().subtract(desktop_rect_.top_left()); |
393 desktop_rect_.Translate(position); | 436 desktop_rect_.Translate(position); |
394 for (auto& duplicator : duplicators_) { | 437 for (auto& duplicator : duplicators_) { |
395 duplicator.TranslateRect(position); | 438 duplicator.TranslateRect(position); |
396 } | 439 } |
397 } | 440 } |
398 | 441 |
399 } // namespace webrtc | 442 } // namespace webrtc |
OLD | NEW |