Break out RemoteBitrateEstimator from RtpRtcp module and make RemoteBitrateEstimator::Process trigger new REMB messages.

Also make sure RTT is computed independently of whether it's time to send RTCP messages or not.

BUG=1298

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3455 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/interface/module_common_types.h b/webrtc/modules/interface/module_common_types.h
index a33e7ef..5815996 100644
--- a/webrtc/modules/interface/module_common_types.h
+++ b/webrtc/modules/interface/module_common_types.h
@@ -320,6 +320,16 @@
   FecMaskType fec_mask_type;
 };
 
+// Interface used by the CallStats class to distribute call statistics.
+// Callbacks will be triggered as soon as the class has been registered to a
+// CallStats object using RegisterStatsObserver.
+class StatsObserver {
+ public:
+  virtual void OnRttUpdate(uint32_t rtt_ms) = 0;
+
+  virtual ~StatsObserver() {}
+};
+
 // class describing a complete, or parts of an encoded frame.
 class EncodedVideoData
 {
diff --git a/webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h b/webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h
index 395ea7e..0ad64dd 100644
--- a/webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h
+++ b/webrtc/modules/remote_bitrate_estimator/include/bwe_defines.h
@@ -19,9 +19,9 @@
 namespace webrtc {
 enum BandwidthUsage
 {
-    kBwNormal,
-    kBwOverusing,
-    kBwUnderusing
+    kBwNormal = 0,
+    kBwUnderusing = 1,
+    kBwOverusing = 2,
 };
 
 enum RateControlState
diff --git a/webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h b/webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h
index 0fa9641..0a1a92c 100644
--- a/webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h
+++ b/webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h
@@ -16,11 +16,15 @@
 #include <map>
 #include <vector>
 
-#include "common_types.h"
-#include "typedefs.h"
+#include "webrtc/common_types.h"
+#include "webrtc/modules/interface/module.h"
+#include "webrtc/modules/interface/module_common_types.h"
+#include "webrtc/typedefs.h"
 
 namespace webrtc {
 
+class Clock;
+
 // RemoteBitrateObserver is used to signal changes in bitrate estimates for
 // the incoming streams.
 class RemoteBitrateObserver {
@@ -33,7 +37,7 @@
   virtual ~RemoteBitrateObserver() {}
 };
 
-class RemoteBitrateEstimator {
+class RemoteBitrateEstimator : public StatsObserver, public Module {
  public:
   enum EstimationMode {
     kMultiStreamEstimation,
@@ -42,9 +46,10 @@
 
   virtual ~RemoteBitrateEstimator() {}
 
-  static RemoteBitrateEstimator* Create(RemoteBitrateObserver* observer,
-                                        const OverUseDetectorOptions& options,
-                                        EstimationMode mode);
+  static RemoteBitrateEstimator* Create(const OverUseDetectorOptions& options,
+                                        EstimationMode mode,
+                                        RemoteBitrateObserver* observer,
+                                        Clock* clock);
 
   // Stores an RTCP SR (NTP, RTP timestamp) tuple for a specific SSRC to be used
   // in future RTP timestamp to NTP time conversions. As soon as any SSRC has
@@ -61,13 +66,6 @@
                               int64_t arrival_time,
                               uint32_t rtp_timestamp) = 0;
 
-  // Triggers a new estimate calculation.
-  virtual void UpdateEstimate(unsigned int ssrc, int64_t time_now) = 0;
-
-  // Set the current round-trip time experienced by the streams going into this
-  // estimator.
-  virtual void SetRtt(unsigned int rtt) = 0;
-
   // Removes all data for |ssrc|.
   virtual void RemoveStream(unsigned int ssrc) = 0;
 
@@ -76,6 +74,10 @@
   // currently being received and of which the bitrate estimate is based upon.
   virtual bool LatestEstimate(std::vector<unsigned int>* ssrcs,
                               unsigned int* bitrate_bps) const = 0;
+
+ protected:
+  static const int kProcessIntervalMs = 1000;
+  static const int kStreamTimeOutMs = 2000;
 };
 
 }  // namespace webrtc
diff --git a/webrtc/modules/remote_bitrate_estimator/overuse_detector.cc b/webrtc/modules/remote_bitrate_estimator/overuse_detector.cc
index dfd6b90..0fbf6f1 100644
--- a/webrtc/modules/remote_bitrate_estimator/overuse_detector.cc
+++ b/webrtc/modules/remote_bitrate_estimator/overuse_detector.cc
@@ -43,7 +43,8 @@
       prev_offset_(0.0),
       time_over_using_(-1),
       over_use_counter_(0),
-      hypothesis_(kBwNormal)
+      hypothesis_(kBwNormal),
+      time_of_last_received_packet_(-1)
 #ifdef WEBRTC_BWE_MATLAB
       , plots_()
 #endif
@@ -80,6 +81,7 @@
                              int64_t timestamp_ms,
                              uint32_t timestamp,
                              const int64_t now_ms) {
+  time_of_last_received_packet_ = now_ms;
 #ifdef WEBRTC_BWE_MATLAB
   // Create plots
   const int64_t startTimeMs = nowMS;
@@ -166,6 +168,10 @@
   }
 }
 
+int64_t OveruseDetector::time_of_last_received_packet() const {
+  return time_of_last_received_packet_;
+}
+
 void OveruseDetector::SwitchTimeBase() {
   current_frame_.size = 0;
   current_frame_.complete_time_ms = -1;
diff --git a/webrtc/modules/remote_bitrate_estimator/overuse_detector.h b/webrtc/modules/remote_bitrate_estimator/overuse_detector.h
index 67b998d..a8a038c 100644
--- a/webrtc/modules/remote_bitrate_estimator/overuse_detector.h
+++ b/webrtc/modules/remote_bitrate_estimator/overuse_detector.h
@@ -23,6 +23,8 @@
 namespace webrtc {
 enum RateControlRegion;
 
+// This class is assumed to be protected by the owner if used by multiple
+// threads.
 class OveruseDetector {
  public:
   explicit OveruseDetector(const OverUseDetectorOptions& options);
@@ -34,6 +36,7 @@
   BandwidthUsage State() const;
   double NoiseVar() const;
   void SetRateControlRegion(RateControlRegion region);
+  int64_t time_of_last_received_packet() const;
 
  private:
   struct FrameSample {
@@ -100,6 +103,7 @@
   double time_over_using_;
   uint16_t over_use_counter_;
   BandwidthUsage hypothesis_;
+  int64_t time_of_last_received_packet_;
 #ifdef WEBRTC_BWE_MATLAB
   DebugPlots plots_;
 #endif
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.cc b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.cc
index 710c412..a69a899 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.cc
+++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.cc
@@ -8,38 +8,43 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.h"
+#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.h"
 
-#include "modules/remote_bitrate_estimator/include/rtp_to_ntp.h"
-#include "modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
-#include "system_wrappers/interface/tick_util.h"
+#include "webrtc/modules/remote_bitrate_estimator/include/rtp_to_ntp.h"
+#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
+#include "webrtc/system_wrappers/interface/clock.h"
+#include "webrtc/system_wrappers/interface/tick_util.h"
 
 namespace webrtc {
 
 RemoteBitrateEstimator* RemoteBitrateEstimator::Create(
-    RemoteBitrateObserver* observer,
     const OverUseDetectorOptions& options,
-    EstimationMode mode) {
+    EstimationMode mode,
+    RemoteBitrateObserver* observer,
+    Clock* clock) {
   switch (mode) {
     case kMultiStreamEstimation:
-      return new RemoteBitrateEstimatorMultiStream(observer, options);
+      return new RemoteBitrateEstimatorMultiStream(options, observer, clock);
     case kSingleStreamEstimation:
-      return new RemoteBitrateEstimatorSingleStream(observer, options);
+      return new RemoteBitrateEstimatorSingleStream(options, observer, clock);
   }
   return NULL;
 }
 
 RemoteBitrateEstimatorMultiStream::RemoteBitrateEstimatorMultiStream(
+    const OverUseDetectorOptions& options,
     RemoteBitrateObserver* observer,
-    const OverUseDetectorOptions& options)
-    : remote_rate_(),
+    Clock* clock)
+    : clock_(clock),
+      remote_rate_(),
       overuse_detector_(options),
       incoming_bitrate_(),
       observer_(observer),
       streams_(),
       crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
       initial_ssrc_(0),
-      multi_stream_(false) {
+      multi_stream_(false),
+      last_process_time_(-1) {
   assert(observer_);
 }
 
@@ -108,16 +113,45 @@
   }
   overuse_detector_.Update(payload_size, timestamp_in_ms, rtp_timestamp,
                            arrival_time);
-  if (prior_state != kBwOverusing &&
-      overuse_detector_.State() == kBwOverusing) {
-    // The first overuse should immediately trigger a new estimate.
-    UpdateEstimate(1, arrival_time);
+  if (overuse_detector_.State() == kBwOverusing) {
+    unsigned int incoming_bitrate = incoming_bitrate_.BitRate(arrival_time);
+    if (prior_state != kBwOverusing ||
+        remote_rate_.TimeToReduceFurther(arrival_time, incoming_bitrate)) {
+      // The first overuse should immediately trigger a new estimate.
+      // We also have to update the estimate immediately if we are overusing
+      // and the target bitrate is too high compared to what we are receiving.
+      UpdateEstimate(arrival_time);
+    }
   }
 }
 
-void RemoteBitrateEstimatorMultiStream::UpdateEstimate(unsigned int ssrc,
-                                                       int64_t time_now) {
+int32_t RemoteBitrateEstimatorMultiStream::Process() {
+  if (TimeUntilNextProcess() > 0) {
+    return 0;
+  }
+  UpdateEstimate(clock_->TimeInMilliseconds());
+  last_process_time_ = clock_->TimeInMilliseconds();
+  return 0;
+}
+
+int32_t RemoteBitrateEstimatorMultiStream::TimeUntilNextProcess() {
+  if (last_process_time_ < 0) {
+    return 0;
+  }
+  return last_process_time_ + kProcessIntervalMs - clock_->TimeInMilliseconds();
+}
+
+void RemoteBitrateEstimatorMultiStream::UpdateEstimate(int64_t time_now) {
   CriticalSectionScoped cs(crit_sect_.get());
+  const int64_t time_of_last_received_packet =
+      overuse_detector_.time_of_last_received_packet();
+  if (time_of_last_received_packet >= 0 &&
+      time_now - time_of_last_received_packet > kStreamTimeOutMs) {
+    // This over-use detector hasn't received packets for |kStreamTimeOutMs|
+    // milliseconds and is considered stale.
+    remote_rate_.Reset();
+    return;
+  }
   const RateControlInput input(overuse_detector_.State(),
                                incoming_bitrate_.BitRate(time_now),
                                overuse_detector_.NoiseVar());
@@ -133,7 +167,7 @@
   overuse_detector_.SetRateControlRegion(region);
 }
 
-void RemoteBitrateEstimatorMultiStream::SetRtt(unsigned int rtt) {
+void RemoteBitrateEstimatorMultiStream::OnRttUpdate(uint32_t rtt) {
   CriticalSectionScoped cs(crit_sect_.get());
   remote_rate_.SetRtt(rtt);
 }
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.h b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.h
index 088ef01..713b961 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.h
+++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_multi_stream.h
@@ -27,10 +27,13 @@
 
 namespace webrtc {
 
+class Clock;
+
 class RemoteBitrateEstimatorMultiStream : public RemoteBitrateEstimator {
  public:
-  RemoteBitrateEstimatorMultiStream(RemoteBitrateObserver* observer,
-                                    const OverUseDetectorOptions& options);
+  RemoteBitrateEstimatorMultiStream(const OverUseDetectorOptions& options,
+                                    RemoteBitrateObserver* observer,
+                                    Clock* clock);
 
   ~RemoteBitrateEstimatorMultiStream() {}
 
@@ -52,11 +55,12 @@
                       uint32_t rtp_timestamp);
 
   // Triggers a new estimate calculation.
-  void UpdateEstimate(unsigned int ssrc, int64_t time_now);
-
-  // Set the current round-trip time experienced by the streams going into this
-  // estimator.
-  void SetRtt(unsigned int rtt);
+  // Implements the Module interface.
+  virtual int32_t Process();
+  virtual int32_t TimeUntilNextProcess();
+  // Set the current round-trip time experienced by the stream.
+  // Implements the StatsObserver interface.
+  virtual void OnRttUpdate(uint32_t rtt);
 
   // Removes all data for |ssrc|.
   void RemoveStream(unsigned int ssrc);
@@ -70,8 +74,12 @@
  private:
   typedef std::map<unsigned int, synchronization::RtcpList> StreamMap;
 
+  // Triggers a new estimate calculation.
+  void UpdateEstimate(int64_t time_now);
+
   void GetSsrcs(std::vector<unsigned int>* ssrcs) const;
 
+  Clock* clock_;
   RemoteRateControl remote_rate_;
   OveruseDetector overuse_detector_;
   BitRateStats incoming_bitrate_;
@@ -80,6 +88,7 @@
   scoped_ptr<CriticalSectionWrapper> crit_sect_;
   unsigned int initial_ssrc_;
   bool multi_stream_;
+  int32_t last_process_time_;
 
   DISALLOW_COPY_AND_ASSIGN(RemoteBitrateEstimatorMultiStream);
 };
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc
index a9d1ece..6098b2f 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc
+++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.cc
@@ -8,17 +8,21 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
+#include "webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h"
 
-#include "system_wrappers/interface/tick_util.h"
+#include "webrtc/system_wrappers/interface/clock.h"
 
 namespace webrtc {
 
 RemoteBitrateEstimatorSingleStream::RemoteBitrateEstimatorSingleStream(
-    RemoteBitrateObserver* observer, const OverUseDetectorOptions& options)
+    const OverUseDetectorOptions& options,
+    RemoteBitrateObserver* observer,
+    Clock* clock)
     : options_(options),
+      clock_(clock),
       observer_(observer),
-      crit_sect_(CriticalSectionWrapper::CreateCriticalSection()) {
+      crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
+      last_process_time_(-1) {
   assert(observer_);
 }
 
@@ -45,37 +49,80 @@
   incoming_bitrate_.Update(payload_size, arrival_time);
   const BandwidthUsage prior_state = overuse_detector->State();
   overuse_detector->Update(payload_size, -1, rtp_timestamp, arrival_time);
-  if (prior_state != overuse_detector->State() &&
-      overuse_detector->State() == kBwOverusing) {
-    // The first overuse should immediately trigger a new estimate.
-    UpdateEstimate(ssrc, arrival_time);
+  if (overuse_detector->State() == kBwOverusing) {
+    unsigned int incoming_bitrate = incoming_bitrate_.BitRate(arrival_time);
+    if (prior_state != kBwOverusing ||
+        remote_rate_.TimeToReduceFurther(arrival_time, incoming_bitrate)) {
+      // The first overuse should immediately trigger a new estimate.
+      // We also have to update the estimate immediately if we are overusing
+      // and the target bitrate is too high compared to what we are receiving.
+      UpdateEstimate(arrival_time);
+    }
   }
 }
 
-void RemoteBitrateEstimatorSingleStream::UpdateEstimate(unsigned int ssrc,
-                                                        int64_t time_now) {
+int32_t RemoteBitrateEstimatorSingleStream::Process() {
+  if (TimeUntilNextProcess() > 0) {
+    return 0;
+  }
+  UpdateEstimate(clock_->TimeInMilliseconds());
+  last_process_time_ = clock_->TimeInMilliseconds();
+  return 0;
+}
+
+int32_t RemoteBitrateEstimatorSingleStream::TimeUntilNextProcess() {
+  if (last_process_time_ < 0) {
+    return 0;
+  }
+  return last_process_time_ + kProcessIntervalMs - clock_->TimeInMilliseconds();
+}
+
+void RemoteBitrateEstimatorSingleStream::UpdateEstimate(int64_t time_now) {
   CriticalSectionScoped cs(crit_sect_.get());
-  SsrcOveruseDetectorMap::iterator it = overuse_detectors_.find(ssrc);
-  if (it == overuse_detectors_.end()) {
+  BandwidthUsage bw_state = kBwNormal;
+  double sum_noise_var = 0.0;
+  SsrcOveruseDetectorMap::iterator it = overuse_detectors_.begin();
+  while (it != overuse_detectors_.end()) {
+    const int64_t time_of_last_received_packet =
+         it->second.time_of_last_received_packet();
+    if (time_of_last_received_packet >= 0 &&
+        time_now - time_of_last_received_packet > kStreamTimeOutMs) {
+      // This over-use detector hasn't received packets for |kStreamTimeOutMs|
+      // milliseconds and is considered stale.
+      overuse_detectors_.erase(it++);
+    } else {
+      sum_noise_var += it->second.NoiseVar();
+      // Make sure that we trigger an over-use if any of the over-use detectors
+      // is detecting over-use.
+      if (it->second.State() > bw_state) {
+        bw_state = it->second.State();
+      }
+      ++it;
+    }
+  }
+  // We can't update the estimate if we don't have any active streams.
+  if (overuse_detectors_.empty()) {
+    remote_rate_.Reset();
     return;
   }
-  OveruseDetector* overuse_detector = &it->second;
-  const RateControlInput input(overuse_detector->State(),
+  double mean_noise_var = sum_noise_var /
+      static_cast<double>(overuse_detectors_.size());
+  const RateControlInput input(bw_state,
                                incoming_bitrate_.BitRate(time_now),
-                               overuse_detector->NoiseVar());
+                               mean_noise_var);
   const RateControlRegion region = remote_rate_.Update(&input, time_now);
   unsigned int target_bitrate = remote_rate_.UpdateBandwidthEstimate(time_now);
   if (remote_rate_.ValidEstimate()) {
     std::vector<unsigned int> ssrcs;
     GetSsrcs(&ssrcs);
-    if (!ssrcs.empty()) {
-      observer_->OnReceiveBitrateChanged(&ssrcs, target_bitrate);
-    }
+    observer_->OnReceiveBitrateChanged(&ssrcs, target_bitrate);
   }
-  overuse_detector->SetRateControlRegion(region);
+  for (it = overuse_detectors_.begin(); it != overuse_detectors_.end(); ++it) {
+    it->second.SetRateControlRegion(region);
+  }
 }
 
-void RemoteBitrateEstimatorSingleStream::SetRtt(unsigned int rtt) {
+void RemoteBitrateEstimatorSingleStream::OnRttUpdate(uint32_t rtt) {
   CriticalSectionScoped cs(crit_sect_.get());
   remote_rate_.SetRtt(rtt);
 }
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h
index 46819db..575ed09 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h
+++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_single_stream.h
@@ -15,20 +15,25 @@
 
 #include <map>
 
-#include "modules/remote_bitrate_estimator/bitrate_estimator.h"
-#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
-#include "modules/remote_bitrate_estimator/overuse_detector.h"
-#include "modules/remote_bitrate_estimator/remote_rate_control.h"
-#include "system_wrappers/interface/critical_section_wrapper.h"
-#include "system_wrappers/interface/scoped_ptr.h"
-#include "typedefs.h"
+#include "webrtc/modules/remote_bitrate_estimator/bitrate_estimator.h"
+#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
+#include "webrtc/modules/remote_bitrate_estimator/overuse_detector.h"
+#include "webrtc/modules/remote_bitrate_estimator/remote_rate_control.h"
+#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
+#include "webrtc/system_wrappers/interface/scoped_ptr.h"
+#include "webrtc/typedefs.h"
 
 namespace webrtc {
 
+class Clock;
+
 class RemoteBitrateEstimatorSingleStream : public RemoteBitrateEstimator {
  public:
-  RemoteBitrateEstimatorSingleStream(RemoteBitrateObserver* observer,
-                                     const OverUseDetectorOptions& options);
+  RemoteBitrateEstimatorSingleStream(const OverUseDetectorOptions& options,
+                                     RemoteBitrateObserver* observer,
+                                     Clock* clock);
+
+  virtual ~RemoteBitrateEstimatorSingleStream() {}
 
   void IncomingRtcp(unsigned int ssrc, uint32_t ntp_secs, uint32_t ntp_frac,
                     uint32_t rtp_timestamp) {}
@@ -43,12 +48,13 @@
                       int64_t arrival_time,
                       uint32_t rtp_timestamp);
 
-  // Triggers a new estimate calculation for the stream identified by |ssrc|.
-  void UpdateEstimate(unsigned int ssrc, int64_t time_now);
-
-  // Set the current round-trip time experienced by the stream identified by
-  // |ssrc|.
-  void SetRtt(unsigned int ssrc);
+  // Triggers a new estimate calculation.
+  // Implements the Module interface.
+  virtual int32_t Process();
+  virtual int32_t TimeUntilNextProcess();
+  // Set the current round-trip time experienced by the stream.
+  // Implements the StatsObserver interface.
+  virtual void OnRttUpdate(uint32_t rtt);
 
   // Removes all data for |ssrc|.
   void RemoveStream(unsigned int ssrc);
@@ -62,14 +68,19 @@
  private:
   typedef std::map<unsigned int, OveruseDetector> SsrcOveruseDetectorMap;
 
+  // Triggers a new estimate calculation.
+  void UpdateEstimate(int64_t time_now);
+
   void GetSsrcs(std::vector<unsigned int>* ssrcs) const;
 
   const OverUseDetectorOptions& options_;
+  Clock* clock_;
   SsrcOveruseDetectorMap overuse_detectors_;
   BitRateStats incoming_bitrate_;
   RemoteRateControl remote_rate_;
   RemoteBitrateObserver* observer_;
   scoped_ptr<CriticalSectionWrapper> crit_sect_;
+  int64_t last_process_time_;
 };
 
 }  // namespace webrtc
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest.cc b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest.cc
index 83d5142..e60fdde 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest.cc
+++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest.cc
@@ -23,73 +23,84 @@
 namespace webrtc {
 
 TEST_F(RemoteBitrateEstimatorTest, TestInitialBehavior) {
+  const int kFramerate = 50;  // 50 fps to avoid rounding errors.
+  const int kFrameIntervalMs = 1000 / kFramerate;
   unsigned int bitrate_bps = 0;
-  int64_t time_now = 0;
   uint32_t timestamp = 0;
   std::vector<unsigned int> ssrcs;
   EXPECT_FALSE(bitrate_estimator_->LatestEstimate(&ssrcs, &bitrate_bps));
   EXPECT_EQ(0u, ssrcs.size());
-  bitrate_estimator_->UpdateEstimate(kDefaultSsrc, time_now);
+  clock_.AdvanceTimeMilliseconds(1000);
+  bitrate_estimator_->Process();
   EXPECT_FALSE(bitrate_estimator_->LatestEstimate(&ssrcs, &bitrate_bps));
   EXPECT_FALSE(bitrate_observer_->updated());
   bitrate_observer_->Reset();
+  clock_.AdvanceTimeMilliseconds(1000);
   // Inserting a packet. Still no valid estimate. We need to wait 1 second.
-  bitrate_estimator_->IncomingPacket(kDefaultSsrc, kMtu, time_now,
-                                     timestamp);
-  bitrate_estimator_->UpdateEstimate(kDefaultSsrc, time_now);
+  bitrate_estimator_->IncomingPacket(kDefaultSsrc, kMtu,
+                                     clock_.TimeInMilliseconds(), timestamp);
+  bitrate_estimator_->Process();
   EXPECT_FALSE(bitrate_estimator_->LatestEstimate(&ssrcs, &bitrate_bps));
   EXPECT_EQ(0u, ssrcs.size());
   EXPECT_FALSE(bitrate_observer_->updated());
   bitrate_observer_->Reset();
-  // Waiting more than one second gives us a valid estimate.
-  // We need at least two packets for the incoming bitrate to be > 0 since the
-  // window is 500 ms.
-  time_now += 499;
-  bitrate_estimator_->IncomingPacket(kDefaultSsrc, kMtu, time_now,
-                                     timestamp);
-  time_now += 2;
-  bitrate_estimator_->UpdateEstimate(kDefaultSsrc, time_now);
+  // Inserting packets for one second to get a valid estimate.
+  for (int i = 0; i < kFramerate; ++i) {
+    bitrate_estimator_->IncomingPacket(kDefaultSsrc, kMtu,
+                                       clock_.TimeInMilliseconds(), timestamp);
+    clock_.AdvanceTimeMilliseconds(1000 / kFramerate);
+    timestamp += 90 * kFrameIntervalMs;
+  }
+  bitrate_estimator_->Process();
   EXPECT_TRUE(bitrate_estimator_->LatestEstimate(&ssrcs, &bitrate_bps));
-  EXPECT_EQ(1u, ssrcs.size());
+  ASSERT_EQ(1u, ssrcs.size());
   EXPECT_EQ(kDefaultSsrc, ssrcs.front());
-  EXPECT_EQ(20607u, bitrate_bps);
+  EXPECT_EQ(498075u, bitrate_bps);
   EXPECT_TRUE(bitrate_observer_->updated());
   bitrate_observer_->Reset();
   EXPECT_EQ(bitrate_observer_->latest_bitrate(), bitrate_bps);
 }
 
 TEST_F(RemoteBitrateEstimatorTest, TestRateIncreaseReordering) {
-  int64_t time_now = 0;
   uint32_t timestamp = 0;
-  const int framerate = 50;  // 50 fps to avoid rounding errors.
-  const int frame_interval_ms = 1000 / framerate;
-  bitrate_estimator_->IncomingPacket(kDefaultSsrc, 1000, time_now, timestamp);
-  bitrate_estimator_->UpdateEstimate(kDefaultSsrc, time_now);
+  const int kFramerate = 50;  // 50 fps to avoid rounding errors.
+  const int kFrameIntervalMs = 1000 / kFramerate;
+  bitrate_estimator_->IncomingPacket(kDefaultSsrc, 1000,
+                                     clock_.TimeInMilliseconds(), timestamp);
+  bitrate_estimator_->Process();
   EXPECT_FALSE(bitrate_observer_->updated());  // No valid estimate.
-  // Increase time with 1 second to get a valid estimate.
-  time_now += 1000;
-  timestamp += 90 * 1000;
-  bitrate_estimator_->IncomingPacket(kDefaultSsrc, 1000, time_now, timestamp);
-  bitrate_estimator_->UpdateEstimate(kDefaultSsrc, time_now);
+  // Inserting packets for one second to get a valid estimate.
+  for (int i = 0; i < kFramerate; ++i) {
+    bitrate_estimator_->IncomingPacket(kDefaultSsrc, kMtu,
+                                       clock_.TimeInMilliseconds(), timestamp);
+    clock_.AdvanceTimeMilliseconds(kFrameIntervalMs);
+    timestamp += 90 * kFrameIntervalMs;
+  }
+  bitrate_estimator_->Process();
   EXPECT_TRUE(bitrate_observer_->updated());
-  EXPECT_EQ(17645u, bitrate_observer_->latest_bitrate());
+  EXPECT_EQ(498136u, bitrate_observer_->latest_bitrate());
   for (int i = 0; i < 10; ++i) {
-    time_now += 2 * frame_interval_ms;
-    timestamp += 2 * 90 * frame_interval_ms;
-    bitrate_estimator_->IncomingPacket(kDefaultSsrc, 1000, time_now, timestamp);
+    clock_.AdvanceTimeMilliseconds(2 * kFrameIntervalMs);
+    timestamp += 2 * 90 * kFrameIntervalMs;
+    bitrate_estimator_->IncomingPacket(kDefaultSsrc, 1000,
+                                       clock_.TimeInMilliseconds(), timestamp);
     bitrate_estimator_->IncomingPacket(kDefaultSsrc,
                                        1000,
-                                       time_now - frame_interval_ms,
-                                       timestamp - 90 * frame_interval_ms);
+                                       clock_.TimeInMilliseconds() -
+                                           kFrameIntervalMs,
+                                       timestamp - 90 * kFrameIntervalMs);
   }
-  bitrate_estimator_->UpdateEstimate(kDefaultSsrc, time_now);
+  bitrate_estimator_->Process();
   EXPECT_TRUE(bitrate_observer_->updated());
-  EXPECT_EQ(18985u, bitrate_observer_->latest_bitrate());
+  EXPECT_EQ(498136u, bitrate_observer_->latest_bitrate());
 }
 
 // Make sure we initially increase the bitrate as expected.
 TEST_F(RemoteBitrateEstimatorTest, TestRateIncreaseRtpTimestamps) {
-  const int kExpectedIterations = 276;
+  // This threshold corresponds approximately to increasing linearly with
+  // bitrate(i) = 1.04 * bitrate(i-1) + 1000
+  // until bitrate(i) > 500000, with bitrate(1) ~= 30000.
+  const int kExpectedIterations = 1621;
   unsigned int bitrate_bps = 30000;
   int iterations = 0;
   AddDefaultStream();
@@ -114,32 +125,35 @@
 // Verify that the time it takes for the estimator to reduce the bitrate when
 // the capacity is tightened stays the same.
 TEST_F(RemoteBitrateEstimatorTest, TestCapacityDropRtpTimestamps) {
-  const int kNumberOfFrames= 300;
+  const int kNumberOfFrames = 300;
   const int kStartBitrate = 900e3;
   const int kMinExpectedBitrate = 800e3;
   const int kMaxExpectedBitrate = 1100e3;
   AddDefaultStream();
   // Run in steady state to make the estimator converge.
+  unsigned int capacity_bps = 1000e3;
   stream_generator_->set_capacity_bps(1000e3);
   unsigned int bitrate_bps = SteadyStateRun(kDefaultSsrc, kNumberOfFrames,
                                             kStartBitrate, kMinExpectedBitrate,
-                                            kMaxExpectedBitrate);
+                                            kMaxExpectedBitrate, capacity_bps);
   // Reduce the capacity and verify the decrease time.
-  stream_generator_->set_capacity_bps(500e3);
+  capacity_bps = 500e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
+  int64_t overuse_start_time = clock_.TimeInMilliseconds();
   int64_t bitrate_drop_time = -1;
   for (int i = 0; i < 200; ++i) {
     GenerateAndProcessFrame(kDefaultSsrc, bitrate_bps);
     // Check for either increase or decrease.
     if (bitrate_observer_->updated()) {
       if (bitrate_drop_time == -1 &&
-          bitrate_observer_->latest_bitrate() <= 500e3) {
-        bitrate_drop_time = time_now_;
+          bitrate_observer_->latest_bitrate() <= capacity_bps) {
+        bitrate_drop_time = clock_.TimeInMilliseconds();
       }
       bitrate_bps = bitrate_observer_->latest_bitrate();
       bitrate_observer_->Reset();
     }
   }
-  EXPECT_EQ(10333, bitrate_drop_time);
+  EXPECT_EQ(367, bitrate_drop_time - overuse_start_time);
 }
 
 // Verify that the time it takes for the estimator to reduce the bitrate when
@@ -156,29 +170,33 @@
   stream_generator_->set_rtp_timestamp_offset(kDefaultSsrc,
       std::numeric_limits<uint32_t>::max() - kSteadyStateTime * 90000);
   // Run in steady state to make the estimator converge.
+  unsigned int capacity_bps = 1000e3;
   stream_generator_->set_capacity_bps(1000e3);
   unsigned int bitrate_bps = SteadyStateRun(kDefaultSsrc,
                                             kSteadyStateTime * kFramerate,
                                             kStartBitrate,
                                             kMinExpectedBitrate,
-                                            kMaxExpectedBitrate);
+                                            kMaxExpectedBitrate,
+                                            capacity_bps);
   bitrate_observer_->Reset();
   // Reduce the capacity and verify the decrease time.
-  stream_generator_->set_capacity_bps(500e3);
+  capacity_bps = 500e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
+  int64_t overuse_start_time = clock_.TimeInMilliseconds();
   int64_t bitrate_drop_time = -1;
   for (int i = 0; i < 200; ++i) {
     GenerateAndProcessFrame(kDefaultSsrc, bitrate_bps);
     // Check for either increase or decrease.
     if (bitrate_observer_->updated()) {
       if (bitrate_drop_time == -1 &&
-          bitrate_observer_->latest_bitrate() <= 500e3) {
-        bitrate_drop_time = time_now_;
+          bitrate_observer_->latest_bitrate() <= capacity_bps) {
+        bitrate_drop_time = clock_.TimeInMilliseconds();
       }
       bitrate_bps = bitrate_observer_->latest_bitrate();
       bitrate_observer_->Reset();
     }
   }
-  EXPECT_EQ(8299, bitrate_drop_time);
+  EXPECT_EQ(367, bitrate_drop_time - overuse_start_time);
 }
 
 // Verify that the time it takes for the estimator to reduce the bitrate when
@@ -196,29 +214,33 @@
   stream_generator_->set_rtp_timestamp_offset(kDefaultSsrc,
       std::numeric_limits<uint32_t>::max() - kSteadyStateTime * 90000);
   // Run in steady state to make the estimator converge.
-  stream_generator_->set_capacity_bps(1000e3);
+  unsigned int capacity_bps = 1000e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
   unsigned int bitrate_bps = SteadyStateRun(kDefaultSsrc,
                                             kSteadyStateTime * kFramerate,
                                             kStartBitrate,
                                             kMinExpectedBitrate,
-                                            kMaxExpectedBitrate);
+                                            kMaxExpectedBitrate,
+                                            capacity_bps);
   bitrate_observer_->Reset();
   // Reduce the capacity and verify the decrease time.
-  stream_generator_->set_capacity_bps(500e3);
+  capacity_bps = 500e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
+  int64_t overuse_start_time = clock_.TimeInMilliseconds();
   int64_t bitrate_drop_time = -1;
   for (int i = 0; i < 200; ++i) {
     GenerateAndProcessFrame(kDefaultSsrc, bitrate_bps);
     // Check for either increase or decrease.
     if (bitrate_observer_->updated()) {
       if (bitrate_drop_time == -1 &&
-          bitrate_observer_->latest_bitrate() <= 500e3) {
-        bitrate_drop_time = time_now_;
+          bitrate_observer_->latest_bitrate() <= capacity_bps) {
+        bitrate_drop_time = clock_.TimeInMilliseconds();
       }
       bitrate_bps = bitrate_observer_->latest_bitrate();
       bitrate_observer_->Reset();
     }
   }
-  EXPECT_EQ(8299, bitrate_drop_time);
+  EXPECT_EQ(367, bitrate_drop_time - overuse_start_time);
 }
 
 // Verify that the time it takes for the estimator to reduce the bitrate when
@@ -229,7 +251,7 @@
   const int kStartBitrate = 900e3;
   const int kMinExpectedBitrate = 800e3;
   const int kMaxExpectedBitrate = 1100e3;
-  const int kSteadyStateTime = 7;  // Seconds.
+  const int kSteadyStateFrames = 9 * kFramerate;
   stream_generator_->AddStream(new testing::RtpStream(
       30,               // Frames per second.
       kStartBitrate/2,  // Bitrate.
@@ -247,31 +269,35 @@
       0));         // RTCP receive time.
   // Trigger wrap right after the steady state run.
   stream_generator_->set_rtp_timestamp_offset(kDefaultSsrc,
-      std::numeric_limits<uint32_t>::max() - kSteadyStateTime * 90000);
+      std::numeric_limits<uint32_t>::max() - kSteadyStateFrames * 90000);
   // Run in steady state to make the estimator converge.
-  stream_generator_->set_capacity_bps(1000e3);
+  unsigned int capacity_bps = 1000e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
   unsigned int bitrate_bps = SteadyStateRun(kDefaultSsrc,
-                                            kSteadyStateTime * kFramerate,
+                                            kSteadyStateFrames,
                                             kStartBitrate,
                                             kMinExpectedBitrate,
-                                            kMaxExpectedBitrate);
+                                            kMaxExpectedBitrate,
+                                            capacity_bps);
   bitrate_observer_->Reset();
   // Reduce the capacity and verify the decrease time.
-  stream_generator_->set_capacity_bps(500e3);
+  capacity_bps = 500e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
+  int64_t overuse_start_time = clock_.TimeInMilliseconds();
   int64_t bitrate_drop_time = -1;
   for (int i = 0; i < 200; ++i) {
     GenerateAndProcessFrame(kDefaultSsrc, bitrate_bps);
     // Check for either increase or decrease.
     if (bitrate_observer_->updated()) {
       if (bitrate_drop_time == -1 &&
-          bitrate_observer_->latest_bitrate() <= 500e3) {
-        bitrate_drop_time = time_now_;
+          bitrate_observer_->latest_bitrate() <= capacity_bps) {
+        bitrate_drop_time = clock_.TimeInMilliseconds();
       }
       bitrate_bps = bitrate_observer_->latest_bitrate();
       bitrate_observer_->Reset();
     }
   }
-  EXPECT_EQ(4933, bitrate_drop_time);
+  EXPECT_EQ(567, bitrate_drop_time - overuse_start_time);
 }
 
 // Verify that the time it takes for the estimator to reduce the bitrate when
@@ -282,9 +308,9 @@
   const int kStartBitrate = 900e3;
   const int kMinExpectedBitrate = 800e3;
   const int kMaxExpectedBitrate = 1100e3;
-  const int kSteadyStateTime = 11;  // Seconds.
+  const int kSteadyStateFrames = 12 * kFramerate;
   stream_generator_->AddStream(new testing::RtpStream(
-      30,           // Frames per second.
+      kFramerate,       // Frames per second.
       kStartBitrate/2,  // Bitrate.
       1,            // SSRC.
       90000,        // RTP frequency.
@@ -292,7 +318,7 @@
       0));          // RTCP receive time.
 
   stream_generator_->AddStream(new testing::RtpStream(
-      30,           // Frames per second.
+      kFramerate,       // Frames per second.
       kStartBitrate/3,  // Bitrate.
       2,            // SSRC.
       90000,        // RTP frequency.
@@ -300,7 +326,7 @@
       0));          // RTCP receive time.
 
   stream_generator_->AddStream(new testing::RtpStream(
-      30,           // Frames per second.
+      kFramerate,       // Frames per second.
       kStartBitrate/6,  // Bitrate.
       3,            // SSRC.
       90000,        // RTP frequency.
@@ -308,31 +334,35 @@
       0));          // RTCP receive time.
   // Trigger wrap right after the steady state run.
   stream_generator_->set_rtp_timestamp_offset(kDefaultSsrc,
-      std::numeric_limits<uint32_t>::max() - kSteadyStateTime * 90000);
+      std::numeric_limits<uint32_t>::max() - kSteadyStateFrames * 90000);
   // Run in steady state to make the estimator converge.
-  stream_generator_->set_capacity_bps(1000e3);
+  unsigned int capacity_bps = 1000e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
   unsigned int bitrate_bps = SteadyStateRun(kDefaultSsrc,
-                                            kSteadyStateTime * kFramerate,
+                                            kSteadyStateFrames,
                                             kStartBitrate,
                                             kMinExpectedBitrate,
-                                            kMaxExpectedBitrate);
+                                            kMaxExpectedBitrate,
+                                            capacity_bps);
   bitrate_observer_->Reset();
   // Reduce the capacity and verify the decrease time.
-  stream_generator_->set_capacity_bps(500e3);
+  capacity_bps = 500e3;
+  stream_generator_->set_capacity_bps(capacity_bps);
+  int64_t overuse_start_time = clock_.TimeInMilliseconds();
   int64_t bitrate_drop_time = -1;
   for (int i = 0; i < 200; ++i) {
     GenerateAndProcessFrame(kDefaultSsrc, bitrate_bps);
     // Check for either increase or decrease.
     if (bitrate_observer_->updated()) {
       if (bitrate_drop_time == -1 &&
-          bitrate_observer_->latest_bitrate() <= 500e3) {
-        bitrate_drop_time = time_now_;
+          bitrate_observer_->latest_bitrate() <= capacity_bps) {
+        bitrate_drop_time = clock_.TimeInMilliseconds();
       }
       bitrate_bps = bitrate_observer_->latest_bitrate();
       bitrate_observer_->Reset();
     }
   }
-  EXPECT_EQ(3966, bitrate_drop_time);
+  EXPECT_EQ(433, bitrate_drop_time - overuse_start_time);
 }
 
 }  // namespace webrtc
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.cc b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.cc
index 23fc8cf..7aa68f0 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.cc
+++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.cc
@@ -39,25 +39,25 @@
 // Generates a new frame for this stream. If called too soon after the
 // previous frame, no frame will be generated. The frame is split into
 // packets.
