Add support for transport wide sequence numbers

Also refactor packet router to use a map rather than iterate over all
rtp modules for each packet sent.

BUG=webrtc:4311

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

Cr-Commit-Position: refs/heads/master@{#9670}
diff --git a/webrtc/modules/bitrate_controller/BUILD.gn b/webrtc/modules/bitrate_controller/BUILD.gn
index 9280f03..4ef536b 100644
--- a/webrtc/modules/bitrate_controller/BUILD.gn
+++ b/webrtc/modules/bitrate_controller/BUILD.gn
@@ -17,8 +17,6 @@
     "include/bitrate_controller.h",
     "send_side_bandwidth_estimation.cc",
     "send_side_bandwidth_estimation.h",
-    "send_time_history.cc",
-    "send_time_history.h",
   ]
 
   if (is_win) {
diff --git a/webrtc/modules/bitrate_controller/bitrate_controller.gypi b/webrtc/modules/bitrate_controller/bitrate_controller.gypi
index a0c2fc9..44c1b89 100644
--- a/webrtc/modules/bitrate_controller/bitrate_controller.gypi
+++ b/webrtc/modules/bitrate_controller/bitrate_controller.gypi
@@ -22,8 +22,6 @@
         'include/bitrate_allocator.h',
         'send_side_bandwidth_estimation.cc',
         'send_side_bandwidth_estimation.h',
-        'send_time_history.cc',
-        'send_time_history.h',
       ],
       # TODO(jschuh): Bug 1348: fix size_t to int truncations.
       'msvs_disabled_warnings': [ 4267, ],
diff --git a/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc b/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc
index d54da99..4a045d9 100644
--- a/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc
+++ b/webrtc/modules/bitrate_controller/bitrate_controller_impl.cc
@@ -87,7 +87,6 @@
     : clock_(clock),
       observer_(observer),
       last_bitrate_update_ms_(clock_->TimeInMilliseconds()),
-      critsect_(CriticalSectionWrapper::CreateCriticalSection()),
       bandwidth_estimation_(),
       reserved_bitrate_bps_(0),
       last_bitrate_bps_(0),
