Use injected clock in Windows Capturers

Bug: webrtc:42223992
Change-Id: I3cf1634de964a07898e2c139025f33c96a6a6964
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/465580
Commit-Queue: Evan Shrubsole <eshr@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47510}
diff --git a/modules/desktop_capture/BUILD.gn b/modules/desktop_capture/BUILD.gn
index ac1b886..55546d0 100644
--- a/modules/desktop_capture/BUILD.gn
+++ b/modules/desktop_capture/BUILD.gn
@@ -287,6 +287,7 @@
   defines = []
   deps = [
     "../../api/environment",
+    "../../api/units:timestamp",
     "../../media:video_common",
     "../../rtc_base/containers:flat_map",
     "../../rtc_base/containers:flat_set",
diff --git a/modules/desktop_capture/win/screen_capturer_win_gdi.cc b/modules/desktop_capture/win/screen_capturer_win_gdi.cc
index 7f25e37..eeda674 100644
--- a/modules/desktop_capture/win/screen_capturer_win_gdi.cc
+++ b/modules/desktop_capture/win/screen_capturer_win_gdi.cc
@@ -17,6 +17,8 @@
 #include <string>
 #include <utility>
 
+#include "api/units/time_delta.h"
+#include "api/units/timestamp.h"
 #include "modules/desktop_capture/desktop_capture_metrics_helper.h"
 #include "modules/desktop_capture/desktop_capture_options.h"
 #include "modules/desktop_capture/desktop_capture_types.h"
@@ -30,7 +32,6 @@
 #include "modules/desktop_capture/win/screen_capture_utils.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
 #include "system_wrappers/include/metrics.h"
 
@@ -46,8 +47,8 @@
 
 }  // namespace
 