-int64_t RtpStream::GenerateFrame(double time_now, PacketList* packets) {
-  if (time_now < next_rtp_time_) {
+int64_t RtpStream::GenerateFrame(int64_t time_now_us, PacketList* packets) {
+  if (time_now_us < next_rtp_time_) {
     return next_rtp_time_;
   }
   assert(packets != NULL);
   int bits_per_frame = (bitrate_bps_ + fps_ / 2) / fps_;
-  int n_packets = std::max((bits_per_frame + 8 * kMtu) / (8 * kMtu), 1);
+  int n_packets = std::max((bits_per_frame + 4 * kMtu) / (8 * kMtu), 1);
   int packet_size = (bits_per_frame + 4 * n_packets) / (8 * n_packets);
   assert(n_packets >= 0);
   for (int i = 0; i < n_packets; ++i) {
     RtpPacket* packet = new RtpPacket;
-    packet->send_time = time_now + kSendSideOffsetMs + 0.5f;
+    packet->send_time = time_now_us + kSendSideOffsetUs;
     packet->size = packet_size;
     packet->rtp_timestamp = rtp_timestamp_offset_ + static_cast<uint32_t>(
-        (frequency_ / 1000.0) * packet->send_time + 0.5);
+        ((frequency_ / 1000) * packet->send_time + 500) / 1000);
     packet->ssrc = ssrc_;
     packets->push_back(packet);
   }
-  next_rtp_time_ = time_now + 1000.0 / static_cast<double>(fps_);
+  next_rtp_time_ = time_now_us + (1000000 + fps_ / 2) / fps_;
   return next_rtp_time_;
 }
 
@@ -67,18 +67,18 @@
 }
 
 // Generates an RTCP packet.
