| 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/screen_capturer_win_directx.h" | 11 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" |
| 12 | 12 |
| 13 #include <string.h> | 13 #include <utility> |
| 14 | |
| 15 #include <comdef.h> | |
| 16 #include <wincodec.h> | |
| 17 #include <wingdi.h> | |
| 18 #include <DXGI.h> | |
| 19 | 14 |
| 20 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 21 #include "webrtc/base/criticalsection.h" | 16 #include "webrtc/base/logging.h" |
| 22 #include "webrtc/base/scoped_ref_ptr.h" | |
| 23 #include "webrtc/base/timeutils.h" | 17 #include "webrtc/base/timeutils.h" |
| 24 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 18 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 25 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" | |
| 26 #include "webrtc/system_wrappers/include/atomic32.h" | |
| 27 #include "webrtc/system_wrappers/include/logging.h" | |
| 28 | 19 |
| 29 namespace webrtc { | 20 namespace webrtc { |
| 30 | 21 |
| 31 using Microsoft::WRL::ComPtr; | 22 using Microsoft::WRL::ComPtr; |
| 32 | 23 |
| 33 namespace { | 24 bool ScreenCapturerWinDirectx::IsSupported() { |
| 34 | 25 // Forward IsSupported function call to DxgiDuplicatorController. |
| 35 // Timeout for AcquireNextFrame() call. | 26 return DxgiDuplicatorController::Instance()->IsSupported(); |
| 36 const int kAcquireTimeoutMs = 10; | |
| 37 | |
| 38 // Wait time between two DuplicateOutput operations, DuplicateOutput may fail if | |
| 39 // display mode is changing. | |
| 40 const int kDuplicateOutputWaitMs = 50; | |
| 41 | |
| 42 // How many times we attempt to DuplicateOutput before returning an error to | |
| 43 // upstream components. | |
| 44 const int kDuplicateOutputAttempts = 10; | |
| 45 | |
| 46 rtc::GlobalLockPod g_initialize_lock; | |
| 47 | |
| 48 // A container of all the objects we need to call Windows API. Note, one | |
| 49 // application can only have one IDXGIOutputDuplication instance, that's the | |
| 50 // reason the container is singleton. | |
| 51 struct DxgiContainer { | |
| 52 rtc::CriticalSection duplication_lock; | |
| 53 rtc::CriticalSection acquire_lock; | |
| 54 bool initialize_result GUARDED_BY(g_initialize_lock) = false; | |
| 55 ID3D11Device* device GUARDED_BY(g_initialize_lock) = nullptr; | |
| 56 ID3D11DeviceContext* context GUARDED_BY(g_initialize_lock) = nullptr; | |
| 57 IDXGIOutput1* output1 GUARDED_BY(g_initialize_lock) = nullptr; | |
| 58 ComPtr<IDXGIOutputDuplication> duplication | |
| 59 GUARDED_BY(duplication_lock); | |
| 60 DXGI_OUTDUPL_DESC duplication_desc; | |
| 61 std::vector<uint8_t> metadata GUARDED_BY(acquire_lock); | |
| 62 }; | |
| 63 | |
| 64 DxgiContainer* g_container GUARDED_BY(g_initialize_lock); | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 // A pair of an ID3D11Texture2D and an IDXGISurface. We need an | |
| 69 // ID3D11Texture2D instance to copy GPU texture to RAM, but an IDXGISurface to | |
| 70 // map the texture into a bitmap buffer. These two instances are always | |
| 71 // pointing to a same object. | |
| 72 // This class also has two DesktopRegions, one is the updated region from | |
| 73 // returned from Windows API, the other is the region intersects with the | |
| 74 // updated region of last frame. | |
| 75 // | |
| 76 // This class is not thread safe. | |
| 77 class ScreenCapturerWinDirectx::Texture { | |
| 78 public: | |
| 79 // Copy a frame represented by frame_info and resource. Returns false if | |
| 80 // anything wrong. | |
| 81 bool CopyFrom(const DXGI_OUTDUPL_FRAME_INFO& frame_info, | |
| 82 IDXGIResource* resource, | |
| 83 const DesktopRegion& last_updated_region) { | |
| 84 if (!resource || frame_info.AccumulatedFrames == 0) { | |
| 85 // Nothing updated, but current data is still valid. | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 ComPtr<ID3D11Texture2D> texture; | |
| 90 _com_error error = resource->QueryInterface( | |
| 91 __uuidof(ID3D11Texture2D), | |
| 92 reinterpret_cast<void**>(texture.GetAddressOf())); | |
| 93 if (error.Error() != S_OK || !texture) { | |
| 94 LOG(LS_ERROR) << "Failed to convert IDXGIResource to ID3D11Texture2D, " | |
| 95 "error " | |
| 96 << error.ErrorMessage() << ", code " << error.Error(); | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 // AcquireNextFrame returns a CPU inaccessible IDXGIResource, so we need to | |
| 101 // make a copy. | |
| 102 if (!InitializeStage(texture.Get())) { | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 updated_region_.Clear(); | |
| 107 if (!DetectUpdatedRegion(frame_info, &updated_region_)) { | |
| 108 updated_region_.SetRect(DesktopRect::MakeSize(size())); | |
| 109 } | |
| 110 // We need to copy changed area in both this frame and last frame, since | |
| 111 // currently this frame stores the bitmap of the one before last frame. | |
| 112 copied_region_.Clear(); | |
| 113 copied_region_.AddRegion(updated_region_); | |
| 114 copied_region_.AddRegion(last_updated_region); | |
| 115 copied_region_.IntersectWith(DesktopRect::MakeSize(size())); | |
| 116 | |
| 117 for (DesktopRegion::Iterator it(copied_region_); | |
| 118 !it.IsAtEnd(); | |
| 119 it.Advance()) { | |
| 120 D3D11_BOX box; | |
| 121 box.left = it.rect().left(); | |
| 122 box.top = it.rect().top(); | |
| 123 box.right = it.rect().right(); | |
| 124 box.bottom = it.rect().bottom(); | |
| 125 box.front = 0; | |
| 126 box.back = 1; | |
| 127 g_container->context->CopySubresourceRegion( | |
| 128 static_cast<ID3D11Resource*>(stage_.Get()), | |
| 129 0, it.rect().left(), it.rect().top(), 0, | |
| 130 static_cast<ID3D11Resource*>(texture.Get()), | |
| 131 0, &box); | |
| 132 } | |
| 133 | |
| 134 rect_ = {0}; | |
| 135 error = _com_error(surface_->Map(&rect_, DXGI_MAP_READ)); | |
| 136 if (error.Error() != S_OK) { | |
| 137 rect_ = {0}; | |
| 138 LOG(LS_ERROR) << "Failed to map the IDXGISurface to a bitmap, error " | |
| 139 << error.ErrorMessage() << ", code " << error.Error(); | |
| 140 return false; | |
| 141 } | |
| 142 | |
| 143 // surface_->Unmap() will be called next time we capture an image to avoid | |
| 144 // memory copy without shared_memory. | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 uint8_t* bits() const { return static_cast<uint8_t*>(rect_.pBits); } | |
| 149 int pitch() const { return static_cast<int>(rect_.Pitch); } | |
| 150 const DesktopSize& size() const { return size_; } | |
| 151 const DesktopVector& dpi() const { return dpi_; } | |
| 152 | |
| 153 int32_t AddRef() { | |
| 154 return ++ref_count_; | |
| 155 } | |
| 156 | |
| 157 int32_t Release() { | |
| 158 int32_t ref_count; | |
| 159 ref_count = --ref_count_; | |
| 160 if (ref_count == 0) { | |
| 161 delete this; | |
| 162 } | |
| 163 return ref_count; | |
| 164 } | |
| 165 | |
| 166 const DesktopRegion& updated_region() { | |
| 167 return updated_region_; | |
| 168 } | |
| 169 | |
| 170 const DesktopRegion& copied_region() { | |
| 171 return copied_region_; | |
| 172 } | |
| 173 | |
| 174 private: | |
| 175 // Texture should only be deleted by Release function. | |
| 176 ~Texture() = default; | |
| 177 | |
| 178 // Initializes stage_ from a CPU inaccessible IDXGIResource. Returns false | |
| 179 // if it fails to execute windows api. | |
| 180 bool InitializeStage(ID3D11Texture2D* texture) { | |
| 181 RTC_DCHECK(texture); | |
| 182 D3D11_TEXTURE2D_DESC desc = {0}; | |
| 183 texture->GetDesc(&desc); | |
| 184 desc.BindFlags = 0; | |
| 185 desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; | |
| 186 desc.MiscFlags = 0; | |
| 187 desc.Usage = D3D11_USAGE_STAGING; | |
| 188 if (stage_) { | |
| 189 // Make sure stage_ and surface_ are always pointing to a same object. | |
| 190 // We need an ID3D11Texture2D instance for | |
| 191 // ID3D11DeviceContext::CopySubresourceRegion, but an IDXGISurface for | |
| 192 // IDXGISurface::Map. | |
| 193 { | |
| 194 ComPtr<IUnknown> left; | |
| 195 ComPtr<IUnknown> right; | |
| 196 bool left_result = SUCCEEDED(stage_.As(&left)); | |
| 197 bool right_result = SUCCEEDED(surface_.As(&right)); | |
| 198 RTC_DCHECK(left_result); | |
| 199 RTC_DCHECK(right_result); | |
| 200 RTC_DCHECK(left.Get() == right.Get()); | |
| 201 } | |
| 202 | |
| 203 // This buffer should be used already. | |
| 204 _com_error error = _com_error(surface_->Unmap()); | |
| 205 if (error.Error() == S_OK) { | |
| 206 D3D11_TEXTURE2D_DESC current_desc; | |
| 207 stage_->GetDesc(¤t_desc); | |
| 208 if (memcmp(&desc, ¤t_desc, sizeof(D3D11_TEXTURE2D_DESC)) == 0) { | |
| 209 return true; | |
| 210 } | |
| 211 } else { | |
| 212 // Let's recreate stage_ and surface_ later. | |
| 213 LOG(LS_ERROR) << "Failed to unmap surface, error " | |
| 214 << error.ErrorMessage() << ", code " << error.Error(); | |
| 215 } | |
| 216 | |
| 217 stage_.Reset(); | |
| 218 surface_.Reset(); | |
| 219 } else { | |
| 220 RTC_DCHECK(!surface_); | |
| 221 } | |
| 222 | |
| 223 HDC hdc = GetDC(nullptr); | |
| 224 // Use old DPI value if failed. | |
| 225 if (hdc != nullptr) { | |
| 226 dpi_.set(GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY)); | |
| 227 ReleaseDC(nullptr, hdc); | |
| 228 } | |
| 229 | |
| 230 _com_error error = _com_error(g_container->device->CreateTexture2D( | |
| 231 &desc, nullptr, stage_.GetAddressOf())); | |
| 232 if (error.Error() != S_OK || !stage_) { | |
| 233 LOG(LS_ERROR) << "Failed to create a new ID3D11Texture2D as stage, " | |
| 234 "error " | |
| 235 << error.ErrorMessage() << ", code " << error.Error(); | |
| 236 return false; | |
| 237 } | |
| 238 | |
| 239 error = _com_error(stage_.As(&surface_)); | |
| 240 if (error.Error() != S_OK || !surface_) { | |
| 241 LOG(LS_ERROR) << "Failed to convert ID3D11Texture2D to IDXGISurface, " | |
| 242 "error " | |
| 243 << error.ErrorMessage() << ", code " << error.Error(); | |
| 244 return false; | |
| 245 } | |
| 246 | |
| 247 size_.set(desc.Width, desc.Height); | |
| 248 return true; | |
| 249 } | |
| 250 | |
| 251 ComPtr<ID3D11Texture2D> stage_; | |
| 252 ComPtr<IDXGISurface> surface_; | |
| 253 DXGI_MAPPED_RECT rect_; | |
| 254 DesktopSize size_; | |
| 255 Atomic32 ref_count_; | |
| 256 // The updated region from Windows API. | |
| 257 DesktopRegion updated_region_; | |
| 258 // Combination of updated regions from both current frame and previous frame. | |
| 259 DesktopRegion copied_region_; | |
| 260 // The DPI of current frame. | |
| 261 DesktopVector dpi_; | |
| 262 }; | |
| 263 | |
| 264 // A DesktopFrame which does not own the data buffer, and also does not have | |
| 265 // shared memory. This uses in IT2ME scenario only. | |
| 266 class ScreenCapturerWinDirectx::DxgiDesktopFrame : public DesktopFrame { | |
| 267 public: | |
| 268 DxgiDesktopFrame( | |
| 269 const rtc::scoped_refptr<ScreenCapturerWinDirectx::Texture>& texture) | |
| 270 : DesktopFrame(texture.get()->size(), | |
| 271 texture.get()->pitch(), | |
| 272 texture.get()->bits(), | |
| 273 nullptr), | |
| 274 texture_(texture) { | |
| 275 set_dpi(texture->dpi()); | |
| 276 } | |
| 277 | |
| 278 virtual ~DxgiDesktopFrame() {} | |
| 279 | |
| 280 private: | |
| 281 // Keep a reference to the Texture instance to make sure we can still access | |
| 282 // its bytes array. | |
| 283 rtc::scoped_refptr<ScreenCapturerWinDirectx::Texture> texture_; | |
| 284 }; | |
| 285 | |
| 286 bool ScreenCapturerWinDirectx::Initialize() { | |
| 287 if (!g_container) { | |
| 288 rtc::GlobalLockScope lock(&g_initialize_lock); | |
| 289 if (!g_container) { | |
| 290 g_container = new DxgiContainer(); | |
| 291 g_container->initialize_result = DoInitialize(); | |
| 292 if (g_container->initialize_result) { | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 296 // Clean up if DirectX cannot work on the system. | |
| 297 if (g_container->duplication) { | |
| 298 g_container->duplication.Reset(); | |
| 299 } | |
| 300 | |
| 301 if (g_container->output1) { | |
| 302 g_container->output1->Release(); | |
| 303 g_container->output1 = nullptr; | |
| 304 } | |
| 305 | |
| 306 if (g_container->context) { | |
| 307 g_container->context->Release(); | |
| 308 g_container->context = nullptr; | |
| 309 } | |
| 310 | |
| 311 if (g_container->device) { | |
| 312 g_container->device->Release(); | |
| 313 g_container->device = nullptr; | |
| 314 } | |
| 315 | |
| 316 return false; | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 return g_container->initialize_result; | |
| 321 } | |
| 322 | |
| 323 bool ScreenCapturerWinDirectx::DoInitialize() { | |
| 324 D3D_FEATURE_LEVEL feature_level; | |
| 325 _com_error error = D3D11CreateDevice( | |
| 326 nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, | |
| 327 D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED, | |
| 328 nullptr, 0, D3D11_SDK_VERSION, &g_container->device, &feature_level, | |
| 329 &g_container->context); | |
| 330 if (error.Error() != S_OK || !g_container->device || !g_container->context) { | |
| 331 LOG(LS_WARNING) << "D3D11CreateDeivce returns error " | |
| 332 << error.ErrorMessage() << " with code " << error.Error(); | |
| 333 return false; | |
| 334 } | |
| 335 | |
| 336 if (feature_level < D3D_FEATURE_LEVEL_11_0) { | |
| 337 LOG(LS_WARNING) << "D3D11CreateDevice returns an instance without DirectX " | |
| 338 "11 support, level " | |
| 339 << feature_level; | |
| 340 return false; | |
| 341 } | |
| 342 | |
| 343 ComPtr<IDXGIDevice> device; | |
| 344 error = _com_error(g_container->device->QueryInterface( | |
| 345 __uuidof(IDXGIDevice), reinterpret_cast<void**>(device.GetAddressOf()))); | |
| 346 if (error.Error() != S_OK || !device) { | |
| 347 LOG(LS_WARNING) << "ID3D11Device is not an implementation of IDXGIDevice, " | |
| 348 "this usually means the system does not support DirectX " | |
| 349 "11"; | |
| 350 return false; | |
| 351 } | |
| 352 | |
| 353 ComPtr<IDXGIAdapter> adapter; | |
| 354 error = _com_error(device->GetAdapter(adapter.GetAddressOf())); | |
| 355 if (error.Error() != S_OK || !adapter) { | |
| 356 LOG(LS_WARNING) << "Failed to get an IDXGIAdapter implementation from " | |
| 357 "IDXGIDevice."; | |
| 358 return false; | |
| 359 } | |
| 360 | |
| 361 ComPtr<IDXGIOutput> output; | |
| 362 for (int i = 0;; i++) { | |
| 363 error = _com_error(adapter->EnumOutputs(i, output.GetAddressOf())); | |
| 364 if (error.Error() == DXGI_ERROR_NOT_FOUND) { | |
| 365 LOG(LS_WARNING) << "No output detected."; | |
| 366 return false; | |
| 367 } | |
| 368 if (error.Error() == S_OK && output) { | |
| 369 DXGI_OUTPUT_DESC desc; | |
| 370 error = _com_error(output->GetDesc(&desc)); | |
| 371 if (error.Error() == S_OK) { | |
| 372 if (desc.AttachedToDesktop) { | |
| 373 // Current output instance is the device attached to desktop. | |
| 374 break; | |
| 375 } | |
| 376 } else { | |
| 377 LOG(LS_WARNING) << "Failed to get output description of device " << i | |
| 378 << ", ignore."; | |
| 379 } | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 RTC_DCHECK(output); | |
| 384 error = _com_error(output.CopyTo( | |
| 385 __uuidof(IDXGIOutput1), reinterpret_cast<void**>(&g_container->output1))); | |
| 386 if (error.Error() != S_OK || !g_container->output1) { | |
| 387 LOG(LS_WARNING) << "Failed to convert IDXGIOutput to IDXGIOutput1, this " | |
| 388 "usually means the system does not support DirectX 11"; | |
| 389 return false; | |
| 390 } | |
| 391 | |
| 392 // When we are initializing the DXGI, retrying several times to avoid any | |
| 393 // temporary issue, such as display mode changing, to block us from using | |
| 394 // DXGI based capturer. | |
| 395 for (int i = 0; i < kDuplicateOutputAttempts; i++) { | |
| 396 if (DuplicateOutput()) { | |
| 397 return true; | |
| 398 } | |
| 399 Sleep(kDuplicateOutputWaitMs); | |
| 400 } | |
| 401 return false; | |
| 402 } | |
| 403 | |
| 404 bool ScreenCapturerWinDirectx::DuplicateOutput() { | |
| 405 // We are updating the instance. | |
| 406 rtc::CritScope lock(&g_container->duplication_lock); | |
| 407 // Make sure nobody is using current instance. | |
| 408 rtc::CritScope lock2(&g_container->acquire_lock); | |
| 409 if (g_container->duplication) { | |
| 410 return true; | |
| 411 } | |
| 412 | |
| 413 _com_error error = g_container->output1->DuplicateOutput( | |
| 414 static_cast<IUnknown*>(g_container->device), | |
| 415 g_container->duplication.GetAddressOf()); | |
| 416 if (error.Error() != S_OK || !g_container->duplication) { | |
| 417 g_container->duplication.Reset(); | |
| 418 LOG(LS_WARNING) << "Failed to duplicate output from IDXGIOutput1, error " | |
| 419 << error.ErrorMessage() << ", with code " | |
| 420 << error.Error(); | |
| 421 return false; | |
| 422 } | |
| 423 | |
| 424 memset(&g_container->duplication_desc, 0, sizeof(DXGI_OUTDUPL_DESC)); | |
| 425 g_container->duplication->GetDesc(&g_container->duplication_desc); | |
| 426 if (g_container->duplication_desc.ModeDesc.Format != | |
| 427 DXGI_FORMAT_B8G8R8A8_UNORM) { | |
| 428 g_container->duplication.Reset(); | |
| 429 LOG(LS_ERROR) << "IDXGIDuplicateOutput does not use RGBA (8 bit) " | |
| 430 "format, which is required by downstream components, " | |
| 431 "format is " | |
| 432 << g_container->duplication_desc.ModeDesc.Format; | |
| 433 return false; | |
| 434 } | |
| 435 | |
| 436 return true; | |
| 437 } | |
| 438 | |
| 439 bool ScreenCapturerWinDirectx::ForceDuplicateOutput() { | |
| 440 // We are updating the instance. | |
| 441 rtc::CritScope lock(&g_container->duplication_lock); | |
| 442 // Make sure nobody is using current instance. | |
| 443 rtc::CritScope lock2(&g_container->acquire_lock); | |
| 444 | |
| 445 if (g_container->duplication) { | |
| 446 g_container->duplication->ReleaseFrame(); | |
| 447 g_container->duplication.Reset(); | |
| 448 } | |
| 449 | |
| 450 return DuplicateOutput(); | |
| 451 } | 27 } |
| 452 | 28 |
| 453 ScreenCapturerWinDirectx::ScreenCapturerWinDirectx( | 29 ScreenCapturerWinDirectx::ScreenCapturerWinDirectx( |
| 454 const DesktopCaptureOptions& options) | 30 const DesktopCaptureOptions& options) |
| 455 : callback_(nullptr) { | 31 : callback_(nullptr) {} |
| 456 RTC_DCHECK(g_container && g_container->initialize_result); | |
| 457 | |
| 458 // Texture instance won't change forever. | |
| 459 while (!surfaces_.current_frame()) { | |
| 460 surfaces_.ReplaceCurrentFrame(std::unique_ptr<rtc::scoped_refptr<Texture>>( | |
| 461 new rtc::scoped_refptr<Texture>(new Texture()))); | |
| 462 surfaces_.MoveToNextFrame(); | |
| 463 } | |
| 464 } | |
| 465 | 32 |
| 466 ScreenCapturerWinDirectx::~ScreenCapturerWinDirectx() {} | 33 ScreenCapturerWinDirectx::~ScreenCapturerWinDirectx() {} |
| 467 | 34 |
| 468 void ScreenCapturerWinDirectx::Start(Callback* callback) { | 35 void ScreenCapturerWinDirectx::Start(Callback* callback) { |
| 469 RTC_DCHECK(!callback_); | 36 RTC_DCHECK(!callback_); |
| 470 RTC_DCHECK(callback); | 37 RTC_DCHECK(callback); |
| 471 | 38 |
| 472 callback_ = callback; | 39 callback_ = callback; |
| 473 } | 40 } |
| 474 | 41 |
| 475 void ScreenCapturerWinDirectx::SetSharedMemoryFactory( | 42 void ScreenCapturerWinDirectx::SetSharedMemoryFactory( |
| 476 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { | 43 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) { |
| 477 shared_memory_factory_ = std::move(shared_memory_factory); | 44 shared_memory_factory_ = std::move(shared_memory_factory); |
| 478 } | 45 } |
| 479 | 46 |
| 480 bool ScreenCapturerWinDirectx::HandleDetectUpdatedRegionError( | 47 DesktopSize ScreenCapturerWinDirectx::SelectedDesktopSize() const { |
| 481 const _com_error& error, | 48 if (current_screen_id == kFullDesktopScreenId) { |
| 482 const char* stage) { | 49 return DxgiDuplicatorController::Instance()->desktop_size(); |
| 483 if (error.Error() != S_OK) { | |
| 484 if (error.Error() == DXGI_ERROR_ACCESS_LOST) { | |
| 485 ForceDuplicateOutput(); | |
| 486 } else { | |
| 487 LOG(LS_ERROR) << "Failed to get " << stage << " rectangles, error " | |
| 488 << error.ErrorMessage() << ", code " << error.Error(); | |
| 489 } | |
| 490 // Send entire desktop as we cannot get dirty or move rectangles. | |
| 491 return false; | |
| 492 } | 50 } |
| 493 | 51 return DxgiDuplicatorController::Instance() |
| 494 return true; | 52 ->ScreenRect(current_screen_id) |
| 495 } | 53 .size(); |
| 496 | |
| 497 bool ScreenCapturerWinDirectx::DetectUpdatedRegion( | |
| 498 const DXGI_OUTDUPL_FRAME_INFO& frame_info, | |
| 499 DesktopRegion* updated_region) { | |
| 500 RTC_DCHECK(g_container->duplication); | |
| 501 RTC_DCHECK(updated_region); | |
| 502 updated_region->Clear(); | |
| 503 if (frame_info.TotalMetadataBufferSize == 0) { | |
| 504 // This should not happen, since frame_info.AccumulatedFrames > 0. | |
| 505 LOG(LS_ERROR) << "frame_info.AccumulatedFrames > 0, " | |
| 506 "but TotalMetadataBufferSize == 0"; | |
| 507 return false; | |
| 508 } | |
| 509 | |
| 510 if (g_container->metadata.capacity() < frame_info.TotalMetadataBufferSize) { | |
| 511 g_container->metadata.clear(); // Avoid data copy | |
| 512 g_container->metadata.reserve(frame_info.TotalMetadataBufferSize); | |
| 513 } | |
| 514 | |
| 515 UINT buff_size = 0; | |
| 516 DXGI_OUTDUPL_MOVE_RECT* move_rects = | |
| 517 reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(g_container->metadata.data()); | |
| 518 size_t move_rects_count = 0; | |
| 519 _com_error error = _com_error(g_container->duplication->GetFrameMoveRects( | |
| 520 static_cast<UINT>(g_container->metadata.capacity()), | |
| 521 move_rects, &buff_size)); | |
| 522 if (!HandleDetectUpdatedRegionError(error, "move")) { | |
| 523 return false; | |
| 524 } | |
| 525 move_rects_count = buff_size / sizeof(DXGI_OUTDUPL_MOVE_RECT); | |
| 526 | |
| 527 RECT* dirty_rects = | |
| 528 reinterpret_cast<RECT*>(g_container->metadata.data() + buff_size); | |
| 529 size_t dirty_rects_count = 0; | |
| 530 error = _com_error(g_container->duplication->GetFrameDirtyRects( | |
| 531 static_cast<UINT>(g_container->metadata.capacity()) - buff_size, | |
| 532 dirty_rects, &buff_size)); | |
| 533 if (!HandleDetectUpdatedRegionError(error, "dirty")) { | |
| 534 return false; | |
| 535 } | |
| 536 dirty_rects_count = buff_size / sizeof(RECT); | |
| 537 | |
| 538 while (move_rects_count > 0) { | |
| 539 updated_region->AddRect(DesktopRect::MakeXYWH( | |
| 540 move_rects->SourcePoint.x, move_rects->SourcePoint.y, | |
| 541 move_rects->DestinationRect.right - move_rects->DestinationRect.left, | |
| 542 move_rects->DestinationRect.bottom - move_rects->DestinationRect.top)); | |
| 543 updated_region->AddRect(DesktopRect::MakeLTRB( | |
| 544 move_rects->DestinationRect.left, move_rects->DestinationRect.top, | |
| 545 move_rects->DestinationRect.right, move_rects->DestinationRect.bottom)); | |
| 546 move_rects++; | |
| 547 move_rects_count--; | |
| 548 } | |
| 549 | |
| 550 while (dirty_rects_count > 0) { | |
| 551 updated_region->AddRect(DesktopRect::MakeLTRB( | |
| 552 dirty_rects->left, dirty_rects->top, | |
| 553 dirty_rects->right, dirty_rects->bottom)); | |
| 554 dirty_rects++; | |
| 555 dirty_rects_count--; | |
| 556 } | |
| 557 | |
| 558 return true; | |
| 559 } | |
| 560 | |
| 561 std::unique_ptr<DesktopFrame> ScreenCapturerWinDirectx::ProcessFrame( | |
| 562 const DXGI_OUTDUPL_FRAME_INFO& frame_info, | |
| 563 IDXGIResource* resource) { | |
| 564 RTC_DCHECK(resource); | |
| 565 RTC_DCHECK(frame_info.AccumulatedFrames > 0); | |
| 566 // We have something to update, so move to next surface. | |
| 567 surfaces_.MoveToNextFrame(); | |
| 568 if (shared_memory_factory_) { | |
| 569 // Make sure frames_ and surfaces_ are synchronized if we are using both. | |
| 570 frames_.MoveToNextFrame(); | |
| 571 } | |
| 572 RTC_DCHECK(surfaces_.current_frame()); | |
| 573 if (!surfaces_.current_frame()->get()->CopyFrom(frame_info, resource, | |
| 574 surfaces_.previous_frame()->get()->updated_region())) { | |
| 575 return std::unique_ptr<DesktopFrame>(); | |
| 576 } | |
| 577 | |
| 578 std::unique_ptr<DesktopFrame> result; | |
| 579 if (shared_memory_factory_) { | |
| 580 // When using shared memory, |frames_| is used to store a queue of | |
| 581 // SharedMemoryDesktopFrame's. | |
| 582 if (!frames_.current_frame() || | |
| 583 !frames_.current_frame()->size().equals( | |
| 584 surfaces_.current_frame()->get()->size())) { | |
| 585 // Current frame does not have a same size as last captured surface. | |
| 586 std::unique_ptr<DesktopFrame> new_frame = | |
| 587 SharedMemoryDesktopFrame::Create( | |
| 588 surfaces_.current_frame()->get()->size(), | |
| 589 shared_memory_factory_.get()); | |
| 590 if (!new_frame) { | |
| 591 LOG(LS_ERROR) << "Failed to allocate a new SharedMemoryDesktopFrame"; | |
| 592 return std::unique_ptr<DesktopFrame>(); | |
| 593 } | |
| 594 frames_.ReplaceCurrentFrame( | |
| 595 SharedDesktopFrame::Wrap(std::move(new_frame))); | |
| 596 } | |
| 597 result = frames_.current_frame()->Share(); | |
| 598 | |
| 599 std::unique_ptr<DesktopFrame> frame( | |
| 600 new DxgiDesktopFrame(*surfaces_.current_frame())); | |
| 601 // Copy data into SharedMemory. | |
| 602 for (DesktopRegion::Iterator it( | |
| 603 surfaces_.current_frame()->get()->copied_region()); | |
| 604 !it.IsAtEnd(); | |
| 605 it.Advance()) { | |
| 606 result->CopyPixelsFrom(*frame, it.rect().top_left(), it.rect()); | |
| 607 } | |
| 608 result->set_dpi(frame->dpi()); | |
| 609 } else { | |
| 610 result.reset(new DxgiDesktopFrame(*surfaces_.current_frame())); | |
| 611 } | |
| 612 RTC_DCHECK(result); | |
| 613 *result->mutable_updated_region() = | |
| 614 surfaces_.current_frame()->get()->updated_region(); | |
| 615 return result; | |
| 616 } | 54 } |
| 617 | 55 |
| 618 void ScreenCapturerWinDirectx::Capture(const DesktopRegion& region) { | 56 void ScreenCapturerWinDirectx::Capture(const DesktopRegion& region) { |
| 619 RTC_DCHECK(callback_); | 57 RTC_DCHECK(callback_); |
| 620 | 58 |
| 621 if (!g_container->duplication && !DuplicateOutput()) { | 59 int64_t capture_start_time_nanos = rtc::TimeNanos(); |
| 622 // Failed to initialize desktop duplication. This usually happens when | 60 |
| 623 // Windows is switching display mode. Retrying later usually resolves the | 61 frames_.MoveToNextFrame(); |
| 624 // issue. | 62 if (!frames_.current_frame()) { |
| 625 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); | 63 std::unique_ptr<DesktopFrame> new_frame; |
| 626 return; | 64 if (shared_memory_factory_) { |
| 65 new_frame = SharedMemoryDesktopFrame::Create( |
| 66 SelectedDesktopSize(), shared_memory_factory_.get()); |
| 67 } else { |
| 68 new_frame.reset(new BasicDesktopFrame(SelectedDesktopSize())); |
| 69 } |
| 70 if (!new_frame) { |
| 71 LOG(LS_ERROR) << "Failed to allocate a new DesktopFrame."; |
| 72 // This usually means we do not have enough memory or SharedMemoryFactory |
| 73 // cannot work correctly. |
| 74 callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); |
| 75 return; |
| 76 } |
| 77 frames_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(std::move(new_frame))); |
| 627 } | 78 } |
| 628 | 79 |
| 629 RTC_DCHECK(g_container->duplication); | 80 if (current_screen_id == kFullDesktopScreenId) { |
| 630 int64_t capture_start_time_nanos = rtc::TimeNanos(); | 81 if (!DxgiDuplicatorController::Instance()->Duplicate( |
| 631 | 82 &context_, frames_.previous_frame(), frames_.current_frame())) { |
| 632 DXGI_OUTDUPL_FRAME_INFO frame_info; | 83 // Screen size may be changed, so we need to reset the frames. |
| 633 memset(&frame_info, 0, sizeof(DXGI_OUTDUPL_FRAME_INFO)); | 84 frames_.Reset(); |
| 634 ComPtr<IDXGIResource> resource; | 85 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); |
| 635 rtc::CritScope lock(&g_container->acquire_lock); | 86 return; |
| 636 _com_error error = g_container->duplication->AcquireNextFrame( | 87 } |
| 637 kAcquireTimeoutMs, &frame_info, resource.GetAddressOf()); | 88 } else { |
| 638 | 89 if (!DxgiDuplicatorController::Instance()->DuplicateMonitor( |
| 639 if (error.Error() == DXGI_ERROR_WAIT_TIMEOUT) { | 90 &context_, current_screen_id, frames_.previous_frame(), |
| 640 // Nothing changed. | 91 frames_.current_frame())) { |
| 641 EmitCurrentFrame(); | 92 // Screen size may be changed, so we need to reset the frames. |
| 642 return; | 93 frames_.Reset(); |
| 94 if (current_screen_id >= |
| 95 DxgiDuplicatorController::Instance()->ScreenCount()) { |
| 96 // Current monitor has been removed from the system. |
| 97 callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); |
| 98 } else { |
| 99 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); |
| 100 } |
| 101 return; |
| 102 } |
| 643 } | 103 } |
| 644 | 104 |
| 645 if (error.Error() != S_OK) { | 105 std::unique_ptr<DesktopFrame> result = frames_.current_frame()->Share(); |
| 646 LOG(LS_ERROR) << "Failed to capture frame, error " << error.ErrorMessage() | |
| 647 << ", code " << error.Error(); | |
| 648 if (ForceDuplicateOutput()) { | |
| 649 EmitCurrentFrame(); | |
| 650 } else { | |
| 651 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); | |
| 652 } | |
| 653 return; | |
| 654 } | |
| 655 | |
| 656 if (frame_info.AccumulatedFrames == 0) { | |
| 657 g_container->duplication->ReleaseFrame(); | |
| 658 EmitCurrentFrame(); | |
| 659 return; | |
| 660 } | |
| 661 | |
| 662 // Everything looks good so far, build next frame. | |
| 663 std::unique_ptr<DesktopFrame> result = | |
| 664 ProcessFrame(frame_info, resource.Get()); | |
| 665 // DetectUpdatedRegion may release last g_container->duplication. But | |
| 666 // ForctDuplicateOutput function will always release last frame, so there is | |
| 667 // no potential leak. | |
| 668 if (g_container->duplication) { | |
| 669 g_container->duplication->ReleaseFrame(); | |
| 670 } | |
| 671 if (!result) { | |
| 672 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); | |
| 673 return; | |
| 674 } | |
| 675 | |
| 676 result->set_capture_time_ms( | 106 result->set_capture_time_ms( |
| 677 (rtc::TimeNanos() - capture_start_time_nanos) / | 107 (rtc::TimeNanos() - capture_start_time_nanos) / |
| 678 rtc::kNumNanosecsPerMillisec); | 108 rtc::kNumNanosecsPerMillisec); |
| 679 callback_->OnCaptureResult(Result::SUCCESS, std::move(result)); | 109 callback_->OnCaptureResult(Result::SUCCESS, std::move(result)); |
| 680 } | 110 } |
| 681 | 111 |
| 682 bool ScreenCapturerWinDirectx::GetScreenList(ScreenList* screens) { | 112 bool ScreenCapturerWinDirectx::GetScreenList(ScreenList* screens) { |
| 113 int screen_count = DxgiDuplicatorController::Instance()->ScreenCount(); |
| 114 for (int i = 0; i < screen_count; i++) { |
| 115 screens->push_back(Screen{i}); |
| 116 } |
| 683 return true; | 117 return true; |
| 684 } | 118 } |
| 685 | 119 |
| 686 bool ScreenCapturerWinDirectx::SelectScreen(ScreenId id) { | 120 bool ScreenCapturerWinDirectx::SelectScreen(ScreenId id) { |
| 687 // Only full desktop capture is supported. | 121 if (id == current_screen_id) { |
| 688 return id == kFullDesktopScreenId; | 122 return true; |
| 689 } | |
| 690 | |
| 691 void ScreenCapturerWinDirectx::EmitCurrentFrame() { | |
| 692 if (!surfaces_.current_frame()->get()->bits()) { | |
| 693 // At the very begining, we have not captured any frames. | |
| 694 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); | |
| 695 return; | |
| 696 } | 123 } |
| 697 | 124 |
| 698 if (shared_memory_factory_) { | 125 // Changing target screen may or may not impact frame size. So resetting |
| 699 // If shared_memory_factory_ is provided, last frame is stored in frames_ | 126 // frames only when a Duplicate() function call returns false. |
| 700 // queue. If there is not an existing frame (at the very begining), we can | 127 if (id == kFullDesktopScreenId) { |
| 701 // only return a nullptr. | 128 current_screen_id = id; |
| 702 if (frames_.current_frame()) { | 129 return true; |
| 703 std::unique_ptr<SharedDesktopFrame> frame = | |
| 704 frames_.current_frame()->Share(); | |
| 705 frame->mutable_updated_region()->Clear(); | |
| 706 callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); | |
| 707 } else { | |
| 708 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); | |
| 709 } | |
| 710 return; | |
| 711 } | 130 } |
| 712 | 131 |
| 713 // If there is no shared_memory_factory_, last frame is stored in surfaces_ | 132 int screen_count = DxgiDuplicatorController::Instance()->ScreenCount(); |
| 714 // queue. | 133 if (id >= 0 && id < screen_count) { |
| 715 std::unique_ptr<DesktopFrame> frame( | 134 current_screen_id = id; |
| 716 new DxgiDesktopFrame(*surfaces_.current_frame())); | 135 return true; |
| 717 callback_->OnCaptureResult(Result::SUCCESS, std::move(frame)); | 136 } |
| 137 return false; |
| 718 } | 138 } |
| 719 | 139 |
| 720 } // namespace webrtc | 140 } // namespace webrtc |
| OLD | NEW |