-ScreenCapturerWinGdi::ScreenCapturerWinGdi(
-    const DesktopCaptureOptions& options) {
+ScreenCapturerWinGdi::ScreenCapturerWinGdi(const DesktopCaptureOptions& options)
+    : clock_(options.clock()) {
   if (options.disable_effects()) {
     // Load dwmapi.dll dynamically since it is not available on XP.
     if (!dwmapi_library_)
@@ -81,7 +82,7 @@
 
 void ScreenCapturerWinGdi::CaptureFrame() {
   TRACE_EVENT0("webrtc", "ScreenCapturerWinGdi::CaptureFrame");
-  int64_t capture_start_time_nanos = TimeNanos();
+  Timestamp capture_start_time = clock_.CurrentTime();
 
   queue_.MoveToNextFrame();
   if (queue_.current_frame() && queue_.current_frame()->IsShared()) {
@@ -104,8 +105,7 @@
   frame->mutable_updated_region()->SetRect(
       DesktopRect::MakeSize(frame->size()));
 
-  int capture_time_ms =
-      (TimeNanos() - capture_start_time_nanos) / kNumNanosecsPerMillisec;
+  int capture_time_ms = (clock_.CurrentTime() - capture_start_time).ms();
   RTC_HISTOGRAM_COUNTS_1000(
       "WebRTC.DesktopCapture.Win.ScreenGdiCapturerFrameTime", capture_time_ms);
   frame->set_capture_time_ms(capture_time_ms);
diff --git a/modules/desktop_capture/win/screen_capturer_win_gdi.h b/modules/desktop_capture/win/screen_capturer_win_gdi.h
index ecbeb41..e236034 100644
--- a/modules/desktop_capture/win/screen_capturer_win_gdi.h
+++ b/modules/desktop_capture/win/screen_capturer_win_gdi.h
@@ -25,6 +25,7 @@
 #include "modules/desktop_capture/shared_memory.h"
 #include "modules/desktop_capture/win/display_configuration_monitor.h"
 #include "modules/desktop_capture/win/scoped_thread_desktop.h"
+#include "system_wrappers/include/clock.h"
 
 namespace webrtc {
 
@@ -81,6 +82,7 @@
 
   HMODULE dwmapi_library_ = NULL;
   DwmEnableCompositionFunc composition_func_ = nullptr;
+  Clock& clock_;
 };
 
 }  // namespace webrtc
diff --git a/modules/desktop_capture/win/screen_capturer_win_magnifier.cc b/modules/desktop_capture/win/screen_capturer_win_magnifier.cc
index 106d8d8..35a1ce2 100644
--- a/modules/desktop_capture/win/screen_capturer_win_magnifier.cc
+++ b/modules/desktop_capture/win/screen_capturer_win_magnifier.cc
@@ -17,7 +17,10 @@
 #include <string>
 #include <utility>
 
+#include "api/units/time_delta.h"
+#include "api/units/timestamp.h"
 #include "modules/desktop_capture/desktop_capture_metrics_helper.h"
+#include "modules/desktop_capture/desktop_capture_options.h"
 #include "modules/desktop_capture/desktop_capture_types.h"
 #include "modules/desktop_capture/desktop_frame.h"
 #include "modules/desktop_capture/desktop_geometry.h"
@@ -28,7 +31,6 @@
 #include "modules/desktop_capture/win/screen_capture_utils.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/time_utils.h"
 #include "system_wrappers/include/metrics.h"
 
 namespace webrtc {
@@ -49,7 +51,9 @@
 static wchar_t kMagnifierWindowClass[] = L"Magnifier";
 static wchar_t kMagnifierWindowName[] = L"MagnifierWindow";
 
-ScreenCapturerWinMagnifier::ScreenCapturerWinMagnifier() = default;
+ScreenCapturerWinMagnifier::ScreenCapturerWinMagnifier(
+    const DesktopCaptureOptions& options)
+    : clock_(options.clock()) {}
 ScreenCapturerWinMagnifier::~ScreenCapturerWinMagnifier() {
   // DestroyWindow must be called before MagUninitialize. magnifier_window_ is
   // destroyed automatically when host_window_ is destroyed.
@@ -91,7 +95,7 @@
     return;
   }
 
-  int64_t capture_start_time_nanos = TimeNanos();
+  Timestamp capture_start_time = clock_.CurrentTime();
 
   // Switch to the desktop receiving user input if different from the current
   // one.
@@ -125,8 +129,7 @@
   frame->mutable_updated_region()->SetRect(
       DesktopRect::MakeSize(frame->size()));
 
-  int capture_time_ms =
-      (TimeNanos() - capture_start_time_nanos) / kNumNanosecsPerMillisec;
+  int capture_time_ms = (clock_.CurrentTime() - capture_start_time).ms();
   RTC_HISTOGRAM_COUNTS_1000(
       "WebRTC.DesktopCapture.Win.MagnifierCapturerFrameTime", capture_time_ms);
   frame->set_capture_time_ms(capture_time_ms);
diff --git a/modules/desktop_capture/win/screen_capturer_win_magnifier.h b/modules/desktop_capture/win/screen_capturer_win_magnifier.h
index 8093705..087fb02 100644
--- a/modules/desktop_capture/win/screen_capturer_win_magnifier.h
+++ b/modules/desktop_capture/win/screen_capturer_win_magnifier.h
@@ -28,6 +28,7 @@
 #include "modules/desktop_capture/shared_desktop_frame.h"
 #include "modules/desktop_capture/shared_memory.h"
 #include "modules/desktop_capture/win/scoped_thread_desktop.h"
+#include "system_wrappers/include/clock.h"
 
 namespace webrtc {
 
@@ -44,7 +45,7 @@
 // be used if that functionality is necessary.
 class ScreenCapturerWinMagnifier : public DesktopCapturer {
  public:
-  ScreenCapturerWinMagnifier();
+  explicit ScreenCapturerWinMagnifier(const DesktopCaptureOptions& options);
   ~ScreenCapturerWinMagnifier() override;
 
   ScreenCapturerWinMagnifier(const ScreenCapturerWinMagnifier&) = delete;
@@ -139,6 +140,7 @@
   // True if the last OnMagImageScalingCallback was called and handled
   // successfully. Reset at the beginning of each CaptureImage call.
   bool magnifier_capture_succeeded_ = true;
+  Clock& clock_;
 };
 
 }  // namespace webrtc
diff --git a/modules/desktop_capture/win/window_capturer_win_gdi.cc b/modules/desktop_capture/win/window_capturer_win_gdi.cc
index 8e9be70..2b50d08 100644
--- a/modules/desktop_capture/win/window_capturer_win_gdi.cc
+++ b/modules/desktop_capture/win/window_capturer_win_gdi.cc
@@ -20,6 +20,8 @@
 #include <utility>
 #include <vector>
 
+#include "api/units/time_delta.h"
+#include "api/units/timestamp.h"
 #include "modules/desktop_capture/cropped_desktop_frame.h"
 #include "modules/desktop_capture/desktop_capture_metrics_helper.h"
 #include "modules/desktop_capture/desktop_capture_types.h"
@@ -32,7 +34,6 @@
 #include "modules/desktop_capture/win/window_capture_utils.h"
 #include "rtc_base/checks.h"
 #include "rtc_base/logging.h"
-#include "rtc_base/time_utils.h"
 #include "rtc_base/trace_event.h"
 #include "rtc_base/win/windows_version.h"
 #include "system_wrappers/include/metrics.h"
@@ -100,14 +101,13 @@
   return TRUE;
 }
 
-WindowCapturerWinGdi::WindowCapturerWinGdi(
-    bool enumerate_current_process_windows)
-    : enumerate_current_process_windows_(enumerate_current_process_windows) {}
+WindowCapturerWinGdi::WindowCapturerWinGdi(const DesktopCaptureOptions& options)
+    : options_(options) {}
 WindowCapturerWinGdi::~WindowCapturerWinGdi() {}
 
 bool WindowCapturerWinGdi::GetSourceList(SourceList* sources) {
   if (!window_capture_helper_.EnumerateCapturableWindows(
-          sources, enumerate_current_process_windows_))
+          sources, options_.enumerate_current_process_windows()))
     return false;
 
   std::map<HWND, DesktopSize> new_map;
@@ -162,7 +162,7 @@
 
 void WindowCapturerWinGdi::CaptureFrame() {
   RTC_DCHECK(callback_);
-  int64_t capture_start_time_nanos = TimeNanos();
+  Timestamp capture_start_time = options_.clock().CurrentTime();
 
   CaptureResults results = CaptureFrame(/*capture_owned_windows*/ true);
   if (!results.frame) {
@@ -174,7 +174,7 @@
   }
 
   int capture_time_ms =
-      (TimeNanos() - capture_start_time_nanos) / kNumNanosecsPerMillisec;
+      (options_.clock().CurrentTime() - capture_start_time).ms();
   RTC_HISTOGRAM_COUNTS_1000(
       "WebRTC.DesktopCapture.Win.WindowGdiCapturerFrameTime", capture_time_ms);
   results.frame->set_capture_time_ms(capture_time_ms);
@@ -361,8 +361,8 @@
 
       if (!owned_windows_.empty()) {
         if (!owned_window_capturer_) {
-          owned_window_capturer_ = std::make_unique<WindowCapturerWinGdi>(
-              enumerate_current_process_windows_);
+          owned_window_capturer_ =
+              std::make_unique<WindowCapturerWinGdi>(options_);
         }
 
         // Owned windows are stored in top-down z-order, so this iterates in
@@ -405,8 +405,7 @@
 // static
 std::unique_ptr<DesktopCapturer> WindowCapturerWinGdi::CreateRawWindowCapturer(
     const DesktopCaptureOptions& options) {
-  return std::unique_ptr<DesktopCapturer>(
-      new WindowCapturerWinGdi(options.enumerate_current_process_windows()));
+  return std::unique_ptr<DesktopCapturer>(new WindowCapturerWinGdi(options));
 }
 
 }  // namespace webrtc
diff --git a/modules/desktop_capture/win/window_capturer_win_gdi.h b/modules/desktop_capture/win/window_capturer_win_gdi.h
index b8e56e5..2961e60 100644
--- a/modules/desktop_capture/win/window_capturer_win_gdi.h
+++ b/modules/desktop_capture/win/window_capturer_win_gdi.h
@@ -20,12 +20,13 @@
 #include "modules/desktop_capture/desktop_geometry.h"
 #include "modules/desktop_capture/win/window_capture_utils.h"
 #include "modules/desktop_capture/window_finder_win.h"
+#include "system_wrappers/include/clock.h"
 
 namespace webrtc {
 
 class WindowCapturerWinGdi : public DesktopCapturer {
  public:
-  explicit WindowCapturerWinGdi(bool enumerate_current_process_windows);
+  explicit WindowCapturerWinGdi(const DesktopCaptureOptions& options);
 
   // Disallow copy and assign
   WindowCapturerWinGdi(const WindowCapturerWinGdi&) = delete;
@@ -53,6 +54,7 @@
   CaptureResults CaptureFrame(bool capture_owned_windows);
 
   Callback* callback_ = nullptr;
+  DesktopCaptureOptions options_;
 
   // HWND and HDC for the currently selected window or nullptr if window is not
   // selected.
@@ -62,8 +64,6 @@
 
   WindowCaptureHelperWin window_capture_helper_;
 
-  bool enumerate_current_process_windows_;
-
   // This map is used to avoid flickering for the case when SelectWindow() calls
   // are interleaved with Capture() calls.
   std::map<HWND, DesktopSize> window_size_map_;