Simplify CongestionController.

- Removes the dependency on CallStats.
- Implements Module interface so that we don't have to register
  each internal component to the process thread separately.

R=pbos@webrtc.org, solenberg@webrtc.org

Review URL: https://codereview.webrtc.org/1704983002 .

Cr-Commit-Position: refs/heads/master@{#11655}
diff --git a/webrtc/call/congestion_controller.cc b/webrtc/call/congestion_controller.cc
index 59713d3..e2b9c23 100644
--- a/webrtc/call/congestion_controller.cc
+++ b/webrtc/call/congestion_controller.cc
@@ -19,15 +19,11 @@
 #include "webrtc/base/thread_annotations.h"
 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
 #include "webrtc/modules/pacing/paced_sender.h"
-#include "webrtc/modules/pacing/packet_router.h"
 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h"
 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_abs_send_time.h"
 #include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
-#include "webrtc/modules/remote_bitrate_estimator/remote_estimator_proxy.h"
-#include "webrtc/modules/remote_bitrate_estimator/transport_feedback_adapter.h"
 #include "webrtc/modules/utility/include/process_thread.h"
 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
-#include "webrtc/video/call_stats.h"
 #include "webrtc/video/payload_router.h"
 
 namespace webrtc {
@@ -140,55 +136,46 @@
 
 CongestionController::CongestionController(
     Clock* clock,
-    ProcessThread* process_thread,
-    CallStats* call_stats,
     BitrateObserver* bitrate_observer,
     RemoteBitrateObserver* remote_bitrate_observer)
     : clock_(clock),
-      packet_router_(new PacketRouter()),
       pacer_(new PacedSender(clock_,
-                             packet_router_.get(),
-                             BitrateController::kDefaultStartBitrateKbps,
-                             PacedSender::kDefaultPaceMultiplier *
-                                 BitrateController::kDefaultStartBitrateKbps,
-                             0)),
+             &packet_router_,
+             BitrateController::kDefaultStartBitrateKbps,
+             PacedSender::kDefaultPaceMultiplier *
+                 BitrateController::kDefaultStartBitrateKbps,
+             0)),
       remote_bitrate_estimator_(
           new WrappingBitrateEstimator(remote_bitrate_observer, clock_)),
-      remote_estimator_proxy_(
-          new RemoteEstimatorProxy(clock_, packet_router_.get())),
-      process_thread_(process_thread),
-      call_stats_(call_stats),
       pacer_thread_(ProcessThread::Create("PacerThread")),
       // Constructed last as this object calls the provided callback on
       // construction.
       bitrate_controller_(
           BitrateController::CreateBitrateController(clock_, bitrate_observer)),
+      remote_estimator_proxy_(clock_, &packet_router_),
+      transport_feedback_adapter_(bitrate_controller_.get(), clock_),
       min_bitrate_bps_(RemoteBitrateEstimator::kDefaultMinBitrateBps) {
-  call_stats_->RegisterStatsObserver(remote_bitrate_estimator_.get());
-
+  transport_feedback_adapter_.SetBitrateEstimator(
+      new RemoteBitrateEstimatorAbsSendTime(&transport_feedback_adapter_,
+                                            clock_));
+  transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
+      min_bitrate_bps_);
   pacer_thread_->RegisterModule(pacer_.get());
-  pacer_thread_->RegisterModule(remote_estimator_proxy_.get());
+  pacer_thread_->RegisterModule(&remote_estimator_proxy_);
   pacer_thread_->Start();
