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

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