Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify TWCC egress loss #196

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 5 additions & 20 deletions src/rtp/rtcp/twcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,29 +1102,15 @@ impl TwccSendRegister {
/// **Note:** The register only keeps a limited number of records and using `duration` values
/// larger than ~1-2 seconds is liable to be inaccurate since some packets sent might have already
/// been evicted from the register.
///
/// Packets sent very recently, within one RTT, are not considered because it's not likely that
/// a TWCC report could've been generated by the sender and received yet.
pub fn loss(&self, duration: Duration, now: Instant) -> Option<f32> {
let recent_rtt = self
.queue
.iter()
.rev()
.find_map(|r| r.rtt())
.map(|rtt| rtt.clamp(Duration::from_millis(5), Duration::from_millis(150)))
.unwrap_or(Duration::from_millis(50));

// Consider only packets that were sent at least one RTT ago. For more recent sends we will
// likely not have received the TWCC report in any case.
let upper_bound = now - recent_rtt;
// Consider only packets in the span specified by the caller
let lower_bound = now - duration;

let packets = self
.queue
.iter()
.rev()
.skip_while(|s| s.local_send_time >= upper_bound)
.skip_while(|s| s.recv_report.is_none())
.take_while(|s| s.local_send_time >= lower_bound);

let (total, lost) = packets.fold((0, 0), |(total, lost), s| {
Expand Down Expand Up @@ -1868,10 +1854,6 @@ mod test {
now = now + Duration::from_millis(15);
}

// This packet was sent too recently to be included when calculating loss
now = now + Duration::from_millis(35);
reg.register_seq(10.into(), now, 0);

now = now + Duration::from_millis(5);
reg.apply_report(
Twcc {
Expand Down Expand Up @@ -1908,6 +1890,9 @@ mod test {

let pct = (loss * 100.0).floor() as u32;

assert_eq!(pct, 33, "The loss percentage should be 33");
assert_eq!(
pct, 25,
"The loss percentage should be 25 as 2 out of 8 packets are lost"
);
}
}
Loading