-
-  process_thread_->RegisterModule(remote_bitrate_estimator_.get());
-  process_thread_->RegisterModule(bitrate_controller_.get());
 }
 
 CongestionController::~CongestionController() {
   pacer_thread_->Stop();
   pacer_thread_->DeRegisterModule(pacer_.get());
-  pacer_thread_->DeRegisterModule(remote_estimator_proxy_.get());
-  process_thread_->DeRegisterModule(bitrate_controller_.get());
-  process_thread_->DeRegisterModule(remote_bitrate_estimator_.get());
-  call_stats_->DeregisterStatsObserver(remote_bitrate_estimator_.get());
-  if (transport_feedback_adapter_.get())
-    call_stats_->DeregisterStatsObserver(transport_feedback_adapter_.get());
+  pacer_thread_->DeRegisterModule(&remote_estimator_proxy_);
 }
 
 
 void CongestionController::SetBweBitrates(int min_bitrate_bps,
                                           int start_bitrate_bps,
                                           int max_bitrate_bps) {
+  RTC_DCHECK(config_thread_checker_.CalledOnValidThread());
   // TODO(holmer): We should make sure the default bitrates are set to 10 kbps,
   // and that we don't try to set the min bitrate to 0 from any applications.
   // The congestion controller should allow a min bitrate of 0.
@@ -202,12 +189,11 @@
     bitrate_controller_->SetStartBitrate(start_bitrate_bps);
   }
   bitrate_controller_->SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
-  if (remote_bitrate_estimator_.get())
+  if (remote_bitrate_estimator_)
     remote_bitrate_estimator_->SetMinBitrate(min_bitrate_bps);
-  if (transport_feedback_adapter_.get())
-    transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate(
-        min_bitrate_bps);
   min_bitrate_bps_ = min_bitrate_bps;
+  transport_feedback_adapter_.GetBitrateEstimator()->SetMinBitrate(
+      min_bitrate_bps_);
 }
 
 BitrateController* CongestionController::GetBitrateController() const {
@@ -215,27 +201,18 @@
 }
 
 RemoteBitrateEstimator* CongestionController::GetRemoteBitrateEstimator(
-    bool send_side_bwe) const {
-
-  if (send_side_bwe)
-    return remote_estimator_proxy_.get();
-  else
+    bool send_side_bwe) {
+  if (send_side_bwe) {
+    return &remote_estimator_proxy_;
+  } else {
     return remote_bitrate_estimator_.get();
+  }
 }
 
 TransportFeedbackObserver*
 CongestionController::GetTransportFeedbackObserver() {
-  if (transport_feedback_adapter_.get() == nullptr) {
-    transport_feedback_adapter_.reset(new TransportFeedbackAdapter(
-        bitrate_controller_.get(), clock_, process_thread_));
-    transport_feedback_adapter_->SetBitrateEstimator(
-        new RemoteBitrateEstimatorAbsSendTime(transport_feedback_adapter_.get(),
-                                              clock_));
-    transport_feedback_adapter_->GetBitrateEstimator()->SetMinBitrate(
-        min_bitrate_bps_);
-    call_stats_->RegisterStatsObserver(transport_feedback_adapter_.get());
-  }
-  return transport_feedback_adapter_.get();
+  RTC_DCHECK(config_thread_checker_.CalledOnValidThread());
+  return &transport_feedback_adapter_;
 }
 
 void CongestionController::UpdatePacerBitrate(int bitrate_kbps,
@@ -257,9 +234,24 @@
 }
 
 void CongestionController::OnSentPacket(const rtc::SentPacket& sent_packet) {
-  if (transport_feedback_adapter_) {
-    transport_feedback_adapter_->OnSentPacket(sent_packet.packet_id,
-                                              sent_packet.send_time_ms);
-  }
+  transport_feedback_adapter_.OnSentPacket(sent_packet.packet_id,
+                                            sent_packet.send_time_ms);
 }
+
+void CongestionController::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
+  remote_bitrate_estimator_->OnRttUpdate(avg_rtt_ms, max_rtt_ms);
+  transport_feedback_adapter_.OnRttUpdate(avg_rtt_ms, max_rtt_ms);
+}
+
+int64_t CongestionController::TimeUntilNextProcess() {
+  return std::min(bitrate_controller_->TimeUntilNextProcess(),
+                  remote_bitrate_estimator_->TimeUntilNextProcess());
+}
+
+int32_t CongestionController::Process() {
+  bitrate_controller_->Process();
+  remote_bitrate_estimator_->Process();
+  return 0;
+}
+
 }  // namespace webrtc