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 "modules/desktop_capture/desktop_capturer_differ_wrapper.h"
12
13 #include <stdint.h>
14 #include <string.h>
15
16 #include <utility>
17
18 #include "modules/desktop_capture/desktop_geometry.h"
19 #include "modules/desktop_capture/desktop_region.h"
20 #include "modules/desktop_capture/differ_block.h"
21 #include "rtc_base/checks.h"
22 #include "rtc_base/time_utils.h"
23
24 namespace webrtc {
25
26 namespace {
27
28 // Returns true if (0, 0) - (|width|, |height|) vector in |old_buffer| and
29 // |new_buffer| are equal. |width| should be less than 32
30 // (defined by kBlockSize), otherwise BlockDifference() should be used.
PartialBlockDifference(const uint8_t * old_buffer,const uint8_t * new_buffer,int width,int height,int stride)31 bool PartialBlockDifference(const uint8_t* old_buffer,
32 const uint8_t* new_buffer,
33 int width,
34 int height,
35 int stride) {
36 RTC_DCHECK_LT(width, kBlockSize);
37 const int width_bytes = width * DesktopFrame::kBytesPerPixel;
38 for (int i = 0; i < height; i++) {
39 if (memcmp(old_buffer, new_buffer, width_bytes) != 0) {
40 return true;
41 }
42 old_buffer += stride;
43 new_buffer += stride;
44 }
45 return false;
46 }
47
48 // Compares columns in the range of [|left|, |right|), in a row in the
49 // range of [|top|, |top| + |height|), starts from |old_buffer| and
50 // |new_buffer|, and outputs updated regions into |output|. |stride| is the
51 // DesktopFrame::stride().
CompareRow(const uint8_t * old_buffer,const uint8_t * new_buffer,const int left,const int right,const int top,const int bottom,const int stride,DesktopRegion * const output)52 void CompareRow(const uint8_t* old_buffer,
53 const uint8_t* new_buffer,
54 const int left,
55 const int right,
56 const int top,
57 const int bottom,
58 const int stride,
59 DesktopRegion* const output) {
60 const int block_x_offset = kBlockSize * DesktopFrame::kBytesPerPixel;
61 const int width = right - left;
62 const int height = bottom - top;
63 const int block_count = (width - 1) / kBlockSize;
64 const int last_block_width = width - block_count * kBlockSize;
65 RTC_DCHECK_GT(last_block_width, 0);
66 RTC_DCHECK_LE(last_block_width, kBlockSize);
67
68 // The first block-column in a continuous dirty area in current block-row.
69 int first_dirty_x_block = -1;
70
71 // We always need to add dirty area into |output| in the last block, so handle
72 // it separatedly.
73 for (int x = 0; x < block_count; x++) {
74 if (BlockDifference(old_buffer, new_buffer, height, stride)) {
75 if (first_dirty_x_block == -1) {
76 // This is the first dirty block in a continuous dirty area.
77 first_dirty_x_block = x;
78 }
79 } else if (first_dirty_x_block != -1) {
80 // The block on the left is the last dirty block in a continuous
81 // dirty area.
82 output->AddRect(
83 DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize + left, top,
84 x * kBlockSize + left, bottom));
85 first_dirty_x_block = -1;
86 }
87 old_buffer += block_x_offset;
88 new_buffer += block_x_offset;
89 }
90
91 bool last_block_diff;
92 if (last_block_width < kBlockSize) {
93 // The last one is a partial vector.
94 last_block_diff = PartialBlockDifference(old_buffer, new_buffer,
95 last_block_width, height, stride);
96 } else {
97 last_block_diff = BlockDifference(old_buffer, new_buffer, height, stride);
98 }
99 if (last_block_diff) {
100 if (first_dirty_x_block == -1) {
101 first_dirty_x_block = block_count;
102 }
103 output->AddRect(DesktopRect::MakeLTRB(
104 first_dirty_x_block * kBlockSize + left, top, right, bottom));
105 } else if (first_dirty_x_block != -1) {
106 output->AddRect(
107 DesktopRect::MakeLTRB(first_dirty_x_block * kBlockSize + left, top,
108 block_count * kBlockSize + left, bottom));
109 }
110 }
111
112 // Compares |rect| area in |old_frame| and |new_frame|, and outputs dirty
113 // regions into |output|.
CompareFrames(const DesktopFrame & old_frame,const DesktopFrame & new_frame,DesktopRect rect,DesktopRegion * const output)114 void CompareFrames(const DesktopFrame& old_frame,
115 const DesktopFrame& new_frame,
116 DesktopRect rect,
117 DesktopRegion* const output) {
118 RTC_DCHECK(old_frame.size().equals(new_frame.size()));
119 RTC_DCHECK_EQ(old_frame.stride(), new_frame.stride());
120 rect.IntersectWith(DesktopRect::MakeSize(old_frame.size()));
121
122 const int y_block_count = (rect.height() - 1) / kBlockSize;
123 const int last_y_block_height = rect.height() - y_block_count * kBlockSize;
124 // Offset from the start of one block-row to the next.
125 const int block_y_stride = old_frame.stride() * kBlockSize;
126 const uint8_t* prev_block_row_start =
127 old_frame.GetFrameDataAtPos(rect.top_left());
128 const uint8_t* curr_block_row_start =
129 new_frame.GetFrameDataAtPos(rect.top_left());
130
131 int top = rect.top();
132 // The last row may have a different height, so we handle it separately.
133 for (int y = 0; y < y_block_count; y++) {
134 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(),
135 rect.right(), top, top + kBlockSize, old_frame.stride(), output);
136 top += kBlockSize;
137 prev_block_row_start += block_y_stride;
138 curr_block_row_start += block_y_stride;
139 }
140 CompareRow(prev_block_row_start, curr_block_row_start, rect.left(),
141 rect.right(), top, top + last_y_block_height, old_frame.stride(),
142 output);
143 }
144
145 } // namespace
146
DesktopCapturerDifferWrapper(std::unique_ptr<DesktopCapturer> base_capturer)147 DesktopCapturerDifferWrapper::DesktopCapturerDifferWrapper(
148 std::unique_ptr<DesktopCapturer> base_capturer)
149 : base_capturer_(std::move(base_capturer)) {
150 RTC_DCHECK(base_capturer_);
151 }
152
~DesktopCapturerDifferWrapper()153 DesktopCapturerDifferWrapper::~DesktopCapturerDifferWrapper() {}
154
Start(DesktopCapturer::Callback * callback)155 void DesktopCapturerDifferWrapper::Start(DesktopCapturer::Callback* callback) {
156 callback_ = callback;
157 base_capturer_->Start(this);
158 }
159
SetSharedMemoryFactory(std::unique_ptr<SharedMemoryFactory> shared_memory_factory)160 void DesktopCapturerDifferWrapper::SetSharedMemoryFactory(
161 std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
162 base_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
163 }
164
CaptureFrame()165 void DesktopCapturerDifferWrapper::CaptureFrame() {
166 base_capturer_->CaptureFrame();
167 }
168
SetExcludedWindow(WindowId window)169 void DesktopCapturerDifferWrapper::SetExcludedWindow(WindowId window) {
170 base_capturer_->SetExcludedWindow(window);
171 }
172
GetSourceList(SourceList * sources)173 bool DesktopCapturerDifferWrapper::GetSourceList(SourceList* sources) {
174 return base_capturer_->GetSourceList(sources);
175 }
176
SelectSource(SourceId id)177 bool DesktopCapturerDifferWrapper::SelectSource(SourceId id) {
178 return base_capturer_->SelectSource(id);
179 }
180
FocusOnSelectedSource()181 bool DesktopCapturerDifferWrapper::FocusOnSelectedSource() {
182 return base_capturer_->FocusOnSelectedSource();
183 }
184
IsOccluded(const DesktopVector & pos)185 bool DesktopCapturerDifferWrapper::IsOccluded(const DesktopVector& pos) {
186 return base_capturer_->IsOccluded(pos);
187 }
188
OnCaptureResult(Result result,std::unique_ptr<DesktopFrame> input_frame)189 void DesktopCapturerDifferWrapper::OnCaptureResult(
190 Result result,
191 std::unique_ptr<DesktopFrame> input_frame) {
192 int64_t start_time_nanos = rtc::TimeNanos();
193 if (!input_frame) {
194 callback_->OnCaptureResult(result, nullptr);
195 return;
196 }
197 RTC_DCHECK(result == Result::SUCCESS);
198
199 std::unique_ptr<SharedDesktopFrame> frame =
200 SharedDesktopFrame::Wrap(std::move(input_frame));
201 if (last_frame_ && (last_frame_->size().width() != frame->size().width() ||
202 last_frame_->size().height() != frame->size().height() ||
203 last_frame_->stride() != frame->stride())) {
204 last_frame_.reset();
205 }
206
207 if (last_frame_) {
208 DesktopRegion hints;
209 hints.Swap(frame->mutable_updated_region());
210 for (DesktopRegion::Iterator it(hints); !it.IsAtEnd(); it.Advance()) {
211 CompareFrames(*last_frame_, *frame, it.rect(),
212 frame->mutable_updated_region());
213 }
214 } else {
215 frame->mutable_updated_region()->SetRect(
216 DesktopRect::MakeSize(frame->size()));
217 }
218 last_frame_ = frame->Share();
219
220 frame->set_capture_time_ms(frame->capture_time_ms() +
221 (rtc::TimeNanos() - start_time_nanos) /
222 rtc::kNumNanosecsPerMillisec);
223 callback_->OnCaptureResult(result, std::move(frame));
224 }
225
226 } // namespace webrtc
227