Improves buffer time calculation in network control tester.

The previous solution caused packet reordering if the bandwidth changed
with large buffers. To avoid this, the buffer time is tracked instead.
This means the the bandwidth is applied per packet and can't be
retroactively changed for packets already handled.

Bug: webrtc:8415
Change-Id: Ib6c97ba9b948220e88c79776aa8d96de289dcfb5
Reviewed-on: https://webrtc-review.googlesource.com/83723
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Stefan Holmer <stefan@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23629}
diff --git a/api/transport/test/network_control_tester.cc b/api/transport/test/network_control_tester.cc
index 0ba5302..2edabd0 100644
--- a/api/transport/test/network_control_tester.cc
+++ b/api/transport/test/network_control_tester.cc
@@ -63,7 +63,7 @@
     NetworkControllerConfig initial_config)
     : current_time_(Timestamp::seconds(100000)),
       packet_sequence_number_(1),
-      accumulated_buffer_(DataSize::Zero()) {
+      accumulated_buffer_(TimeDelta::Zero()) {
   initial_config.constraints.at_time = current_time_;
   controller_ = factory->Create(initial_config);
   process_interval_ = factory->GetProcessInterval();
@@ -79,6 +79,7 @@
                                             DataRate actual_bandwidth,
                                             TimeDelta propagation_delay,
                                             PacketProducer next_packet) {
+  RTC_CHECK(actual_bandwidth.bps() > 0);
   Timestamp start_time = current_time_;
   Timestamp last_process_time = current_time_;
   while (current_time_ - start_time < duration) {
@@ -100,9 +101,9 @@
         sent_packet.data_in_flight += packet.sent_packet->size;
       Update(&state_, controller_->OnSentPacket(sent_packet));
 
-      accumulated_buffer_ += sent_packet.size;
-      TimeDelta buffer_delay = accumulated_buffer_ / actual_bandwidth;
-      TimeDelta total_delay = propagation_delay + buffer_delay;
+      TimeDelta time_in_flight = sent_packet.size / actual_bandwidth;
+      accumulated_buffer_ += time_in_flight;
+      TimeDelta total_delay = propagation_delay + accumulated_buffer_;
       PacketResult result;
       result.sent_packet = sent_packet;
       result.receive_time = sent_packet.send_time + total_delay;
@@ -110,8 +111,7 @@
       outstanding_packets_.push_back(result);
     }
 
-    DataSize buffer_consumed =
-        std::min(accumulated_buffer_, packet_interval * actual_bandwidth);
+    TimeDelta buffer_consumed = std::min(accumulated_buffer_, packet_interval);
     accumulated_buffer_ -= buffer_consumed;
 
     if (outstanding_packets_.size() >= 2 &&
diff --git a/api/transport/test/network_control_tester.h b/api/transport/test/network_control_tester.h
index bb6837a..2e9fc02 100644
--- a/api/transport/test/network_control_tester.h
+++ b/api/transport/test/network_control_tester.h
@@ -63,7 +63,7 @@
   TimeDelta process_interval_ = TimeDelta::PlusInfinity();
   Timestamp current_time_;
   int64_t packet_sequence_number_;
-  DataSize accumulated_buffer_;
+  TimeDelta accumulated_buffer_;
   std::deque<PacketResult> outstanding_packets_;
   NetworkControlUpdate state_;
 };