Moving the pacer and the pacer thread to ChannelGroup.

This means all channels within the same group will share the same pacing queue and scheduler. It also means padding will be computed and sent by a single pacer. To accomplish this I also introduce a PacketRouter which finds the RTP module which owns the packet to be paced out.

BUG=4323
R=mflodman@webrtc.org, pbos@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/45549004

Cr-Commit-Position: refs/heads/master@{#8864}
diff --git a/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc b/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc
index d6f64fb..d54da99 100644
--- a/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc
+++ b/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc
@@ -94,10 +94,11 @@
       last_fraction_loss_(0),
       last_rtt_ms_(0),
       last_reserved_bitrate_bps_(0) {
-}
-
-BitrateControllerImpl::~BitrateControllerImpl() {
-  delete critsect_;
+  // This calls the observer_, which means that the observer provided by the
+  // user must be ready to accept a bitrate update when it constructs the
+  // controller. We do this to avoid having to keep synchronized initial values
+  // in both the controller and the allocator.
+  MaybeTriggerOnNetworkChanged();
 }
 
 RtcpBandwidthObserver* BitrateControllerImpl::CreateRtcpBandwidthObserver() {
@@ -105,19 +106,25 @@
 }
 
 void BitrateControllerImpl::SetStartBitrate(int start_bitrate_bps) {
-  CriticalSectionScoped cs(critsect_);
-  bandwidth_estimation_.SetSendBitrate(start_bitrate_bps);
+  {
+    CriticalSectionScoped cs(critsect_.get());
+    bandwidth_estimation_.SetSendBitrate(start_bitrate_bps);
+  }
+  MaybeTriggerOnNetworkChanged();
 }
 
 void BitrateControllerImpl::SetMinMaxBitrate(int min_bitrate_bps,
                                              int max_bitrate_bps) {
-  CriticalSectionScoped cs(critsect_);
-  bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
+  {
+    CriticalSectionScoped cs(critsect_.get());
+    bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
+  }
+  MaybeTriggerOnNetworkChanged();
 }
 
 void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
   {
-    CriticalSectionScoped cs(critsect_);
+    CriticalSectionScoped cs(critsect_.get());
     reserved_bitrate_bps_ = reserved_bitrate_bps;
   }
   MaybeTriggerOnNetworkChanged();
@@ -125,7 +132,7 @@
 
 void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) {
   {
-    CriticalSectionScoped cs(critsect_);
+    CriticalSectionScoped cs(critsect_.get());
     bandwidth_estimation_.UpdateReceiverEstimate(bitrate);
   }
   MaybeTriggerOnNetworkChanged();
@@ -133,7 +140,7 @@
 
 int64_t BitrateControllerImpl::TimeUntilNextProcess() {
   const int64_t kBitrateControllerUpdateIntervalMs = 25;
-  CriticalSectionScoped cs(critsect_);
+  CriticalSectionScoped cs(critsect_.get());
   int64_t time_since_update_ms =
       clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
   return std::max<int64_t>(
@@ -144,7 +151,7 @@
   if (TimeUntilNextProcess() > 0)
     return 0;
   {
-    CriticalSectionScoped cs(critsect_);
+    CriticalSectionScoped cs(critsect_.get());
     bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
   }
   MaybeTriggerOnNetworkChanged();
@@ -158,7 +165,7 @@
     int number_of_packets,
     int64_t now_ms) {
   {
-    CriticalSectionScoped cs(critsect_);
+    CriticalSectionScoped cs(critsect_.get());
     bandwidth_estimation_.UpdateReceiverBlock(fraction_loss, rtt,
                                               number_of_packets, now_ms);
   }
@@ -169,36 +176,44 @@
   uint32_t bitrate;
   uint8_t fraction_loss;
   int64_t rtt;
-  bool new_bitrate = false;
-  {
-    CriticalSectionScoped cs(critsect_);
-    bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
-    bitrate -= std::min(bitrate, reserved_bitrate_bps_);
-    bitrate = std::max(bitrate, bandwidth_estimation_.GetMinBitrate());
-
-    if (bitrate != last_bitrate_bps_ || fraction_loss != last_fraction_loss_ ||
-        rtt != last_rtt_ms_ ||
-        last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
-      last_bitrate_bps_ = bitrate;
-      last_fraction_loss_ = fraction_loss;
-      last_rtt_ms_ = rtt;
-      last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
-      new_bitrate = true;
-    }
-  }
-  if (new_bitrate)
+  if (GetNetworkParameters(&bitrate, &fraction_loss, &rtt))
     observer_->OnNetworkChanged(bitrate, fraction_loss, rtt);
 }
 
+bool BitrateControllerImpl::GetNetworkParameters(uint32_t* bitrate,
+                                                 uint8_t* fraction_loss,
+                                                 int64_t* rtt) {
+  CriticalSectionScoped cs(critsect_.get());
+  int current_bitrate;
+  bandwidth_estimation_.CurrentEstimate(&current_bitrate, fraction_loss, rtt);
+  *bitrate = current_bitrate;
+  *bitrate -= std::min(*bitrate, reserved_bitrate_bps_);
+  *bitrate =
+      std::max<uint32_t>(*bitrate, bandwidth_estimation_.GetMinBitrate());
+
+  bool new_bitrate = false;
+  if (*bitrate != last_bitrate_bps_ || *fraction_loss != last_fraction_loss_ ||
+      *rtt != last_rtt_ms_ ||
+      last_reserved_bitrate_bps_ != reserved_bitrate_bps_) {
+    last_bitrate_bps_ = *bitrate;
+    last_fraction_loss_ = *fraction_loss;
+    last_rtt_ms_ = *rtt;
+    last_reserved_bitrate_bps_ = reserved_bitrate_bps_;
+    new_bitrate = true;
+  }
+  return new_bitrate;
+}
+
 bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
-  CriticalSectionScoped cs(critsect_);
-  uint32_t bitrate;
+  CriticalSectionScoped cs(critsect_.get());
+  int bitrate;
   uint8_t fraction_loss;
   int64_t rtt;
   bandwidth_estimation_.CurrentEstimate(&bitrate, &fraction_loss, &rtt);
-  if (bitrate) {
-    *bandwidth = bitrate - std::min(bitrate, reserved_bitrate_bps_);
-    *bandwidth = std::max(*bandwidth, bandwidth_estimation_.GetMinBitrate());
+  if (bitrate > 0) {
+    bitrate = bitrate - std::min<int>(bitrate, reserved_bitrate_bps_);
+    bitrate = std::max(bitrate, bandwidth_estimation_.GetMinBitrate());
+    *bandwidth = bitrate;
     return true;
   }
   return false;