Fix for calling OnNetworkChanged too often.
Review URL: https://webrtc-codereview.appspot.com/508006

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2085 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/src/modules/bitrate_controller/bitrate_controller_impl.cc b/src/modules/bitrate_controller/bitrate_controller_impl.cc
index f219dd6..30733a3 100644
--- a/src/modules/bitrate_controller/bitrate_controller_impl.cc
+++ b/src/modules/bitrate_controller/bitrate_controller_impl.cc
@@ -207,5 +207,9 @@
     max_it = list_max_bitrates.begin();
   }
 }
+
+bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
+  return bandwidth_estimation_.AvailableBandwidth(bandwidth);
+}
 }  // namespace webrtc
 
diff --git a/src/modules/bitrate_controller/bitrate_controller_impl.h b/src/modules/bitrate_controller/bitrate_controller_impl.h
index d2a59e8..acf28f8 100644
--- a/src/modules/bitrate_controller/bitrate_controller_impl.h
+++ b/src/modules/bitrate_controller/bitrate_controller_impl.h
@@ -33,6 +33,8 @@
   explicit BitrateControllerImpl();
   virtual ~BitrateControllerImpl();
 
+  virtual bool AvailableBandwidth(uint32_t* bandwidth) const;
+
   virtual RtcpBandwidthObserver* CreateRtcpBandwidthObserver();
 
   virtual void SetBitrateObserver(BitrateObserver* observer,
diff --git a/src/modules/bitrate_controller/include/bitrate_controller.h b/src/modules/bitrate_controller/include/bitrate_controller.h
index c3e1dfa..002ab8f 100644
--- a/src/modules/bitrate_controller/include/bitrate_controller.h
+++ b/src/modules/bitrate_controller/include/bitrate_controller.h
@@ -30,12 +30,9 @@
                                 const uint8_t fraction_loss,  // 0 - 255.
                                 const uint32_t rtt) = 0;
 
- protected:
   virtual ~BitrateObserver() {}
 };
 
