Fix race condition in CroppingWindowCapturer https://webrtc-review.googlesource.com/c/src/+/467820 fixed an issue where the captured window may have moved during capture, and thus the frame would have the window at it's original location (e.g. when CaptureFrame was called), but we cropped to it's new location (e.g. when OnCaptureResult was called), thus potential leaking pixels. This addresses the inverse of that. If we actually capture closer to when `OnCaptureResult` is called and the window has moved, we could now be capturing the old location where the window no longer is and is thus leaking pixels. Since we cannot assert whether capture happened closer to `CaptureFrame` or `OnCaptureResult`, we simply attempt to recapture the frame if the window position has moved between these two calls. Fixed: chromium:517207235 Change-Id: I98f41c709b0bcf0285e258e7421af3481692a1ba Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/476400 Reviewed-by: Mark Foltz <mfoltz@chromium.org> Auto-Submit: Alexander Cooper <alcooper@chromium.org> Commit-Queue: Alexander Cooper <alcooper@chromium.org> Cr-Commit-Position: refs/heads/main@{#47851}
diff --git a/modules/desktop_capture/cropping_window_capturer.cc b/modules/desktop_capture/cropping_window_capturer.cc index 3479044..1fb1928 100644 --- a/modules/desktop_capture/cropping_window_capturer.cc +++ b/modules/desktop_capture/cropping_window_capturer.cc
@@ -96,6 +96,16 @@ return; } + // We don't know when capture has occurred, so if the window moved we can't + // assert which pixels we should capture. Capture again and we'll return a + // frame once the window is stationary between the two calls. + DesktopRect current_window_rect = GetWindowRectInVirtualScreen(); + if (!current_window_rect.equals(last_window_rect_)) { + RTC_LOG(LS_INFO) << "Window moved during capture"; + window_capturer_->CaptureFrame(); + return; + } + if (result != Result::SUCCESS) { RTC_LOG(LS_WARNING) << "ScreenCapturer failed to capture a frame"; callback_->OnCaptureResult(result, nullptr);