Allow use of CroppingWindowCapturer via CreateWindowCapturer

Currently, apps using WebRTC for window capture only get the benefits of
using CroppingWindowCapturer on Windows (described below) after changing
calls to DesktopCapturer::CreateWindowCapturer to instead call
CroppingWindowCapturer::CreateCapturer. This change adds a new flag to
DesktopCaptureOptions to allow opting in to the faster capture-screen-
and-crop path via the older & more discoverable API.

Benefits of using CroppingWindowCapturer's capture-screen-and-crop path
when possible:
1) It's significantly faster, up to ~36ms/frame (~160x) faster than the
capture-window-contents path in my testing (more details are in the
bug). This difference increased with the recent fix for
https://crbug.com/webrtc/10734 .
2) It allows capture of menus & tooltips (plus dialogs if
https://crbug.com/webrtc/10767 is fixed), partially mitigating
https://crbug.com/980864 .

Downsides of using it:
1) It may inadvertently capture occluding windows that aren't detected
properly, e.g. some system UI: https://crbug.com/webrtc/10835 .
2) It may capture some neighboring regions when moving/resizing the
captured window.

The new flag is not enabled by default, so the default behavior is
unchanged. This could perhaps be revisited after addressing
https://crbug.com/webrtc/10835 .

Bug: webrtc:10825
Change-Id: Ib77e5facc7240c5df311fe1fe204d0d8ea22a96a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/146823
Commit-Queue: Jamie Walch <jamiewalch@chromium.org>
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#28695}
diff --git a/modules/desktop_capture/desktop_capture_options.h b/modules/desktop_capture/desktop_capture_options.h
index 9473547..11e5f4c 100644
--- a/modules/desktop_capture/desktop_capture_options.h
+++ b/modules/desktop_capture/desktop_capture_options.h
@@ -112,6 +112,22 @@
   void set_allow_directx_capturer(bool enabled) {
     allow_directx_capturer_ = enabled;
   }
+
+  // Flag that may be set to allow use of the cropping window capturer (which
+  // captures the screen & crops that to the window region in some cases). An
+  // advantage of using this is significantly higher capture frame rates than
+  // capturing the window directly. A disadvantage of using this is the
+  // possibility of capturing unrelated content (e.g. overlapping windows that
+  // aren't detected properly, or neighboring regions when moving/resizing the
+  // captured window). Note: this flag influences the behavior of calls to
+  // DesktopCapturer::CreateWindowCapturer; calls to
+  // CroppingWindowCapturer::CreateCapturer ignore the flag (treat it as true).
+  bool allow_cropping_window_capturer() const {
+    return allow_cropping_window_capturer_;
+  }
+  void set_allow_cropping_window_capturer(bool allow) {
+    allow_cropping_window_capturer_ = allow;
+  }
 #endif
 
 #if defined(WEBRTC_USE_PIPEWIRE)
@@ -134,6 +150,7 @@
 #if defined(WEBRTC_WIN)
   bool allow_use_magnification_api_ = false;
   bool allow_directx_capturer_ = false;
+  bool allow_cropping_window_capturer_ = false;
 #endif
 #if defined(USE_X11)
   bool use_update_notifications_ = false;
diff --git a/modules/desktop_capture/desktop_capturer.cc b/modules/desktop_capture/desktop_capturer.cc
index b636611..df625e9 100644
--- a/modules/desktop_capture/desktop_capturer.cc
+++ b/modules/desktop_capture/desktop_capturer.cc
@@ -16,6 +16,7 @@
 #include <cstring>
 #include <utility>
 
+#include "modules/desktop_capture/cropping_window_capturer.h"
 #include "modules/desktop_capture/desktop_capture_options.h"
 #include "modules/desktop_capture/desktop_capturer_differ_wrapper.h"
 
@@ -47,6 +48,12 @@
 // static
 std::unique_ptr<DesktopCapturer> DesktopCapturer::CreateWindowCapturer(
     const DesktopCaptureOptions& options) {
+#if defined(WEBRTC_WIN)
+  if (options.allow_cropping_window_capturer()) {
+    return CroppingWindowCapturer::CreateCapturer(options);
+  }
+#endif  // defined(WEBRTC_WIN)
+
   std::unique_ptr<DesktopCapturer> capturer = CreateRawWindowCapturer(options);
   if (capturer && options.detect_updated_region()) {
     capturer.reset(new DesktopCapturerDifferWrapper(std::move(capturer)));