diff --git a/call/fake_network_pipe.cc b/call/fake_network_pipe.cc index e3f5393e35..234e229dfa 100644 --- a/call/fake_network_pipe.cc +++ b/call/fake_network_pipe.cc @@ -25,6 +25,7 @@ namespace webrtc { namespace { constexpr int64_t kDefaultProcessIntervalMs = 5; +constexpr int64_t kLogIntervalMs = 5000; struct PacketArrivalTimeComparator { bool operator()(const NetworkPacket& p1, const NetworkPacket& p2) { return p1.arrival_time() < p2.arrival_time(); @@ -89,10 +90,10 @@ FakeNetworkPipe::FakeNetworkPipe(Clock* clock, config_(), dropped_packets_(0), sent_packets_(0), - total_packet_delay_(0), + total_packet_delay_us_(0), bursting_(false), - next_process_time_(clock_->TimeInMilliseconds()), - last_log_time_(clock_->TimeInMilliseconds()) { + next_process_time_us_(clock_->TimeInMicroseconds()), + last_log_time_us_(clock_->TimeInMicroseconds()) { SetConfig(config); } @@ -107,10 +108,10 @@ FakeNetworkPipe::FakeNetworkPipe(Clock* clock, config_(), dropped_packets_(0), sent_packets_(0), - total_packet_delay_(0), + total_packet_delay_us_(0), bursting_(false), - next_process_time_(clock_->TimeInMilliseconds()), - last_log_time_(clock_->TimeInMilliseconds()) { + next_process_time_us_(clock_->TimeInMicroseconds()), + last_log_time_us_(clock_->TimeInMicroseconds()) { SetConfig(config); } @@ -193,12 +194,13 @@ bool FakeNetworkPipe::EnqueuePacket(rtc::CopyOnWriteBuffer packet, return false; } - int64_t time_now = clock_->TimeInMilliseconds(); + int64_t time_now_us = clock_->TimeInMicroseconds(); // Delay introduced by the link capacity. int64_t capacity_delay_ms = 0; if (config.link_capacity_kbps > 0) { - const int bytes_per_millisecond = config.link_capacity_kbps / 8; + // Using bytes per millisecond to avoid losing precision. + const int64_t bytes_per_millisecond = config.link_capacity_kbps / 8; // To round to the closest millisecond we add half a milliseconds worth of // bytes to the delay calculation. capacity_delay_ms = (packet.size() + capacity_delay_error_bytes_ + @@ -207,17 +209,17 @@ bool FakeNetworkPipe::EnqueuePacket(rtc::CopyOnWriteBuffer packet, capacity_delay_error_bytes_ += packet.size() - capacity_delay_ms * bytes_per_millisecond; } - int64_t network_start_time = time_now; + int64_t network_start_time_us = time_now_us; // Check if there already are packets on the link and change network start // time forward if there is. if (!capacity_link_.empty() && - network_start_time < capacity_link_.back().arrival_time()) - network_start_time = capacity_link_.back().arrival_time(); + network_start_time_us < capacity_link_.back().arrival_time()) + network_start_time_us = capacity_link_.back().arrival_time(); - int64_t arrival_time = network_start_time + capacity_delay_ms; - capacity_link_.emplace(std::move(packet), time_now, arrival_time, options, - is_rtcp, media_type, packet_time); + int64_t arrival_time_us = network_start_time_us + capacity_delay_ms * 1000; + capacity_link_.emplace(std::move(packet), time_now_us, arrival_time_us, + options, is_rtcp, media_type, packet_time); return true; } @@ -235,8 +237,8 @@ int FakeNetworkPipe::AverageDelay() { if (sent_packets_ == 0) return 0; - return static_cast(total_packet_delay_ / - static_cast(sent_packets_)); + return static_cast(total_packet_delay_us_ / + (1000 * static_cast(sent_packets_))); } size_t FakeNetworkPipe::DroppedPackets() { @@ -250,7 +252,7 @@ size_t FakeNetworkPipe::SentPackets() { } void FakeNetworkPipe::Process() { - int64_t time_now = clock_->TimeInMilliseconds(); + int64_t time_now_us = clock_->TimeInMicroseconds(); std::queue packets_to_deliver; Config config; double prob_loss_bursting; @@ -263,22 +265,22 @@ void FakeNetworkPipe::Process() { } { rtc::CritScope crit(&process_lock_); - if (time_now - last_log_time_ > 5000) { - int64_t queueing_delay_ms = 0; + if (time_now_us - last_log_time_us_ > kLogIntervalMs * 1000) { + int64_t queueing_delay_us = 0; if (!capacity_link_.empty()) { - queueing_delay_ms = time_now - capacity_link_.front().send_time(); + queueing_delay_us = time_now_us - capacity_link_.front().send_time(); } - RTC_LOG(LS_INFO) << "Network queue: " << queueing_delay_ms << " ms."; - last_log_time_ = time_now; + RTC_LOG(LS_INFO) << "Network queue: " << queueing_delay_us << " us."; + last_log_time_us_ = time_now_us; } // Check the capacity link first. if (!capacity_link_.empty()) { - int64_t last_arrival_time = + int64_t last_arrival_time_us = delay_link_.empty() ? -1 : delay_link_.back().arrival_time(); bool needs_sort = false; while (!capacity_link_.empty() && - time_now >= capacity_link_.front().arrival_time()) { + time_now_us >= capacity_link_.front().arrival_time()) { // Time to get this packet. NetworkPacket packet = std::move(capacity_link_.front()); capacity_link_.pop(); @@ -293,18 +295,21 @@ void FakeNetworkPipe::Process() { bursting_ = false; } - int arrival_time_jitter = random_.Gaussian( - config.queue_delay_ms, config.delay_standard_deviation_ms); + int64_t arrival_time_jitter_us = + random_.Gaussian(config.queue_delay_ms, + config.delay_standard_deviation_ms) * + 1000; // If reordering is not allowed then adjust arrival_time_jitter // to make sure all packets are sent in order. if (!config.allow_reordering && !delay_link_.empty() && - packet.arrival_time() + arrival_time_jitter < last_arrival_time) { - arrival_time_jitter = last_arrival_time - packet.arrival_time(); + packet.arrival_time() + arrival_time_jitter_us < + last_arrival_time_us) { + arrival_time_jitter_us = last_arrival_time_us - packet.arrival_time(); } - packet.IncrementArrivalTime(arrival_time_jitter); - if (packet.arrival_time() >= last_arrival_time) { - last_arrival_time = packet.arrival_time(); + packet.IncrementArrivalTime(arrival_time_jitter_us); + if (packet.arrival_time() >= last_arrival_time_us) { + last_arrival_time_us = packet.arrival_time(); } else { needs_sort = true; } @@ -320,14 +325,14 @@ void FakeNetworkPipe::Process() { // Check the extra delay queue. while (!delay_link_.empty() && - time_now >= delay_link_.front().arrival_time()) { + time_now_us >= delay_link_.front().arrival_time()) { // Deliver this packet. NetworkPacket packet(std::move(delay_link_.front())); delay_link_.pop_front(); // |time_now| might be later than when the packet should have arrived, due // to NetworkProcess being called too late. For stats, use the time it // should have been on the link. - total_packet_delay_ += packet.arrival_time() - packet.send_time(); + total_packet_delay_us_ += packet.arrival_time() - packet.send_time(); packets_to_deliver.push(std::move(packet)); } sent_packets_ += packets_to_deliver.size(); @@ -340,9 +345,9 @@ void FakeNetworkPipe::Process() { DeliverPacket(&packet); } - next_process_time_ = !delay_link_.empty() - ? delay_link_.begin()->arrival_time() - : time_now + kDefaultProcessIntervalMs; + next_process_time_us_ = !delay_link_.empty() + ? delay_link_.begin()->arrival_time() + : time_now_us + kDefaultProcessIntervalMs * 1000; } void FakeNetworkPipe::DeliverPacket(NetworkPacket* packet) { @@ -357,9 +362,9 @@ void FakeNetworkPipe::DeliverPacket(NetworkPacket* packet) { } else if (receiver_) { PacketTime packet_time = packet->packet_time(); if (packet_time.timestamp != -1) { - int64_t queue_time = packet->arrival_time() - packet->send_time(); - RTC_CHECK(queue_time >= 0); - packet_time.timestamp += (queue_time * 1000); + int64_t queue_time_us = packet->arrival_time() - packet->send_time(); + RTC_CHECK(queue_time_us >= 0); + packet_time.timestamp += queue_time_us; packet_time.timestamp += (clock_offset_ms_ * 1000); } receiver_->DeliverPacket(packet->media_type(), @@ -369,8 +374,8 @@ void FakeNetworkPipe::DeliverPacket(NetworkPacket* packet) { int64_t FakeNetworkPipe::TimeUntilNextProcess() { rtc::CritScope crit(&process_lock_); - return std::max(next_process_time_ - clock_->TimeInMilliseconds(), - 0); + int64_t delay_us = next_process_time_us_ - clock_->TimeInMicroseconds(); + return std::max((delay_us + 500) / 1000, 0); } bool FakeNetworkPipe::HasTransport() const { @@ -391,7 +396,7 @@ void FakeNetworkPipe::ResetStats() { rtc::CritScope crit(&process_lock_); dropped_packets_ = 0; sent_packets_ = 0; - total_packet_delay_ = 0; + total_packet_delay_us_ = 0; } int FakeNetworkPipe::GetConfigCapacityKbps() const { @@ -409,25 +414,25 @@ void FakeNetworkPipe::AddToPacketSentCount(int count) { sent_packets_ += count; } -void FakeNetworkPipe::AddToTotalDelay(int delay_ms) { +void FakeNetworkPipe::AddToTotalDelay(int delay_us) { rtc::CritScope crit(&process_lock_); - total_packet_delay_ += delay_ms; + total_packet_delay_us_ += delay_us; } -int64_t FakeNetworkPipe::GetTimeInMilliseconds() const { - return clock_->TimeInMilliseconds(); +int64_t FakeNetworkPipe::GetTimeInMicroseconds() const { + return clock_->TimeInMicroseconds(); } bool FakeNetworkPipe::IsRandomLoss(double prob_loss) { return random_.Rand() < prob_loss; } -bool FakeNetworkPipe::ShouldProcess(int64_t time_now) const { - return time_now >= next_process_time_; +bool FakeNetworkPipe::ShouldProcess(int64_t time_now_us) const { + return time_now_us >= next_process_time_us_; } -void FakeNetworkPipe::SetTimeToNextProcess(int64_t skip_ms) { - next_process_time_ += skip_ms; +void FakeNetworkPipe::SetTimeToNextProcess(int64_t skip_us) { + next_process_time_us_ += skip_us; } } // namespace webrtc diff --git a/call/fake_network_pipe.h b/call/fake_network_pipe.h index 7331068d2a..a8c0d372c9 100644 --- a/call/fake_network_pipe.h +++ b/call/fake_network_pipe.h @@ -168,11 +168,11 @@ class FakeNetworkPipe : public Transport, public PacketReceiver, public Module { int GetConfigCapacityKbps() const; void AddToPacketDropCount(); void AddToPacketSentCount(int count); - void AddToTotalDelay(int delay_ms); - int64_t GetTimeInMilliseconds() const; + void AddToTotalDelay(int delay_us); + int64_t GetTimeInMicroseconds() const; bool IsRandomLoss(double prob_loss); - bool ShouldProcess(int64_t time_now) const; - void SetTimeToNextProcess(int64_t skip_ms); + bool ShouldProcess(int64_t time_now_us) const; + void SetTimeToNextProcess(int64_t skip_us); private: // Returns true if enqueued, or false if packet was dropped. @@ -208,7 +208,7 @@ class FakeNetworkPipe : public Transport, public PacketReceiver, public Module { // Statistics. size_t dropped_packets_ RTC_GUARDED_BY(process_lock_); size_t sent_packets_ RTC_GUARDED_BY(process_lock_); - int64_t total_packet_delay_ RTC_GUARDED_BY(process_lock_); + int64_t total_packet_delay_us_ RTC_GUARDED_BY(process_lock_); // Are we currently dropping a burst of packets? bool bursting_; @@ -220,9 +220,9 @@ class FakeNetworkPipe : public Transport, public PacketReceiver, public Module { // The probability to drop a burst of packets. double prob_start_bursting_ RTC_GUARDED_BY(config_lock_); - int64_t next_process_time_; + int64_t next_process_time_us_; - int64_t last_log_time_; + int64_t last_log_time_us_; int64_t capacity_delay_error_bytes_ = 0;