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

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

Issue 1845113002: DirectX based screen capturer logic (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Recreate DXGIDuplicateOutput if AcquireNextFrame does not return a known error code. Created 4 years, 7 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
« no previous file with comments | « webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
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.
9 */
10
11 #include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h"
12
13 #include <string.h>
14
15 #include <comdef.h>
16 #include <wincodec.h>
17 #include <wingdi.h>
18 #include <DXGI.h>
19
20 #include "webrtc/base/checks.h"
21 #include "webrtc/base/criticalsection.h"
22 #include "webrtc/base/scoped_ref_ptr.h"
23 #include "webrtc/base/timeutils.h"
24 #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
29 namespace webrtc {
30
31 using Microsoft::WRL::ComPtr;
32
33 namespace {
34
35 // Timeout for AcquireNextFrame() call.
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(&current_desc);
208 if (memcmp(&desc, &current_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 memset(&g_container->duplication_desc, 0, sizeof(DXGI_OUTDUPL_DESC));
418 g_container->duplication->GetDesc(&g_container->duplication_desc);
419 if (g_container->duplication_desc.ModeDesc.Format !=
420 DXGI_FORMAT_B8G8R8A8_UNORM) {
421 g_container->duplication.Reset();
422 LOG(LS_ERROR) << "IDXGIDuplicateOutput does not use RGBA (8 bit) "
423 "format, which is required by downstream components, "
424 "format is "
425 << g_container->duplication_desc.ModeDesc.Format;
426 return false;
427 }
428 return true;
429 } else {
430 // Make sure we have correct signal and duplicate the output next time.
431 g_container->duplication.Reset();
432 LOG(LS_WARNING) << "Failed to duplicate output from IDXGIOutput1, error "
433 << error.ErrorMessage() << ", with code "
434 << error.Error();
435 }
436
437 return false;
438 }
439
440 bool ScreenCapturerWinDirectx::ForceDuplicateOutput() {
441 // We are updating the instance.
442 rtc::CritScope lock(&g_container->duplication_lock);
443 // Make sure nobody is using current instance.
444 rtc::CritScope lock2(&g_container->acquire_lock);
445
446 if (g_container->duplication) {
447 g_container->duplication->ReleaseFrame();
448 g_container->duplication.Reset();
449 }
450
451 return DuplicateOutput();
452 }
453
454 ScreenCapturerWinDirectx::ScreenCapturerWinDirectx(
455 const DesktopCaptureOptions& options)
456 : callback_(nullptr), set_thread_execution_state_failed_(false) {
457 RTC_DCHECK(g_container && g_container->initialize_result);
458
459 // Texture instance won't change forever.
460 while (!surfaces_.current_frame()) {
461 surfaces_.ReplaceCurrentFrame(
462 new rtc::scoped_refptr<Texture>(new Texture()));
463 surfaces_.MoveToNextFrame();
464 }
465 }
466
467 ScreenCapturerWinDirectx::~ScreenCapturerWinDirectx() {}
468
469 void ScreenCapturerWinDirectx::Start(Callback* callback) {
470 RTC_DCHECK(!callback_);
471 RTC_DCHECK(callback);
472
473 callback_ = callback;
474 }
475
476 void ScreenCapturerWinDirectx::SetSharedMemoryFactory(
477 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
478 shared_memory_factory_ = std::move(shared_memory_factory);
479 }
480
481 bool ScreenCapturerWinDirectx::HandleDetectUpdatedRegionError(
482 const _com_error& error,
483 const char* stage) {
484 if (error.Error() != S_OK) {
485 if (error.Error() == DXGI_ERROR_ACCESS_LOST) {
486 ForceDuplicateOutput();
487 } else {
488 LOG(LS_ERROR) << "Failed to get " << stage << " rectangles, error "
489 << error.ErrorMessage() << ", code " << error.Error();
490 }
491 // Send entire desktop as we cannot get dirty or move rectangles.
492 return false;
493 }
494
495 return true;
496 }
497
498 bool ScreenCapturerWinDirectx::DetectUpdatedRegion(
499 const DXGI_OUTDUPL_FRAME_INFO& frame_info,
500 DesktopRegion* updated_region) {
501 RTC_DCHECK(g_container->duplication);
502 RTC_DCHECK(updated_region);
503 updated_region->Clear();
504 if (frame_info.TotalMetadataBufferSize == 0) {
505 // This should not happen, since frame_info.AccumulatedFrames > 0.
506 LOG(LS_ERROR) << "frame_info.AccumulatedFrames > 0, "
507 "but TotalMetadataBufferSize == 0";
508 return false;
509 }
510
511 if (g_container->metadata.capacity() < frame_info.TotalMetadataBufferSize) {
512 g_container->metadata.clear(); // Avoid data copy
513 g_container->metadata.reserve(frame_info.TotalMetadataBufferSize);
514 }
515
516 UINT buff_size = 0;
517 DXGI_OUTDUPL_MOVE_RECT* move_rects =
518 reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(g_container->metadata.data());
519 size_t move_rects_count = 0;
520 _com_error error = _com_error(g_container->duplication->GetFrameMoveRects(
521 static_cast<UINT>(g_container->metadata.capacity()),
522 move_rects, &buff_size));
523 if (!HandleDetectUpdatedRegionError(error, "move")) {
524 return false;
525 }
526 move_rects_count = buff_size / sizeof(DXGI_OUTDUPL_MOVE_RECT);
527
528 RECT* dirty_rects =
529 reinterpret_cast<RECT*>(g_container->metadata.data() + buff_size);
530 size_t dirty_rects_count = 0;
531 error = _com_error(g_container->duplication->GetFrameDirtyRects(
532 static_cast<UINT>(g_container->metadata.capacity()) - buff_size,
533 dirty_rects, &buff_size));
534 if (!HandleDetectUpdatedRegionError(error, "dirty")) {
535 return false;
536 }
537 dirty_rects_count = buff_size / sizeof(RECT);
538
539 while (move_rects_count > 0) {
540 updated_region->AddRect(DesktopRect::MakeXYWH(
541 move_rects->SourcePoint.x, move_rects->SourcePoint.y,
542 move_rects->DestinationRect.right - move_rects->DestinationRect.left,
543 move_rects->DestinationRect.bottom - move_rects->DestinationRect.top));
544 updated_region->AddRect(DesktopRect::MakeLTRB(
545 move_rects->DestinationRect.left, move_rects->DestinationRect.top,
546 move_rects->DestinationRect.right, move_rects->DestinationRect.bottom));
547 move_rects++;
548 move_rects_count--;
549 }
550
551 while (dirty_rects_count > 0) {
552 updated_region->AddRect(DesktopRect::MakeLTRB(
553 dirty_rects->left, dirty_rects->top,
554 dirty_rects->right, dirty_rects->bottom));
555 dirty_rects++;
556 dirty_rects_count--;
557 }
558
559 return true;
560 }
561
562 std::unique_ptr<DesktopFrame> ScreenCapturerWinDirectx::ProcessFrame(
563 const DXGI_OUTDUPL_FRAME_INFO& frame_info,
564 IDXGIResource* resource) {
565 RTC_DCHECK(resource);
566 RTC_DCHECK(frame_info.AccumulatedFrames > 0);
567 // We have something to update, so move to next surface.
568 surfaces_.MoveToNextFrame();
569 if (shared_memory_factory_) {
570 // Make sure frames_ and surfaces_ are synchronized if we are using both.
571 frames_.MoveToNextFrame();
572 }
573 RTC_DCHECK(surfaces_.current_frame());
574 if (!surfaces_.current_frame()->get()->CopyFrom(frame_info, resource,
575 surfaces_.previous_frame()->get()->updated_region())) {
576 return std::unique_ptr<DesktopFrame>();
577 }
578
579 std::unique_ptr<DesktopFrame> result;
580 if (shared_memory_factory_) {
581 // When using shared memory, |frames_| is used to store a queue of
582 // SharedMemoryDesktopFrame's.
583 if (!frames_.current_frame() ||
584 !frames_.current_frame()->size().equals(
585 surfaces_.current_frame()->get()->size())) {
586 // Current frame does not have a same size as last captured surface.
587 std::unique_ptr<DesktopFrame> new_frame =
588 SharedMemoryDesktopFrame::Create(
589 surfaces_.current_frame()->get()->size(),
590 shared_memory_factory_.get());
591 if (!new_frame) {
592 LOG(LS_ERROR) << "Failed to allocate a new SharedMemoryDesktopFrame";
593 return std::unique_ptr<DesktopFrame>();
594 }
595 frames_.ReplaceCurrentFrame(
596 SharedDesktopFrame::Wrap(new_frame.release()));
597 }
598 result.reset(frames_.current_frame()->Share());
599
600 std::unique_ptr<DesktopFrame> frame(
601 new DxgiDesktopFrame(*surfaces_.current_frame()));
602 // Copy data into SharedMemory.
603 for (DesktopRegion::Iterator it(
604 surfaces_.current_frame()->get()->copied_region());
605 !it.IsAtEnd();
606 it.Advance()) {
607 result->CopyPixelsFrom(*frame, it.rect().top_left(), it.rect());
608 }
609 result->set_dpi(frame->dpi());
610 } else {
611 result.reset(new DxgiDesktopFrame(*surfaces_.current_frame()));
612 }
613 RTC_DCHECK(result);
614 *result->mutable_updated_region() =
615 surfaces_.current_frame()->get()->updated_region();
616 return result;
617 }
618
619 void ScreenCapturerWinDirectx::Capture(const DesktopRegion& region) {
620 RTC_DCHECK(callback_);
621
622 if (!g_container->duplication && !DuplicateOutput()) {
623 // Receive a capture request when application is shutting down, or between
624 // mode change.
625 callback_->OnCaptureCompleted(nullptr);
626 return;
627 }
628
629 RTC_DCHECK(g_container->duplication);
630 int64_t capture_start_time_nanos = rtc::TimeNanos();
631
632 if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) {
633 if (!set_thread_execution_state_failed_) {
634 set_thread_execution_state_failed_ = true;
635 LOG(LS_WARNING) << "Failed to make system & display power assertion: "
636 << GetLastError();
637 }
638 }
639
640 DXGI_OUTDUPL_FRAME_INFO frame_info;
641 memset(&frame_info, 0, sizeof(DXGI_OUTDUPL_FRAME_INFO));
642 ComPtr<IDXGIResource> resource;
643 rtc::CritScope lock(&g_container->acquire_lock);
644 _com_error error = g_container->duplication->AcquireNextFrame(
645 kAcquireTimeoutMs, &frame_info, resource.GetAddressOf());
646
647 if (error.Error() == DXGI_ERROR_WAIT_TIMEOUT) {
648 // Nothing changed.
649 EmitCurrentFrame();
650 return;
651 }
652
653 if (error.Error() != S_OK) {
654 LOG(LS_ERROR) << "Failed to capture frame, error " << error.ErrorMessage()
655 << ", code " << error.Error();
656 if (ForceDuplicateOutput()) {
657 EmitCurrentFrame();
658 } else {
659 callback_->OnCaptureCompleted(nullptr);
660 }
661 return;
662 }
663
664 if (frame_info.AccumulatedFrames == 0) {
665 g_container->duplication->ReleaseFrame();
666 EmitCurrentFrame();
667 return;
668 }
669
670 // Everything looks good so far, build next frame.
671 std::unique_ptr<DesktopFrame> result =
672 ProcessFrame(frame_info, resource.Get());
673 // DetectUpdatedRegion may release last g_container->duplication. But
674 // ForctDuplicateOutput function will always release last frame, so there is
675 // no potential leak.
676 if (g_container->duplication) {
677 g_container->duplication->ReleaseFrame();
678 }
679 if (!result) {
680 callback_->OnCaptureCompleted(nullptr);
681 return;
682 }
683
684 result->set_capture_time_ms(
685 (rtc::TimeNanos() - capture_start_time_nanos) /
686 rtc::kNumNanosecsPerMillisec);
687 callback_->OnCaptureCompleted(result.release());
688 }
689
690 bool ScreenCapturerWinDirectx::GetScreenList(ScreenList* screens) {
691 return true;
692 }
693
694 bool ScreenCapturerWinDirectx::SelectScreen(ScreenId id) {
695 // Only full desktop capture is supported.
696 return id == kFullDesktopScreenId;
697 }
698
699 void ScreenCapturerWinDirectx::EmitCurrentFrame() {
700 if (!surfaces_.current_frame()->get()->bits()) {
701 // At the very begining, we have not captured any frames.
702 callback_->OnCaptureCompleted(nullptr);
703 return;
704 }
705
706 if (shared_memory_factory_) {
707 // If shared_memory_factory_ is provided, last frame is stored in frames_
708 // queue. If there is not an existing frame (at the very begining), we can
709 // only return a nullptr.
710 if (frames_.current_frame()) {
711 std::unique_ptr<SharedDesktopFrame> frame(
712 frames_.current_frame()->Share());
713 frame->mutable_updated_region()->Clear();
714 callback_->OnCaptureCompleted(frame.release());
715 } else {
716 callback_->OnCaptureCompleted(nullptr);
717 }
718 return;
719 }
720
721 // If there is no shared_memory_factory_, last frame is stored in surfaces_
722 // queue.
723 std::unique_ptr<DesktopFrame> frame(
724 new DxgiDesktopFrame(*surfaces_.current_frame()));
725 callback_->OnCaptureCompleted(frame.release());
726 }
727
728 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698