[Overuse] Move EncodeUsageResource/QualityScalerResource to own files.

This CL changes EncodeUsageResource and QualityScalerResource from
private inner classes of OveruseFrameDetectorResourceAdaptationModule to
standalone classes, moving them into separate files.

This CL does not intend to change any lines of code, only move them.
Except for removing an unused method quality_scaler().

Bug: webrtc:11222
Change-Id: I86bf7eb78c80031888c403ac43c2bdf9b24eaea6
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/168198
Reviewed-by: Evan Shrubsole <eshr@google.com>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30472}
diff --git a/video/BUILD.gn b/video/BUILD.gn
index 288dddb..43de7df 100644
--- a/video/BUILD.gn
+++ b/video/BUILD.gn
@@ -176,6 +176,8 @@
 
   # visibility = [ "../api/video:video_stream_encoder_create" ]
   sources = [
+    "encode_usage_resource.cc",
+    "encode_usage_resource.h",
     "encoder_bitrate_adjuster.cc",
     "encoder_bitrate_adjuster.h",
     "encoder_overshoot_detector.cc",
@@ -186,6 +188,8 @@
     "overuse_frame_detector.h",
     "overuse_frame_detector_resource_adaptation_module.cc",
     "overuse_frame_detector_resource_adaptation_module.h",
+    "quality_scaler_resource.cc",
+    "quality_scaler_resource.h",
     "video_source_sink_controller.cc",
     "video_source_sink_controller.h",
     "video_stream_encoder.cc",
diff --git a/video/encode_usage_resource.cc b/video/encode_usage_resource.cc
new file mode 100644
index 0000000..37bc23e
--- /dev/null
+++ b/video/encode_usage_resource.cc
@@ -0,0 +1,85 @@
+/*
+ *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "video/encode_usage_resource.h"
+
+#include <limits>
+#include <utility>
+
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+
+EncodeUsageResource::EncodeUsageResource(
+    std::unique_ptr<OveruseFrameDetector> overuse_detector)
+    : overuse_detector_(std::move(overuse_detector)),
+      is_started_(false),
+      target_frame_rate_(absl::nullopt) {
+  RTC_DCHECK(overuse_detector_);
+}
+
+void EncodeUsageResource::StartCheckForOveruse(CpuOveruseOptions options) {
+  RTC_DCHECK(!is_started_);
+  overuse_detector_->StartCheckForOveruse(TaskQueueBase::Current(),
+                                          std::move(options), this);
+  is_started_ = true;
+  overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt());
+}
+
+void EncodeUsageResource::StopCheckForOveruse() {
+  overuse_detector_->StopCheckForOveruse();
+  is_started_ = false;
+}
+
+void EncodeUsageResource::SetTargetFrameRate(
+    absl::optional<double> target_frame_rate) {
+  if (target_frame_rate == target_frame_rate_)
+    return;
+  target_frame_rate_ = target_frame_rate;
+  if (is_started_)
+    overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt());
+}
+
+void EncodeUsageResource::OnEncodeStarted(const VideoFrame& cropped_frame,
+                                          int64_t time_when_first_seen_us) {
+  // TODO(hbos): Rename FrameCaptured() to something more appropriate (e.g.
+  // "OnEncodeStarted"?) or revise usage.
+  overuse_detector_->FrameCaptured(cropped_frame, time_when_first_seen_us);
+}
+
+void EncodeUsageResource::OnEncodeCompleted(
+    uint32_t timestamp,
+    int64_t time_sent_in_us,
+    int64_t capture_time_us,
+    absl::optional<int> encode_duration_us) {
+  // TODO(hbos): Rename FrameSent() to something more appropriate (e.g.
+  // "OnEncodeCompleted"?).
+  overuse_detector_->FrameSent(timestamp, time_sent_in_us, capture_time_us,
+                               encode_duration_us);
+}
+
+void EncodeUsageResource::AdaptUp(AdaptReason reason) {
+  RTC_DCHECK_EQ(reason, AdaptReason::kCpu);
+  OnResourceUsageStateMeasured(ResourceUsageState::kUnderuse);
+}
+
+bool EncodeUsageResource::AdaptDown(AdaptReason reason) {
+  RTC_DCHECK_EQ(reason, AdaptReason::kCpu);
+  return OnResourceUsageStateMeasured(ResourceUsageState::kOveruse) !=
+         ResourceListenerResponse::kQualityScalerShouldIncreaseFrequency;
+}
+
+int EncodeUsageResource::TargetFrameRateAsInt() {
+  return target_frame_rate_.has_value()
+             ? static_cast<int>(target_frame_rate_.value())
+             : std::numeric_limits<int>::max();
+}
+
+}  // namespace webrtc
diff --git a/video/encode_usage_resource.h b/video/encode_usage_resource.h
new file mode 100644
index 0000000..e03d544
--- /dev/null
+++ b/video/encode_usage_resource.h
@@ -0,0 +1,61 @@
+/*
+ *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VIDEO_ENCODE_USAGE_RESOURCE_H_
+#define VIDEO_ENCODE_USAGE_RESOURCE_H_
+
+#include <memory>
+
+#include "absl/types/optional.h"
+#include "call/adaptation/resource.h"
+#include "modules/video_coding/utility/quality_scaler.h"
+#include "video/overuse_frame_detector.h"
+
+namespace webrtc {
+
+// Handles interaction with the OveruseDetector.
+// TODO(hbos): Add unittests specific to this class, it is currently only tested
+// indirectly by usage in the OveruseFrameDetectorResourceAdaptationModule
+// (which is only tested because of its usage in VideoStreamEncoder); all tests
+// are currently in video_stream_encoder_unittest.cc.
+class EncodeUsageResource : public Resource,
+                            public AdaptationObserverInterface {
+ public:
+  explicit EncodeUsageResource(
+      std::unique_ptr<OveruseFrameDetector> overuse_detector);
+
+  void StartCheckForOveruse(CpuOveruseOptions options);
+  void StopCheckForOveruse();
+
+  void SetTargetFrameRate(absl::optional<double> target_frame_rate);
+  void OnEncodeStarted(const VideoFrame& cropped_frame,
+                       int64_t time_when_first_seen_us);
+  void OnEncodeCompleted(uint32_t timestamp,
+                         int64_t time_sent_in_us,
+                         int64_t capture_time_us,
+                         absl::optional<int> encode_duration_us);
+
+  // AdaptationObserverInterface implementation.
+  // TODO(https://crbug.com/webrtc/11222, 11172): This resource also needs to
+  // signal when its stable to support multi-stream aware modules.
+  void AdaptUp(AdaptReason reason) override;
+  bool AdaptDown(AdaptReason reason) override;
+
+ private:
+  int TargetFrameRateAsInt();
+
+  const std::unique_ptr<OveruseFrameDetector> overuse_detector_;
+  bool is_started_;
+  absl::optional<double> target_frame_rate_;
+};
+
+}  // namespace webrtc
+
+#endif  // VIDEO_ENCODE_USAGE_RESOURCE_H_
diff --git a/video/overuse_frame_detector_resource_adaptation_module.cc b/video/overuse_frame_detector_resource_adaptation_module.cc
index 7f70416..931e215 100644
--- a/video/overuse_frame_detector_resource_adaptation_module.cc
+++ b/video/overuse_frame_detector_resource_adaptation_module.cc
@@ -73,147 +73,6 @@
 
 }  // namespace
 
-// Handles interaction with the OveruseDetector.
-class OveruseFrameDetectorResourceAdaptationModule::EncodeUsageResource
-    : public Resource,
-      public AdaptationObserverInterface {
- public:
-  explicit EncodeUsageResource(
-      std::unique_ptr<OveruseFrameDetector> overuse_detector)
-      : overuse_detector_(std::move(overuse_detector)),
-        is_started_(false),
-        target_frame_rate_(absl::nullopt) {
-    RTC_DCHECK(overuse_detector_);
-  }
-
-  void StartCheckForOveruse(CpuOveruseOptions options) {
-    RTC_DCHECK(!is_started_);
-    overuse_detector_->StartCheckForOveruse(TaskQueueBase::Current(),
-                                            std::move(options), this);
-    is_started_ = true;
-    overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt());
-  }
-
-  void StopCheckForOveruse() {
-    overuse_detector_->StopCheckForOveruse();
-    is_started_ = false;
-  }
-
-  void SetTargetFrameRate(absl::optional<double> target_frame_rate) {
-    if (target_frame_rate == target_frame_rate_)
-      return;
-    target_frame_rate_ = target_frame_rate;
-    if (is_started_)
-      overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt());
-  }
-
-  void OnEncodeStarted(const VideoFrame& cropped_frame,
-                       int64_t time_when_first_seen_us) {
-    // TODO(hbos): Rename FrameCaptured() to something more appropriate (e.g.
-    // "OnEncodeStarted"?) or revise usage.
-    overuse_detector_->FrameCaptured(cropped_frame, time_when_first_seen_us);
-  }
-
-  void OnEncodeCompleted(uint32_t timestamp,
-                         int64_t time_sent_in_us,
-                         int64_t capture_time_us,
-                         absl::optional<int> encode_duration_us) {
-    // TODO(hbos): Rename FrameSent() to something more appropriate (e.g.
-    // "OnEncodeCompleted"?).
-    overuse_detector_->FrameSent(timestamp, time_sent_in_us, capture_time_us,
-                                 encode_duration_us);
-  }
-
-  // AdaptationObserverInterface implementation.
-  // TODO(https://crbug.com/webrtc/11222, 11172): This resource also needs to
-  // signal when its stable to support multi-stream aware modules.
-  void AdaptUp(AdaptReason reason) override {
-    RTC_DCHECK_EQ(reason, AdaptReason::kCpu);
-    OnResourceUsageStateMeasured(ResourceUsageState::kUnderuse);
-  }
-  bool AdaptDown(AdaptReason reason) override {
-    RTC_DCHECK_EQ(reason, AdaptReason::kCpu);
-    return OnResourceUsageStateMeasured(ResourceUsageState::kOveruse) !=
-           ResourceListenerResponse::kQualityScalerShouldIncreaseFrequency;
-  }
-
- private:
-  int TargetFrameRateAsInt() {
-    return target_frame_rate_.has_value()
-               ? static_cast<int>(target_frame_rate_.value())
-               : std::numeric_limits<int>::max();
-  }
-
-  const std::unique_ptr<OveruseFrameDetector> overuse_detector_;
-  bool is_started_;
-  absl::optional<double> target_frame_rate_;
-};
-
-// Handles interaction with the QualityScaler.
-class OveruseFrameDetectorResourceAdaptationModule::QualityScalerResource
-    : public Resource,
-      public AdaptationObserverInterface {
- public:
-  QualityScalerResource() : quality_scaler_(nullptr) {}
-
-  bool is_started() const { return quality_scaler_.get(); }
-  // TODO(https://crbug.com/webrtc/11222): Don't expose the quality scaler.
-  QualityScaler* quality_scaler() const { return quality_scaler_.get(); }
-
-  void StartCheckForOveruse(VideoEncoder::QpThresholds qp_thresholds) {
-    RTC_DCHECK(!is_started());
-    quality_scaler_ =
-        std::make_unique<QualityScaler>(this, std::move(qp_thresholds));
-  }
-
-  void StopCheckForOveruse() { quality_scaler_.reset(); }
-
-  void SetQpThresholds(VideoEncoder::QpThresholds qp_thresholds) {
-    RTC_DCHECK(is_started());
-    quality_scaler_->SetQpThresholds(std::move(qp_thresholds));
-  }
-
-  bool QpFastFilterLow() {
-    RTC_DCHECK(is_started());
-    return quality_scaler_->QpFastFilterLow();
-  }
-
-  void OnEncodeCompleted(const EncodedImage& encoded_image,
-                         int64_t time_sent_in_us) {
-    if (quality_scaler_ && encoded_image.qp_ >= 0)
-      quality_scaler_->ReportQp(encoded_image.qp_, time_sent_in_us);
-  }
-
-  void OnFrameDropped(EncodedImageCallback::DropReason reason) {
-    if (!quality_scaler_)
-      return;
-    switch (reason) {
-      case EncodedImageCallback::DropReason::kDroppedByMediaOptimizations:
-        quality_scaler_->ReportDroppedFrameByMediaOpt();
-        break;
-      case EncodedImageCallback::DropReason::kDroppedByEncoder:
-        quality_scaler_->ReportDroppedFrameByEncoder();
-        break;
-    }
-  }
-
-  // AdaptationObserverInterface implementation.
-  // TODO(https://crbug.com/webrtc/11222, 11172): This resource also needs to
-  // signal when its stable to support multi-stream aware modules.
-  void AdaptUp(AdaptReason reason) override {
-    RTC_DCHECK_EQ(reason, AdaptReason::kQuality);
-    OnResourceUsageStateMeasured(ResourceUsageState::kUnderuse);
-  }
-  bool AdaptDown(AdaptReason reason) override {
-    RTC_DCHECK_EQ(reason, AdaptReason::kQuality);
-    return OnResourceUsageStateMeasured(ResourceUsageState::kOveruse) !=
-           ResourceListenerResponse::kQualityScalerShouldIncreaseFrequency;
-  }
-
- private:
-  std::unique_ptr<QualityScaler> quality_scaler_;
-};
-
 // VideoSourceRestrictor is responsible for keeping track of current
 // VideoSourceRestrictions and how to modify them in response to adapting up or
 // down. It is not reponsible for determining when we should adapt up or down -
diff --git a/video/overuse_frame_detector_resource_adaptation_module.h b/video/overuse_frame_detector_resource_adaptation_module.h
index 3deb2f7..8e2e5a0 100644
--- a/video/overuse_frame_detector_resource_adaptation_module.h
+++ b/video/overuse_frame_detector_resource_adaptation_module.h
@@ -31,7 +31,9 @@
 #include "rtc_base/experiments/quality_rampup_experiment.h"
 #include "rtc_base/experiments/quality_scaler_settings.h"
 #include "system_wrappers/include/clock.h"
+#include "video/encode_usage_resource.h"
 #include "video/overuse_frame_detector.h"
+#include "video/quality_scaler_resource.h"
 
 namespace webrtc {
 
@@ -123,8 +125,6 @@
       AdaptationObserverInterface::AdaptReason reason);
 
  private:
-  class EncodeUsageResource;
-  class QualityScalerResource;
   class VideoSourceRestrictor;
   class AdaptCounter;
 
diff --git a/video/quality_scaler_resource.cc b/video/quality_scaler_resource.cc
new file mode 100644
index 0000000..729cae3
--- /dev/null
+++ b/video/quality_scaler_resource.cc
@@ -0,0 +1,76 @@
+/*
+ *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "video/quality_scaler_resource.h"
+
+#include <utility>
+
+namespace webrtc {
+
+QualityScalerResource::QualityScalerResource() : quality_scaler_(nullptr) {}
+
+bool QualityScalerResource::is_started() const {
+  return quality_scaler_.get();
+}
+
+void QualityScalerResource::StartCheckForOveruse(
+    VideoEncoder::QpThresholds qp_thresholds) {
+  RTC_DCHECK(!is_started());
+  quality_scaler_ =
+      std::make_unique<QualityScaler>(this, std::move(qp_thresholds));
+}
+
+void QualityScalerResource::StopCheckForOveruse() {
+  quality_scaler_.reset();
+}
+
+void QualityScalerResource::SetQpThresholds(
+    VideoEncoder::QpThresholds qp_thresholds) {
+  RTC_DCHECK(is_started());
+  quality_scaler_->SetQpThresholds(std::move(qp_thresholds));
+}
+
+bool QualityScalerResource::QpFastFilterLow() {
+  RTC_DCHECK(is_started());
+  return quality_scaler_->QpFastFilterLow();
+}
+
+void QualityScalerResource::OnEncodeCompleted(const EncodedImage& encoded_image,
+                                              int64_t time_sent_in_us) {
+  if (quality_scaler_ && encoded_image.qp_ >= 0)
+    quality_scaler_->ReportQp(encoded_image.qp_, time_sent_in_us);
+}
+
+void QualityScalerResource::OnFrameDropped(
+    EncodedImageCallback::DropReason reason) {
+  if (!quality_scaler_)
+    return;
+  switch (reason) {
+    case EncodedImageCallback::DropReason::kDroppedByMediaOptimizations:
+      quality_scaler_->ReportDroppedFrameByMediaOpt();
+      break;
+    case EncodedImageCallback::DropReason::kDroppedByEncoder:
+      quality_scaler_->ReportDroppedFrameByEncoder();
+      break;
+  }
+}
+
+void QualityScalerResource::AdaptUp(AdaptReason reason) {
+  RTC_DCHECK_EQ(reason, AdaptReason::kQuality);
+  OnResourceUsageStateMeasured(ResourceUsageState::kUnderuse);
+}
+
+bool QualityScalerResource::AdaptDown(AdaptReason reason) {
+  RTC_DCHECK_EQ(reason, AdaptReason::kQuality);
+  return OnResourceUsageStateMeasured(ResourceUsageState::kOveruse) !=
+         ResourceListenerResponse::kQualityScalerShouldIncreaseFrequency;
+}
+
+}  // namespace webrtc
diff --git a/video/quality_scaler_resource.h b/video/quality_scaler_resource.h
new file mode 100644
index 0000000..21a1b5b
--- /dev/null
+++ b/video/quality_scaler_resource.h
@@ -0,0 +1,55 @@
+/*
+ *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VIDEO_QUALITY_SCALER_RESOURCE_H_
+#define VIDEO_QUALITY_SCALER_RESOURCE_H_
+
+#include <memory>
+
+#include "api/video_codecs/video_encoder.h"
+#include "call/adaptation/resource.h"
+#include "modules/video_coding/utility/quality_scaler.h"
+
+namespace webrtc {
+
+// Handles interaction with the QualityScaler.
+// TODO(hbos): Add unittests specific to this class, it is currently only tested
+// indirectly by usage in the OveruseFrameDetectorResourceAdaptationModule
+// (which is only tested because of its usage in VideoStreamEncoder); all tests
+// are currently in video_stream_encoder_unittest.cc.
+class QualityScalerResource : public Resource,
+                              public AdaptationObserverInterface {
+ public:
+  QualityScalerResource();
+
+  bool is_started() const;
+
+  void StartCheckForOveruse(VideoEncoder::QpThresholds qp_thresholds);
+  void StopCheckForOveruse();
+
+  void SetQpThresholds(VideoEncoder::QpThresholds qp_thresholds);
+  bool QpFastFilterLow();
+  void OnEncodeCompleted(const EncodedImage& encoded_image,
+                         int64_t time_sent_in_us);
+  void OnFrameDropped(EncodedImageCallback::DropReason reason);
+
+  // AdaptationObserverInterface implementation.
+  // TODO(https://crbug.com/webrtc/11222, 11172): This resource also needs to
+  // signal when its stable to support multi-stream aware modules.
+  void AdaptUp(AdaptReason reason) override;
+  bool AdaptDown(AdaptReason reason) override;
+
+ private:
+  std::unique_ptr<QualityScaler> quality_scaler_;
+};
+
+}  // namespace webrtc
+
+#endif  // VIDEO_QUALITY_SCALER_RESOURCE_H_