-// TODO(pwestin) move code from vie_remb in here
-
 class BitrateController {
 /*
  * This class collects feedback from all streams sent to a peer (via
@@ -49,6 +46,8 @@
 
   virtual RtcpBandwidthObserver* CreateRtcpBandwidthObserver() = 0;
 
+  virtual bool AvailableBandwidth(uint32_t* bandwidth) const = 0;
+
   /*
   *  Set the start and max send bitrate used by the bandwidth management.
   *
diff --git a/src/modules/bitrate_controller/send_side_bandwidth_estimation.cc b/src/modules/bitrate_controller/send_side_bandwidth_estimation.cc
index 12c5f42..1fc08c5 100644
--- a/src/modules/bitrate_controller/send_side_bandwidth_estimation.cc
+++ b/src/modules/bitrate_controller/send_side_bandwidth_estimation.cc
@@ -113,9 +113,8 @@
   }
   // Keep for next time.
   last_fraction_loss_ = *loss;
-
-  uint32_t bitrate = ShapeSimple(*loss, rtt, now_ms);
-  if (bitrate == 0) {
+  uint32_t bitrate = 0;
+  if (!ShapeSimple(*loss, rtt, now_ms, &bitrate)) {
     // No change.
     return false;
   }
@@ -158,22 +157,24 @@
   return (static_cast<uint32_t>(X * 8));  // bits/second
 }
 
-uint32_t SendSideBandwidthEstimation::ShapeSimple(uint8_t loss, uint32_t rtt,
-                                                  uint32_t now_ms) {
+bool SendSideBandwidthEstimation::ShapeSimple(const uint8_t loss,
+                                              const uint32_t rtt,
+                                              const uint32_t now_ms,
+                                              uint32_t* bitrate) {
   uint32_t new_bitrate = 0;
   bool reducing = false;
 
   // Limit the rate increases to once a kBWEIncreaseIntervalMs.
   if (loss <= 5) {
     if ((now_ms - time_last_increase_) < kBWEIncreaseIntervalMs) {
-      return bitrate_;
+      return false;
     }
     time_last_increase_ = now_ms;
   }
   // Limit the rate decreases to once a kBWEDecreaseIntervalMs + rtt.
   if (loss > 26) {
     if ((now_ms - time_last_decrease_) < kBWEDecreaseIntervalMs + rtt) {
-      return bitrate_;
+      return false;
     }
     time_last_decrease_ = now_ms;
   }
@@ -218,6 +219,7 @@
                  min_bitrate_configured_ / 1000, new_bitrate / 1000);
     new_bitrate = min_bitrate_configured_;
   }
-  return new_bitrate;
+  *bitrate = new_bitrate;
+  return true;
 }
 }  // namespace webrtc
diff --git a/src/modules/bitrate_controller/send_side_bandwidth_estimation.h b/src/modules/bitrate_controller/send_side_bandwidth_estimation.h
index aa49016..9df9ccd 100644
--- a/src/modules/bitrate_controller/send_side_bandwidth_estimation.h
+++ b/src/modules/bitrate_controller/send_side_bandwidth_estimation.h
@@ -43,7 +43,8 @@
   void SetMinMaxBitrate(const uint32_t min_bitrate, const uint32_t max_bitrate);
 
  private:
-  uint32_t ShapeSimple(uint8_t loss, uint32_t rtt, uint32_t now_ms);
+  bool ShapeSimple(const uint8_t loss, const uint32_t rtt,
+                   const uint32_t now_ms, uint32_t* bitrate);
 
   uint32_t CalcTFRCbps(uint16_t rtt, uint8_t loss);
 
diff --git a/src/modules/bitrate_controller/test/test_bitrate_controller.cc b/src/modules/bitrate_controller/test/test_bitrate_controller.cc
index 0e77f44..281f9e9 100644
--- a/src/modules/bitrate_controller/test/test_bitrate_controller.cc
+++ b/src/modules/bitrate_controller/test/test_bitrate_controller.cc
@@ -74,13 +74,15 @@
 
   // Test start bitrate.
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 1, 1);
-  EXPECT_EQ(bitrate_observer.last_bitrate, 200000u);
+  EXPECT_EQ(bitrate_observer.last_bitrate, 0u);
   EXPECT_EQ(bitrate_observer.last_fraction_loss, 0);
-  EXPECT_EQ(bitrate_observer.last_rtt, 50u);
+  EXPECT_EQ(bitrate_observer.last_rtt, 0u);
 
   // Test bitrate increase 8% per second.
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 21, 1001);
   EXPECT_EQ(bitrate_observer.last_bitrate, 217000u);
+  EXPECT_EQ(bitrate_observer.last_fraction_loss, 0);
+  EXPECT_EQ(bitrate_observer.last_rtt, 50u);
 
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 41, 2001);
   EXPECT_EQ(bitrate_observer.last_bitrate, 235360u);
@@ -88,7 +90,7 @@
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 61, 3001);
   EXPECT_EQ(bitrate_observer.last_bitrate, 255189u);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 81, 4001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 801, 4001);
   EXPECT_EQ(bitrate_observer.last_bitrate, 276604u);
 
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 101, 5001);
@@ -124,14 +126,16 @@
   // Test start bitrate.
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 1, 1);
   second_bandwidth_observer->OnReceivedRtcpReceiverReport(1, 0, 100, 1, 1);
-  EXPECT_EQ(bitrate_observer.last_bitrate, 200000u);
+  EXPECT_EQ(bitrate_observer.last_bitrate, 0u);
   EXPECT_EQ(bitrate_observer.last_fraction_loss, 0);
-  EXPECT_EQ(bitrate_observer.last_rtt, 100u);
+  EXPECT_EQ(bitrate_observer.last_rtt, 0u);
 
   // Test bitrate increase 8% per second.
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 21, 1001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 21, 501);
   second_bandwidth_observer->OnReceivedRtcpReceiverReport(1, 0, 100, 21, 1001);
   EXPECT_EQ(bitrate_observer.last_bitrate, 217000u);
+  EXPECT_EQ(bitrate_observer.last_fraction_loss, 0);
+  EXPECT_EQ(bitrate_observer.last_rtt, 100u);
 
   // Extra report should not change estimate.
   second_bandwidth_observer->OnReceivedRtcpReceiverReport(1, 0, 100, 31, 1501);
@@ -139,6 +143,7 @@
 
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 41, 2001);
   EXPECT_EQ(bitrate_observer.last_bitrate, 235360u);
+
   // Second report should not change estimate.
   second_bandwidth_observer->OnReceivedRtcpReceiverReport(1, 0, 100, 41, 2001);
   EXPECT_EQ(bitrate_observer.last_bitrate, 235360u);
@@ -183,6 +188,9 @@
 
   // Test too low start bitrate, hence lower than sum of min.
   bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 1, 1);
+
+  // Test bitrate increase 8% per second, distributed equally.
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 21, 1001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 100000u);
   EXPECT_EQ(bitrate_observer_1.last_fraction_loss, 0);
   EXPECT_EQ(bitrate_observer_1.last_rtt, 50u);
@@ -191,48 +199,47 @@
   EXPECT_EQ(bitrate_observer_2.last_fraction_loss, 0);
   EXPECT_EQ(bitrate_observer_2.last_rtt, 50u);
 
-  // Test bitrate increase 8% per second, distributed equaly.
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 21, 1001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 41, 2001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 112500u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 212500u);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 41, 2001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 61, 3001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 126000u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 226000u);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 61, 3001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 81, 4001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 140580u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 240580u);
 
   // Check that the bitrate sum honor our REMB.
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 81, 4001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 101, 5001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 150000u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 250000u);
 
   // Remove REMB cap, higher than sum of max.
   bandwidth_observer_->OnReceivedEstimatedBitrate(700000);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 101, 5001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 121, 6001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 166500u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 266500u);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 121, 6001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 141, 7001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 184320u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 284320u);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 141, 7001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 161, 8001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 207130u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 300000u);  // Max cap.
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 161, 8001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 181, 9001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 248700u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 300000u);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 181, 9001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 201, 10001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 293596u);
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 300000u);
 
-  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 201, 10001);
+  bandwidth_observer_->OnReceivedRtcpReceiverReport(1, 0, 50, 221, 11001);
   EXPECT_EQ(bitrate_observer_1.last_bitrate, 300000u);  // Max cap.
   EXPECT_EQ(bitrate_observer_2.last_bitrate, 300000u);