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