Make CroppedWindowCapturer more resilient

Bug: chromium:1245272
Change-Id: I276c98ad0aea3dd0e614b935b9a7566c77d5026a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/233720
Reviewed-by: Joe Downing <joedow@chromium.org>
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35132}
This commit is contained in:
Ilya Nikolaevskiy 2021-10-01 16:14:42 +02:00 committed by WebRTC LUCI CQ
parent 985310ea3b
commit db94869ca3
3 changed files with 22 additions and 8 deletions

View File

@ -36,7 +36,9 @@ std::unique_ptr<DesktopFrame> CreateCroppedDesktopFrame(
const DesktopRect& rect) { const DesktopRect& rect) {
RTC_DCHECK(frame); RTC_DCHECK(frame);
if (!DesktopRect::MakeSize(frame->size()).ContainsRect(rect)) { DesktopRect intersection = DesktopRect::MakeSize(frame->size());
intersection.IntersectWith(rect);
if (intersection.is_empty()) {
return nullptr; return nullptr;
} }
@ -45,7 +47,7 @@ std::unique_ptr<DesktopFrame> CreateCroppedDesktopFrame(
} }
return std::unique_ptr<DesktopFrame>( return std::unique_ptr<DesktopFrame>(
new CroppedDesktopFrame(std::move(frame), rect)); new CroppedDesktopFrame(std::move(frame), intersection));
} }
CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame, CroppedDesktopFrame::CroppedDesktopFrame(std::unique_ptr<DesktopFrame> frame,

View File

@ -32,9 +32,14 @@ TEST(CroppedDesktopFrameTest, DoNotCreateWrapperIfSizeIsNotChanged) {
ASSERT_EQ(cropped.get(), raw_original); ASSERT_EQ(cropped.get(), raw_original);
} }
TEST(CroppedDesktopFrameTest, ReturnNullptrIfSizeIsNotSufficient) { TEST(CroppedDesktopFrameTest, CropWhenPartiallyOutOfBounds) {
ASSERT_EQ(nullptr, CreateCroppedDesktopFrame(CreateTestFrame(), std::unique_ptr<DesktopFrame> cropped =
DesktopRect::MakeWH(11, 10))); CreateCroppedDesktopFrame(CreateTestFrame(), DesktopRect::MakeWH(11, 10));
ASSERT_NE(nullptr, cropped);
ASSERT_EQ(cropped->size().width(), 10);
ASSERT_EQ(cropped->size().height(), 10);
ASSERT_EQ(cropped->top_left().x(), 0);
ASSERT_EQ(cropped->top_left().y(), 0);
} }
TEST(CroppedDesktopFrameTest, ReturnNullIfCropRegionIsOutOfBounds) { TEST(CroppedDesktopFrameTest, ReturnNullIfCropRegionIsOutOfBounds) {

View File

@ -99,9 +99,16 @@ void CroppingWindowCapturer::OnCaptureResult(
return; return;
} }
callback_->OnCaptureResult( std::unique_ptr<DesktopFrame> cropped_frame =
Result::SUCCESS, CreateCroppedDesktopFrame(std::move(screen_frame), window_rect);
CreateCroppedDesktopFrame(std::move(screen_frame), window_rect));
if (!cropped_frame) {
RTC_LOG(LS_WARNING) << "Window is outside of the captured display";
callback_->OnCaptureResult(Result::ERROR_TEMPORARY, nullptr);
return;
}
callback_->OnCaptureResult(Result::SUCCESS, std::move(cropped_frame));
} }
bool CroppingWindowCapturer::IsOccluded(const DesktopVector& pos) { bool CroppingWindowCapturer::IsOccluded(const DesktopVector& pos) {