-RtpStream::RtcpPacket* RtpStream::Rtcp(double time_now) {
-  if (time_now < next_rtcp_time_) {
+RtpStream::RtcpPacket* RtpStream::Rtcp(int64_t time_now_us) {
+  if (time_now_us < next_rtcp_time_) {
     return NULL;
   }
   RtcpPacket* rtcp = new RtcpPacket;
-  int64_t send_time = RtpStream::kSendSideOffsetMs + time_now + 0.5;
+  int64_t send_time_us = RtpStream::kSendSideOffsetUs + time_now_us;
   rtcp->timestamp = rtp_timestamp_offset_ + static_cast<uint32_t>(
-      (frequency_ / 1000.0) * send_time + 0.5);
-  rtcp->ntp_secs = send_time / 1000;
-  rtcp->ntp_frac = (send_time % 1000) * kNtpFracPerMs;
+          ((frequency_ / 1000) * send_time_us + 500) / 1000);
+  rtcp->ntp_secs = send_time_us / 1000000;
+  rtcp->ntp_frac = (send_time_us % 1000000) * kNtpFracPerMs;
   rtcp->ssrc = ssrc_;
-  next_rtcp_time_ = time_now + kRtcpIntervalMs;
+  next_rtcp_time_ = time_now_us + kRtcpIntervalUs;
   return rtcp;
 }
 
@@ -102,7 +102,7 @@
 
 StreamGenerator::StreamGenerator(int capacity, double time_now)
     : capacity_(capacity),
-      prev_arrival_time_(time_now) {}
+      prev_arrival_time_us_(time_now) {}
 
 StreamGenerator::~StreamGenerator() {
   for (StreamMap::iterator it = streams_.begin(); it != streams_.end();
@@ -150,30 +150,33 @@
 
 // TODO(holmer): Break out the channel simulation part from this class to make
 // it possible to simulate different types of channels.
-double StreamGenerator::GenerateFrame(RtpStream::PacketList* packets,
-                                      double time_now) {
+int64_t StreamGenerator::GenerateFrame(RtpStream::PacketList* packets,
+                                       int64_t time_now_us) {
   assert(packets != NULL);
   assert(packets->empty());
   assert(capacity_ > 0);
   StreamMap::iterator it = std::min_element(streams_.begin(), streams_.end(),
                                             RtpStream::Compare);
-  (*it).second->GenerateFrame(time_now, packets);
+  (*it).second->GenerateFrame(time_now_us, packets);
+  int i = 0;
   for (RtpStream::PacketList::iterator packet_it = packets->begin();
       packet_it != packets->end(); ++packet_it) {
-    int required_network_time =
-        (8 * 1000 * (*packet_it)->size + capacity_ / 2) / capacity_;
-    prev_arrival_time_ = std::max(time_now + required_network_time,
-        prev_arrival_time_ + required_network_time);
-    (*packet_it)->arrival_time = prev_arrival_time_ + 0.5;
+    int capacity_bpus = capacity_ / 1000;
+    int64_t required_network_time_us =
+        (8 * 1000 * (*packet_it)->size + capacity_bpus / 2) / capacity_bpus;
+    prev_arrival_time_us_ = std::max(time_now_us + required_network_time_us,
+        prev_arrival_time_us_ + required_network_time_us);
+    (*packet_it)->arrival_time = prev_arrival_time_us_;
+    ++i;
   }
   it = std::min_element(streams_.begin(), streams_.end(), RtpStream::Compare);
   return (*it).second->next_rtp_time();
 }
 
-void StreamGenerator::Rtcps(RtcpList* rtcps, double time_now) const {
+void StreamGenerator::Rtcps(RtcpList* rtcps, int64_t time_now_us) const {
   for (StreamMap::const_iterator it = streams_.begin(); it != streams_.end();
       ++it) {
-    RtpStream::RtcpPacket* rtcp = it->second->Rtcp(time_now);
+    RtpStream::RtcpPacket* rtcp = it->second->Rtcp(time_now_us);
     if (rtcp) {
       rtcps->push_front(rtcp);
     }
@@ -182,22 +185,24 @@
 }  // namespace testing
 
 RemoteBitrateEstimatorTest::RemoteBitrateEstimatorTest()
-    : time_now_(0.0),
+    : clock_(0),
       align_streams_(false) {}
 
 RemoteBitrateEstimatorTest::RemoteBitrateEstimatorTest(bool align_streams)
-    : time_now_(0.0),
+    : clock_(0),
       align_streams_(align_streams) {}
 
 void RemoteBitrateEstimatorTest::SetUp() {
   bitrate_observer_.reset(new testing::TestBitrateObserver);
   bitrate_estimator_.reset(
       RemoteBitrateEstimator::Create(
+          overuse_detector_options_,
+          RemoteBitrateEstimator::kSingleStreamEstimation,
           bitrate_observer_.get(),
-          over_use_detector_options_,
-          RemoteBitrateEstimator::kSingleStreamEstimation));
-  stream_generator_.reset(new testing::StreamGenerator(1e6,  // Capacity.
-                                                       time_now_));
+          &clock_));
+  stream_generator_.reset(new testing::StreamGenerator(
+      1e6,  // Capacity.
+      clock_.TimeInMicroseconds()));
 }
 
 void RemoteBitrateEstimatorTest::AddDefaultStream() {
@@ -220,15 +225,14 @@
     unsigned int bitrate_bps) {
   stream_generator_->set_bitrate_bps(bitrate_bps);
   testing::RtpStream::PacketList packets;
-  time_now_ = stream_generator_->GenerateFrame(&packets, time_now_);
-  int64_t last_arrival_time = -1;
-  bool prev_was_decrease = false;
+  int64_t next_time_us = stream_generator_->GenerateFrame(
+      &packets, clock_.TimeInMicroseconds());
   bool overuse = false;
   while (!packets.empty()) {
     testing::RtpStream::RtpPacket* packet = packets.front();
     if (align_streams_) {
       testing::StreamGenerator::RtcpList rtcps;
-      stream_generator_->Rtcps(&rtcps, time_now_);
+      stream_generator_->Rtcps(&rtcps, clock_.TimeInMicroseconds());
       for (testing::StreamGenerator::RtcpList::iterator it = rtcps.begin();
           it != rtcps.end(); ++it) {
         bitrate_estimator_->IncomingRtcp((*it)->ssrc,
@@ -241,40 +245,39 @@
     bitrate_observer_->Reset();
     bitrate_estimator_->IncomingPacket(packet->ssrc,
                                        packet->size,
-                                       packet->arrival_time,
+                                       (packet->arrival_time + 500) / 1000,
                                        packet->rtp_timestamp);
     if (bitrate_observer_->updated()) {
       // Verify that new estimates only are triggered by an overuse and a
       // rate decrease.
       overuse = true;
       EXPECT_LE(bitrate_observer_->latest_bitrate(), bitrate_bps);
-      EXPECT_FALSE(prev_was_decrease);
-      prev_was_decrease = true;
-    } else {
-      prev_was_decrease = false;
     }
-    last_arrival_time = packet->arrival_time;
+    clock_.AdvanceTimeMicroseconds(packet->arrival_time -
+                                   clock_.TimeInMicroseconds());
     delete packet;
     packets.pop_front();
   }
-  EXPECT_GT(last_arrival_time, -1);
-  bitrate_estimator_->UpdateEstimate(ssrc, last_arrival_time);
+  bitrate_estimator_->Process();
+  clock_.AdvanceTimeMicroseconds(next_time_us - clock_.TimeInMicroseconds());
   return overuse;
 }
 
-// Run the bandwidth estimator with a stream of |number_of_frames| frames.
+// Run the bandwidth estimator with a stream of |number_of_frames| frames, or
+// until it reaches |target_bitrate|.
 // Can for instance be used to run the estimator for some time to get it
 // into a steady state.
 unsigned int RemoteBitrateEstimatorTest::SteadyStateRun(
     unsigned int ssrc,
-    int number_of_frames,
+    int max_number_of_frames,
     unsigned int start_bitrate,
     unsigned int min_bitrate,
-    unsigned int max_bitrate) {
+    unsigned int max_bitrate,
+    unsigned int target_bitrate) {
   unsigned int bitrate_bps = start_bitrate;
   bool bitrate_update_seen = false;
   // Produce |number_of_frames| frames and give them to the estimator.
-  for (int i = 0; i < number_of_frames; ++i) {
+  for (int i = 0; i < max_number_of_frames; ++i) {
     bool overuse = GenerateAndProcessFrame(ssrc, bitrate_bps);
     if (overuse) {
       EXPECT_LT(bitrate_observer_->latest_bitrate(), max_bitrate);
@@ -285,6 +288,9 @@
       bitrate_bps = bitrate_observer_->latest_bitrate();
       bitrate_observer_->Reset();
     }
+    if (bitrate_update_seen && bitrate_bps > target_bitrate) {
+      break;
+    }
   }
   EXPECT_TRUE(bitrate_update_seen);
   return bitrate_bps;
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.h b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.h
index 0eb4b22..34e4fab 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.h
+++ b/webrtc/modules/remote_bitrate_estimator/remote_bitrate_estimator_unittest_helper.h
@@ -17,9 +17,10 @@
 #include <map>
 #include <utility>
 
-#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
-#include "system_wrappers/interface/constructor_magic.h"
-#include "system_wrappers/interface/scoped_ptr.h"
+#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
+#include "webrtc/system_wrappers/interface/clock.h"
+#include "webrtc/system_wrappers/interface/constructor_magic.h"
+#include "webrtc/system_wrappers/interface/scoped_ptr.h"
 
 namespace webrtc {
 
@@ -74,7 +75,7 @@
 
   typedef std::list<RtpPacket*> PacketList;
 
-  enum { kSendSideOffsetMs = 1000 };
+  enum { kSendSideOffsetUs = 1000000 };
 
   RtpStream(int fps, int bitrate_bps, unsigned int ssrc, unsigned int frequency,
       uint32_t timestamp_offset, int64_t rtcp_receive_time);
@@ -83,13 +84,13 @@
   // Generates a new frame for this stream. If called too soon after the
   // previous frame, no frame will be generated. The frame is split into
   // packets.
-  int64_t GenerateFrame(double time_now, PacketList* packets);
+  int64_t GenerateFrame(int64_t time_now_us, PacketList* packets);
 
   // The send-side time when the next frame can be generated.
   double next_rtp_time() const;
 
   // Generates an RTCP packet.
-  RtcpPacket* Rtcp(double time_now);
+  RtcpPacket* Rtcp(int64_t time_now_us);
 
   void set_bitrate_bps(int bitrate_bps);
 
@@ -101,14 +102,14 @@
                       const std::pair<unsigned int, RtpStream*>& right);
 
  private:
-  enum { kRtcpIntervalMs = 1000 };
+  enum { kRtcpIntervalUs = 1000000 };
 
   int fps_;
   int bitrate_bps_;
   unsigned int ssrc_;
   unsigned int frequency_;
-  double next_rtp_time_;
-  double next_rtcp_time_;
+  int64_t next_rtp_time_;
+  int64_t next_rtcp_time_;
   uint32_t rtp_timestamp_offset_;
   const double kNtpFracPerMs;
 
@@ -138,9 +139,9 @@
 
   // TODO(holmer): Break out the channel simulation part from this class to make
   // it possible to simulate different types of channels.
-  double GenerateFrame(RtpStream::PacketList* packets, double time_now);
+  int64_t GenerateFrame(RtpStream::PacketList* packets, int64_t time_now_us);
 
-  void Rtcps(RtcpList* rtcps, double time_now) const;
+  void Rtcps(RtcpList* rtcps, int64_t time_now_us) const;
 
  private:
   typedef std::map<unsigned int, RtpStream*> StreamMap;
@@ -148,7 +149,7 @@
   // Capacity of the simulated channel in bits per second.
   int capacity_;
   // The time when the last packet arrived.
-  double prev_arrival_time_;
+  int64_t prev_arrival_time_us_;
   // All streams being transmitted on this simulated channel.
   StreamMap streams_;
 
@@ -174,19 +175,21 @@
   // target bitrate after the call to this function.
   bool GenerateAndProcessFrame(unsigned int ssrc, unsigned int bitrate_bps);
 
-  // Run the bandwidth estimator with a stream of |number_of_frames| frames.
+  // Run the bandwidth estimator with a stream of |number_of_frames| frames, or
+  // until it reaches |target_bitrate|.
   // Can for instance be used to run the estimator for some time to get it
   // into a steady state.
   unsigned int SteadyStateRun(unsigned int ssrc,
                               int number_of_frames,
                               unsigned int start_bitrate,
                               unsigned int min_bitrate,
-                              unsigned int max_bitrate);
+                              unsigned int max_bitrate,
+                              unsigned int target_bitrate);
 
   enum { kDefaultSsrc = 1 };
 
-  double time_now_;  // Current time at the receiver.
-  OverUseDetectorOptions over_use_detector_options_;
+  SimulatedClock clock_;  // Time at the receiver.
+  OverUseDetectorOptions overuse_detector_options_;
   scoped_ptr<RemoteBitrateEstimator> bitrate_estimator_;
   scoped_ptr<testing::TestBitrateObserver> bitrate_observer_;
   scoped_ptr<testing::StreamGenerator> stream_generator_;
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_rate_control.cc b/webrtc/modules/remote_bitrate_estimator/remote_rate_control.cc
index ae48cc2..eb293bb 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_rate_control.cc
+++ b/webrtc/modules/remote_bitrate_estimator/remote_rate_control.cc
@@ -24,6 +24,9 @@
 #endif
 
 namespace webrtc {
+
+const unsigned int kDefaultRttMs = 200;
+
 RemoteRateControl::RemoteRateControl()
 :
 _minConfiguredBitRate(30000),
@@ -43,7 +46,7 @@
 _avgChangePeriod(1000.0f),
 _lastChangeMs(-1),
 _beta(0.9f),
-_rtt(0)
+_rtt(kDefaultRttMs)
 #ifdef MATLAB
 ,_plot1(NULL),
 _plot2(NULL)
@@ -86,6 +89,20 @@
   return _initializedBitRate;
 }
 
+bool RemoteRateControl::TimeToReduceFurther(
+    int64_t time_now, unsigned int incoming_bitrate) const {
+  const int bitrate_reduction_interval = BWE_MAX(BWE_MIN(_rtt, 200), 10);
+  if (time_now - _lastBitRateChange >= bitrate_reduction_interval) {
+    return true;
+  }
+  if (ValidEstimate()) {
+    const int threshold = static_cast<int>(1.05 * incoming_bitrate);
+    const int bitrate_difference = LatestEstimate() - incoming_bitrate;
+    return bitrate_difference > threshold;
+  }
+  return false;
+}
+
 WebRtc_Word32 RemoteRateControl::SetConfiguredBitRates(
     WebRtc_UWord32 minBitRateBps, WebRtc_UWord32 maxBitRateBps)
 {
diff --git a/webrtc/modules/remote_bitrate_estimator/remote_rate_control.h b/webrtc/modules/remote_bitrate_estimator/remote_rate_control.h
index 6c9d116..5139198 100644
--- a/webrtc/modules/remote_bitrate_estimator/remote_rate_control.h
+++ b/webrtc/modules/remote_bitrate_estimator/remote_rate_control.h
@@ -36,6 +36,12 @@
     // Returns true if there is a valid estimate of the incoming bitrate, false
     // otherwise.
     bool ValidEstimate() const;
+    // Returns true if the bitrate estimate hasn't been changed for more than
+    // an RTT, or if the incoming_bitrate is more than 5% above the current
+    // estimate. Should be used to decide if we should reduce the rate further
+    // when over-using.
+    bool TimeToReduceFurther(int64_t time_now,
+                             unsigned int incoming_bitrate) const;
 
 private:
     WebRtc_UWord32 ChangeBitRate(WebRtc_UWord32 currentBitRate,
diff --git a/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h b/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h
index 540d9ef..3a0105d 100644
--- a/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h
+++ b/webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h
@@ -463,12 +463,6 @@
                              WebRtc_UWord32* nackRate) const = 0;
 
     /*
-     *  Get the receive-side estimate of the available bandwidth.
-     */
-    virtual int EstimatedReceiveBandwidth(
-        WebRtc_UWord32* available_bandwidth) const = 0;
-
-    /*
     *   Used by the codec module to deliver a video or audio frame for
     *   packetization.
     *
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_format_remb_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtcp_format_remb_unittest.cc
index 0438510..5340ed2 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_format_remb_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_format_remb_unittest.cc
@@ -60,11 +60,13 @@
  protected:
   RtcpFormatRembTest()
       : over_use_detector_options_(),
+        system_clock_(Clock::GetRealTimeClock()),
         remote_bitrate_observer_(),
         remote_bitrate_estimator_(RemoteBitrateEstimator::Create(
-            &remote_bitrate_observer_,
             over_use_detector_options_,
-            RemoteBitrateEstimator::kMultiStreamEstimation)) {}
+            RemoteBitrateEstimator::kMultiStreamEstimation,
+            &remote_bitrate_observer_,
+            system_clock_)) {}
   virtual void SetUp();
   virtual void TearDown();
 
@@ -79,7 +81,6 @@
 };
 
 void RtcpFormatRembTest::SetUp() {
-  system_clock_ = Clock::GetRealTimeClock();
   RtpRtcp::Configuration configuration;
   configuration.id = 0;
   configuration.audio = false;
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc b/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc
index e300439..d05dd2d 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_receiver.cc
@@ -113,6 +113,19 @@
     return _lastReceived;
 }
 
+WebRtc_Word64
+RTCPReceiver::LastReceivedReceiverReport() const {
+    CriticalSectionScoped lock(_criticalSectionRTCPReceiver);
+    WebRtc_Word64 last_received_rr = -1;
+    for (ReceivedInfoMap::const_iterator it = _receivedInfoMap.begin();
+         it != _receivedInfoMap.end(); ++it) {
+      if (it->second->lastTimeReceived > last_received_rr) {
+        last_received_rr = it->second->lastTimeReceived;
+      }
+    }
+    return last_received_rr;
+}
+
 WebRtc_Word32
 RTCPReceiver::SetRemoteSSRC( const WebRtc_UWord32 ssrc)
 {
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_receiver.h b/webrtc/modules/rtp_rtcp/source/rtcp_receiver.h
index 08ff37b..befe2df 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_receiver.h
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_receiver.h
@@ -37,6 +37,7 @@
     WebRtc_Word32 SetRTCPStatus(const RTCPMethod method);
 
     WebRtc_Word64 LastReceived();
+    WebRtc_Word64 LastReceivedReceiverReport() const;
 
     void SetSSRC( const WebRtc_UWord32 ssrc);
     void SetRelaySSRC( const WebRtc_UWord32 ssrc);
@@ -197,6 +198,8 @@
                        RTCPHelp::RTCPPacketInformation& rtcpPacketInformation);
 
  private:
+  typedef std::map<WebRtc_UWord32, RTCPHelp::RTCPReceiveInformation*>
+      ReceivedInfoMap;
   WebRtc_Word32           _id;
   Clock*                  _clock;
   RTCPMethod              _method;
@@ -221,8 +224,7 @@
   // Received report blocks.
   std::map<WebRtc_UWord32, RTCPHelp::RTCPReportBlockInformation*>
       _receivedReportBlockMap;
-  std::map<WebRtc_UWord32, RTCPHelp::RTCPReceiveInformation*>
-      _receivedInfoMap;
+  ReceivedInfoMap _receivedInfoMap;
   std::map<WebRtc_UWord32, RTCPUtility::RTCPCnameInformation*>
       _receivedCnameMap;
 
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
index 783b13a..11d158a 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_receiver_unittest.cc
@@ -172,9 +172,10 @@
         remote_bitrate_observer_(),
         remote_bitrate_estimator_(
             RemoteBitrateEstimator::Create(
-                &remote_bitrate_observer_,
                 over_use_detector_options_,
-                RemoteBitrateEstimator::kMultiStreamEstimation)) {
+                RemoteBitrateEstimator::kMultiStreamEstimation,
+                &remote_bitrate_observer_,
+                &system_clock_)) {
     test_transport_ = new TestTransport();
 
     RtpRtcp::Configuration configuration;
diff --git a/webrtc/modules/rtp_rtcp/source/rtcp_sender_unittest.cc b/webrtc/modules/rtp_rtcp/source/rtcp_sender_unittest.cc
index 7bd2931..e3cadbd 100644
--- a/webrtc/modules/rtp_rtcp/source/rtcp_sender_unittest.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtcp_sender_unittest.cc
@@ -99,13 +99,14 @@
  protected:
   RtcpSenderTest()
       : over_use_detector_options_(),
+        system_clock_(Clock::GetRealTimeClock()),
         remote_bitrate_observer_(),
         remote_bitrate_estimator_(
             RemoteBitrateEstimator::Create(
-                &remote_bitrate_observer_,
                 over_use_detector_options_,
-                RemoteBitrateEstimator::kMultiStreamEstimation)) {
-    system_clock_ = Clock::GetRealTimeClock();
+                RemoteBitrateEstimator::kMultiStreamEstimation,
+                &remote_bitrate_observer_,
+                system_clock_)) {
     test_transport_ = new TestTransport();
 
     RtpRtcp::Configuration configuration;
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h
index 066b3c2..e128a10 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h
+++ b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_config.h
@@ -15,7 +15,8 @@
 namespace webrtc {
 enum { kRtpRtcpMaxIdleTimeProcess = 5,
        kRtpRtcpBitrateProcessTimeMs = 10,
-       kRtpRtcpPacketTimeoutProcessTimeMs = 100 };
+       kRtpRtcpPacketTimeoutProcessTimeMs = 100,
+       kRtpRtcpRttProcessTimeMs = 1000 };
 
 enum { NACK_PACKETS_MAX_SIZE    = 256 }; // in packets
 enum { NACK_BYTECOUNT_SIZE      = 60};   // size of our NACK history
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
index 2cc17a3..3cb7994 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
+++ b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.cc
@@ -11,7 +11,6 @@
 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
 
 #include <string.h>
-
 #include <cassert>
 
 #include "webrtc/common_types.h"
@@ -39,8 +38,6 @@
 
 namespace webrtc {
 
-const WebRtc_UWord16 kDefaultRtt = 200;
-
 static RtpData* NullObjectRtpData() {
   static NullRtpData null_rtp_data;
   return &null_rtp_data;
@@ -107,6 +104,7 @@
       last_bitrate_process_time_(configuration.clock->TimeInMilliseconds()),
       last_packet_timeout_process_time_(
           configuration.clock->TimeInMilliseconds()),
+      last_rtt_process_time_(configuration.clock->TimeInMilliseconds()),
       packet_overhead_(28),  // IPV4 UDP.
       critical_section_module_ptrs_(
           CriticalSectionWrapper::CreateCriticalSection()),
@@ -258,33 +256,30 @@
   ProcessDeadOrAliveTimer();
 
   const bool default_instance(child_modules_.empty() ? false : true);
-  if (!default_instance && rtcp_sender_.TimeToSendRTCPReport()) {
-    WebRtc_UWord16 max_rtt = 0;
+  if (!default_instance) {
     if (rtcp_sender_.Sending()) {
-      std::vector<RTCPReportBlock> receive_blocks;
-      rtcp_receiver_.StatisticsReceived(&receive_blocks);
-      for (std::vector<RTCPReportBlock>::iterator it = receive_blocks.begin();
-           it != receive_blocks.end(); ++it) {
-        WebRtc_UWord16 rtt = 0;
-        rtcp_receiver_.RTT(it->remoteSSRC, &rtt, NULL, NULL, NULL);
-        max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
+      // Process RTT if we have received a receiver report and we haven't
+      // processed RTT for at least |kRtpRtcpRttProcessTimeMs| milliseconds.
+      if (rtcp_receiver_.LastReceivedReceiverReport() >
+          last_rtt_process_time_ && now >= last_rtt_process_time_ +
+          kRtpRtcpRttProcessTimeMs) {
+        last_rtt_process_time_ = now;
+        std::vector<RTCPReportBlock> receive_blocks;
+        rtcp_receiver_.StatisticsReceived(&receive_blocks);
+        uint16_t max_rtt = 0;
+        for (std::vector<RTCPReportBlock>::iterator it = receive_blocks.begin();
+             it != receive_blocks.end(); ++it) {
+          uint16_t rtt = 0;
+          rtcp_receiver_.RTT(it->remoteSSRC, &rtt, NULL, NULL, NULL);
+          max_rtt = (rtt > max_rtt) ? rtt : max_rtt;
+        }
+        // Report the rtt.
+        if (rtt_observer_ && max_rtt != 0)
+          rtt_observer_->OnRttUpdate(max_rtt);
       }
-      // Report the rtt.
-      if (rtt_observer_ && max_rtt != 0)
-        rtt_observer_->OnRttUpdate(max_rtt);
-    } else {
-      // No valid RTT estimate, probably since this is a receive only channel.
-      // Use an estimate set by a send module.
-      max_rtt = rtcp_receiver_.RTT();
-    }
-    if (max_rtt == 0) {
-      // No own rtt calculation or set rtt, use default value.
-      max_rtt = kDefaultRtt;
-    }
 
-    // Verify receiver reports are delivered and the reported sequence number is
-    // increasing.
-    if (rtcp_sender_.Sending()) {
+      // Verify receiver reports are delivered and the reported sequence number
+      // is increasing.
       int64_t rtcp_interval = RtcpReportInterval();
       if (rtcp_receiver_.RtcpRrTimeout(rtcp_interval)) {
         LOG_F(LS_WARNING) << "Timeout: No RTCP RR received.";
@@ -292,13 +287,8 @@
         LOG_F(LS_WARNING) <<
             "Timeout: No increase in RTCP RR extended highest sequence number.";
       }
-    }
 
-    if (remote_bitrate_) {
-      // TODO(mflodman) Remove this and let this be propagated by CallStats.
-      remote_bitrate_->SetRtt(max_rtt);
-      remote_bitrate_->UpdateEstimate(rtp_receiver_->SSRC(), now);
-      if (TMMBR()) {
+      if (remote_bitrate_ && TMMBR()) {
         unsigned int target_bitrate = 0;
         std::vector<unsigned int> ssrcs;
         if (remote_bitrate_->LatestEstimate(&ssrcs, &target_bitrate)) {
@@ -309,7 +299,8 @@
         }
       }
     }
-    rtcp_sender_.SendRTCP(kRtcpReport);
+    if (rtcp_sender_.TimeToSendRTCPReport())
+      rtcp_sender_.SendRTCP(kRtcpReport);
   }
 
   if (UpdateRTCPReceiveInformationTimers()) {
@@ -1995,22 +1986,6 @@
     *nack_rate = rtp_sender_.NackOverheadRate();
 }
 
-int ModuleRtpRtcpImpl::EstimatedReceiveBandwidth(
-    WebRtc_UWord32* available_bandwidth) const {
-  if (remote_bitrate_) {
-    std::vector<unsigned int> ssrcs;
-    if (!remote_bitrate_->LatestEstimate(&ssrcs, available_bandwidth)) {
-      return -1;
-    }
-    if (!ssrcs.empty()) {
-      *available_bandwidth /= ssrcs.size();
-    }
-    return 0;
-  }
-  // No bandwidth receive-side bandwidth estimation is connected to this module.
-  return -1;
-}
-
 // Bad state of RTP receiver request a keyframe.
 void ModuleRtpRtcpImpl::OnRequestIntraFrame() {
   RequestKeyFrame();
diff --git a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h
index 5883a11..666612b 100644
--- a/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h
+++ b/webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h
@@ -427,9 +427,6 @@
                            WebRtc_UWord32* fec_rate,
                            WebRtc_UWord32* nackRate) const;
 
-  virtual int EstimatedReceiveBandwidth(
-      WebRtc_UWord32* available_bandwidth) const;
-
   virtual void SetRemoteSSRC(const WebRtc_UWord32 ssrc);
 
   virtual WebRtc_UWord32 SendTimeOfSendReport(const WebRtc_UWord32 send_report);
@@ -493,6 +490,7 @@
   WebRtc_Word64             last_process_time_;
   WebRtc_Word64             last_bitrate_process_time_;
   WebRtc_Word64             last_packet_timeout_process_time_;
+  WebRtc_Word64             last_rtt_process_time_;
   WebRtc_UWord16            packet_overhead_;
 
   scoped_ptr<CriticalSectionWrapper> critical_section_module_ptrs_;
diff --git a/webrtc/modules/rtp_rtcp/test/testAPI/test_api_rtcp.cc b/webrtc/modules/rtp_rtcp/test/testAPI/test_api_rtcp.cc
index d2ea532..d15dcc8 100644
--- a/webrtc/modules/rtp_rtcp/test/testAPI/test_api_rtcp.cc
+++ b/webrtc/modules/rtp_rtcp/test/testAPI/test_api_rtcp.cc
@@ -271,7 +271,7 @@
   std::vector<RTCPReportBlock> report_blocks;
   EXPECT_EQ(-1, module1->RemoteRTCPStat(NULL));
   EXPECT_EQ(0, module1->RemoteRTCPStat(&report_blocks));
-  EXPECT_EQ(1u, report_blocks.size());
+  ASSERT_EQ(1u, report_blocks.size());
   const RTCPReportBlock& reportBlockReceived = report_blocks[0];
 
   float secSinceLastReport =