Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.cc

Issue 2937663003: Ensure Dxgi duplicator works correctly in session 0 (Closed)
Patch Set: Resolve review comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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, no priviledge?";
36 return true;
37 }
38 return session_id == 0;
39 }
40
41 } // namespace
42
28 // static 43 // static
29 rtc::scoped_refptr<DxgiDuplicatorController> 44 rtc::scoped_refptr<DxgiDuplicatorController>
30 DxgiDuplicatorController::Instance() { 45 DxgiDuplicatorController::Instance() {
31 // The static instance won't be deleted to ensure it can be used by other 46 // The static instance won't be deleted to ensure it can be used by other
32 // threads even during program exiting. 47 // threads even during program exiting.
33 static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); 48 static DxgiDuplicatorController* instance = new DxgiDuplicatorController();
34 return rtc::scoped_refptr<DxgiDuplicatorController>(instance); 49 return rtc::scoped_refptr<DxgiDuplicatorController>(instance);
35 } 50 }
36 51
37 DxgiDuplicatorController::DxgiDuplicatorController() 52 DxgiDuplicatorController::DxgiDuplicatorController()
(...skipping 13 matching lines...) Expand all
51 Unload(); 66 Unload();
52 } 67 }
53 } 68 }
54 69
55 bool DxgiDuplicatorController::IsSupported() { 70 bool DxgiDuplicatorController::IsSupported() {
56 rtc::CritScope lock(&lock_); 71 rtc::CritScope lock(&lock_);
57 return Initialize(); 72 return Initialize();
58 } 73 }
59 74
60 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { 75 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) {
61 rtc::CritScope lock(&lock_); 76 bool result = false;
62 if (!Initialize()) { 77 {
63 return false; 78 rtc::CritScope lock(&lock_);
79 result = Initialize();
80 *info = d3d_info_;
64 } 81 }
65 *info = d3d_info_; 82 if (!result) {
66 return true; 83 LOG(LS_WARNING) << "Failed to initialize DXGI components, the D3dInfo "
84 "retrieved may not accurate or out of date.";
85 }
86 return result;
67 } 87 }
68 88
69 DxgiDuplicatorController::Result 89 DxgiDuplicatorController::Result
70 DxgiDuplicatorController::Duplicate(DxgiFrame* frame) { 90 DxgiDuplicatorController::Duplicate(DxgiFrame* frame) {
71 return DoDuplicate(frame, -1); 91 return DoDuplicate(frame, -1);
72 } 92 }
73 93
74 DxgiDuplicatorController::Result 94 DxgiDuplicatorController::Result
75 DxgiDuplicatorController::DuplicateMonitor(DxgiFrame* frame, int monitor_id) { 95 DxgiDuplicatorController::DuplicateMonitor(DxgiFrame* frame, int monitor_id) {
76 RTC_DCHECK_GE(monitor_id, 0); 96 RTC_DCHECK_GE(monitor_id, 0);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 // size. 130 // size.
111 // TODO(zijiehe): Confirm whether IDXGIOutput::GetDesc() and 131 // TODO(zijiehe): Confirm whether IDXGIOutput::GetDesc() and
112 // IDXGIOutputDuplication::GetDesc() can detect the resolution change without 132 // IDXGIOutputDuplication::GetDesc() can detect the resolution change without
113 // reinitialization. 133 // reinitialization.
114 if (resolution_change_detector_.IsChanged( 134 if (resolution_change_detector_.IsChanged(
115 GetScreenRect(kFullDesktopScreenId, std::wstring()).size())) { 135 GetScreenRect(kFullDesktopScreenId, std::wstring()).size())) {
116 Deinitialize(); 136 Deinitialize();
117 } 137 }
118 138
119 if (!Initialize()) { 139 if (!Initialize()) {
140 if (succeeded_duplications_ == 0 && IsRunningInSession0()) {
141 LOG(LS_WARNING) << "Current binary is running in session 0. DXGI "
142 "components cannot be initialized.";
143 return Result::UNSUPPORTED_SESSION;
144 }
145
120 // Cannot initialize COM components now, display mode may be changing. 146 // Cannot initialize COM components now, display mode may be changing.
121 return Result::INITIALIZATION_FAILED; 147 return Result::INITIALIZATION_FAILED;
122 } 148 }
123 149
124 if (!frame->Prepare(SelectedDesktopSize(monitor_id), monitor_id)) { 150 if (!frame->Prepare(SelectedDesktopSize(monitor_id), monitor_id)) {
125 return Result::FRAME_PREPARE_FAILED; 151 return Result::FRAME_PREPARE_FAILED;
126 } 152 }
127 153
128 frame->frame()->mutable_updated_region()->Clear(); 154 frame->frame()->mutable_updated_region()->Clear();
129 155
130 if (DoDuplicateUnlocked(frame->context(), monitor_id, frame->frame())) { 156 if (DoDuplicateUnlocked(frame->context(), monitor_id, frame->frame())) {
157 succeeded_duplications_++;
131 return Result::SUCCEEDED; 158 return Result::SUCCEEDED;
132 } 159 }
133 if (monitor_id >= ScreenCountUnlocked()) { 160 if (monitor_id >= ScreenCountUnlocked()) {
134 // It's a user error to provide a |monitor_id| larger than screen count. We 161 // It's a user error to provide a |monitor_id| larger than screen count. We
135 // do not need to deinitialize. 162 // do not need to deinitialize.
136 return Result::INVALID_MONITOR_ID; 163 return Result::INVALID_MONITOR_ID;
137 } 164 }
138 165
139 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something 166 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something
140 // must be wrong from capturer APIs. We should Deinitialize(). 167 // must be wrong from capturer APIs. We should Deinitialize().
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 200
174 bool DxgiDuplicatorController::DoInitialize() { 201 bool DxgiDuplicatorController::DoInitialize() {
175 RTC_DCHECK(desktop_rect_.is_empty()); 202 RTC_DCHECK(desktop_rect_.is_empty());
176 RTC_DCHECK(duplicators_.empty()); 203 RTC_DCHECK(duplicators_.empty());
177 204
178 d3d_info_.min_feature_level = static_cast<D3D_FEATURE_LEVEL>(0); 205 d3d_info_.min_feature_level = static_cast<D3D_FEATURE_LEVEL>(0);
179 d3d_info_.max_feature_level = static_cast<D3D_FEATURE_LEVEL>(0); 206 d3d_info_.max_feature_level = static_cast<D3D_FEATURE_LEVEL>(0);
180 207
181 std::vector<D3dDevice> devices = D3dDevice::EnumDevices(); 208 std::vector<D3dDevice> devices = D3dDevice::EnumDevices();
182 if (devices.empty()) { 209 if (devices.empty()) {
210 LOG(LS_WARNING) << "No D3dDevice found.";
183 return false; 211 return false;
184 } 212 }
185 213
186 for (size_t i = 0; i < devices.size(); i++) { 214 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 = 215 D3D_FEATURE_LEVEL feature_level =
193 devices[i].d3d_device()->GetFeatureLevel(); 216 devices[i].d3d_device()->GetFeatureLevel();
194 if (d3d_info_.max_feature_level == 0 || 217 if (d3d_info_.max_feature_level == 0 ||
195 feature_level > d3d_info_.max_feature_level) { 218 feature_level > d3d_info_.max_feature_level) {
196 d3d_info_.max_feature_level = feature_level; 219 d3d_info_.max_feature_level = feature_level;
197 } 220 }
198 if (d3d_info_.min_feature_level == 0 || 221 if (d3d_info_.min_feature_level == 0 ||
199 feature_level < d3d_info_.min_feature_level) { 222 feature_level < d3d_info_.min_feature_level) {
200 d3d_info_.min_feature_level = feature_level; 223 d3d_info_.min_feature_level = feature_level;
201 } 224 }
202 225
226 DxgiAdapterDuplicator duplicator(devices[i]);
227 // There may be several video cards on the system, some of them may not
Sergey Ulanov 2017/06/28 01:29:59 Is this related to Session 0? If not then mention
Hzj_jie 2017/06/28 05:01:57 I cannot tell, it may happen on any session. Yes,
228 // support IDXGOutputDuplication. But they should not impact others from
229 // taking effect, so we should continually try other adapters. This usually
230 // happens when a non-official virtual adapter is installed on the system.
231 if (!duplicator.Initialize()) {
232 LOG(LS_WARNING) << "Failed to initialize DxgiAdapterDuplicator on "
233 "adapter "
234 << i;
235 continue;
236 }
237 RTC_DCHECK(!duplicator.desktop_rect().is_empty());
238 duplicators_.push_back(std::move(duplicator));
239
203 desktop_rect_.UnionWith(duplicators_.back().desktop_rect()); 240 desktop_rect_.UnionWith(duplicators_.back().desktop_rect());
204 } 241 }
205 TranslateRect(); 242 TranslateRect();
206 243
207 HDC hdc = GetDC(nullptr); 244 HDC hdc = GetDC(nullptr);
208 // Use old DPI value if failed. 245 // Use old DPI value if failed.
209 if (hdc) { 246 if (hdc) {
210 dpi_.set(GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY)); 247 dpi_.set(GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY));
211 ReleaseDC(nullptr, hdc); 248 ReleaseDC(nullptr, hdc);
212 } 249 }
213 250
214 identity_++; 251 identity_++;
215 return true; 252
253 if (duplicators_.empty()) {
254 LOG(LS_WARNING) << "Cannot initialize any DxgiAdapterDuplicator instance.";
255 }
256
257 return !duplicators_.empty();
216 } 258 }
217 259
218 void DxgiDuplicatorController::Deinitialize() { 260 void DxgiDuplicatorController::Deinitialize() {
219 desktop_rect_ = DesktopRect(); 261 desktop_rect_ = DesktopRect();
220 duplicators_.clear(); 262 duplicators_.clear();
221 resolution_change_detector_.Reset(); 263 resolution_change_detector_.Reset();
222 } 264 }
223 265
224 bool DxgiDuplicatorController::ContextExpired( 266 bool DxgiDuplicatorController::ContextExpired(
225 const Context* const context) const { 267 const Context* const context) const {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 void DxgiDuplicatorController::TranslateRect() { 432 void DxgiDuplicatorController::TranslateRect() {
391 const DesktopVector position = 433 const DesktopVector position =
392 DesktopVector().subtract(desktop_rect_.top_left()); 434 DesktopVector().subtract(desktop_rect_.top_left());
393 desktop_rect_.Translate(position); 435 desktop_rect_.Translate(position);
394 for (auto& duplicator : duplicators_) { 436 for (auto& duplicator : duplicators_) {
395 duplicator.TranslateRect(position); 437 duplicator.TranslateRect(position);
396 } 438 }
397 } 439 }
398 440
399 } // namespace webrtc 441 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698