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> |
13 #include <utility> | 14 #include <utility> |
14 | 15 |
15 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
16 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
17 #include "webrtc/base/timeutils.h" | 18 #include "webrtc/base/timeutils.h" |
18 #include "webrtc/modules/desktop_capture/desktop_frame.h" | 19 #include "webrtc/modules/desktop_capture/desktop_frame.h" |
| 20 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
19 | 21 |
20 namespace webrtc { | 22 namespace webrtc { |
21 | 23 |
22 using Microsoft::WRL::ComPtr; | 24 using Microsoft::WRL::ComPtr; |
23 | 25 |
24 // static | 26 // static |
25 bool ScreenCapturerWinDirectx::IsSupported() { | 27 bool ScreenCapturerWinDirectx::IsSupported() { |
26 // Forwards IsSupported() function call to DxgiDuplicatorController. | 28 // Forwards IsSupported() function call to DxgiDuplicatorController. |
27 return DxgiDuplicatorController::Instance()->IsSupported(); | 29 return DxgiDuplicatorController::Instance()->IsSupported(); |
28 } | 30 } |
(...skipping 30 matching lines...) Expand all Loading... |
59 return DxgiDuplicatorController::Instance() | 61 return DxgiDuplicatorController::Instance() |
60 ->ScreenRect(current_screen_id_) | 62 ->ScreenRect(current_screen_id_) |
61 .size(); | 63 .size(); |
62 } | 64 } |
63 | 65 |
64 void ScreenCapturerWinDirectx::CaptureFrame() { | 66 void ScreenCapturerWinDirectx::CaptureFrame() { |
65 RTC_DCHECK(callback_); | 67 RTC_DCHECK(callback_); |
66 | 68 |
67 int64_t capture_start_time_nanos = rtc::TimeNanos(); | 69 int64_t capture_start_time_nanos = rtc::TimeNanos(); |
68 | 70 |
| 71 // The dxgi components and APIs do not update the screen resolution without |
| 72 // a reinitialization. So we use the GetDC() function to retrieve the screen |
| 73 // resolution to decide whether dxgi components need to be reinitialized. |
| 74 // If the screen resolution changed, it's very likely the next Duplicate() |
| 75 // function call will fail because of a missing monitor or the frame size is |
| 76 // not enough to store the output. So we reinitialize dxgi components in-place |
| 77 // to avoid a capture failure. |
| 78 // But there is no guarantee GetDC() function returns the same resolution as |
| 79 // dxgi APIs, we still rely on dxgi components to return the output frame |
| 80 // size. |
| 81 // TODO(zijiehe): Confirm whether IDXGIOutput::GetDesc() and |
| 82 // IDXGIOutputDuplication::GetDesc() can detect the resolution change without |
| 83 // reinitialization. |
| 84 if (resolution_change_detector_.IsChanged( |
| 85 GetScreenRect(kFullDesktopScreenId, std::wstring()).size())) { |
| 86 frames_.Reset(); |
| 87 DxgiDuplicatorController::Instance()->Reset(); |
| 88 resolution_change_detector_.Reset(); |
| 89 } |
| 90 |
69 frames_.MoveToNextFrame(); | 91 frames_.MoveToNextFrame(); |
70 if (!frames_.current_frame()) { | 92 if (!frames_.current_frame()) { |
71 std::unique_ptr<DesktopFrame> new_frame; | 93 std::unique_ptr<DesktopFrame> new_frame; |
72 if (shared_memory_factory_) { | 94 if (shared_memory_factory_) { |
73 new_frame = SharedMemoryDesktopFrame::Create( | 95 new_frame = SharedMemoryDesktopFrame::Create( |
74 SelectedDesktopSize(), shared_memory_factory_.get()); | 96 SelectedDesktopSize(), shared_memory_factory_.get()); |
75 } else { | 97 } else { |
76 new_frame.reset(new BasicDesktopFrame(SelectedDesktopSize())); | 98 new_frame.reset(new BasicDesktopFrame(SelectedDesktopSize())); |
77 } | 99 } |
78 if (!new_frame) { | 100 if (!new_frame) { |
79 LOG(LS_ERROR) << "Failed to allocate a new DesktopFrame."; | 101 LOG(LS_ERROR) << "Failed to allocate a new DesktopFrame."; |
80 // This usually means we do not have enough memory or SharedMemoryFactory | 102 // This usually means we do not have enough memory or SharedMemoryFactory |
81 // cannot work correctly. | 103 // cannot work correctly. |
82 callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); | 104 callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); |
83 return; | 105 return; |
84 } | 106 } |
85 frames_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(std::move(new_frame))); | 107 frames_.ReplaceCurrentFrame(SharedDesktopFrame::Wrap(std::move(new_frame))); |
86 } | 108 } |
87 | 109 |
88 if (current_screen_id_ == kFullDesktopScreenId) { | 110 if (current_screen_id_ == kFullDesktopScreenId) { |
89 if (!DxgiDuplicatorController::Instance()->Duplicate( | 111 if (!DxgiDuplicatorController::Instance()->Duplicate( |
90 &context_, frames_.current_frame())) { | 112 &context_, frames_.current_frame())) { |
91 // Screen size may be changed, so we need to reset the frames. | 113 // Screen size may be changed, so we need to reset the frames. |
92 frames_.Reset(); | 114 frames_.Reset(); |
| 115 resolution_change_detector_.Reset(); |
93 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); | 116 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); |
94 return; | 117 return; |
95 } | 118 } |
96 } else { | 119 } else { |
97 if (!DxgiDuplicatorController::Instance()->DuplicateMonitor( | 120 if (!DxgiDuplicatorController::Instance()->DuplicateMonitor( |
98 &context_, current_screen_id_, frames_.current_frame())) { | 121 &context_, current_screen_id_, frames_.current_frame())) { |
99 // Screen size may be changed, so we need to reset the frames. | 122 // Screen size may be changed, so we need to reset the frames. |
100 frames_.Reset(); | 123 frames_.Reset(); |
| 124 resolution_change_detector_.Reset(); |
101 if (current_screen_id_ >= | 125 if (current_screen_id_ >= |
102 DxgiDuplicatorController::Instance()->ScreenCount()) { | 126 DxgiDuplicatorController::Instance()->ScreenCount()) { |
103 // Current monitor has been removed from the system. | 127 // Current monitor has been removed from the system. |
104 callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); | 128 callback_->OnCaptureResult(Result::ERROR_PERMANENT, nullptr); |
105 } else { | 129 } else { |
106 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); | 130 callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr); |
107 } | 131 } |
108 return; | 132 return; |
109 } | 133 } |
110 } | 134 } |
(...skipping 15 matching lines...) Expand all Loading... |
126 | 150 |
127 bool ScreenCapturerWinDirectx::SelectSource(SourceId id) { | 151 bool ScreenCapturerWinDirectx::SelectSource(SourceId id) { |
128 if (id == current_screen_id_) { | 152 if (id == current_screen_id_) { |
129 return true; | 153 return true; |
130 } | 154 } |
131 | 155 |
132 // Changing target screen may or may not impact frame size. So resetting | 156 // Changing target screen may or may not impact frame size. So resetting |
133 // frames only when a Duplicate() function call returns false. | 157 // frames only when a Duplicate() function call returns false. |
134 if (id == kFullDesktopScreenId) { | 158 if (id == kFullDesktopScreenId) { |
135 current_screen_id_ = id; | 159 current_screen_id_ = id; |
| 160 context_.Reset(); |
136 return true; | 161 return true; |
137 } | 162 } |
138 | 163 |
139 int screen_count = DxgiDuplicatorController::Instance()->ScreenCount(); | 164 int screen_count = DxgiDuplicatorController::Instance()->ScreenCount(); |
140 if (id >= 0 && id < screen_count) { | 165 if (id >= 0 && id < screen_count) { |
141 current_screen_id_ = id; | 166 current_screen_id_ = id; |
| 167 context_.Reset(); |
142 return true; | 168 return true; |
143 } | 169 } |
144 return false; | 170 return false; |
145 } | 171 } |
146 | 172 |
147 } // namespace webrtc | 173 } // namespace webrtc |
OLD | NEW |