@@ -107,7 +106,7 @@
 
 void BitrateControllerImpl::SetStartBitrate(int start_bitrate_bps) {
   {
-    CriticalSectionScoped cs(critsect_.get());
+    rtc::CritScope cs(&critsect_);
     bandwidth_estimation_.SetSendBitrate(start_bitrate_bps);
   }
   MaybeTriggerOnNetworkChanged();
@@ -116,7 +115,7 @@
 void BitrateControllerImpl::SetMinMaxBitrate(int min_bitrate_bps,
                                              int max_bitrate_bps) {
   {
-    CriticalSectionScoped cs(critsect_.get());
+    rtc::CritScope cs(&critsect_);
     bandwidth_estimation_.SetMinMaxBitrate(min_bitrate_bps, max_bitrate_bps);
   }
   MaybeTriggerOnNetworkChanged();
@@ -124,7 +123,7 @@
 
 void BitrateControllerImpl::SetReservedBitrate(uint32_t reserved_bitrate_bps) {
   {
-    CriticalSectionScoped cs(critsect_.get());
+    rtc::CritScope cs(&critsect_);
     reserved_bitrate_bps_ = reserved_bitrate_bps;
   }
   MaybeTriggerOnNetworkChanged();
@@ -132,7 +131,7 @@
 
 void BitrateControllerImpl::OnReceivedEstimatedBitrate(uint32_t bitrate) {
   {
-    CriticalSectionScoped cs(critsect_.get());
+    rtc::CritScope cs(&critsect_);
     bandwidth_estimation_.UpdateReceiverEstimate(bitrate);
   }
   MaybeTriggerOnNetworkChanged();
@@ -140,7 +139,7 @@
 
 int64_t BitrateControllerImpl::TimeUntilNextProcess() {
   const int64_t kBitrateControllerUpdateIntervalMs = 25;
-  CriticalSectionScoped cs(critsect_.get());
+  rtc::CritScope cs(&critsect_);
   int64_t time_since_update_ms =
       clock_->TimeInMilliseconds() - last_bitrate_update_ms_;
   return std::max<int64_t>(
@@ -151,7 +150,7 @@
   if (TimeUntilNextProcess() > 0)
     return 0;
   {
-    CriticalSectionScoped cs(critsect_.get());
+    rtc::CritScope cs(&critsect_);
     bandwidth_estimation_.UpdateEstimate(clock_->TimeInMilliseconds());
   }
   MaybeTriggerOnNetworkChanged();
@@ -165,7 +164,7 @@
     int number_of_packets,
     int64_t now_ms) {
   {
-    CriticalSectionScoped cs(critsect_.get());
+    rtc::CritScope cs(&critsect_);
     bandwidth_estimation_.UpdateReceiverBlock(fraction_loss, rtt,
                                               number_of_packets, now_ms);
   }
@@ -183,7 +182,7 @@
 bool BitrateControllerImpl::GetNetworkParameters(uint32_t* bitrate,
                                                  uint8_t* fraction_loss,
                                                  int64_t* rtt) {
-  CriticalSectionScoped cs(critsect_.get());
+  rtc::CritScope cs(&critsect_);
   int current_bitrate;
   bandwidth_estimation_.CurrentEstimate(&current_bitrate, fraction_loss, rtt);
   *bitrate = current_bitrate;
@@ -205,7 +204,7 @@
 }
 
 bool BitrateControllerImpl::AvailableBandwidth(uint32_t* bandwidth) const {
-  CriticalSectionScoped cs(critsect_.get());
+  rtc::CritScope cs(&critsect_);
   int bitrate;
   uint8_t fraction_loss;
   int64_t rtt;
@@ -218,5 +217,4 @@
   }
   return false;
 }
-
 }  // namespace webrtc
diff --git a/webrtc/modules/bitrate_controller/bitrate_controller_impl.h b/webrtc/modules/bitrate_controller/bitrate_controller_impl.h
index 3d38a54..a0131e2 100644
--- a/webrtc/modules/bitrate_controller/bitrate_controller_impl.h
+++ b/webrtc/modules/bitrate_controller/bitrate_controller_impl.h
@@ -20,9 +20,9 @@
 #include <list>
 #include <utility>
 
+#include "webrtc/base/criticalsection.h"
 #include "webrtc/base/scoped_ptr.h"
 #include "webrtc/modules/bitrate_controller/send_side_bandwidth_estimation.h"
-#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
 
 namespace webrtc {
 
@@ -64,22 +64,21 @@
 
   void OnNetworkChanged(uint32_t bitrate,
                         uint8_t fraction_loss,  // 0 - 255.
-                        int64_t rtt)
-      EXCLUSIVE_LOCKS_REQUIRED(*critsect_);
+                        int64_t rtt) EXCLUSIVE_LOCKS_REQUIRED(critsect_);
 
   // Used by process thread.
   Clock* clock_;
   BitrateObserver* observer_;
   int64_t last_bitrate_update_ms_;
 
-  const rtc::scoped_ptr<CriticalSectionWrapper> critsect_;
-  SendSideBandwidthEstimation bandwidth_estimation_ GUARDED_BY(*critsect_);
-  uint32_t reserved_bitrate_bps_ GUARDED_BY(*critsect_);
+  mutable rtc::CriticalSection critsect_;
+  SendSideBandwidthEstimation bandwidth_estimation_ GUARDED_BY(critsect_);
+  uint32_t reserved_bitrate_bps_ GUARDED_BY(critsect_);
 
-  uint32_t last_bitrate_bps_ GUARDED_BY(*critsect_);
-  uint8_t last_fraction_loss_ GUARDED_BY(*critsect_);
-  int64_t last_rtt_ms_ GUARDED_BY(*critsect_);
-  uint32_t last_reserved_bitrate_bps_ GUARDED_BY(*critsect_);
+  uint32_t last_bitrate_bps_ GUARDED_BY(critsect_);
+  uint8_t last_fraction_loss_ GUARDED_BY(critsect_);
+  int64_t last_rtt_ms_ GUARDED_BY(critsect_);
+  uint32_t last_reserved_bitrate_bps_ GUARDED_BY(critsect_);
 
   DISALLOW_IMPLICIT_CONSTRUCTORS(BitrateControllerImpl);
 };
diff --git a/webrtc/modules/bitrate_controller/include/bitrate_controller.h b/webrtc/modules/bitrate_controller/include/bitrate_controller.h
index 7303d06..bb53288 100644
--- a/webrtc/modules/bitrate_controller/include/bitrate_controller.h
+++ b/webrtc/modules/bitrate_controller/include/bitrate_controller.h
@@ -23,6 +23,7 @@
 namespace webrtc {
 
 class CriticalSectionWrapper;
+struct PacketInfo;
 
 class BitrateObserver {
   // Observer class for bitrate changes announced due to change in bandwidth
diff --git a/webrtc/modules/bitrate_controller/send_time_history.cc b/webrtc/modules/bitrate_controller/send_time_history.cc
deleted file mode 100644
index 7e0c89e..0000000
--- a/webrtc/modules/bitrate_controller/send_time_history.cc
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include <assert.h>
-
-#include "webrtc/modules/bitrate_controller/send_time_history.h"
-
-namespace webrtc {
-
-SendTimeHistory::SendTimeHistory(int64_t packet_age_limit)
-    : packet_age_limit_(packet_age_limit), oldest_sequence_number_(0) {
-}
-
-SendTimeHistory::~SendTimeHistory() {
-}
-
-void SendTimeHistory::Clear() {
-  history_.clear();
-}
-
-void SendTimeHistory::AddAndRemoveOldSendTimes(uint16_t sequence_number,
-                                               int64_t timestamp) {
-  EraseOld(timestamp - packet_age_limit_);
-
-  if (history_.empty())
-    oldest_sequence_number_ = sequence_number;
-
-  history_[sequence_number] = timestamp;
-}
-
-void SendTimeHistory::EraseOld(int64_t limit) {
-  while (!history_.empty()) {
-    auto it = history_.find(oldest_sequence_number_);
-    assert(it != history_.end());
-
-    if (it->second > limit)
-      return;  // Oldest packet within age limit, return.
-
-    // TODO(sprang): Warn if erasing (too many) old items?
-    history_.erase(it);
-    UpdateOldestSequenceNumber();
-  }
-}
-
-void SendTimeHistory::UpdateOldestSequenceNumber() {
-  // After removing an element from the map, update oldest_sequence_number_ to
-  // the element with the lowest sequence number higher than the previous
-  // value (there might be gaps).
-  if (history_.empty())
-    return;
-  auto it = history_.upper_bound(oldest_sequence_number_);
-  if (it == history_.end()) {
-    // No element with higher sequence number than oldest_sequence_number_
-    // found, check wrap around. Note that history_.upper_bound(0) will not
-    // find 0 even if it is there, need to explicitly check for 0.
-    it = history_.find(0);
-    if (it == history_.end())
-      it = history_.upper_bound(0);
-  }
-  assert(it != history_.end());
-  oldest_sequence_number_ = it->first;
-}
-
-bool SendTimeHistory::GetSendTime(uint16_t sequence_number,
-                                  int64_t* timestamp,
-                                  bool remove) {
-  auto it = history_.find(sequence_number);
-  if (it == history_.end())
-    return false;
-  *timestamp = it->second;
-  if (remove) {
-    history_.erase(it);
-    if (sequence_number == oldest_sequence_number_)
-      UpdateOldestSequenceNumber();
-  }
-  return true;
-}
-
-}  // namespace webrtc
diff --git a/webrtc/modules/bitrate_controller/send_time_history.h b/webrtc/modules/bitrate_controller/send_time_history.h
deleted file mode 100644
index 8835856..0000000
--- a/webrtc/modules/bitrate_controller/send_time_history.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#ifndef WEBRTC_MODULES_BITRATE_CONTROLLER_SEND_TIME_HISTORY_H_
-#define WEBRTC_MODULES_BITRATE_CONTROLLER_SEND_TIME_HISTORY_H_
-
-#include <map>
-
-#include "webrtc/base/constructormagic.h"
-#include "webrtc/base/basictypes.h"
-
-namespace webrtc {
-
-class SendTimeHistory {
- public:
-  explicit SendTimeHistory(int64_t packet_age_limit);
-  virtual ~SendTimeHistory();
-
-  void AddAndRemoveOldSendTimes(uint16_t sequence_number, int64_t timestamp);
-  bool GetSendTime(uint16_t sequence_number, int64_t* timestamp, bool remove);
-  void Clear();
-
- private:
-  void EraseOld(int64_t limit);
-  void UpdateOldestSequenceNumber();
-
-  const int64_t packet_age_limit_;
-  uint16_t oldest_sequence_number_;  // Oldest may not be lowest.
-  std::map<uint16_t, int64_t> history_;
-
-  DISALLOW_COPY_AND_ASSIGN(SendTimeHistory);
-};
-
-}  // namespace webrtc
-#endif  // WEBRTC_MODULES_BITRATE_CONTROLLER_SEND_TIME_HISTORY_H_
diff --git a/webrtc/modules/bitrate_controller/send_time_history_unittest.cc b/webrtc/modules/bitrate_controller/send_time_history_unittest.cc
deleted file mode 100644
index fc7099d..0000000
--- a/webrtc/modules/bitrate_controller/send_time_history_unittest.cc
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include <algorithm>
-#include <limits>
-#include <vector>
-
-#include "testing/gtest/include/gtest/gtest.h"
-#include "webrtc/modules/bitrate_controller/send_time_history.h"
-#include "webrtc/system_wrappers/interface/clock.h"
-
-namespace webrtc {
-
-static const int kDefaultHistoryLengthMs = 1000;
-
-class SendTimeHistoryTest : public ::testing::Test {
- protected:
-  SendTimeHistoryTest() : history_(kDefaultHistoryLengthMs), clock_(0) {}
-  ~SendTimeHistoryTest() {}
-
-  virtual void SetUp() {}
-
-  virtual void TearDown() {}
-
-  SendTimeHistory history_;
-  webrtc::SimulatedClock clock_;
-};
-
-TEST_F(SendTimeHistoryTest, AddRemoveOne) {
-  const uint16_t kSeqNo = 1;
-  const int64_t kTimestamp = 2;
-  history_.AddAndRemoveOldSendTimes(kSeqNo, kTimestamp);
-
-  int64_t time = 0;
-  EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, false));
-  EXPECT_EQ(kTimestamp, time);
-
-  time = 0;
-  EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, true));
-  EXPECT_EQ(kTimestamp, time);
-
-  time = 0;
-  EXPECT_FALSE(history_.GetSendTime(kSeqNo, &time, true));
-}
-
-TEST_F(SendTimeHistoryTest, AddThenRemoveOutOfOrder) {
-  struct Timestamp {
-    Timestamp(uint16_t sequence_number, int64_t timestamp)
-        : sequence_number(sequence_number), timestamp(timestamp) {}
-    uint16_t sequence_number;
-    int64_t timestamp;
-  };
-  std::vector<Timestamp> timestamps;
-  const size_t num_items = 100;
-  for (size_t i = 0; i < num_items; ++i) {
-    timestamps.push_back(
-        Timestamp(static_cast<uint16_t>(i), static_cast<int64_t>(i)));
-  }
-  std::vector<Timestamp> randomized_timestamps = timestamps;
-  std::random_shuffle(randomized_timestamps.begin(),
-                      randomized_timestamps.end());
-  for (size_t i = 0; i < num_items; ++i) {
-    history_.AddAndRemoveOldSendTimes(timestamps[i].sequence_number,
-                                      timestamps[i].timestamp);
-  }
-  for (size_t i = 0; i < num_items; ++i) {
-    int64_t timestamp;
-    EXPECT_TRUE(history_.GetSendTime(randomized_timestamps[i].sequence_number,
-                                     &timestamp, false));
-    EXPECT_EQ(randomized_timestamps[i].timestamp, timestamp);
-    EXPECT_TRUE(history_.GetSendTime(randomized_timestamps[i].sequence_number,
-                                     &timestamp, true));
-  }
-  for (size_t i = 0; i < num_items; ++i) {
-    int64_t timestamp;
-    EXPECT_FALSE(
-        history_.GetSendTime(timestamps[i].sequence_number, &timestamp, false));
-  }
-}
-
-TEST_F(SendTimeHistoryTest, HistorySize) {
-  const int kItems = kDefaultHistoryLengthMs / 100;
-  for (int i = 0; i < kItems; ++i) {
-    history_.AddAndRemoveOldSendTimes(i, i * 100);
-  }
-  int64_t timestamp;
-  for (int i = 0; i < kItems; ++i) {
-    EXPECT_TRUE(history_.GetSendTime(i, &timestamp, false));
-    EXPECT_EQ(i * 100, timestamp);
-  }
-  history_.AddAndRemoveOldSendTimes(kItems, kItems * 100);
-  EXPECT_FALSE(history_.GetSendTime(0, &timestamp, false));
-  for (int i = 1; i < (kItems + 1); ++i) {
-    EXPECT_TRUE(history_.GetSendTime(i, &timestamp, false));
-    EXPECT_EQ(i * 100, timestamp);
-  }
-}
-
-TEST_F(SendTimeHistoryTest, HistorySizeWithWraparound) {
-  const int kMaxSeqNo = std::numeric_limits<uint16_t>::max();
-  history_.AddAndRemoveOldSendTimes(kMaxSeqNo - 2, 0);
-  history_.AddAndRemoveOldSendTimes(kMaxSeqNo - 1, 100);
-  history_.AddAndRemoveOldSendTimes(kMaxSeqNo, 200);
-  history_.AddAndRemoveOldSendTimes(0, 1000);
-  int64_t timestamp;
-  EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 2, &timestamp, false));
-  EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo - 1, &timestamp, false));
-  EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo, &timestamp, false));
-  EXPECT_TRUE(history_.GetSendTime(0, &timestamp, false));
-
-  // Create a gap (kMaxSeqNo - 1) -> 0.
-  EXPECT_TRUE(history_.GetSendTime(kMaxSeqNo, &timestamp, true));
-
-  history_.AddAndRemoveOldSendTimes(1, 1100);
-
-  EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 2, &timestamp, false));
-  EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo - 1, &timestamp, false));
-  EXPECT_FALSE(history_.GetSendTime(kMaxSeqNo, &timestamp, false));
-  EXPECT_TRUE(history_.GetSendTime(0, &timestamp, false));
-  EXPECT_TRUE(history_.GetSendTime(1, &timestamp, false));
-}
-
-TEST_F(SendTimeHistoryTest, InterlievedGetAndRemove) {
-  const uint16_t kSeqNo = 1;
-  const int64_t kTimestamp = 2;
-
-  history_.AddAndRemoveOldSendTimes(kSeqNo, kTimestamp);
-  history_.AddAndRemoveOldSendTimes(kSeqNo + 1, kTimestamp + 1);
-
-  int64_t time = 0;
-  EXPECT_TRUE(history_.GetSendTime(kSeqNo, &time, true));
-  EXPECT_EQ(kTimestamp, time);
-
-  history_.AddAndRemoveOldSendTimes(kSeqNo + 2, kTimestamp + 2);
-
-  EXPECT_TRUE(history_.GetSendTime(kSeqNo + 1, &time, true));
-  EXPECT_EQ(kTimestamp + 1, time);
-  EXPECT_TRUE(history_.GetSendTime(kSeqNo + 2, &time, true));
-  EXPECT_EQ(kTimestamp + 2, time);
-}
-
-}  // namespace webrtc