inter_arrival_delta.cc (5798B)
1 /* 2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "modules/congestion_controller/goog_cc/inter_arrival_delta.h" 12 13 #include <algorithm> 14 #include <cstddef> 15 16 #include "api/units/time_delta.h" 17 #include "api/units/timestamp.h" 18 #include "rtc_base/checks.h" 19 #include "rtc_base/logging.h" 20 21 namespace webrtc { 22 23 static constexpr TimeDelta kBurstDeltaThreshold = TimeDelta::Millis(5); 24 static constexpr TimeDelta kMaxBurstDuration = TimeDelta::Millis(100); 25 26 InterArrivalDelta::InterArrivalDelta(TimeDelta send_time_group_length) 27 : send_time_group_length_(send_time_group_length), 28 current_timestamp_group_(), 29 prev_timestamp_group_(), 30 num_consecutive_reordered_packets_(0) {} 31 32 bool InterArrivalDelta::ComputeDeltas(Timestamp send_time, 33 Timestamp arrival_time, 34 Timestamp system_time, 35 size_t packet_size, 36 TimeDelta* send_time_delta, 37 TimeDelta* arrival_time_delta, 38 int* packet_size_delta) { 39 bool calculated_deltas = false; 40 if (current_timestamp_group_.IsFirstPacket()) { 41 // We don't have enough data to update the filter, so we store it until we 42 // have two frames of data to process. 43 current_timestamp_group_.send_time = send_time; 44 current_timestamp_group_.first_send_time = send_time; 45 current_timestamp_group_.first_arrival = arrival_time; 46 } else if (current_timestamp_group_.first_send_time > send_time) { 47 // Reordered packet. 48 return false; 49 } else if (NewTimestampGroup(arrival_time, send_time)) { 50 // First packet of a later send burst, the previous packets sample is ready. 51 if (prev_timestamp_group_.complete_time.IsFinite()) { 52 *send_time_delta = 53 current_timestamp_group_.send_time - prev_timestamp_group_.send_time; 54 *arrival_time_delta = current_timestamp_group_.complete_time - 55 prev_timestamp_group_.complete_time; 56 57 TimeDelta system_time_delta = current_timestamp_group_.last_system_time - 58 prev_timestamp_group_.last_system_time; 59 60 if (*arrival_time_delta - system_time_delta >= 61 kArrivalTimeOffsetThreshold) { 62 RTC_LOG(LS_WARNING) 63 << "The arrival time clock offset has changed (diff = " 64 << arrival_time_delta->ms() - system_time_delta.ms() 65 << " ms), resetting."; 66 Reset(); 67 return false; 68 } 69 if (*arrival_time_delta < TimeDelta::Zero()) { 70 // The group of packets has been reordered since receiving its local 71 // arrival timestamp. 72 ++num_consecutive_reordered_packets_; 73 if (num_consecutive_reordered_packets_ >= kReorderedResetThreshold) { 74 RTC_LOG(LS_WARNING) 75 << "Packets between send burst arrived out of order, resetting:" 76 << " arrival_time_delta_ms=" << arrival_time_delta->ms() 77 << ", send_time_delta_ms=" << send_time_delta->ms(); 78 Reset(); 79 } 80 return false; 81 } else { 82 num_consecutive_reordered_packets_ = 0; 83 } 84 *packet_size_delta = static_cast<int>(current_timestamp_group_.size) - 85 static_cast<int>(prev_timestamp_group_.size); 86 calculated_deltas = true; 87 } 88 prev_timestamp_group_ = current_timestamp_group_; 89 // The new timestamp is now the current frame. 90 current_timestamp_group_.first_send_time = send_time; 91 current_timestamp_group_.send_time = send_time; 92 current_timestamp_group_.first_arrival = arrival_time; 93 current_timestamp_group_.size = 0; 94 } else { 95 current_timestamp_group_.send_time = 96 std::max(current_timestamp_group_.send_time, send_time); 97 } 98 // Accumulate the frame size. 99 current_timestamp_group_.size += packet_size; 100 current_timestamp_group_.complete_time = arrival_time; 101 current_timestamp_group_.last_system_time = system_time; 102 103 return calculated_deltas; 104 } 105 106 // Assumes that `timestamp` is not reordered compared to 107 // `current_timestamp_group_`. 108 bool InterArrivalDelta::NewTimestampGroup(Timestamp arrival_time, 109 Timestamp send_time) const { 110 if (current_timestamp_group_.IsFirstPacket()) { 111 return false; 112 } else if (BelongsToBurst(arrival_time, send_time)) { 113 return false; 114 } else { 115 return send_time - current_timestamp_group_.first_send_time > 116 send_time_group_length_; 117 } 118 } 119 120 bool InterArrivalDelta::BelongsToBurst(Timestamp arrival_time, 121 Timestamp send_time) const { 122 RTC_DCHECK(current_timestamp_group_.complete_time.IsFinite()); 123 TimeDelta arrival_time_delta = 124 arrival_time - current_timestamp_group_.complete_time; 125 TimeDelta send_time_delta = send_time - current_timestamp_group_.send_time; 126 if (send_time_delta.IsZero()) 127 return true; 128 TimeDelta propagation_delta = arrival_time_delta - send_time_delta; 129 if (propagation_delta < TimeDelta::Zero() && 130 arrival_time_delta <= kBurstDeltaThreshold && 131 arrival_time - current_timestamp_group_.first_arrival < kMaxBurstDuration) 132 return true; 133 return false; 134 } 135 136 void InterArrivalDelta::Reset() { 137 num_consecutive_reordered_packets_ = 0; 138 current_timestamp_group_ = SendTimeGroup(); 139 prev_timestamp_group_ = SendTimeGroup(); 140 } 141 } // namespace webrtc