For ScreenCapturerSck implement SetMaxFrameRate

Bug: webrtc:367915807
Change-Id: I2bb138fc8c5f9ce6dd8b7f6a10b0e03135ae9fc4
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/365089
Reviewed-by: Johannes Kron <kron@webrtc.org>
Commit-Queue: Andreas Pehrson <apehrson@mozilla.com>
Reviewed-by: Alexander Cooper <alcooper@chromium.org>
Cr-Commit-Position: refs/heads/main@{#44219}
diff --git a/modules/desktop_capture/mac/screen_capturer_sck.mm b/modules/desktop_capture/mac/screen_capturer_sck.mm
index 479b5ba..1bc70cf 100644
--- a/modules/desktop_capture/mac/screen_capturer_sck.mm
+++ b/modules/desktop_capture/mac/screen_capturer_sck.mm
@@ -111,6 +111,9 @@
   // first display.
   CGDirectDisplayID current_display_ RTC_GUARDED_BY(lock_) = 0;
 
+  // Configured maximum frame rate in frames per second.
+  uint32_t max_frame_rate_ RTC_GUARDED_BY(lock_) = 0;
+
   // Used by CaptureFrame() to detect if the screen configuration has changed.
   // Only used on the caller's thread.
   MacDesktopConfiguration desktop_config_;
@@ -151,8 +154,22 @@
   StartOrReconfigureCapturer();
 }
 
-void ScreenCapturerSck::SetMaxFrameRate(uint32_t /* max_frame_rate */) {
-  // TODO: crbug.com/327458809 - Implement this.
+void ScreenCapturerSck::SetMaxFrameRate(uint32_t max_frame_rate) {
+  RTC_LOG(LS_INFO) << "ScreenCapturerSck " << this << " SetMaxFrameRate("
+                   << max_frame_rate << ").";
+  bool stream_started = false;
+  {
+    MutexLock lock(&lock_);
+    if (max_frame_rate_ == max_frame_rate) {
+      return;
+    }
+
+    max_frame_rate_ = max_frame_rate;
+    stream_started = stream_;
+  }
+  if (stream_started) {
+    StartOrReconfigureCapturer();
+  }
 }
 
 void ScreenCapturerSck::CaptureFrame() {
@@ -275,6 +292,9 @@
   config.width = filter.contentRect.size.width * filter.pointPixelScale;
   config.height = filter.contentRect.size.height * filter.pointPixelScale;
   config.captureResolution = SCCaptureResolutionNominal;
+  config.minimumFrameInterval = max_frame_rate_ > 0 ?
+      CMTimeMake(1, static_cast<int32_t>(max_frame_rate_)) :
+      kCMTimeZero;
 
   {
     MutexLock lock(&latest_frame_lock_);
@@ -284,7 +304,8 @@
   if (stream_) {
     RTC_LOG(LS_INFO) << "ScreenCapturerSck " << this
                      << " Updating stream configuration to size="
-                     << config.width << "x" << config.height << ".";
+                     << config.width << "x" << config.height
+                     << " and max_frame_rate=" << max_frame_rate_ << ".";
     [stream_ updateContentFilter:filter completionHandler:nil];
     [stream_ updateConfiguration:config completionHandler:nil];
   } else {