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