diff --git a/Cargo.lock b/Cargo.lock index b30c0b4a72c..f6b94b07919 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5583,6 +5583,7 @@ dependencies = [ "rand 0.10.3", "rand_pcg", "stats_alloc", + "tracing", ] [[package]] diff --git a/quest/m2/quic-realtime/README.md b/quest/m2/quic-realtime/README.md index f15e8f5fa39..9e5ef198a75 100644 --- a/quest/m2/quic-realtime/README.md +++ b/quest/m2/quic-realtime/README.md @@ -105,7 +105,6 @@ owns that feedback. ## Required -- [GCC candidate](/quest/m2/quic-realtime/gcc.md) - libwebrtc's GoogCC vendored into `moq-congestion` and refreshed to current WebRTC - [BBR3 delay bound](/quest/m2/quic-realtime/bbr-delay-bound.md) - BBR3 with inflight capped when the forward delay gradient shows queue growth - [SCReAMv2 candidate](/quest/m2/quic-realtime/scream.md) - the RFC 8298bis delay path ported to `moq-congestion` - [io_uring runs an application's controller](/quest/m2/quic-realtime/uring-controller.md) - moq-uring takes a controller factory, so the winner installs the same way on both runtimes diff --git a/quest/m2/quic-realtime/gcc.md b/quest/m2/quic-realtime/gcc.md deleted file mode 100644 index 33b399288a8..00000000000 --- a/quest/m2/quic-realtime/gcc.md +++ /dev/null @@ -1,31 +0,0 @@ -# [L] GCC candidate - -## Goal - -libwebrtc's Google Congestion Control runs as a controller in -`moq-congestion`, fed by receive timestamps, and reports its queuing delay, -throughput, and reaction time on the shared moq-shaper uplink presets. - -## Plan - -Decided 2026-10-17: vendor [goog_cc](https://github.com/kixelated/goog_cc) -into `rs/moq-congestion/src/gcc/` and refresh it to current WebRTC rather -than depend on the crates.io 0.1.4 release. The port predates AI-assisted -review and tracks a 2024-12-09 WebRTC snapshot, so owning it in-tree lets us -trim it to what QUIC needs and review it the way we review the rest of the -fork. - -- Refresh against current `modules/congestion_controller/goog_cc` and record - the WebRTC revision in the module docs. -- Trim what QUIC makes redundant: RTP packet feedback types become the - Controller's per-packet send and receive instants; drop the parts that only - serve RTCP or probe clusters unless the comparison needs them. -- GCC outputs a target rate and pacing rate; map them to the Controller's - window and `Metrics::{pacing_rate, bandwidth_estimate}`, keeping - its congestion-window pushback as the inflight bound. -- Keep the BSD license and PATENTS file with the vendored code. -- Implement `Controller::probes` and report `probe_inflight` so moq-quic's - native probing still raises the estimate (decided 2026-10-25, maintainer; - see the [epic](/quest/m2/quic-realtime/README.md)). Measure that a stopped - rung recovers under it. -- Unit tests drive the controller with mocked time, not wall clock. diff --git a/quest/m2/webrtc/README.md b/quest/m2/webrtc/README.md index 86bbb454ce6..7371cd8f3e6 100644 --- a/quest/m2/webrtc/README.md +++ b/quest/m2/webrtc/README.md @@ -12,7 +12,7 @@ Decided 2026-10-30 with the maintainer (planning interview), after the str0m wor - **Vendor, then replace.** Copy the upstream crates in, trim them, and fix them in-tree. Replacing a layer with our own (SDP, RTP/RTCP, SRTP, SCTP) is a later child once a concrete need appears; none is planned yet. - **One crate.** `moq-ice` merges into it, since its only users are `moq-rtc` and moq-tokio's data-channel transport. moq-tokio's `webrtc` feature moves into `moq-webrtc` as well, which breaks the cycle (`moq-webrtc` depends on moq-tokio; moq-tokio would otherwise need ICE and SCTP). The vendored crates become private modules, not separate published crates, keeping their MIT/Apache-2.0 notices (str0m's `packet/` carries webrtc-rs's Apache notice). -- **What goes** (from the 2026-10-30 inventory: we use frame mode, full ICE, and DTLS 1.2, and never enable str0m's BWE, pacer, or stats): GCC bandwidth estimation (8k lines; GCC is being ported into [moq-congestion](/quest/m2/quic-realtime/gcc.md)), the leaky pacer (replaced by moq-congestion's pacer, made public by [rtc-pacing](/quest/m2/rtc-pacing.md)), H.266, RED, DTMF, G.7xx and comfort noise, VLA and simulcast layers, RTCP XR and app feedback, stats, internal test exports, and dimpl's DTLS 1.3 and version auto-selection. Roughly 17k of str0m's 72k lines and 11k of dimpl's 33k. +- **What goes** (from the 2026-10-30 inventory: we use frame mode, full ICE, and DTLS 1.2, and never enable str0m's BWE, pacer, or stats): GCC bandwidth estimation (8k lines; GCC is ported into `moq-congestion` as `moq_congestion::gcc`), the leaky pacer (replaced by moq-congestion's pacer, made public by [rtc-pacing](/quest/m2/rtc-pacing.md)), H.266, RED, DTMF, G.7xx and comfort noise, VLA and simulcast layers, RTCP XR and app feedback, stats, internal test exports, and dimpl's DTLS 1.3 and version auto-selection. Roughly 17k of str0m's 72k lines and 11k of dimpl's 33k. - **DTLS 1.2 only.** Every browser negotiates 1.2 today. [moq-rtc on rind](/quest/m2/rind/rtc.md) narrows to 1.2 to match. - **SCTP stays**, for browser data channels in [P2P](/quest/m2/p2p/README.md), but compiles only with the data-channel feature, so WHIP/WHEP builds carry none of it. Vendored, it can later be simplified or put on moq-congestion. - **Upstream fixes:** after vendoring, nobody merges upstream automatically. Record the vendored upstream versions in the crate docs; the release-cut audit checks the upstream changelogs for security fixes (malformed SRTP/RTCP input, for example) and ports them. @@ -30,4 +30,3 @@ Decided 2026-10-30 with the maintainer (planning interview), after the str0m wor - [moq-rtc paces WHEP viewers](/quest/m2/rtc-pacing.md) - adds the NACK hook and the shared pacer on the vendored stack - [moq-rtc on rind](/quest/m2/rind/rtc.md) - the vendored DTLS and SRTP take rind's crypto -- [GCC candidate](/quest/m2/quic-realtime/gcc.md) - where bandwidth estimation lives instead of str0m diff --git a/rs/moq-congestion/Cargo.toml b/rs/moq-congestion/Cargo.toml index 7e71c22d833..594044b6531 100644 --- a/rs/moq-congestion/Cargo.toml +++ b/rs/moq-congestion/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" edition = "2024" rust-version.workspace = true repository = "https://github.com/moq-dev/moq" -license = "MIT OR Apache-2.0" +license = "(MIT OR Apache-2.0) AND BSD-3-Clause" [features] # Test-only `bbr3::Config::probe_rng_seed`, for moq-quic's deterministic transport tests. @@ -15,6 +15,7 @@ testing = [] kio = { workspace = true } rand = { workspace = true } rand_pcg = { workspace = true } +tracing = { workspace = true } [dev-dependencies] cpu-time = "1.0.0" diff --git a/rs/moq-congestion/README.md b/rs/moq-congestion/README.md index 051cb2ed53a..5eed580d649 100644 --- a/rs/moq-congestion/README.md +++ b/rs/moq-congestion/README.md @@ -1,15 +1,17 @@ # moq-congestion -BBR3, Cubic, and Unlimited congestion controllers, consumed by moq-quic and the native transport drivers. +BBR3, Cubic, GCC, and Unlimited congestion controllers, consumed by moq-quic and the native transport drivers. The controllers take explicit `kio::clock::Instant` values and never drive a runtime. The `Controller` trait names no transport types: `on_sent` returns an opaque per-packet state that the transport stores and hands back with each acknowledgement or loss, and the controller paces its own sends. -Construct controllers with `Factory::build` on an `Arc`, `Arc`, or `Arc`. `Bbr3`, `Cubic`, `Unlimited`, and `Metrics` live at the crate root. +Construct controllers with `Factory::build` on an `Arc`, `Arc`, `Arc`, or `Arc`. `Bbr3`, `Cubic`, `Gcc`, `Unlimited`, and `Metrics` live at the crate root. For callers of the unpublished API, replace `Bbr3Config` with `bbr3::Config`, `CubicConfig` with `cubic::Config`, `ControllerFactory` with `Factory`, and `ControllerMetrics` with `Metrics`. Remove `Controller::into_any` from custom implementations; inspect the controller's behavior or metrics instead of downcasting. `clone_box` still copies controller state for path migration. The test-only `Bbr3::probing_rtt` and `min_rtt` accessors are removed; model internals are tested in this crate. The code was extracted from [moq-quic](../moq-quic), under the same MIT or Apache-2.0 license. Cubic originated in quinn; BBR3 was ported from moq-dev/noq and carries the fixes listed in [moq-quic's fork history](../moq-quic/README.md). +GCC is libwebrtc's Google Congestion Control, a delay-based controller that reads the peer's receive timestamps (draft-ietf-quic-receive-ts). `src/gcc` vendors a Rust port of WebRTC's `goog_cc` under WebRTC's BSD license, kept in [`src/gcc/LICENSE`](src/gcc/LICENSE) with its `PATENTS` grant; its module docs record the WebRTC revision it tracks and what it trims. + Controller tests run with `just rs test -p moq-congestion`. The per-packet benchmark runs nightly, or locally with: ```sh diff --git a/rs/moq-congestion/src/gcc/AUTHORS b/rs/moq-congestion/src/gcc/AUTHORS new file mode 100644 index 00000000000..46587ac6f38 --- /dev/null +++ b/rs/moq-congestion/src/gcc/AUTHORS @@ -0,0 +1,212 @@ +# Names should be added to this file with this pattern: +# +# For individuals: +# Name +# +# For organizations: +# Organization +# +# See python fnmatch module documentation for more information. +# +# Please keep the list sorted. + +# BEGIN individuals section. +Aaron Clauson +Adam Fedor +Akshay Shah +Alexander Brauckmann +Alexandre Gouaillard +Alex Henrie +Andrew MacDonald +Andrey Efremov +Andrew Johnson +Andy Tamilo +Anil Kumar +Anton Barkov +Ben Strong +Berthold Herrmann +Bob Withers +Brett Hebert +Brett Hebert +Bridger Maxwell +Bruno Pitrus +Cheng Qian +Christophe Dumez +Chris Tserng +Cody Barnes +Colin Plumb +Corby Hoback +Cyril Lashkevich +CZ Theng +Danail Kirov +Dave Cowart +David Porter +David Sanders +Dax Booysen +Denis Genestier +Dennis Angelo +Dharmesh Chauhan +Di Wu +Dirk-Jan C. Binnema +Dmitry Lizin +Eike Rathke +Eric Rescorla, RTFM Inc. +Filip Hlasek +Frederik Riedel, Frogg GmbH +Gao Chun +Giji Gangadharan +Graham Yoakum +Guillaume Petit +Gustavo Garcia +Hans Knoechel +Helmut Januschka +Hugues Ekra +Hyungjoo Na +Jake Hilton +James H. Brown +Jan Grulich +Jan Kalab +Jens Nielsen +Jesús Leganés-Combarro +Jiawei Ou +Jie Mao +Jiwon Kim +Johnny Wong +Jose Antonio Olivera Ortega +Karim Hammache +Keiichi Enomoto +Kiran Thind +Korniltsev Anatoly +Kyutae Lee +lauren n. liberda +Lennart Grahl +Luke Weber +Maksim Khobat +Mallikarjuna Rao V +Manish Jethani +Martin Storsjo +Matthias Liebig +Maksim Sisov +Maxim Pavlov +Maxim Potapov +Michael Iedema +Michael Klingbeil +Michał Zarach +Michel Promonet +Miguel Paris +Mike Gilbert +Mike Wei +Min Wang +Mike Woodworth +Mo Zanaty +Mohamed Risaldar UT +Nico Schlumprecht +Niek van der Maas +Olivier Crête +Pali Rohar +Paul Kapustin +Peng Yu +Pete Makeev +Philipp Hancke +Piasy Xu +Rafael Lopez Diez +Ralph Giles +Raman Budny +Ramprakash Jelari +Riku Voipio +Robert Bares +Robert Mader +Robert Mader +Robert Nagy +Ryan Yoakum +Samuel Attard +Sarah Thompson +Satender Saroha +Saul Kravitz +Sergio Garcia Murillo +Shaofan Qi +Shigemasa Watanabe +Shuhai Peng +Shunbo Li +Shunbo Li +Seija +Silviu Caragea +Stefan Gula +Stephan Hartmann +Steve Reid +Tao chen +Takaaki Suzuki +Tarun Chawla +Todd Wong +Tomas Popela +Trevor Hayes +Uladzislau Susha +Vicken Simonian +Victor Costan +Vinzenz Feenstra +Vladimir Beloborodov +Xiaohong Xu +Xiaolei Yu +Xinchao Tian +Yaowen Guo +Youfa +Yura Yaroshevich +Yuriy Pavlyshak +Yusuke Suzuki +Pengfei Han +Yingying Ma +Hailin Zhao +# END individuals section. + +# BEGIN organizations section. +8x8 Inc. <*@8x8.com> +8x8 Inc. <*@jitsi.org> +8x8 Inc. <*@sip-communicator.org> +Agora IO <*@agora.io> +ARM Holdings <*@arm.com> +BroadSoft Inc. <*@broadsoft.com> +Canonical Ltd <*@canonical.com> +CoSMo Software Consulting, Pte Ltd <*@cosmosoftware.io> +Discord Inc. <*@discordapp.com> +Facebook Inc. <*@fb.com> +Google Inc. <*@google.com> +Highfive, Inc. <*@highfive.com> +Hopin Ltd. <*@hopin.to> +HyperConnect Inc. <*@hpcnt.com> +Intel Corporation <*@intel.com> +LG Electronics, Inc. <*@lge.com> +Life On Air Inc. <*@lifeonair.com> +LiveKit <*@livekit.io> +Meta Platforms, Inc. <*@meta.com> +Microsoft Corporation <*@microsoft.com> +MIPS Technologies <*@mips.com> +Mozilla Foundation <*@mozilla.com> +Netgem S.A. <*@netgem.com> +Nutanix Inc. <*@nutanix.com> +NVIDIA Corporation <*@nvidia.com> +Opera Software ASA <*@opera.com> +Optical Tone Ltd <*@opticaltone.com> +Pengutronix e.K. <*@pengutronix.de> +Quebic Inc. <*@quebic.com> +Raptor Computing Systems, LLC <*@raptorcs.com> +RingCentral, Inc. <*@ringcentral.com> +Signal Messenger, LLC <*@signal.org> +Sinch AB <*@sinch.com> +struktur AG <*@struktur.de> +Telenor Digital AS <*@telenor.com> +Temasys Communications <*@temasys.io> +The Chromium Authors <*@chromium.org> +The WebRTC Authors <*@webrtc.org> +Threema GmbH <*@threema.ch> +Tuple, LLC <*@tuple.app> +Twilio, Inc. <*@twilio.com> +Twitch Interactive, Inc. <*@justin.tv> +Vewd Software AS <*@vewd.com> +Videona Socialmedia <*@videona.com> +Videxio AS <*@videxio.com> +Vidyo, Inc. <*@vidyo.com> +Vonage Holdings Corp. <*@vonage.com> +Wang Qing +Wire Swiss GmbH <*@wire.com> +&yet LLC <*@andyet.com> +# END organizations section. diff --git a/rs/moq-congestion/src/gcc/LICENSE b/rs/moq-congestion/src/gcc/LICENSE new file mode 100644 index 00000000000..4c41b7b251e --- /dev/null +++ b/rs/moq-congestion/src/gcc/LICENSE @@ -0,0 +1,29 @@ +Copyright (c) 2011, The WebRTC project authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + * Neither the name of Google nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/rs/moq-congestion/src/gcc/PATENTS b/rs/moq-congestion/src/gcc/PATENTS new file mode 100644 index 00000000000..190607ac261 --- /dev/null +++ b/rs/moq-congestion/src/gcc/PATENTS @@ -0,0 +1,24 @@ +Additional IP Rights Grant (Patents) + +"This implementation" means the copyrightable works distributed by +Google as part of the WebRTC code package. + +Google hereby grants to you a perpetual, worldwide, non-exclusive, +no-charge, irrevocable (except as stated in this section) patent +license to make, have made, use, offer to sell, sell, import, +transfer, and otherwise run, modify and propagate the contents of this +implementation of the WebRTC code package, where such license applies +only to those patent claims, both currently owned by Google and +acquired in the future, licensable by Google that are necessarily +infringed by this implementation of the WebRTC code package. This +grant does not include claims that would be infringed only as a +consequence of further modification of this implementation. If you or +your agent or exclusive licensee institute or order or agree to the +institution of patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that this +implementation of the WebRTC code package or any code incorporated +within this implementation of the WebRTC code package constitutes +direct or contributory patent infringement, or inducement of patent +infringement, then any patent rights granted to you under this License +for this implementation of the WebRTC code package shall terminate as +of the date such litigation is filed. diff --git a/rs/moq-congestion/src/gcc/alr_detector.rs b/rs/moq-congestion/src/gcc/alr_detector.rs new file mode 100644 index 00000000000..eba328f6dec --- /dev/null +++ b/rs/moq-congestion/src/gcc/alr_detector.rs @@ -0,0 +1,369 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::{ + api::units::{DataRate, DataSize, Timestamp}, + pacing::IntervalBudget, +}; + +#[derive(Clone, Debug)] +pub struct AlrDetectorConfig { + // Sent traffic ratio as a function of network capacity used to determine + // application-limited region. ALR region start when bandwidth usage drops + // below AlrStartUsageRatio and ends when it raises above + // AlrEndUsageRatio. NOTE: This is intentionally conservative at the moment + // until BW adjustments of application limited region is fine tuned. + pub bandwidth_usage_ratio: f64, + pub start_budget_level_ratio: f64, + pub stop_budget_level_ratio: f64, +} + +impl Default for AlrDetectorConfig { + fn default() -> Self { + // WebRTC-ProbingScreenshareBwe is default-on with the fixed settings + // "1.0,2875,80,40,-60,3", which override the detector's own defaults. + Self { + bandwidth_usage_ratio: 0.80, + start_budget_level_ratio: 0.40, + stop_budget_level_ratio: -0.60, + } + } +} + +// Application limited region detector is a class that utilizes signals of +// elapsed time and bytes sent to estimate whether network traffic is +// currently limited by the application's ability to generate traffic. +// +// AlrDetector provides a signal that can be utilized to adjust +// estimate bandwidth. +// Note: This class is not thread-safe. +#[derive(Clone)] +pub struct AlrDetector { + conf: AlrDetectorConfig, + + last_send_time: Option, + + alr_budget: IntervalBudget, + alr_started_time: Option, +} + +impl Default for AlrDetector { + fn default() -> Self { + Self::new(AlrDetectorConfig::default()) + } +} + +impl AlrDetector { + pub fn new(conf: AlrDetectorConfig) -> Self { + Self { + conf, + last_send_time: None, + alr_budget: IntervalBudget::new(0, true), + alr_started_time: None, + } + } + + // Upstream stamps the region's start with the clock when the send is processed; the send + // time is that instant here, and keeps the detector on the controller's mocked time. + pub fn on_bytes_sent(&mut self, bytes_sent: DataSize, send_time: Timestamp) { + let Some(last_send_time) = self.last_send_time else { + // Since the duration for sending the bytes is unknwon, return without + // updating alr state. + self.last_send_time = Some(send_time); + return; + }; + // A deliberate departure from upstream, which rounds each interval to whole milliseconds + // (`(send_time - last_send_time).ms()`), so sends under half a millisecond apart never + // refill its budget and sends 0.5-1ms apart refill it for a whole millisecond. Rounding + // each time instead, as upstream did before its send times became `Timestamp`s, keeps the + // intervals adding up to the time between the sends. + let delta_time_ms = send_time.ms() - last_send_time.ms(); + self.last_send_time = Some(send_time); + + self.alr_budget.use_budget(bytes_sent.bytes() as usize); + self.alr_budget.increase_budget(delta_time_ms); + if self.alr_budget.budget_ratio() > self.conf.start_budget_level_ratio + && self.alr_started_time.is_none() + { + self.alr_started_time = Some(send_time); + } else if self.alr_budget.budget_ratio() < self.conf.stop_budget_level_ratio + && self.alr_started_time.is_some() + { + self.alr_started_time = None; + } + } + + // Set current estimated bandwidth. + pub fn set_estimated_bitrate(&mut self, bitrate: DataRate) { + debug_assert!(bitrate > DataRate::zero()); + self.alr_budget + .set_target_rate_kbps((bitrate * self.conf.bandwidth_usage_ratio).kbps()); + } + + // Returns time when the current application-limited region started or empty + // result if the sender is currently not application-limited. + pub fn get_application_limited_region_start_time(&self) -> Option { + self.alr_started_time + } +} + +#[cfg(test)] +mod test { + use super::*; + use crate::gcc::api::units::TimeDelta; + + const ESTIMATED_BITRATE_BPS: i64 = 300000; + + pub struct SimulateOutgoingTrafficIn<'a> { + alr_detector: &'a mut AlrDetector, + timestamp_ms: &'a mut i64, + interval_ms: Option, + usage_percentage: Option, + } + + impl<'a> SimulateOutgoingTrafficIn<'a> { + pub fn new(alr_detector: &'a mut AlrDetector, timestamp_ms: &'a mut i64) -> Self { + Self { + alr_detector, + timestamp_ms, + interval_ms: None, + usage_percentage: None, + } + } + + pub fn for_time_ms(mut self, time_ms: i64) -> Self { + self.interval_ms.replace(time_ms); + self + } + + pub fn at_percent_of_estimated_bitrate(mut self, usage_percentage: i64) { + self.usage_percentage.replace(usage_percentage); + self.produce_traffic(); + } + + fn produce_traffic(&mut self) { + let interval_ms = self.interval_ms.unwrap(); + let usage_percentage = self.usage_percentage.unwrap(); + const TIME_STEP_MS: i64 = 10; + let mut t: i64 = 0; + + while t < interval_ms { + *self.timestamp_ms += TIME_STEP_MS; + self.alr_detector.on_bytes_sent( + DataSize::from_bytes( + ESTIMATED_BITRATE_BPS * usage_percentage * TIME_STEP_MS / (8 * 100 * 1000), + ), + Timestamp::from_millis(*self.timestamp_ms), + ); + t += TIME_STEP_MS; + } + let remainder_ms: i64 = interval_ms % TIME_STEP_MS; + if remainder_ms > 0 { + *self.timestamp_ms += TIME_STEP_MS; + self.alr_detector.on_bytes_sent( + DataSize::from_bytes( + ESTIMATED_BITRATE_BPS * usage_percentage * remainder_ms / (8 * 100 * 1000), + ), + Timestamp::from_millis(*self.timestamp_ms), + ); + } + } + } + + #[test] + fn alr_detection() { + let mut timestamp_ms: i64 = 1000; + let mut alr_detector = AlrDetector::default(); + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS)); + + // Start in non-ALR state. + assert!( + alr_detector + .get_application_limited_region_start_time() + .is_none() + ); + + // Stay in non-ALR state when usage is close to 100%. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(1000) + .at_percent_of_estimated_bitrate(90); + assert!( + alr_detector + .get_application_limited_region_start_time() + .is_none() + ); + + // Verify that we ALR starts when bitrate drops below 20%. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(1500) + .at_percent_of_estimated_bitrate(20); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_some()) + ); + + // Verify that ALR ends when usage is above 65%. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(4000) + .at_percent_of_estimated_bitrate(100); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_none()) + ); + } + + #[test] + fn short_spike() { + let mut timestamp_ms: i64 = 1000; + let mut alr_detector = AlrDetector::default(); + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS)); + // Start in non-ALR state. + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_none()) + ); + + // Verify that we ALR starts when bitrate drops below 20%. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(1000) + .at_percent_of_estimated_bitrate(20); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_some()) + ); + + // Verify that we stay in ALR region even after a short bitrate spike. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(100) + .at_percent_of_estimated_bitrate(150); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_some()) + ); + + // ALR ends when usage is above 65%. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(3000) + .at_percent_of_estimated_bitrate(100); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_none()) + ); + } + + #[test] + fn bandwidth_estimate_changes() { + let mut timestamp_ms: i64 = 1000; + let mut alr_detector = AlrDetector::default(); + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS)); + + // Start in non-ALR state. + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_none()) + ); + + // ALR starts when bitrate drops below 20%. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(1000) + .at_percent_of_estimated_bitrate(20); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_some()) + ); + + // When bandwidth estimate drops the detector should stay in ALR mode and quit + // it shortly afterwards as the sender continues sending the same amount of + // traffic. This is necessary to ensure that ProbeController can still react + // to the BWE drop by initiating a new probe. + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS / 5)); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_some()) + ); + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(1000) + .at_percent_of_estimated_bitrate(50); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_none()) + ); + } + + #[test] + fn parse_alr_specific_field_trial() { + let config = AlrDetectorConfig { + bandwidth_usage_ratio: 0.90, + start_budget_level_ratio: 0.0, + stop_budget_level_ratio: -0.10, + }; + let mut alr_detector = AlrDetector::new(config); + let mut timestamp_ms: i64 = 1000; + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS)); + + // Start in non-ALR state. + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_none()) + ); + + // ALR does not start at 100% utilization. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(1000) + .at_percent_of_estimated_bitrate(100); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_none()) + ); + + // ALR does start at 85% utilization. + // Overused 10% above so it should take about 2s to reach a budget level of + // 0%. + SimulateOutgoingTrafficIn::new(&mut alr_detector, &mut timestamp_ms) + .for_time_ms(2100) + .at_percent_of_estimated_bitrate(85); + assert!( + (alr_detector + .get_application_limited_region_start_time() + .is_some()) + ); + } + + /// Packets under a millisecond apart refill the budget for the time between them: 1200 bytes + /// every 400us is 24 Mbit/s, under 80% of a 40 Mbit/s estimate, so the sender is + /// application-limited + #[test] + fn sub_millisecond_sends_refill_the_budget() { + let mut alr_detector = AlrDetector::default(); + alr_detector.set_estimated_bitrate(DataRate::from_kilobits_per_sec(40_000)); + let mut send_time = Timestamp::from_millis(1000); + for _ in 0..10_000 { + alr_detector.on_bytes_sent(DataSize::from_bytes(1200), send_time); + send_time += TimeDelta::from_micros(400); + } + assert!( + alr_detector + .get_application_limited_region_start_time() + .is_some() + ); + } +} diff --git a/rs/moq-congestion/src/gcc/api/mod.rs b/rs/moq-congestion/src/gcc/api/mod.rs new file mode 100644 index 00000000000..1fc28f0ed8e --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/mod.rs @@ -0,0 +1,5 @@ +/// The messages GoogCC exchanges with its transport. +pub mod transport; + +/// WebRTC's unit types, such as [DataSize](units::DataSize) and [DataRate](units::DataRate). +pub mod units; diff --git a/rs/moq-congestion/src/gcc/api/transport/bandwidth_usage.rs b/rs/moq-congestion/src/gcc/api/transport/bandwidth_usage.rs new file mode 100644 index 00000000000..10c917c19cd --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/transport/bandwidth_usage.rs @@ -0,0 +1,6 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BandwidthUsage { + Normal, + Underusing, + Overusing, +} diff --git a/rs/moq-congestion/src/gcc/api/transport/mod.rs b/rs/moq-congestion/src/gcc/api/transport/mod.rs new file mode 100644 index 00000000000..a057b687a16 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/transport/mod.rs @@ -0,0 +1,5 @@ +mod bandwidth_usage; +mod network_types; + +pub use bandwidth_usage::*; +pub use network_types::*; diff --git a/rs/moq-congestion/src/gcc/api/transport/network_types.rs b/rs/moq-congestion/src/gcc/api/transport/network_types.rs new file mode 100644 index 00000000000..9e99091042c --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/transport/network_types.rs @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! The messages GoogCC exchanges with its transport, trimmed to what a QUIC sender has: each +//! packet's send time, size and probe cluster, and its receive time or loss. RTP's sequence +//! numbers, ECN feedback, audio flag and untracked data, REMB, RTCP loss reports, route changes and +//! the network state estimate have no source here, and the pacer has no padding to send. + +use crate::gcc::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; + +/// Use StreamsConfig for information about streams that is required for specific +/// adjustments to the algorithms in network controllers. Especially useful +/// for experiments. +#[derive(Debug, Clone, Copy, Default)] +pub struct StreamsConfig { + pub at_time: Timestamp, + pub requests_alr_probing: Option, + /// If `enable_repeated_initial_probing` is set to true, Probes are sent + /// periodically every 1s during the first 5s after the network becomes + /// available. The probes ignores max_total_allocated_bitrate. + pub enable_repeated_initial_probing: bool, + pub pacing_factor: Option, + + // TODO(srte): Use BitrateAllocationLimits here. + pub min_total_allocated_bitrate: Option, + pub max_total_allocated_bitrate: Option, +} + +#[derive(Debug, Clone, Copy)] +pub struct TargetRateConstraints { + pub at_time: Timestamp, + pub min_data_rate: Option, + pub max_data_rate: Option, + /// The initial bandwidth estimate to base target rate on. This should be used + /// as the basis for initial OnTargetTransferRate and OnPacerConfig callbacks. + pub starting_rate: Option, +} + +impl Default for TargetRateConstraints { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + min_data_rate: None, + max_data_rate: None, + starting_rate: None, + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct NetworkAvailability { + pub at_time: Timestamp, + pub network_available: bool, +} + +impl Default for NetworkAvailability { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + network_available: false, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct PacedPacketInfo { + pub probe_cluster_id: i32, + pub probe_cluster_min_probes: i64, + pub probe_cluster_min_bytes: i64, +} + +impl PacedPacketInfo { + pub const NOT_APROBE: i32 = -1; + + pub const fn new( + probe_cluster_id: i32, + probe_cluster_min_probes: i64, + probe_cluster_min_bytes: i64, + ) -> Self { + Self { + probe_cluster_id, + probe_cluster_min_probes, + probe_cluster_min_bytes, + } + } +} + +impl Default for PacedPacketInfo { + fn default() -> Self { + Self::new(Self::NOT_APROBE, -1, -1) + } +} + +#[derive(Debug, Clone, Copy)] +pub struct SentPacket { + pub send_time: Timestamp, + /// Size of packet with overhead up to IP layer. + pub size: DataSize, + /// Probe cluster id and parameters including bitrate, number of packets and + /// number of bytes. + pub pacing_info: PacedPacketInfo, + /// Tracked data in flight when the packet was sent, excluding unacked data. + pub data_in_flight: DataSize, +} + +impl Default for SentPacket { + fn default() -> Self { + Self { + send_time: Timestamp::plus_infinity(), + size: DataSize::zero(), + pacing_info: PacedPacketInfo::default(), + data_in_flight: DataSize::zero(), + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct RoundTripTimeUpdate { + pub round_trip_time: TimeDelta, +} + +/// Packet level feedback +#[derive(Debug, Clone, Copy)] +pub struct PacketResult { + pub sent_packet: SentPacket, + pub receive_time: Timestamp, + /// Whether `receive_time` is only a stand-in, so the packet counts as received but not in + /// delay or RTT calculations, as upstream marks a retransmission it cannot match + pub ambiguous_receive_time: bool, +} + +impl PacketResult { + pub const fn is_received(&self) -> bool { + !self.receive_time.is_plus_infinity() + } +} + +impl Default for PacketResult { + fn default() -> Self { + Self { + sent_packet: SentPacket::default(), + receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, + } + } +} + +#[derive(Debug, Clone)] +pub struct TransportPacketsFeedback { + pub feedback_time: Timestamp, + pub data_in_flight: DataSize, + pub packet_feedbacks: Vec, +} + +impl Default for TransportPacketsFeedback { + fn default() -> Self { + Self { + feedback_time: Timestamp::plus_infinity(), + data_in_flight: DataSize::zero(), + packet_feedbacks: Vec::new(), + } + } +} + +impl TransportPacketsFeedback { + /// Packets received at a known time; one received at an ambiguous time is left out, since + /// every caller reads the time + pub fn received_with_send_info(&self) -> impl Iterator { + self.packet_feedbacks + .iter() + .filter(|fb| fb.is_received() && !fb.ambiguous_receive_time) + } + + pub fn sorted_by_receive_time(&self) -> Vec { + let mut res: Vec = self.received_with_send_info().cloned().collect(); + // Upstream breaks a tie on send time, then sequence number; a stable sort keeps send + // order, which QUIC acknowledges in. + res.sort_by(|a, b| { + a.receive_time + .cmp(&b.receive_time) + .then(a.sent_packet.send_time.cmp(&b.sent_packet.send_time)) + }); + res + } +} + +#[derive(Debug, Clone, Copy)] +pub struct PacerConfig { + /// Pacer should send at most data_window data over time_window duration. + /// If `time_window` is zero, the pacer should pace every packet as accurately + /// as possible and data_rate() is calculated over one second. + pub data_window: DataSize, + pub time_window: TimeDelta, +} + +impl Default for PacerConfig { + fn default() -> Self { + Self { + data_window: DataSize::infinity(), + time_window: TimeDelta::plus_infinity(), + } + } +} + +impl PacerConfig { + pub const DEFAULT_TIME_INTERVAL: TimeDelta = TimeDelta::from_millis(40); + + pub fn create(send_rate: DataRate, time_window: TimeDelta) -> Self { + let mut pacer_config = Self { + time_window, + ..Default::default() + }; + // Note that precision is lost here, since DataSize store number of bytes. + pacer_config.data_window = send_rate * pacer_config.rate_window(); + pacer_config + } + + pub fn rate_window(&self) -> TimeDelta { + if self.time_window.is_zero() { + TimeDelta::from_seconds(1) + } else { + self.time_window + } + } + + pub fn data_rate(&self) -> DataRate { + self.data_window / self.rate_window() + } +} + +#[derive(Debug, Clone)] +pub struct ProbeClusterConfig { + pub at_time: Timestamp, + pub target_data_rate: DataRate, + /// Duration of a probe. + pub target_duration: TimeDelta, + /// Delta time between sent bursts of packets during probe. + pub min_probe_delta: TimeDelta, + pub target_probe_count: i32, + pub id: i32, +} + +impl Default for ProbeClusterConfig { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + target_data_rate: DataRate::zero(), + target_duration: TimeDelta::zero(), + min_probe_delta: TimeDelta::from_millis(2), + target_probe_count: 0, + id: 0, + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct TargetTransferRate { + pub target_rate: DataRate, +} + +/// Contains updates of network controller comand state. Using optionals to +/// indicate whether a member has been updated. The array of probe clusters +/// should be used to send out probes if not empty. +#[derive(Default, Debug, Clone)] +pub struct NetworkControlUpdate { + pub congestion_window: Option, + pub pacer_config: Option, + pub probe_cluster_configs: Vec, + pub target_rate: Option, +} + +// Process control +pub struct ProcessInterval { + pub at_time: Timestamp, + pub pacer_queue: Option, +} + +impl Default for ProcessInterval { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + pacer_queue: None, + } + } +} diff --git a/rs/moq-congestion/src/gcc/api/units/data_rate.rs b/rs/moq-congestion/src/gcc/api/units/data_rate.rs new file mode 100644 index 00000000000..b534adb2ffc --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/data_rate.rs @@ -0,0 +1,392 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! DataRate is a class that represents a given data rate. This can be used to +//! represent bandwidth, encoding bitrate, etc. The internal storage is bits per +//! second (bps). + +use std::fmt; +use std::ops::*; + +use super::{DataSize, Frequency, TimeDelta}; + +super::relative_unit!(DataRate); + +impl DataRate { + const ONE_SIDED: bool = true; + + pub const fn from_bits_per_sec(value: i64) -> Self { + Self::from_value(value) + } + + pub fn from_bits_per_sec_float(value: f64) -> Self { + Self::from_value_float(value) + } + + pub const fn from_bytes_per_sec(value: i64) -> Self { + Self::from_fraction(8, value) + } + + pub fn from_bytes_per_sec_float(value: f64) -> Self { + Self::from_fraction_float(8.0, value) + } + + pub const fn from_kilobits_per_sec(value: i64) -> Self { + Self::from_fraction(1000, value) + } + + pub fn from_kilobits_per_sec_float(value: f64) -> Self { + Self::from_fraction_float(1000.0, value) + } + + pub const fn infinity() -> Self { + Self::plus_infinity() + } + + pub const fn bps(&self) -> i64 { + self.to_value() + } + + pub const fn bps_float(&self) -> f64 { + self.to_value_float() + } + + pub const fn bytes_per_sec(&self) -> i64 { + self.to_fraction(8) + } + + pub fn bytes_per_sec_float(&self) -> f64 { + self.to_fraction_float(8.0) + } + + pub const fn kbps(&self) -> i64 { + self.to_fraction(1000) + } + + pub fn kbps_float(&self) -> f64 { + self.to_fraction_float(1000.0) + } + + pub const fn bps_or(&self, fallback_value: i64) -> i64 { + self.to_fraction_or(1, fallback_value) + } + + pub const fn kbps_or(&self, fallback_value: i64) -> i64 { + self.to_fraction_or(1000, fallback_value) + } + + pub const fn millibyte_per_sec(&self) -> i64 { + const MAX_BEFORE_CONVERSION: i64 = i64::MAX / (1000 / 8); + assert!( + self.bps() < MAX_BEFORE_CONVERSION, + "rate is too large to be expressed in microbytes per second" + ); + self.bps() * (1000 / 8) + } +} + +impl Div for DataSize { + type Output = DataRate; + + fn div(self, duration: TimeDelta) -> Self::Output { + DataRate::from_bits_per_sec(self.microbits() / duration.us()) + } +} + +impl Div for DataSize { + type Output = TimeDelta; + + fn div(self, rate: DataRate) -> Self::Output { + TimeDelta::from_micros(self.microbits() / rate.bps()) + } +} + +impl Mul for DataRate { + type Output = DataSize; + + fn mul(self, duration: TimeDelta) -> Self::Output { + let microbits: i64 = self.bps() * duration.us(); + DataSize::from_bytes((microbits + 4000000) / 8000000) + } +} + +impl Mul for TimeDelta { + type Output = DataSize; + + fn mul(self, rate: DataRate) -> Self::Output { + rate * self + } +} + +impl Div for DataRate { + type Output = DataSize; + + fn div(self, frequency: Frequency) -> Self::Output { + let millihertz: i64 = frequency.millihertz(); + // Note that the value is truncated here reather than rounded, potentially + // introducing an error of .5 bytes if rounding were expected. + DataSize::from_bytes(self.millibyte_per_sec() / millihertz) + } +} + +impl Div for DataRate { + type Output = Frequency; + + fn div(self, size: DataSize) -> Self::Output { + Frequency::from_milli_hertz(self.millibyte_per_sec() / size.bytes()) + } +} + +impl Mul for DataSize { + type Output = DataRate; + + fn mul(self, frequency: Frequency) -> Self::Output { + assert!(frequency.is_zero() || self.bytes() <= i64::MAX / 8 / frequency.millihertz()); + let millibits_per_second: i64 = self.bytes() * 8 * frequency.millihertz(); + DataRate::from_bits_per_sec((millibits_per_second + 500) / 1000) + } +} + +impl Mul for Frequency { + type Output = DataRate; + + fn mul(self, size: DataSize) -> Self::Output { + size * self + } +} + +impl fmt::Debug for DataRate { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.is_plus_infinity() { + write!(f, "+inf bps") + } else if self.is_minus_infinity() { + write!(f, "-inf bps") + } else if self.bps() == 0 || self.bps() % 1000 != 0 { + write!(f, "{} bps", self.bps()) + } else { + write!(f, "{} kbps", self.kbps()) + } + } +} + +#[cfg(test)] +mod test { + + use super::*; + + #[test] + fn compiles_with_checks_and_logs() { + let a: DataRate = DataRate::from_kilobits_per_sec(300); + let b: DataRate = DataRate::from_kilobits_per_sec(210); + assert!(a > b); + } + + #[test] + fn const_expr() { + const VALUE: i64 = 12345; + const DATA_RATE_ZERO: DataRate = DataRate::zero(); + const DATA_RATE_INF: DataRate = DataRate::infinity(); + assert_eq!(DataRate::default(), DATA_RATE_ZERO); + assert!(DATA_RATE_ZERO.is_zero()); + assert!(DATA_RATE_INF.is_infinite()); + assert!(DATA_RATE_INF.bps_or(-1) == -1); + assert!(DATA_RATE_INF > DATA_RATE_ZERO); + + const DATA_RATE_BPS: DataRate = DataRate::from_bits_per_sec(VALUE); + const DATA_RATE_KBPS: DataRate = DataRate::from_kilobits_per_sec(VALUE); + assert!(DATA_RATE_BPS.bps() == VALUE); + assert!(DATA_RATE_BPS.bps_or(0) == VALUE); + assert!(DATA_RATE_KBPS.kbps_or(0) == VALUE); + } + + #[test] + fn get_back_same_values() { + const VALUE: i64 = 123 * 8; + assert_eq!(DataRate::from_bits_per_sec(VALUE).bps(), VALUE); + assert_eq!(DataRate::from_kilobits_per_sec(VALUE).kbps(), VALUE); + } + + #[test] + fn get_different_prefix() { + const VALUE: i64 = 123 * 8000; + assert_eq!(DataRate::from_bits_per_sec(VALUE).kbps(), VALUE / 1000); + } + + #[test] + fn identity_checks() { + const VALUE: i64 = 3000; + assert!(DataRate::zero().is_zero()); + assert!(!DataRate::from_bits_per_sec(VALUE).is_zero()); + + assert!(DataRate::infinity().is_infinite()); + assert!(!DataRate::zero().is_infinite()); + assert!(!DataRate::from_bits_per_sec(VALUE).is_infinite()); + + assert!(!DataRate::infinity().is_finite()); + assert!(DataRate::from_bits_per_sec(VALUE).is_finite()); + assert!(DataRate::zero().is_finite()); + } + + #[test] + fn comparison_operators() { + const SMALL: i64 = 450; + const LARGE: i64 = 451; + let small: DataRate = DataRate::from_bits_per_sec(SMALL); + let large: DataRate = DataRate::from_bits_per_sec(LARGE); + + assert_eq!(DataRate::zero(), DataRate::from_bits_per_sec(0)); + assert_eq!(DataRate::infinity(), DataRate::infinity()); + assert_eq!(small, small); + assert!(small <= small); + assert!(small >= small); + assert!(small != large); + assert!(small <= large); + assert!(small < large); + assert!(large >= small); + assert!(large > small); + assert!(DataRate::zero() < small); + assert!(DataRate::infinity() > large); + } + + #[test] + fn converts_to_and_from_double() { + const VALUE: i64 = 128; + const DOUBLE_VALUE: f64 = VALUE as f64; + const DOUBLE_KBPS: f64 = VALUE as f64 * 1e-3; + const FLOAT_KBPS: f32 = DOUBLE_KBPS as f32; + + assert_eq!(DataRate::from_bits_per_sec(VALUE).bps_float(), DOUBLE_VALUE); + assert_eq!(DataRate::from_bits_per_sec(VALUE).kbps_float(), DOUBLE_KBPS); + assert_eq!( + DataRate::from_bits_per_sec(VALUE).kbps_float() as f32, + FLOAT_KBPS + ); + assert_eq!(DataRate::from_bits_per_sec_float(DOUBLE_VALUE).bps(), VALUE); + assert_eq!( + DataRate::from_kilobits_per_sec_float(DOUBLE_KBPS).bps(), + VALUE + ); + + const INFINITY: f64 = f64::INFINITY; + assert_eq!(DataRate::infinity().bps_float(), INFINITY); + assert!(DataRate::from_bits_per_sec_float(INFINITY).is_infinite()); + assert!(DataRate::from_kilobits_per_sec_float(INFINITY).is_infinite()); + } + #[test] + fn clamping() { + const UPPER: DataRate = DataRate::from_kilobits_per_sec(800); + const LOWER: DataRate = DataRate::from_kilobits_per_sec(100); + const UNDER: DataRate = DataRate::from_kilobits_per_sec(100); + const INSIDE: DataRate = DataRate::from_kilobits_per_sec(500); + const OVER: DataRate = DataRate::from_kilobits_per_sec(1000); + assert_eq!(UNDER.clamp(LOWER, UPPER), LOWER); + assert_eq!(INSIDE.clamp(LOWER, UPPER), INSIDE); + assert_eq!(OVER.clamp(LOWER, UPPER), UPPER); + + let mut mutable_rate: DataRate = LOWER; + mutable_rate.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_rate, LOWER); + mutable_rate = INSIDE; + mutable_rate.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_rate, INSIDE); + mutable_rate = OVER; + mutable_rate.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_rate, UPPER); + } + + #[test] + fn math_operations() { + const VALUE_A: i64 = 450; + const VALUE_B: i64 = 267; + const RATE_A: DataRate = DataRate::from_bits_per_sec(VALUE_A); + const RATE_B: DataRate = DataRate::from_bits_per_sec(VALUE_B); + const INT32_VALUE: i32 = 123; + const FLOAT_VALUE: f64 = 123.0; + + assert_eq!((RATE_A + RATE_B).bps(), VALUE_A + VALUE_B); + assert_eq!((RATE_A - RATE_B).bps(), VALUE_A - VALUE_B); + + assert_eq!((RATE_A * VALUE_B).bps(), VALUE_A * VALUE_B); + assert_eq!((RATE_A * INT32_VALUE).bps(), VALUE_A * INT32_VALUE as i64); + assert_eq!((RATE_A * FLOAT_VALUE).bps(), VALUE_A * FLOAT_VALUE as i64); + + assert_eq!(RATE_A / RATE_B, VALUE_A as f64 / VALUE_B as f64); + + assert_eq!((RATE_A / 10).bps(), VALUE_A / 10); + assert_relative_eq!( + (RATE_A / 0.5).bps_float(), + VALUE_A as f64 * 2.0, + epsilon = 1.0 + ); + + let mut mutable_rate: DataRate = DataRate::from_bits_per_sec(VALUE_A); + mutable_rate += RATE_B; + assert_eq!(mutable_rate.bps(), VALUE_A + VALUE_B); + mutable_rate -= RATE_A; + assert_eq!(mutable_rate.bps(), VALUE_B); + } + + #[test] + fn data_rate_and_data_size_and_time_delta() { + const SECONDS: i64 = 5; + const BITS_PER_SECOND: i64 = 440; + const BYTES: i64 = 44000; + const DELTA_A: TimeDelta = TimeDelta::from_seconds(SECONDS); + const RATE_B: DataRate = DataRate::from_bits_per_sec(BITS_PER_SECOND); + const SIZE_C: DataSize = DataSize::from_bytes(BYTES); + assert_eq!((DELTA_A * RATE_B).bytes(), SECONDS * BITS_PER_SECOND / 8); + assert_eq!((RATE_B * DELTA_A).bytes(), SECONDS * BITS_PER_SECOND / 8); + assert_eq!((SIZE_C / DELTA_A).bps(), BYTES * 8 / SECONDS); + assert_eq!((SIZE_C / RATE_B).seconds(), BYTES * 8 / BITS_PER_SECOND); + } + + #[test] + fn data_rate_and_data_size_and_frequency() { + const HERTZ: i64 = 30; + const BITS_PER_SECOND: i64 = 96000; + const BYTES: i64 = 1200; + const FREQ_A: Frequency = Frequency::from_hertz(HERTZ); + const RATE_B: DataRate = DataRate::from_bits_per_sec(BITS_PER_SECOND); + const SIZE_C: DataSize = DataSize::from_bytes(BYTES); + assert_eq!((FREQ_A * SIZE_C).bps(), HERTZ * BYTES * 8); + assert_eq!((SIZE_C * FREQ_A).bps(), HERTZ * BYTES * 8); + assert_eq!((RATE_B / SIZE_C).hertz(), BITS_PER_SECOND / BYTES / 8); + assert_eq!((RATE_B / FREQ_A).bytes(), BITS_PER_SECOND / HERTZ / 8); + } + + const JUST_SMALL_ENOUGH_FOR_DIVISION: i64 = i64::MAX / 8000000; + const TOOLARGE_FOR_DIVISION: DataSize = + DataSize::from_bytes(JUST_SMALL_ENOUGH_FOR_DIVISION + 1); + + #[test] + fn division_fails_on_large_size() { + // Note that the failure is expected since the current implementation is + // implementated in a way that does not support division of large sizes. If + // the implementation is changed, this test can safely be removed. + const LARGE_SIZE: DataSize = DataSize::from_bytes(JUST_SMALL_ENOUGH_FOR_DIVISION); + const DATA_RATE: DataRate = DataRate::from_kilobits_per_sec(100); + const TIME_DELTA: TimeDelta = TimeDelta::from_millis(100); + assert!((LARGE_SIZE / DATA_RATE).is_finite()); + assert!((LARGE_SIZE / TIME_DELTA).is_finite()); + } + + #[test] + #[should_panic] + fn division_fails_on_large_size1() { + const DATA_RATE: DataRate = DataRate::from_kilobits_per_sec(100); + let _ = TOOLARGE_FOR_DIVISION / DATA_RATE; + } + #[test] + #[should_panic] + fn division_fails_on_large_size2() { + const TIME_DELTA: TimeDelta = TimeDelta::from_millis(100); + let _ = TOOLARGE_FOR_DIVISION / TIME_DELTA; + } +} diff --git a/rs/moq-congestion/src/gcc/api/units/data_size.rs b/rs/moq-congestion/src/gcc/api/units/data_size.rs new file mode 100644 index 00000000000..e2514aea440 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/data_size.rs @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use std::fmt; + +super::relative_unit!(DataSize); + +impl DataSize { + const ONE_SIDED: bool = true; + + pub const fn from_bytes(value: i64) -> Self { + Self::from_value(value) + } + + pub fn from_bytes_float(value: f64) -> Self { + Self::from_value_float(value) + } + + pub const fn infinity() -> Self { + Self::plus_infinity() + } + + pub const fn bytes(&self) -> i64 { + self.to_value() + } + + pub const fn bytes_float(&self) -> f64 { + self.to_value_float() + } + + pub const fn bytes_or(&self, fallback_value: i64) -> i64 { + self.to_value_or(fallback_value) + } + + pub const fn microbits(&self) -> i64 { + const MAX_BEFORE_CONVERSION: i64 = i64::MAX / 8000000; + assert!( + self.bytes() <= MAX_BEFORE_CONVERSION, + "size is too large to be expressed in microbits" + ); + self.bytes() * 8000000 + } +} + +impl fmt::Debug for DataSize { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.is_plus_infinity() { + write!(f, "+inf bytes") + } else if self.is_minus_infinity() { + write!(f, "-inf bytes") + } else { + write!(f, "{} bytes", self.bytes()) + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn const_expr() { + const VALUE: i64 = 12345; + const DATA_SIZE_ZERO: DataSize = DataSize::zero(); + const DATA_SIZE_INF: DataSize = DataSize::infinity(); + assert!(DataSize::default() == DATA_SIZE_ZERO); + assert!(DATA_SIZE_ZERO.is_zero()); + assert!(DATA_SIZE_INF.is_infinite()); + assert!(DATA_SIZE_INF.bytes_or(-1) == -1); + assert!(DATA_SIZE_INF > DATA_SIZE_ZERO); + + const DATA_SIZE: DataSize = DataSize::from_bytes(VALUE); + assert!(DATA_SIZE.bytes_or(-1) == VALUE); + + assert_eq!(DATA_SIZE.bytes(), VALUE); + } + + #[test] + fn get_back_same_values() { + const VALUE: i64 = 123 * 8; + assert_eq!(DataSize::from_bytes(VALUE).bytes(), VALUE); + } + + #[test] + fn identity_checks() { + const VALUE: i64 = 3000; + assert!(DataSize::zero().is_zero()); + assert!(!DataSize::from_bytes(VALUE).is_zero()); + + assert!(DataSize::infinity().is_infinite()); + assert!(!DataSize::zero().is_infinite()); + assert!(!DataSize::from_bytes(VALUE).is_infinite()); + + assert!(!DataSize::infinity().is_finite()); + assert!(DataSize::from_bytes(VALUE).is_finite()); + assert!(DataSize::zero().is_finite()); + } + + #[test] + fn comparison_operators() { + const SMALL: i64 = 450; + const LARGE: i64 = 451; + let small: DataSize = DataSize::from_bytes(SMALL); + let large: DataSize = DataSize::from_bytes(LARGE); + + assert_eq!(DataSize::zero(), DataSize::from_bytes(0)); + assert_eq!(DataSize::infinity(), DataSize::infinity()); + assert_eq!(small, small); + assert!(small <= small); + assert!(small >= small); + assert!(small != large); + assert!(small <= large); + assert!(small < large); + assert!(large >= small); + assert!(large > small); + assert!(DataSize::zero() < small); + assert!(DataSize::infinity() > large); + } + + #[test] + fn converts_to_and_from_double() { + const VALUE: i64 = 128; + const DOUBLE_VALUE: f64 = VALUE as f64; + + assert_eq!(DataSize::from_bytes(VALUE).bytes_float(), DOUBLE_VALUE); + assert_eq!(DataSize::from_bytes_float(DOUBLE_VALUE).bytes(), VALUE); + + const INFINITY: f64 = f64::INFINITY; + assert_eq!(DataSize::infinity().bytes_float(), INFINITY); + assert!(DataSize::from_bytes_float(INFINITY).is_infinite()); + } + + #[test] + fn math_operations() { + const VALUE_A: i64 = 450; + const VALUE_B: i64 = 267; + const SIZE_A: DataSize = DataSize::from_bytes(VALUE_A); + const SIZE_B: DataSize = DataSize::from_bytes(VALUE_B); + assert_eq!((SIZE_A + SIZE_B).bytes(), VALUE_A + VALUE_B); + assert_eq!((SIZE_A - SIZE_B).bytes(), VALUE_A - VALUE_B); + + const INT32_VALUE: i32 = 123; + const FLOAT_VALUE: f64 = 123.0; + assert_eq!((SIZE_A * VALUE_B).bytes(), VALUE_A * VALUE_B); + assert_eq!((SIZE_A * INT32_VALUE).bytes(), VALUE_A * INT32_VALUE as i64); + assert_eq!( + (SIZE_A * FLOAT_VALUE).bytes_float(), + VALUE_A as f64 * FLOAT_VALUE + ); + + assert_eq!((SIZE_A / 10).bytes(), VALUE_A / 10); + assert_eq!(SIZE_A / SIZE_B, VALUE_A as f64 / VALUE_B as f64); + + let mut mutable_size: DataSize = DataSize::from_bytes(VALUE_A); + mutable_size += SIZE_B; + assert_eq!(mutable_size.bytes(), VALUE_A + VALUE_B); + mutable_size -= SIZE_A; + assert_eq!(mutable_size.bytes(), VALUE_B); + } +} diff --git a/rs/moq-congestion/src/gcc/api/units/frequency.rs b/rs/moq-congestion/src/gcc/api/units/frequency.rs new file mode 100644 index 00000000000..84e251982e0 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/frequency.rs @@ -0,0 +1,295 @@ +/* + * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use std::{fmt, ops::*}; + +use super::TimeDelta; + +super::relative_unit!(Frequency); + +impl Frequency { + const ONE_SIDED: bool = true; + + pub const fn from_milli_hertz(value: i64) -> Self { + Self::from_value(value) + } + + pub fn from_milli_hertz_float(value: f64) -> Self { + Self::from_value_float(value) + } + + pub const fn from_hertz(value: i64) -> Self { + Self::from_fraction(1_000, value) + } + + pub fn from_hertz_float(value: f64) -> Self { + Self::from_fraction_float(1_000.0, value) + } + + pub const fn from_kilo_hertz(value: i64) -> Self { + Self::from_fraction(1_000_000, value) + } + + pub fn from_kilo_hertz_float(value: f64) -> Self { + Self::from_fraction_float(1_000_000.0, value) + } + + pub const fn hertz(&self) -> i64 { + self.to_fraction(1000) + } + + pub fn hertz_float(&self) -> f64 { + self.to_fraction_float(1000.0) + } + + pub const fn millihertz(&self) -> i64 { + self.to_value() + } + + pub const fn millihertz_float(&self) -> f64 { + self.to_value_float() + } +} + +impl Div for i64 { + type Output = Frequency; + + fn div(self, interval: TimeDelta) -> Frequency { + const KILO_PER_MICRO: i64 = 1000 * 1000000; + assert!(self <= i64::MAX / KILO_PER_MICRO); + assert!(interval.is_finite()); + assert!(!interval.is_zero()); + Frequency::from_milli_hertz(self * KILO_PER_MICRO / interval.us()) + } +} + +impl Div for i64 { + type Output = TimeDelta; + + fn div(self, frequency: Frequency) -> TimeDelta { + const MEGA_PER_MILLI: i64 = 1000000 * 1000; + assert!(self <= i64::MAX / MEGA_PER_MILLI); + assert!(frequency.is_finite()); + assert!(!frequency.is_zero()); + TimeDelta::from_micros(self * MEGA_PER_MILLI / frequency.millihertz()) + } +} + +impl Mul for Frequency { + type Output = f64; + + fn mul(self, time_delta: TimeDelta) -> f64 { + self.hertz_float() * time_delta.seconds_float() + } +} + +impl Mul for TimeDelta { + type Output = f64; + + fn mul(self, frequency: Frequency) -> f64 { + frequency * self + } +} + +impl fmt::Debug for Frequency { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.is_plus_infinity() { + write!(f, "+inf Hz") + } else if self.is_minus_infinity() { + write!(f, "-inf Hz") + } else if self.millihertz() % 1000 != 0 { + write!(f, "{:.3} Hz", self.hertz_float()) + } else { + write!(f, "{} Hz", self.hertz()) + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn const_expr() { + const FREQUENCY_ZERO: Frequency = Frequency::zero(); + const FREQUENCY_PLUS_INF: Frequency = Frequency::plus_infinity(); + const FREQUENCY_MINUS_INF: Frequency = Frequency::minus_infinity(); + assert!(Frequency::default() == FREQUENCY_ZERO); + assert!(FREQUENCY_ZERO.is_zero()); + assert!(FREQUENCY_PLUS_INF.is_plus_infinity()); + assert!(FREQUENCY_MINUS_INF.is_minus_infinity()); + + assert!(FREQUENCY_PLUS_INF > FREQUENCY_ZERO); + } + + #[test] + fn get_back_same_values() { + const VALUE: i64 = 31; + assert_eq!(Frequency::from_hertz(VALUE).hertz(), VALUE); + assert_eq!(Frequency::zero().hertz(), 0); + } + + #[test] + fn get_different_prefix() { + const VALUE: i64 = 30000; + assert_eq!(Frequency::from_milli_hertz(VALUE).hertz(), VALUE / 1000); + assert_eq!(Frequency::from_hertz(VALUE).millihertz(), VALUE * 1000); + assert_eq!(Frequency::from_kilo_hertz(VALUE).hertz(), VALUE * 1000); + } + + #[test] + fn identity_checks() { + const VALUE: i64 = 31; + assert!(Frequency::zero().is_zero()); + assert!(!Frequency::from_hertz(VALUE).is_zero()); + + assert!(Frequency::plus_infinity().is_infinite()); + assert!(Frequency::minus_infinity().is_infinite()); + assert!(!Frequency::zero().is_infinite()); + assert!(!Frequency::from_hertz(VALUE).is_infinite()); + + assert!(!Frequency::plus_infinity().is_finite()); + assert!(!Frequency::minus_infinity().is_finite()); + assert!(Frequency::from_hertz(VALUE).is_finite()); + assert!(Frequency::zero().is_finite()); + + assert!(Frequency::plus_infinity().is_plus_infinity()); + assert!(!Frequency::minus_infinity().is_plus_infinity()); + + assert!(Frequency::minus_infinity().is_minus_infinity()); + assert!(!Frequency::plus_infinity().is_minus_infinity()); + } + + #[test] + fn comparison_operators() { + const SMALL: i64 = 42; + const LARGE: i64 = 45; + let small: Frequency = Frequency::from_hertz(SMALL); + let large: Frequency = Frequency::from_hertz(LARGE); + + assert_eq!(Frequency::zero(), Frequency::from_hertz(0)); + assert_eq!(Frequency::plus_infinity(), Frequency::plus_infinity()); + assert_eq!(small, Frequency::from_hertz(SMALL)); + assert!(small <= Frequency::from_hertz(SMALL)); + assert!(small >= Frequency::from_hertz(SMALL)); + assert!(small != Frequency::from_hertz(LARGE)); + assert!(small <= Frequency::from_hertz(LARGE)); + assert!(small < Frequency::from_hertz(LARGE)); + assert!(large >= Frequency::from_hertz(SMALL)); + assert!(large > Frequency::from_hertz(SMALL)); + assert!(Frequency::zero() < small); + + assert!(Frequency::plus_infinity() > large); + assert!(Frequency::minus_infinity() < Frequency::zero()); + } + + #[test] + fn clamping() { + const UPPER: Frequency = Frequency::from_hertz(800); + const LOWER: Frequency = Frequency::from_hertz(100); + const UNDER: Frequency = Frequency::from_hertz(100); + const INSIDE: Frequency = Frequency::from_hertz(500); + const OVER: Frequency = Frequency::from_hertz(1000); + assert_eq!(UNDER.clamp(LOWER, UPPER), LOWER); + assert_eq!(INSIDE.clamp(LOWER, UPPER), INSIDE); + assert_eq!(OVER.clamp(LOWER, UPPER), UPPER); + + let mut mutable_frequency: Frequency = LOWER; + mutable_frequency.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_frequency, LOWER); + mutable_frequency = INSIDE; + mutable_frequency.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_frequency, INSIDE); + mutable_frequency = OVER; + mutable_frequency.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_frequency, UPPER); + } + + #[test] + fn math_operations() { + const VALUE_A: i64 = 457; + const VALUE_B: i64 = 260; + const FREQUENCY_A: Frequency = Frequency::from_hertz(VALUE_A); + const FREQUENCY_B: Frequency = Frequency::from_hertz(VALUE_B); + assert_eq!((FREQUENCY_A + FREQUENCY_B).hertz(), VALUE_A + VALUE_B); + assert_eq!((FREQUENCY_A - FREQUENCY_B).hertz(), VALUE_A - VALUE_B); + + assert_eq!( + (Frequency::from_hertz(VALUE_A) * VALUE_B).hertz(), + VALUE_A * VALUE_B + ); + + assert_eq!((FREQUENCY_B / 10).hertz(), VALUE_B / 10); + assert_eq!(FREQUENCY_B / FREQUENCY_A, VALUE_B as f64 / VALUE_A as f64); + + let mut mutable_frequency: Frequency = Frequency::from_hertz(VALUE_A); + mutable_frequency += Frequency::from_hertz(VALUE_B); + assert_eq!(mutable_frequency, Frequency::from_hertz(VALUE_A + VALUE_B)); + mutable_frequency -= Frequency::from_hertz(VALUE_B); + assert_eq!(mutable_frequency, Frequency::from_hertz(VALUE_A)); + } + #[test] + fn rounding() { + let freq_high: Frequency = Frequency::from_hertz_float(23.976); + assert_eq!(freq_high.hertz(), 24); + assert_eq!( + freq_high.round_down_to(Frequency::from_hertz(1)), + Frequency::from_hertz(23) + ); + assert_eq!( + freq_high.round_to(Frequency::from_hertz(1)), + Frequency::from_hertz(24) + ); + assert_eq!( + freq_high.round_up_to(Frequency::from_hertz(1)), + Frequency::from_hertz(24) + ); + + let freq_low: Frequency = Frequency::from_hertz_float(23.4); + assert_eq!(freq_low.hertz(), 23); + assert_eq!( + freq_low.round_down_to(Frequency::from_hertz(1)), + Frequency::from_hertz(23) + ); + assert_eq!( + freq_low.round_to(Frequency::from_hertz(1)), + Frequency::from_hertz(23) + ); + assert_eq!( + freq_low.round_up_to(Frequency::from_hertz(1)), + Frequency::from_hertz(24) + ); + } + + #[test] + fn infinity_operations() { + const VALUE: f64 = 267.0; + let finite: Frequency = Frequency::from_hertz_float(VALUE); + assert!((Frequency::plus_infinity() + finite).is_plus_infinity()); + assert!((Frequency::plus_infinity() - finite).is_plus_infinity()); + assert!((finite + Frequency::plus_infinity()).is_plus_infinity()); + assert!((finite - Frequency::minus_infinity()).is_plus_infinity()); + + assert!((Frequency::minus_infinity() + finite).is_minus_infinity()); + assert!((Frequency::minus_infinity() - finite).is_minus_infinity()); + assert!((finite + Frequency::minus_infinity()).is_minus_infinity()); + assert!((finite - Frequency::plus_infinity()).is_minus_infinity()); + } + + #[test] + fn time_delta_and_frequency() { + assert_eq!(1 / Frequency::from_hertz(50), TimeDelta::from_millis(20)); + assert_eq!(1 / TimeDelta::from_millis(20), Frequency::from_hertz(50)); + assert_eq!( + Frequency::from_kilo_hertz(200) * TimeDelta::from_millis(2), + 400.0 + ); + } +} diff --git a/rs/moq-congestion/src/gcc/api/units/mod.rs b/rs/moq-congestion/src/gcc/api/units/mod.rs new file mode 100644 index 00000000000..4a04ce5e0b6 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/mod.rs @@ -0,0 +1,14 @@ +mod data_rate; +mod data_size; +mod frequency; +mod time_delta; +mod timestamp; +mod unit_base; + +pub use data_rate::*; +pub use data_size::*; +pub use frequency::*; +pub use time_delta::*; +pub use timestamp::*; + +pub(crate) use unit_base::*; diff --git a/rs/moq-congestion/src/gcc/api/units/time_delta.rs b/rs/moq-congestion/src/gcc/api/units/time_delta.rs new file mode 100644 index 00000000000..3f9451abaad --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/time_delta.rs @@ -0,0 +1,393 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! TimeDelta represents the difference between two timestamps. Commonly this can +//! be a duration. However since two Timestamps are not guaranteed to have the +//! same epoch (they might come from different computers, making exact +//! synchronisation infeasible), the duration covered by a TimeDelta can be +//! undefined. To simplify usage, it can be constructed and converted to +//! different units, specifically seconds (s), milliseconds (ms) and +//! microseconds (us). +super::relative_unit!(TimeDelta); + +use std::{fmt, time::Duration}; + +impl TimeDelta { + const ONE_SIDED: bool = false; + + pub const fn from_minutes(value: i64) -> Self { + Self::from_fraction(60_000_000, value) + } + + pub fn from_minutes_float(value: f64) -> Self { + Self::from_fraction_float(60_000_000.0, value) + } + + pub const fn from_seconds(value: i64) -> Self { + Self::from_fraction(1_000_000, value) + } + + pub fn from_seconds_float(value: f64) -> Self { + Self::from_fraction_float(1_000_000.0, value) + } + + pub const fn from_millis(value: i64) -> Self { + Self::from_fraction(1_000, value) + } + + pub fn from_millis_float(value: f64) -> Self { + Self::from_fraction_float(1_000.0, value) + } + + pub const fn from_micros(value: i64) -> Self { + Self::from_value(value) + } + + pub fn from_micros_float(value: f64) -> Self { + Self::from_value_float(value) + } + + pub const fn seconds(&self) -> i64 { + self.to_fraction(1_000_000) + } + + pub fn seconds_float(&self) -> f64 { + self.to_fraction_float(1_000_000.0) + } + + pub const fn ms(&self) -> i64 { + self.to_fraction(1_000) + } + + pub fn ms_float(&self) -> f64 { + self.to_fraction_float(1_000.0) + } + + pub const fn us(&self) -> i64 { + self.to_value() + } + + pub const fn us_float(&self) -> f64 { + self.to_value_float() + } + + pub const fn ns(&self) -> i64 { + self.to_multiple(1000) + } + + pub fn ns_float(&self) -> f64 { + self.to_multiple_float(1000.0) + } + + pub const fn seconds_or(&self, fallback_value: i64) -> i64 { + self.to_fraction_or(1_000_000, fallback_value) + } + + pub const fn ms_or(&self, fallback_value: i64) -> i64 { + self.to_fraction_or(1_000, fallback_value) + } + + pub const fn us_or(&self, fallback_value: i64) -> i64 { + self.to_value_or(fallback_value) + } + + pub const fn abs(&self) -> Self { + if self.us() < 0 { + Self::from_micros(-self.us()) + } else { + *self + } + } +} + +impl fmt::Debug for TimeDelta { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.is_plus_infinity() { + write!(f, "+inf ms") + } else if self.is_minus_infinity() { + write!(f, "-inf ms") + } else if self.us() == 0 || (self.us() % 1000) != 0 { + write!(f, "{} us", self.us()) + } else if self.ms() % 1000 != 0 { + write!(f, "{} ms", self.ms()) + } else { + write!(f, "{} s", self.seconds()) + } + } +} + +// Custom +impl From for TimeDelta { + fn from(duration: Duration) -> Self { + Self::from_micros(duration.as_micros() as i64) + } +} + +impl TryFrom for Duration { + type Error = std::num::TryFromIntError; + + fn try_from(delta: TimeDelta) -> Result { + Ok(Duration::from_micros(delta.us().try_into()?)) + } +} + +#[cfg(test)] +mod test { + + use super::*; + + #[test] + fn const_expr() { + const VALUE: i64 = -12345; + const TIME_DELTA_ZERO: TimeDelta = TimeDelta::zero(); + const TIME_DELTA_PLUS_INF: TimeDelta = TimeDelta::plus_infinity(); + const TIME_DELTA_MINUS_INF: TimeDelta = TimeDelta::minus_infinity(); + assert!(TimeDelta::default() == TIME_DELTA_ZERO); + assert!(TIME_DELTA_ZERO.is_zero()); + assert!(TIME_DELTA_PLUS_INF.is_plus_infinity()); + assert!(TIME_DELTA_MINUS_INF.is_minus_infinity()); + assert!(TIME_DELTA_PLUS_INF.ms_or(-1) == -1); + + assert!(TIME_DELTA_PLUS_INF > TIME_DELTA_ZERO); + + const TIME_DELTA_MINUTES: TimeDelta = TimeDelta::from_minutes(VALUE); + const TIME_DELTA_SECONDS: TimeDelta = TimeDelta::from_seconds(VALUE); + const TIME_DELTA_MS: TimeDelta = TimeDelta::from_millis(VALUE); + const TIME_DELTA_US: TimeDelta = TimeDelta::from_micros(VALUE); + + assert!(TIME_DELTA_MINUTES.seconds_or(0) == VALUE * 60); + assert!(TIME_DELTA_SECONDS.seconds_or(0) == VALUE); + assert!(TIME_DELTA_MS.ms_or(0) == VALUE); + assert!(TIME_DELTA_US.us_or(0) == VALUE); + } + + #[test] + fn get_back_same_values() { + const VALUE: i64 = 499; + for sign in [-1, 0, 1] { + let value: i64 = VALUE * sign; + assert_eq!(TimeDelta::from_millis(value).ms(), value); + assert_eq!(TimeDelta::from_micros(value).us(), value); + assert_eq!(TimeDelta::from_seconds(value).seconds(), value); + assert_eq!(TimeDelta::from_seconds(value).seconds(), value); + } + assert_eq!(TimeDelta::zero().us(), 0); + } + + #[test] + fn get_different_prefix() { + const VALUE: i64 = 3000000; + assert_eq!(TimeDelta::from_micros(VALUE).seconds(), VALUE / 1000000); + assert_eq!(TimeDelta::from_millis(VALUE).seconds(), VALUE / 1000); + assert_eq!(TimeDelta::from_micros(VALUE).ms(), VALUE / 1000); + assert_eq!(TimeDelta::from_minutes(VALUE / 60).seconds(), VALUE); + + assert_eq!(TimeDelta::from_millis(VALUE).us(), VALUE * 1000); + assert_eq!(TimeDelta::from_seconds(VALUE).ms(), VALUE * 1000); + assert_eq!(TimeDelta::from_seconds(VALUE).us(), VALUE * 1000000); + assert_eq!(TimeDelta::from_minutes(VALUE / 60).seconds(), VALUE); + } + + #[test] + fn identity_checks() { + const VALUE: i64 = 3000; + assert!(TimeDelta::zero().is_zero()); + assert!(!TimeDelta::from_millis(VALUE).is_zero()); + + assert!(TimeDelta::plus_infinity().is_infinite()); + assert!(TimeDelta::minus_infinity().is_infinite()); + assert!(!TimeDelta::zero().is_infinite()); + assert!(!TimeDelta::from_millis(-VALUE).is_infinite()); + assert!(!TimeDelta::from_millis(VALUE).is_infinite()); + + assert!(!TimeDelta::plus_infinity().is_finite()); + assert!(!TimeDelta::minus_infinity().is_finite()); + assert!(TimeDelta::from_millis(-VALUE).is_finite()); + assert!(TimeDelta::from_millis(VALUE).is_finite()); + assert!(TimeDelta::zero().is_finite()); + + assert!(TimeDelta::plus_infinity().is_plus_infinity()); + assert!(!TimeDelta::minus_infinity().is_plus_infinity()); + + assert!(TimeDelta::minus_infinity().is_minus_infinity()); + assert!(!TimeDelta::plus_infinity().is_minus_infinity()); + } + + #[test] + fn comparison_operators() { + const SMALL: i64 = 450; + const LARGE: i64 = 451; + let small: TimeDelta = TimeDelta::from_millis(SMALL); + let large: TimeDelta = TimeDelta::from_millis(LARGE); + + assert_eq!(TimeDelta::zero(), TimeDelta::from_millis(0)); + assert_eq!(TimeDelta::plus_infinity(), TimeDelta::plus_infinity()); + assert_eq!(small, TimeDelta::from_millis(SMALL)); + assert!(small <= TimeDelta::from_millis(SMALL)); + assert!(small >= TimeDelta::from_millis(SMALL)); + assert!(small != TimeDelta::from_millis(LARGE)); + assert!(small <= TimeDelta::from_millis(LARGE)); + assert!(small < TimeDelta::from_millis(LARGE)); + assert!(large >= TimeDelta::from_millis(SMALL)); + assert!(large > TimeDelta::from_millis(SMALL)); + assert!(TimeDelta::zero() < small); + assert!(TimeDelta::zero() > TimeDelta::from_millis(-SMALL)); + assert!(TimeDelta::zero() > TimeDelta::from_millis(-SMALL)); + + assert!(TimeDelta::plus_infinity() > large); + assert!(TimeDelta::minus_infinity() < TimeDelta::zero()); + } + + #[test] + fn clamping() { + const UPPER: TimeDelta = TimeDelta::from_millis(800); + const LOWER: TimeDelta = TimeDelta::from_millis(100); + const UNDER: TimeDelta = TimeDelta::from_millis(100); + const INSIDE: TimeDelta = TimeDelta::from_millis(500); + const OVER: TimeDelta = TimeDelta::from_millis(1000); + assert_eq!(UNDER.clamp(LOWER, UPPER), LOWER); + assert_eq!(INSIDE.clamp(LOWER, UPPER), INSIDE); + assert_eq!(OVER.clamp(LOWER, UPPER), UPPER); + + let mut mutable_delta: TimeDelta = LOWER; + mutable_delta.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_delta, LOWER); + mutable_delta = INSIDE; + mutable_delta.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_delta, INSIDE); + mutable_delta = OVER; + mutable_delta.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_delta, UPPER); + } + + #[test] + fn can_be_inititialized_from_large_int() { + const MAX_INT: i32 = i32::MAX; + assert_eq!( + TimeDelta::from_seconds(MAX_INT as i64).us(), + MAX_INT as i64 * 1000000 + ); + assert_eq!( + TimeDelta::from_millis(MAX_INT as i64).us(), + MAX_INT as i64 * 1000 + ); + } + + #[test] + fn converts_to_and_from_double() { + const MICROS: i64 = 17017; + const NANOS_DOUBLE: f64 = MICROS as f64 * 1e3; + const MICROS_DOUBLE: f64 = MICROS as f64; + const MILLIS_DOUBLE: f64 = MICROS as f64 * 1e-3; + const SECONDS_DOUBLE: f64 = MILLIS_DOUBLE * 1e-3; + + assert_eq!( + TimeDelta::from_micros(MICROS).seconds_float(), + SECONDS_DOUBLE + ); + assert_eq!(TimeDelta::from_seconds_float(SECONDS_DOUBLE).us(), MICROS); + + assert_eq!(TimeDelta::from_micros(MICROS).ms_float(), MILLIS_DOUBLE); + assert_eq!(TimeDelta::from_millis_float(MILLIS_DOUBLE).us(), MICROS); + + assert_eq!(TimeDelta::from_micros(MICROS).us_float(), MICROS_DOUBLE); + assert_eq!(TimeDelta::from_micros_float(MICROS_DOUBLE).us(), MICROS); + + assert_relative_eq!( + TimeDelta::from_micros(MICROS).ns_float(), + NANOS_DOUBLE, + epsilon = 1.0 + ); + + const PLUS_INFINITY: f64 = f64::INFINITY; + const MINUS_INFINITY: f64 = -PLUS_INFINITY; + + assert_eq!(TimeDelta::plus_infinity().seconds_float(), PLUS_INFINITY); + assert_eq!(TimeDelta::minus_infinity().seconds_float(), MINUS_INFINITY); + assert_eq!(TimeDelta::plus_infinity().ms_float(), PLUS_INFINITY); + assert_eq!(TimeDelta::minus_infinity().ms_float(), MINUS_INFINITY); + assert_eq!(TimeDelta::plus_infinity().us_float(), PLUS_INFINITY); + assert_eq!(TimeDelta::minus_infinity().us_float(), MINUS_INFINITY); + assert_eq!(TimeDelta::plus_infinity().ns_float(), PLUS_INFINITY); + assert_eq!(TimeDelta::minus_infinity().ns_float(), MINUS_INFINITY); + + assert!(TimeDelta::from_seconds_float(PLUS_INFINITY).is_plus_infinity()); + assert!(TimeDelta::from_seconds_float(MINUS_INFINITY).is_minus_infinity()); + assert!(TimeDelta::from_millis_float(PLUS_INFINITY).is_plus_infinity()); + assert!(TimeDelta::from_millis_float(MINUS_INFINITY).is_minus_infinity()); + assert!(TimeDelta::from_micros_float(PLUS_INFINITY).is_plus_infinity()); + assert!(TimeDelta::from_micros_float(MINUS_INFINITY).is_minus_infinity()); + } + + #[test] + fn math_operations() { + const VALUE_A: i64 = 267; + const VALUE_B: i64 = 450; + const DELTA_A: TimeDelta = TimeDelta::from_millis(VALUE_A); + const DELTA_B: TimeDelta = TimeDelta::from_millis(VALUE_B); + assert_eq!((DELTA_A + DELTA_B).ms(), VALUE_A + VALUE_B); + assert_eq!((DELTA_A - DELTA_B).ms(), VALUE_A - VALUE_B); + + assert_eq!((DELTA_B / 10).ms(), VALUE_B / 10); + assert_eq!(DELTA_B / DELTA_A, VALUE_B as f64 / VALUE_A as f64); + + assert_eq!(TimeDelta::from_micros(-VALUE_A).abs().us(), VALUE_A); + assert_eq!(TimeDelta::from_micros(VALUE_A).abs().us(), VALUE_A); + + let mut mutable_delta: TimeDelta = TimeDelta::from_millis(VALUE_A); + mutable_delta += TimeDelta::from_millis(VALUE_B); + assert_eq!(mutable_delta, TimeDelta::from_millis(VALUE_A + VALUE_B)); + mutable_delta -= TimeDelta::from_millis(VALUE_B); + assert_eq!(mutable_delta, TimeDelta::from_millis(VALUE_A)); + } + + #[test] + fn multiply_by_scalar() { + const VALUE: TimeDelta = TimeDelta::from_micros(267); + const INT64: i64 = 450; + const INT32: i32 = 123; + const UNSIGNED_INT: usize = 125; + const FLOAT: f64 = 123.0; + + assert_eq!((VALUE * INT64).us(), VALUE.us() * INT64); + assert_eq!(VALUE * INT64, INT64 * VALUE); + + assert_eq!((VALUE * INT32).us(), VALUE.us() * INT32 as i64); + assert_eq!(VALUE * INT32, INT32 * VALUE); + + assert_eq!( + (VALUE * UNSIGNED_INT).us(), + VALUE.us() * UNSIGNED_INT as i64 + ); + assert_eq!(VALUE * UNSIGNED_INT, UNSIGNED_INT * VALUE); + + assert_relative_eq!( + (VALUE * FLOAT).us_float(), + VALUE.us_float() * FLOAT, + epsilon = 0.1 + ); + assert_eq!(VALUE * FLOAT, FLOAT * VALUE); + } + + #[test] + fn infinity_operations() { + const VALUE: i64 = 267; + const FINITE: TimeDelta = TimeDelta::from_millis(VALUE); + assert!((TimeDelta::plus_infinity() + FINITE).is_plus_infinity()); + assert!((TimeDelta::plus_infinity() - FINITE).is_plus_infinity()); + assert!((FINITE + TimeDelta::plus_infinity()).is_plus_infinity()); + assert!((FINITE - TimeDelta::minus_infinity()).is_plus_infinity()); + + assert!((TimeDelta::minus_infinity() + FINITE).is_minus_infinity()); + assert!((TimeDelta::minus_infinity() - FINITE).is_minus_infinity()); + assert!((FINITE + TimeDelta::minus_infinity()).is_minus_infinity()); + assert!((FINITE - TimeDelta::plus_infinity()).is_minus_infinity()); + } +} diff --git a/rs/moq-congestion/src/gcc/api/units/timestamp.rs b/rs/moq-congestion/src/gcc/api/units/timestamp.rs new file mode 100644 index 00000000000..5f7dd5f43dd --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/timestamp.rs @@ -0,0 +1,338 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! Timestamp represents the time that has passed since some unspecified epoch. +//! The epoch is assumed to be before any represented timestamps, this means that +//! negative values are not valid. The most notable feature is that the +//! difference of two Timestamps results in a TimeDelta. +super::unit_base!(Timestamp); + +use std::fmt; +use std::ops::*; + +use super::TimeDelta; + +#[allow( + dead_code, + reason = "WebRTC's unit API, kept whole so a refresh diffs cleanly" +)] +impl Timestamp { + const ONE_SIDED: bool = false; + + pub const fn from_seconds(value: i64) -> Self { + Self::from_fraction(1_000_000, value) + } + + pub fn from_seconds_float(value: f64) -> Self { + Self::from_fraction_float(1_000_000.0, value) + } + + pub const fn from_millis(value: i64) -> Self { + Self::from_fraction(1_000, value) + } + + pub fn from_millis_float(value: f64) -> Self { + Self::from_fraction_float(1_000.0, value) + } + + pub const fn from_micros(value: i64) -> Self { + Self::from_value(value) + } + + pub fn from_micros_float(value: f64) -> Self { + Self::from_value_float(value) + } + + pub const fn seconds(&self) -> i64 { + self.to_fraction(1_000_000) + } + + pub fn seconds_float(&self) -> f64 { + self.to_fraction_float(1_000_000.0) + } + + pub const fn ms(&self) -> i64 { + self.to_fraction(1_000) + } + + pub fn ms_float(&self) -> f64 { + self.to_fraction_float(1_000.0) + } + + pub const fn us(&self) -> i64 { + self.to_value() + } + + pub const fn us_float(&self) -> f64 { + self.to_value_float() + } + + pub const fn seconds_or(&self, fallback_value: i64) -> i64 { + self.to_fraction_or(1_000_000, fallback_value) + } + + pub const fn ms_or(&self, fallback_value: i64) -> i64 { + self.to_fraction_or(1_000, fallback_value) + } + + pub const fn us_or(&self, fallback_value: i64) -> i64 { + self.to_value_or(fallback_value) + } +} + +impl Add for Timestamp { + type Output = Self; + + fn add(self, delta: TimeDelta) -> Self { + if self.is_plus_infinity() || delta.is_plus_infinity() { + assert!(!self.is_minus_infinity()); + assert!(!delta.is_minus_infinity()); + return Self::plus_infinity(); + } else if self.is_minus_infinity() || delta.is_minus_infinity() { + assert!(!self.is_plus_infinity()); + assert!(!delta.is_plus_infinity()); + return Self::minus_infinity(); + } + Timestamp::from_micros(self.us() + delta.us()) + } +} + +impl Sub for Timestamp { + type Output = Self; + + fn sub(self, delta: TimeDelta) -> Self { + if self.is_plus_infinity() || delta.is_minus_infinity() { + assert!(!self.is_minus_infinity()); + assert!(!delta.is_plus_infinity()); + return Self::plus_infinity(); + } else if self.is_minus_infinity() || delta.is_plus_infinity() { + assert!(!self.is_plus_infinity()); + assert!(!delta.is_minus_infinity()); + return Self::minus_infinity(); + } + Timestamp::from_micros(self.us() - delta.us()) + } +} + +impl Sub for Timestamp { + type Output = TimeDelta; + + fn sub(self, other: Self) -> TimeDelta { + if self.is_plus_infinity() || other.is_minus_infinity() { + assert!(!self.is_minus_infinity()); + assert!(!other.is_plus_infinity()); + return TimeDelta::plus_infinity(); + } else if self.is_minus_infinity() || other.is_plus_infinity() { + assert!(!self.is_plus_infinity()); + assert!(!other.is_minus_infinity()); + return TimeDelta::minus_infinity(); + } + TimeDelta::from_micros(self.us() - other.us()) + } +} + +impl AddAssign for Timestamp { + fn add_assign(&mut self, delta: TimeDelta) { + *self = *self + delta; + } +} + +impl SubAssign for Timestamp { + fn sub_assign(&mut self, delta: TimeDelta) { + *self = *self - delta; + } +} + +impl fmt::Debug for Timestamp { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.is_plus_infinity() { + write!(f, "+inf ms") + } else if self.is_minus_infinity() { + write!(f, "-inf ms") + } else if self.us() == 0 || (self.us() % 1000) != 0 { + write!(f, "{} us", self.us()) + } else if self.ms() % 1000 != 0 { + write!(f, "{} ms", self.ms()) + } else { + write!(f, "{} s", self.seconds()) + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn const_expr() { + const VALUE: i64 = 12345; + const TIMESTAMP_INF: Timestamp = Timestamp::plus_infinity(); + assert!(TIMESTAMP_INF.is_infinite()); + assert!(TIMESTAMP_INF.ms_or(-1) == -1); + + const TIMESTAMP_SECONDS: Timestamp = Timestamp::from_seconds(VALUE); + const TIMESTAMP_MS: Timestamp = Timestamp::from_millis(VALUE); + const TIMESTAMP_US: Timestamp = Timestamp::from_micros(VALUE); + + assert!(TIMESTAMP_SECONDS.seconds_or(0) == VALUE); + assert!(TIMESTAMP_MS.ms_or(0) == VALUE); + assert!(TIMESTAMP_US.us_or(0) == VALUE); + + assert!(TIMESTAMP_MS > TIMESTAMP_US); + + assert_eq!(TIMESTAMP_SECONDS.seconds(), VALUE); + assert_eq!(TIMESTAMP_MS.ms(), VALUE); + assert_eq!(TIMESTAMP_US.us(), VALUE); + } + + #[test] + fn get_back_same_values() { + const VALUE: i64 = 499; + assert_eq!(Timestamp::from_millis(VALUE).ms(), VALUE); + assert_eq!(Timestamp::from_micros(VALUE).us(), VALUE); + assert_eq!(Timestamp::from_seconds(VALUE).seconds(), VALUE); + } + + #[test] + fn get_different_prefix() { + const VALUE: i64 = 3000000; + assert_eq!(Timestamp::from_micros(VALUE).seconds(), VALUE / 1000000); + assert_eq!(Timestamp::from_millis(VALUE).seconds(), VALUE / 1000); + assert_eq!(Timestamp::from_micros(VALUE).ms(), VALUE / 1000); + + assert_eq!(Timestamp::from_millis(VALUE).us(), VALUE * 1000); + assert_eq!(Timestamp::from_seconds(VALUE).ms(), VALUE * 1000); + assert_eq!(Timestamp::from_seconds(VALUE).us(), VALUE * 1000000); + } + + #[test] + fn identity_checks() { + const VALUE: i64 = 3000; + + assert!(Timestamp::plus_infinity().is_infinite()); + assert!(Timestamp::minus_infinity().is_infinite()); + assert!(!Timestamp::from_millis(VALUE).is_infinite()); + + assert!(!Timestamp::plus_infinity().is_finite()); + assert!(!Timestamp::minus_infinity().is_finite()); + assert!(Timestamp::from_millis(VALUE).is_finite()); + + assert!(Timestamp::plus_infinity().is_plus_infinity()); + assert!(!Timestamp::minus_infinity().is_plus_infinity()); + + assert!(Timestamp::minus_infinity().is_minus_infinity()); + assert!(!Timestamp::plus_infinity().is_minus_infinity()); + } + + #[test] + fn comparison_operators() { + const SMALL: i64 = 450; + const LARGE: i64 = 451; + + assert_eq!(Timestamp::plus_infinity(), Timestamp::plus_infinity()); + assert!(Timestamp::plus_infinity() >= Timestamp::plus_infinity()); + assert!(Timestamp::plus_infinity() > Timestamp::from_millis(LARGE)); + assert_eq!(Timestamp::from_millis(SMALL), Timestamp::from_millis(SMALL)); + assert!(Timestamp::from_millis(SMALL) <= Timestamp::from_millis(SMALL)); + assert!(Timestamp::from_millis(SMALL) >= Timestamp::from_millis(SMALL)); + assert!(Timestamp::from_millis(SMALL) != Timestamp::from_millis(LARGE)); + assert!(Timestamp::from_millis(SMALL) <= Timestamp::from_millis(LARGE)); + assert!(Timestamp::from_millis(SMALL) < Timestamp::from_millis(LARGE)); + assert!(Timestamp::from_millis(LARGE) >= Timestamp::from_millis(SMALL)); + assert!(Timestamp::from_millis(LARGE) > Timestamp::from_millis(SMALL)); + } + + #[test] + fn can_be_inititialized_from_large_int() { + const MAX_INT: i32 = i32::MAX; + assert_eq!( + Timestamp::from_seconds(MAX_INT as _).us(), + MAX_INT as i64 * 1000000 + ); + assert_eq!( + Timestamp::from_millis(MAX_INT as _).us(), + MAX_INT as i64 * 1000 + ); + } + + #[test] + fn converts_to_and_from_double() { + const MICROS: i64 = 17017; + const MICROS_DOUBLE: f64 = MICROS as f64; + const MILLIS_DOUBLE: f64 = MICROS as f64 * 1e-3; + const SECONDS_DOUBLE: f64 = MILLIS_DOUBLE * 1e-3; + + assert_eq!( + Timestamp::from_micros(MICROS).seconds_float(), + SECONDS_DOUBLE + ); + assert_eq!(Timestamp::from_seconds_float(SECONDS_DOUBLE).us(), MICROS); + + assert_eq!(Timestamp::from_micros(MICROS).ms_float(), MILLIS_DOUBLE); + assert_eq!(Timestamp::from_millis_float(MILLIS_DOUBLE).us(), MICROS); + + assert_eq!(Timestamp::from_micros(MICROS).us_float(), MICROS_DOUBLE); + assert_eq!(Timestamp::from_micros_float(MICROS_DOUBLE).us(), MICROS); + + const PLUS_INFINITY: f64 = f64::INFINITY; + const MINUS_INFINITY: f64 = -PLUS_INFINITY; + + assert_eq!(Timestamp::plus_infinity().seconds_float(), PLUS_INFINITY); + assert_eq!(Timestamp::minus_infinity().seconds_float(), MINUS_INFINITY); + assert_eq!(Timestamp::plus_infinity().ms_float(), PLUS_INFINITY); + assert_eq!(Timestamp::minus_infinity().ms_float(), MINUS_INFINITY); + assert_eq!(Timestamp::plus_infinity().us_float(), PLUS_INFINITY); + assert_eq!(Timestamp::minus_infinity().us_float(), MINUS_INFINITY); + + assert!(Timestamp::from_seconds_float(PLUS_INFINITY).is_plus_infinity()); + assert!(Timestamp::from_seconds_float(MINUS_INFINITY).is_minus_infinity()); + assert!(Timestamp::from_millis_float(PLUS_INFINITY).is_plus_infinity()); + assert!(Timestamp::from_millis_float(MINUS_INFINITY).is_minus_infinity()); + assert!(Timestamp::from_micros_float(PLUS_INFINITY).is_plus_infinity()); + assert!(Timestamp::from_micros_float(MINUS_INFINITY).is_minus_infinity()); + } + + #[test] + fn timestamp_and_time_delta_math() { + const VALUE_A: i64 = 267; + const VALUE_B: i64 = 450; + const TIME_A: Timestamp = Timestamp::from_millis(VALUE_A); + const TIME_B: Timestamp = Timestamp::from_millis(VALUE_B); + const DELTA_A: TimeDelta = TimeDelta::from_millis(VALUE_A); + const DELTA_B: TimeDelta = TimeDelta::from_millis(VALUE_B); + + assert_eq!((TIME_A - TIME_B), TimeDelta::from_millis(VALUE_A - VALUE_B)); + assert_eq!( + (TIME_B - DELTA_A), + Timestamp::from_millis(VALUE_B - VALUE_A) + ); + assert_eq!( + (TIME_B + DELTA_A), + Timestamp::from_millis(VALUE_B + VALUE_A) + ); + + let mut mutable_time: Timestamp = TIME_A; + mutable_time += DELTA_B; + assert_eq!(mutable_time, TIME_A + DELTA_B); + mutable_time -= DELTA_B; + assert_eq!(mutable_time, TIME_A); + } + + #[test] + fn infinity_operations() { + const VALUE: i64 = 267; + const FINITE_TIME: Timestamp = Timestamp::from_millis(VALUE); + const FINITE_DELTA: TimeDelta = TimeDelta::from_millis(VALUE); + assert!((Timestamp::plus_infinity() + FINITE_DELTA).is_infinite()); + assert!((Timestamp::plus_infinity() - FINITE_DELTA).is_infinite()); + assert!((FINITE_TIME + TimeDelta::plus_infinity()).is_infinite()); + assert!((FINITE_TIME - TimeDelta::minus_infinity()).is_infinite()); + } +} // namespace test diff --git a/rs/moq-congestion/src/gcc/api/units/unit_base.rs b/rs/moq-congestion/src/gcc/api/units/unit_base.rs new file mode 100644 index 00000000000..76cb88c44c7 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/unit_base.rs @@ -0,0 +1,800 @@ +// UnitBase is a superclass in C++. +// The closest we can do in Rust is a macro, as traits don't support const. +macro_rules! unit_base { + ($ty:ident) => { + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)] + pub struct $ty(i64); + + #[allow( + dead_code, + reason = "WebRTC's unit API, kept whole so a refresh diffs cleanly" + )] + impl $ty { + pub const fn zero() -> Self { + Self(0) + } + + pub const fn plus_infinity() -> Self { + Self(i64::MAX) + } + + pub const fn minus_infinity() -> Self { + Self(i64::MIN) + } + + pub const fn is_zero(&self) -> bool { + self.0 == 0 + } + + pub const fn is_finite(&self) -> bool { + !self.is_infinite() + } + + pub const fn is_infinite(&self) -> bool { + self.0 == i64::MAX || self.0 == i64::MIN + } + + pub const fn is_plus_infinity(&self) -> bool { + self.0 == i64::MAX + } + + pub const fn is_minus_infinity(&self) -> bool { + self.0 == i64::MIN + } + + pub const fn round_to(&self, resolution: Self) -> Self { + assert!(self.is_finite()); + assert!(resolution.is_finite()); + assert!(resolution.0 > 0); + Self::from_value(((self.0 + resolution.0 / 2) / resolution.0) * resolution.0) + } + + pub const fn round_up_to(&self, resolution: Self) -> Self { + assert!(self.is_finite()); + assert!(resolution.is_finite()); + assert!(resolution.0 > 0); + Self::from_value(((self.0 + resolution.0 - 1) / resolution.0) * resolution.0) + } + + pub const fn round_down_to(&self, resolution: Self) -> Self { + assert!(self.is_finite()); + assert!(resolution.is_finite()); + assert!(resolution.0 > 0); + Self::from_value((self.0 / resolution.0) * resolution.0) + } + + #[allow(dead_code, reason = "each unit takes the conversions it needs")] + const fn from_fraction(denominator: i64, value: i64) -> Self { + assert!(denominator >= 0); + Self::from_value(value * denominator) + } + + #[allow(dead_code, reason = "each unit takes the conversions it needs")] + fn from_fraction_float(denominator: f64, value: f64) -> Self { + Self::from_value_float(value * denominator) + } + + #[allow(dead_code, reason = "each unit takes the conversions it needs")] + const fn to_fraction(self, denominator: i64) -> i64 { + self.divide_round_to_nearest(denominator) + } + + const fn divide_round_to_nearest(&self, d: i64) -> i64 { + assert!(d >= 0); + + let v = self.to_value(); + let mut result = v / d; + let remainder = v % d; + + if remainder.abs() * 2 >= d.abs() { + if (v < 0) != (d < 0) { + result -= 1 + } else { + result += 1 + } + } + result + } + + #[allow(dead_code, reason = "each unit takes the conversions it needs")] + fn to_fraction_float(self, denominator: f64) -> f64 { + assert!(denominator >= 0.0); + self.to_value_float() / denominator + } + + #[allow(dead_code, reason = "each unit takes the conversions it needs")] + const fn to_fraction_or(self, denominator: i64, fallback_value: i64) -> i64 { + assert!(denominator >= 0); + if self.is_finite() { + self.divide_round_to_nearest(denominator) + } else { + fallback_value + } + } + + pub const fn to_multiple(self, factor: i64) -> i64 { + assert!(factor >= 0); + self.to_value() * factor + } + + pub fn to_multiple_float(self, factor: f64) -> f64 { + assert!(factor >= 0.0); + self.to_value_float() * factor + } + + const fn from_value(value: i64) -> Self { + assert!(value != i64::MAX && value != i64::MIN); + if Self::ONE_SIDED { + assert!(value >= 0); + } + + Self(value) + } + + fn from_value_float(value: f64) -> Self { + assert!(!value.is_nan()); + + if value == f64::INFINITY { + return Self::plus_infinity(); + } + + if Self::ONE_SIDED { + assert!(value >= 0.0); + } + + if value == f64::NEG_INFINITY { + Self::minus_infinity() + } else { + Self(value as i64) + } + } + + const fn to_value(self) -> i64 { + assert!(self.is_finite()); + self.0 + } + + #[allow(dead_code, reason = "each unit takes the conversions it needs")] + const fn to_value_or(self, fallback_value: i64) -> i64 { + if self.is_finite() { + self.0 + } else { + fallback_value + } + } + + const fn to_value_float(self) -> f64 { + if self.is_plus_infinity() { + f64::INFINITY + } else if self.is_minus_infinity() { + f64::NEG_INFINITY + } else { + self.0 as f64 + } + } + } + }; +} + +macro_rules! relative_unit { + ($ty:ident) => { + crate::gcc::api::units::unit_base!($ty); + + impl $ty { + pub fn clamp_mut(&mut self, min_value: Self, max_value: Self) { + *self = Self(self.0.max(min_value.0).min(max_value.0)); + } + } + + impl ::std::ops::Add for $ty { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + if self.is_plus_infinity() || rhs.is_plus_infinity() { + assert!(!self.is_minus_infinity()); + assert!(!rhs.is_minus_infinity()); + return Self::plus_infinity(); + } else if self.is_minus_infinity() || rhs.is_minus_infinity() { + assert!(!self.is_plus_infinity()); + assert!(!rhs.is_plus_infinity()); + return Self::minus_infinity(); + } + Self::from_value(self.to_value() + rhs.to_value()) + } + } + + impl ::std::ops::Sub for $ty { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + if self.is_plus_infinity() || rhs.is_minus_infinity() { + assert!(!self.is_minus_infinity()); + assert!(!rhs.is_plus_infinity()); + return Self::plus_infinity(); + } else if self.is_minus_infinity() || rhs.is_plus_infinity() { + assert!(!self.is_plus_infinity()); + assert!(!rhs.is_minus_infinity()); + return Self::minus_infinity(); + } + Self::from_value(self.to_value() - rhs.to_value()) + } + } + + impl ::std::ops::AddAssign for $ty { + fn add_assign(&mut self, rhs: Self) { + *self = Self::from_value(self.to_value() + rhs.to_value()); + } + } + + impl ::std::ops::SubAssign for $ty { + fn sub_assign(&mut self, rhs: Self) { + *self = Self::from_value(self.to_value() - rhs.to_value()); + } + } + + impl ::std::ops::Div for $ty { + type Output = f64; + + fn div(self, rhs: Self) -> Self::Output { + self.to_value_float() / rhs.to_value_float() + } + } + + impl ::std::ops::Div for $ty { + type Output = Self; + + fn div(self, rhs: f64) -> Self::Output { + Self::from_value_float((self.to_value_float() / rhs).round()) + } + } + + impl ::std::ops::Div for $ty { + type Output = Self; + + fn div(self, rhs: i64) -> Self::Output { + Self::from_value(self.to_value() / rhs) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: isize) -> Self::Output { + Self::from_value(self.to_value() * rhs as i64) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: i64) -> Self::Output { + Self::from_value(self.to_value() * rhs) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: i32) -> Self::Output { + Self::from_value(self.to_value() * rhs as i64) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: i16) -> Self::Output { + Self::from_value(self.to_value() * rhs as i64) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: usize) -> Self::Output { + Self::from_value(self.to_value() * i64::try_from(rhs).unwrap()) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: u64) -> Self::Output { + Self::from_value(self.to_value() * i64::try_from(rhs).unwrap()) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: u32) -> Self::Output { + Self::from_value(self.to_value() * i64::try_from(rhs).unwrap()) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: u16) -> Self::Output { + Self::from_value(self.to_value() * i64::try_from(rhs).unwrap()) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: f64) -> Self::Output { + Self::from_value_float((self.to_value_float() * rhs).round()) + } + } + + impl ::std::ops::Mul for $ty { + type Output = Self; + + fn mul(self, rhs: f32) -> Self::Output { + Self::from_value_float((self.to_value_float() * rhs as f64).round()) + } + } + + impl ::std::ops::Neg for $ty { + type Output = Self; + + fn neg(self) -> Self::Output { + if self.is_plus_infinity() { + Self::minus_infinity() + } else if self.is_minus_infinity() { + Self::plus_infinity() + } else { + Self::from_value(-self.to_value()) + } + } + } + + impl ::std::ops::Mul<$ty> for isize { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for i64 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for i32 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for i16 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for usize { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for u64 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for u32 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for u16 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for f64 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::Mul<$ty> for f32 { + type Output = $ty; + + fn mul(self, rhs: $ty) -> Self::Output { + rhs * self + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: isize) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: i64) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: i32) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: i16) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: usize) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: u64) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: u32) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: u16) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: f64) { + *self = *self * rhs; + } + } + + impl ::std::ops::MulAssign for $ty { + fn mul_assign(&mut self, rhs: f32) { + *self = *self * rhs; + } + } + }; +} + +pub(crate) use relative_unit; +pub(crate) use unit_base; + +#[cfg(test)] +mod test { + use std::fmt; + + relative_unit!(TestUnit); + + impl TestUnit { + const ONE_SIDED: bool = false; + + pub const fn from_kilo(kilo: i64) -> Self { + Self::from_fraction(1000, kilo) + } + + pub fn from_kilo_float(kilo: f64) -> Self { + Self::from_fraction_float(1000.0, kilo) + } + + pub const fn to_kilo(self) -> i64 { + self.to_fraction(1000) + } + + pub fn to_kilo_float(self) -> f64 { + self.to_fraction_float(1000.0) + } + + pub const fn to_kilo_or(self, fallback: i64) -> i64 { + self.to_fraction_or(1000, fallback) + } + + pub const fn to_milli(self) -> i64 { + self.to_multiple(1000) + } + + pub fn to_milli_float(self) -> f64 { + self.to_multiple_float(1000.0) + } + } + + impl fmt::Debug for TestUnit { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.is_plus_infinity() { + write!(f, "+inf") + } else if self.is_minus_infinity() { + write!(f, "-inf") + } else { + write!(f, "{}", self.0) + } + } + } + + fn test_unit_add_kilo(mut value: TestUnit, add_kilo: i64) -> TestUnit { + value += TestUnit::from_kilo(add_kilo); + value + } + + #[test] + fn const_expr() { + const VALUE: i64 = -12345; + const TEST_UNIT_ZERO: TestUnit = TestUnit::zero(); + const TEST_UNIT_PLUS_INF: TestUnit = TestUnit::plus_infinity(); + const TEST_UNIT_MINUS_INF: TestUnit = TestUnit::minus_infinity(); + + assert!(TEST_UNIT_ZERO.is_zero()); + assert!(TEST_UNIT_PLUS_INF.is_plus_infinity()); + assert!(TEST_UNIT_MINUS_INF.is_minus_infinity()); + assert!(TEST_UNIT_PLUS_INF.to_kilo_or(-1) == -1); + + // Check from is constexpr for floats. + assert!(TestUnit::from_value_float(0.0).is_zero()); + assert!(TestUnit::from_value_float(f64::INFINITY).is_plus_infinity()); + assert!(TestUnit::from_value_float(-f64::INFINITY).is_minus_infinity()); + assert!(TestUnit::from_value_float(250.0) == TestUnit::from_value(250)); + assert!(TestUnit::from_value_float(-250.0) == TestUnit::from_value(-250)); + + assert!(TEST_UNIT_PLUS_INF > TEST_UNIT_ZERO); + + const TEST_UNIT_KILO: TestUnit = TestUnit::from_kilo(VALUE); + const TEST_UNIT_VALUE: TestUnit = TestUnit::from_value(VALUE); + + assert!(TEST_UNIT_KILO.to_kilo_or(0) == VALUE); + assert!(TEST_UNIT_VALUE.to_value_or(0) == VALUE); + assert!(test_unit_add_kilo(TEST_UNIT_VALUE, 2).to_value() == VALUE + 2000); + assert!(TestUnit::from_value(500) / 2 == TestUnit::from_value(250)); + assert!(TestUnit::from_value_float(500.0) / 2 == TestUnit::from_value_float(250.0)); + } + + #[test] + fn get_back_same_values() { + const VALUE: i64 = 499; + for sign in [-1, 0, 1] { + let value: i64 = VALUE * sign; + assert_eq!(TestUnit::from_kilo(value).to_kilo(), value); + assert_eq!(TestUnit::from_value(value).to_value(), value); + } + assert_eq!(TestUnit::zero().to_value(), 0); + } + + #[test] + fn get_different_prefix() { + const VALUE: i64 = 3000000; + assert_eq!(TestUnit::from_value(VALUE).to_kilo(), VALUE / 1000); + assert_eq!(TestUnit::from_kilo(VALUE).to_value(), VALUE * 1000); + } + + #[test] + fn identity_checks() { + const VALUE: i64 = 3000; + assert!(TestUnit::zero().is_zero()); + assert!(!TestUnit::from_kilo(VALUE).is_zero()); + + assert!(TestUnit::plus_infinity().is_infinite()); + assert!(TestUnit::minus_infinity().is_infinite()); + assert!(!TestUnit::zero().is_infinite()); + assert!(!TestUnit::from_kilo(-VALUE).is_infinite()); + assert!(!TestUnit::from_kilo(VALUE).is_infinite()); + + assert!(!TestUnit::plus_infinity().is_finite()); + assert!(!TestUnit::minus_infinity().is_finite()); + assert!(TestUnit::from_kilo(-VALUE).is_finite()); + assert!(TestUnit::from_kilo(VALUE).is_finite()); + assert!(TestUnit::zero().is_finite()); + + assert!(TestUnit::plus_infinity().is_plus_infinity()); + assert!(!TestUnit::minus_infinity().is_plus_infinity()); + + assert!(TestUnit::minus_infinity().is_minus_infinity()); + assert!(!TestUnit::plus_infinity().is_minus_infinity()); + } + + #[test] + fn comparison_operators() { + const SMALL: i64 = 450; + const LARGE: i64 = 451; + let small: TestUnit = TestUnit::from_kilo(SMALL); + let large: TestUnit = TestUnit::from_kilo(LARGE); + + assert_eq!(TestUnit::zero(), TestUnit::from_kilo(0)); + assert_eq!(TestUnit::plus_infinity(), TestUnit::plus_infinity()); + assert_eq!(small, TestUnit::from_kilo(SMALL)); + assert!(small <= TestUnit::from_kilo(SMALL)); + assert!(small >= TestUnit::from_kilo(SMALL)); + assert!(small != TestUnit::from_kilo(LARGE)); + assert!(small <= TestUnit::from_kilo(LARGE)); + assert!(small < TestUnit::from_kilo(LARGE)); + assert!(large >= TestUnit::from_kilo(SMALL)); + assert!(large > TestUnit::from_kilo(SMALL)); + assert!(TestUnit::zero() < small); + assert!(TestUnit::zero() > TestUnit::from_kilo(-SMALL)); + assert!(TestUnit::zero() > TestUnit::from_kilo(-SMALL)); + + assert!(TestUnit::plus_infinity() > large); + assert!(TestUnit::minus_infinity() < TestUnit::zero()); + } + + #[test] + fn can_be_inititialized_from_large_int() { + const MAX_INT: i32 = i32::MAX; + assert_eq!( + TestUnit::from_kilo(MAX_INT as i64).to_value(), + (MAX_INT as i64) * 1000 + ); + } + + #[test] + fn converts_to_and_from_double() { + const VALUE: i64 = 17017; + const MILLI_DOUBLE: f64 = VALUE as f64 * 1e3; + const VALUE_DOUBLE: f64 = VALUE as f64; + const KILO_DOUBLE: f64 = VALUE as f64 * 1e-3; + + assert_eq!(TestUnit::from_value(VALUE).to_kilo_float(), KILO_DOUBLE); + assert_eq!(TestUnit::from_kilo_float(KILO_DOUBLE).to_value(), VALUE); + + assert_eq!(TestUnit::from_value(VALUE).to_value_float(), VALUE_DOUBLE); + assert_eq!(TestUnit::from_value_float(VALUE_DOUBLE).to_value(), VALUE); + + assert_relative_eq!( + TestUnit::from_value(VALUE).to_milli_float(), + MILLI_DOUBLE, + epsilon = 1.0 + ); + + const PLUS_INFINITY: f64 = f64::INFINITY; + const MINUS_INFINITY: f64 = -PLUS_INFINITY; + + assert_eq!(TestUnit::plus_infinity().to_kilo_float(), PLUS_INFINITY); + assert_eq!(TestUnit::minus_infinity().to_kilo_float(), MINUS_INFINITY); + assert_eq!(TestUnit::plus_infinity().to_kilo_float(), PLUS_INFINITY); + assert_eq!(TestUnit::minus_infinity().to_kilo_float(), MINUS_INFINITY); + assert_eq!(TestUnit::plus_infinity().to_milli_float(), PLUS_INFINITY); + assert_eq!(TestUnit::minus_infinity().to_milli_float(), MINUS_INFINITY); + + assert!(TestUnit::from_kilo_float(PLUS_INFINITY).is_plus_infinity()); + assert!(TestUnit::from_kilo_float(MINUS_INFINITY).is_minus_infinity()); + assert!(TestUnit::from_value_float(PLUS_INFINITY).is_plus_infinity()); + assert!(TestUnit::from_value_float(MINUS_INFINITY).is_minus_infinity()); + } + + #[test] + #[should_panic] + fn crashes_when_created_from_nan1() { + TestUnit::from_value_float(f64::NAN); + } + + #[test] + #[should_panic] + fn crashes_when_created_from_nan2() { + #[allow(clippy::zero_divided_by_zero, reason = "builds a NaN")] + TestUnit::from_value_float(0.0 / 0.0); + } + + #[test] + #[should_panic] + fn crashes_when_created_from_nan3() { + TestUnit::from_value_float(f64::INFINITY - f64::INFINITY); + } + + #[test] + fn clamping() { + const UPPER: TestUnit = TestUnit::from_value(800); + const LOWER: TestUnit = TestUnit::from_value(100); + const UNDER: TestUnit = TestUnit::from_value(100); + const INSIDE: TestUnit = TestUnit::from_value(500); + const OVER: TestUnit = TestUnit::from_value(1000); + assert_eq!(UNDER.clamp(LOWER, UPPER), LOWER); + assert_eq!(INSIDE.clamp(LOWER, UPPER), INSIDE); + assert_eq!(OVER.clamp(LOWER, UPPER), UPPER); + + let mut mutable_delta: TestUnit = LOWER; + mutable_delta.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_delta, LOWER); + mutable_delta = INSIDE; + mutable_delta.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_delta, INSIDE); + mutable_delta = OVER; + mutable_delta.clamp_mut(LOWER, UPPER); + assert_eq!(mutable_delta, UPPER); + } + + #[test] + fn math_operations() { + const VALUE_A: i64 = 267; + const VALUE_B: i64 = 450; + const DELTA_A: TestUnit = TestUnit::from_kilo(VALUE_A); + const DELTA_B: TestUnit = TestUnit::from_kilo(VALUE_B); + assert_eq!((DELTA_A + DELTA_B).to_kilo(), VALUE_A + VALUE_B); + assert_eq!((DELTA_A - DELTA_B).to_kilo(), VALUE_A - VALUE_B); + + const INT32_VALUE: i32 = 123; + const FLOAT_VALUE: f64 = 123.0; + assert_eq!( + (TestUnit::from_value(VALUE_A) * VALUE_B).to_value(), + VALUE_A * VALUE_B + ); + assert_eq!( + (TestUnit::from_value(VALUE_A) * INT32_VALUE).to_value(), + VALUE_A * INT32_VALUE as i64 + ); + assert_eq!( + (TestUnit::from_value(VALUE_A) * FLOAT_VALUE).to_value(), + VALUE_A * FLOAT_VALUE as i64 + ); + + assert_eq!((DELTA_B / 10).to_kilo(), VALUE_B / 10); + assert_eq!(DELTA_B / DELTA_A, VALUE_B as f64 / VALUE_A as f64); + + let mut mutable_delta: TestUnit = TestUnit::from_kilo(VALUE_A); + mutable_delta += TestUnit::from_kilo(VALUE_B); + assert_eq!(mutable_delta, TestUnit::from_kilo(VALUE_A + VALUE_B)); + mutable_delta -= TestUnit::from_kilo(VALUE_B); + assert_eq!(mutable_delta, TestUnit::from_kilo(VALUE_A)); + + // Division by an int rounds towards zero to follow regular int division. + assert_eq!(TestUnit::from_value(789) / 10, TestUnit::from_value(78)); + assert_eq!(TestUnit::from_value(-789) / 10, TestUnit::from_value(-78)); + } + + #[test] + fn infinity_operations() { + const VALUE: i64 = 267; + const FINITE: TestUnit = TestUnit::from_value(VALUE); + assert!((TestUnit::plus_infinity() + FINITE).is_plus_infinity()); + assert!((TestUnit::plus_infinity() - FINITE).is_plus_infinity()); + assert!((FINITE + TestUnit::plus_infinity()).is_plus_infinity()); + assert!((FINITE - TestUnit::minus_infinity()).is_plus_infinity()); + + assert!((TestUnit::minus_infinity() + FINITE).is_minus_infinity()); + assert!((TestUnit::minus_infinity() - FINITE).is_minus_infinity()); + assert!((FINITE + TestUnit::minus_infinity()).is_minus_infinity()); + assert!((FINITE - TestUnit::plus_infinity()).is_minus_infinity()); + } + + #[test] + fn unary_minus() { + const VALUE: i64 = 1337; + const UNIT: TestUnit = TestUnit::from_value(VALUE); + assert_eq!(-UNIT.to_value(), -VALUE); + + // Check infinity. + assert_eq!(-TestUnit::plus_infinity(), TestUnit::minus_infinity()); + assert_eq!(-TestUnit::minus_infinity(), TestUnit::plus_infinity()); + } +} diff --git a/rs/moq-congestion/src/gcc/bench.rs b/rs/moq-congestion/src/gcc/bench.rs new file mode 100644 index 00000000000..1fa6ca9e01d --- /dev/null +++ b/rs/moq-congestion/src/gcc/bench.rs @@ -0,0 +1,94 @@ +//! Controller-only per-packet measurements, as BBR3's: each ACK becomes GoogCC transport feedback, +//! so its cost grows with the packets each ACK carries. + +use super::*; +use crate::Acked; +use cpu_time::ThreadTime; + +const PACKET: u16 = 1200; + +/// Times the controller callbacks for `flight` packets acknowledged `batch` at a time, refilled as +/// they are acknowledged if `steady`, each with a receive time +fn measure(flight: usize, batch: usize, steady: bool) -> Duration { + let base = Instant::ZERO + Duration::from_secs(1); + let mut gcc = Gcc::new(Init { + now: base, + mtu: PACKET, + rtt: Duration::from_millis(10), + max_pacing_rate: None, + }); + let bytes = u64::from(PACKET); + let mut sent = Vec::with_capacity(2 * flight); + for pn in 0..flight { + let now = base + Duration::from_micros(pn as u64); + sent.push(( + now, + gcc.on_sent(Sent { + now, + bytes: PACKET, + in_flight: (pn as u64 + 1) * bytes, + }), + )); + } + let mut acked = Vec::with_capacity(batch); + let mut elapsed = Duration::ZERO; + for first in (0..flight).step_by(batch) { + let end = (first + batch).min(flight); + let now = base + Duration::from_millis(10) + Duration::from_micros(first as u64); + let in_flight = if steady { + flight - (end - first) + } else { + flight - end + } as u64 + * bytes; + let start = ThreadTime::now(); + acked.clear(); + acked.extend(sent[first..end].iter().map(|&(at, packet)| Acked { + sent: at, + bytes: PACKET, + packet, + received: Some(at - base + Duration::from_millis(5)), + })); + gcc.on_ack(Ack { + now, + packets: &acked, + in_flight, + app_limited: false, + rtt: Duration::from_millis(10), + }); + if steady { + for pn in first..end { + let packet = gcc.on_sent(Sent { + now, + bytes: PACKET, + in_flight: in_flight + (pn - first + 1) as u64 * bytes, + }); + sent.push((now, packet)); + } + } + elapsed += start.elapsed(); + } + std::hint::black_box(&gcc); + elapsed +} + +#[test] +#[ignore = "controller benchmark: run with --release and --success-output immediate"] +fn benchmark_per_packet() { + println!("gcc_packet controller_stack_bytes={}", size_of::()); + for steady in [false, true] { + for flight in [16, 64, 1000, 4000, 16000] { + for batch in [1, 8, 32] { + let mut samples = [Duration::ZERO; 3]; + for sample in &mut samples { + *sample = measure(flight, batch, steady); + } + samples.sort(); + println!( + "gcc_packet steady={steady} flight={flight} batch={batch} cpu_ns={}", + samples[1].as_nanos() + ); + } + } + } +} diff --git a/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs b/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs new file mode 100644 index 00000000000..a7975e67f06 --- /dev/null +++ b/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::{ + api::units::DataSize, + experiments::{FieldTrials, RateControlSettings}, +}; + +// This class enables pushback from congestion window directly to video encoder. +// When the congestion window is filling up, the video encoder target bitrate +// will be reduced accordingly to accommodate the network changes. To avoid +// pausing video too frequently, a minimum encoder target bitrate threshold is +// used to prevent video pause due to a full congestion window. +#[derive(Clone)] +pub struct CongestionWindowPushbackController { + add_pacing: bool, + min_pushback_target_bitrate_bps: u32, + current_data_window: Option, + outstanding_bytes: i64, + pacing_bytes: i64, + encoding_rate_ratio: f64, +} + +// const char CongestionWindowDefaultFieldTrialString[] = "QueueSize:350,MinBitrate:30000,DropFrame:true" +impl Default for CongestionWindowPushbackController { + fn default() -> Self { + Self { + add_pacing: false, + min_pushback_target_bitrate_bps: 30000, + current_data_window: None, + outstanding_bytes: 0, + pacing_bytes: 0, + encoding_rate_ratio: 1.0, + } + } +} + +impl CongestionWindowPushbackController { + pub fn new(field_trials: &FieldTrials) -> Self { + let rate_control_settings = RateControlSettings::new(field_trials); + Self { + add_pacing: field_trials.add_pacing_to_congestion_window_pushback, + min_pushback_target_bitrate_bps: rate_control_settings + .congestion_window_min_pushback_target_bitrate_bps(), + current_data_window: rate_control_settings.congestion_window_initial_data_window(), + outstanding_bytes: 0, + pacing_bytes: 0, + encoding_rate_ratio: 1.0, + } + } + + pub fn update_outstanding_data(&mut self, outstanding_bytes: i64) { + self.outstanding_bytes = outstanding_bytes; + } + pub fn update_pacing_queue(&mut self, pacing_bytes: i64) { + self.pacing_bytes = pacing_bytes; + } + pub fn update_target_bitrate(&mut self, mut bitrate_bps: u32) -> u32 { + let data_window = match self.current_data_window { + Some(data_window) if !data_window.is_zero() => data_window, + _ => return bitrate_bps, + }; + let mut total_bytes: i64 = self.outstanding_bytes; + if self.add_pacing { + total_bytes += self.pacing_bytes; + } + let fill_ratio: f64 = total_bytes as f64 / (data_window.bytes() as f64); + if fill_ratio > 1.5 { + self.encoding_rate_ratio *= 0.9; + } else if fill_ratio > 1.0 { + self.encoding_rate_ratio *= 0.95; + } else if fill_ratio < 0.1 { + self.encoding_rate_ratio = 1.0; + } else { + self.encoding_rate_ratio *= 1.05; + self.encoding_rate_ratio = self.encoding_rate_ratio.min(1.0); + } + let adjusted_target_bitrate_bps: u32 = + (bitrate_bps as f64 * self.encoding_rate_ratio) as u32; + + // Do not adjust below the minimum pushback bitrate but do obey if the + // original estimate is below it. + bitrate_bps = if adjusted_target_bitrate_bps < self.min_pushback_target_bitrate_bps { + std::cmp::min(bitrate_bps, self.min_pushback_target_bitrate_bps) + } else { + adjusted_target_bitrate_bps + }; + bitrate_bps + } + + pub fn set_data_window(&mut self, data_window: DataSize) { + self.current_data_window = Some(data_window); + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn full_congestion_window() { + let mut cwnd_controller = CongestionWindowPushbackController::default(); + + cwnd_controller.update_outstanding_data(100000); + cwnd_controller.set_data_window(DataSize::from_bytes(50000)); + + let mut bitrate_bps: u32 = 80000; + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert_eq!(72000, bitrate_bps); + + cwnd_controller.set_data_window(DataSize::from_bytes(50000)); + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert_eq!((72000.0 * 0.9 * 0.9) as u32, bitrate_bps); + } + + #[test] + fn normal_congestion_window() { + let mut cwnd_controller = CongestionWindowPushbackController::default(); + + cwnd_controller.update_outstanding_data(199999); + cwnd_controller.set_data_window(DataSize::from_bytes(200000)); + + let mut bitrate_bps: u32 = 80000; + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert_eq!(80000, bitrate_bps); + } + + #[test] + fn low_bitrate() { + let mut cwnd_controller = CongestionWindowPushbackController::default(); + + cwnd_controller.update_outstanding_data(100000); + cwnd_controller.set_data_window(DataSize::from_bytes(50000)); + + let mut bitrate_bps: u32 = 35000; + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert_eq!((35000.0 * 0.9) as u32, bitrate_bps); + + cwnd_controller.set_data_window(DataSize::from_bytes(20000)); + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert_eq!(30000, bitrate_bps); + } + + #[test] + fn no_pushback_on_data_window_unset() { + let mut cwnd_controller = CongestionWindowPushbackController::default(); + + cwnd_controller.update_outstanding_data(100_000_000); // Large number + + let mut bitrate_bps: u32 = 80000; + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert_eq!(80000, bitrate_bps); + } + + #[test] + fn pushback_on_inititial_data_window() { + let mut cwnd_controller = CongestionWindowPushbackController::default(); + cwnd_controller.set_data_window(DataSize::from_bytes(100000)); + cwnd_controller.update_outstanding_data(100_000_000); // Large number + + let mut bitrate_bps: u32 = 80000; + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert!(80000 > bitrate_bps); + } + + #[test] + fn pushback_drop_frame() { + let mut cwnd_controller = CongestionWindowPushbackController::default(); + // Not sure what this is meant to test. The DropFrame field is not used. + //CongestionWindowPushbackController cwnd_controller( + // ExplicitKeyValueConfig("WebRTC-CongestionWindow/DropFrame:true/")); + + cwnd_controller.update_outstanding_data(100_000_000); // Large number + cwnd_controller.set_data_window(DataSize::from_bytes(50000)); + + let mut bitrate_bps: u32 = 80000; + bitrate_bps = cwnd_controller.update_target_bitrate(bitrate_bps); + assert!(80000 > bitrate_bps); + } +} diff --git a/rs/moq-congestion/src/gcc/delay_based_bwe.rs b/rs/moq-congestion/src/gcc/delay_based_bwe.rs new file mode 100644 index 00000000000..f461af14759 --- /dev/null +++ b/rs/moq-congestion/src/gcc/delay_based_bwe.rs @@ -0,0 +1,1238 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! Upstream can run a second detector over audio packets (`WebRTC-Bwe-SeparateAudioPackets`, off +//! by default). QUIC packets carry no media type, so there is one detector. + +use crate::gcc::{ + DelayIncreaseDetectorInterface, InterArrivalDelta, TrendlineEstimator, + TrendlineEstimatorSettings, + api::{ + transport::{BandwidthUsage, PacketResult, TransportPacketsFeedback}, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + experiments::FieldTrials, + remote_bitrate_estimator::{AimdRateControl, RateControlInput}, +}; + +pub struct DelayBasedBweResult { + pub updated: bool, + pub probe: bool, + pub target_bitrate: DataRate, + pub recovered_from_overuse: bool, + pub delay_detector_state: BandwidthUsage, +} + +impl Default for DelayBasedBweResult { + fn default() -> Self { + Self { + updated: false, + probe: false, + target_bitrate: DataRate::zero(), + recovered_from_overuse: false, + delay_detector_state: BandwidthUsage::Normal, + } + } +} + +#[derive(Clone)] +pub struct DelayBasedBwe { + trendline_settings: TrendlineEstimatorSettings, + video_inter_arrival_delta: InterArrivalDelta, + video_delay_detector: TrendlineEstimator, + + last_seen_packet: Timestamp, + rate_control: AimdRateControl, + prev_bitrate: DataRate, + prev_state: BandwidthUsage, +} + +impl DelayBasedBwe { + const STREAM_TIME_OUT: TimeDelta = TimeDelta::from_seconds(2); + const SEND_TIME_GROUP_LENGTH: TimeDelta = TimeDelta::from_millis(5); + + // This ssrc is used to fulfill the current API but will be removed + // after the API has been changed. + #[cfg(test)] + const FIXED_SSRC: u32 = 0; + + pub fn new(field_trials: &FieldTrials) -> Self { + Self { + trendline_settings: field_trials.trendline_estimator_settings.clone(), + video_delay_detector: TrendlineEstimator::new( + field_trials.trendline_estimator_settings.clone(), + ), + last_seen_packet: Timestamp::minus_infinity(), + rate_control: AimdRateControl::new(field_trials, true), // send_side + prev_bitrate: DataRate::zero(), + prev_state: BandwidthUsage::Normal, + + // Note: Not initialized in C++ but this avoids needing an Option + video_inter_arrival_delta: InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH), + } + } + + fn active_delay_detector_state(&self) -> BandwidthUsage { + self.video_delay_detector.state() + } + + /// `packet_feedback_vector` is `msg.sorted_by_receive_time()`, which the caller already has. + pub fn incoming_packet_feedback_vector( + &mut self, + msg: &TransportPacketsFeedback, + packet_feedback_vector: &[PacketResult], + acked_bitrate: Option, + probe_bitrate: Option, + in_alr: bool, + ) -> DelayBasedBweResult { + // TODO(holmer): An empty feedback vector here likely means that + // all acks were too late and that the send time history had + // timed out. We should reduce the rate when this occurs. + if packet_feedback_vector.is_empty() { + tracing::debug!("Very late feedback received."); + return DelayBasedBweResult::default(); + } + + let mut delayed_feedback: bool = true; + let mut recovered_from_overuse: bool = false; + let mut prev_detector_state: BandwidthUsage = self.active_delay_detector_state(); + for packet_feedback in packet_feedback_vector { + delayed_feedback = false; + self.incoming_packet_feedback(packet_feedback, msg.feedback_time); + if prev_detector_state == BandwidthUsage::Underusing + && self.active_delay_detector_state() == BandwidthUsage::Normal + { + recovered_from_overuse = true; + } + prev_detector_state = self.active_delay_detector_state(); + } + + if delayed_feedback { + // TODO(bugs.webrtc.org/10125): Design a better mechanism to safe-guard + // against building very large network queues. + return DelayBasedBweResult::default(); + } + self.rate_control.set_in_application_limited_region(in_alr); + self.maybe_update_estimate( + acked_bitrate, + probe_bitrate, + recovered_from_overuse, + in_alr, + msg.feedback_time, + ) + } + + pub fn on_rtt_update(&mut self, avg_rtt: TimeDelta) { + self.rate_control.set_rtt(avg_rtt); + } + + #[cfg(test)] + pub fn latest_estimate(&self, ssrcs: &mut Vec, bitrate: &mut DataRate) -> bool { + // Currently accessed from both the process thread (see + // ModuleRtpRtcpImpl::Process()) and the configuration thread (see + // Call::GetStats()). Should in the future only be accessed from a single + // thread. + if !self.rate_control.valid_estimate() { + return false; + } + + *ssrcs = vec![Self::FIXED_SSRC]; + *bitrate = self.rate_control.latest_estimate(); + true + } + + pub fn set_start_bitrate(&mut self, start_bitrate: DataRate) { + tracing::debug!("BWE Setting start bitrate to: {:?}", start_bitrate); + self.rate_control.set_start_bitrate(start_bitrate); + } + pub fn set_min_bitrate(&mut self, min_bitrate: DataRate) { + // Called from both the configuration thread and the network thread. Shouldn't + // be called from the network thread in the future. + self.rate_control.set_min_bitrate(min_bitrate); + } + + /* unused? + pub fn trigger_overuse( + &mut self, + at_time: Timestamp, + link_capacity: Option, + ) -> DataRate { + let input = RateControlInput::new(BandwidthUsage::Overusing, link_capacity); + self.rate_control.update(input, at_time) + } + */ + + pub fn last_estimate(&self) -> DataRate { + self.prev_bitrate + } + + pub fn last_state(&self) -> BandwidthUsage { + self.prev_state + } + + fn incoming_packet_feedback(&mut self, packet_feedback: &PacketResult, at_time: Timestamp) { + // Reset if the stream has timed out. + if self.last_seen_packet.is_infinite() + || at_time - self.last_seen_packet > Self::STREAM_TIME_OUT + { + self.video_inter_arrival_delta = InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH); + self.video_delay_detector = TrendlineEstimator::new(self.trendline_settings.clone()); + } + self.last_seen_packet = at_time; + + let packet_size: DataSize = packet_feedback.sent_packet.size; + + let mut send_delta: TimeDelta = TimeDelta::zero(); + let mut recv_delta: TimeDelta = TimeDelta::zero(); + let mut size_delta: i64 = 0; + + let calculated_deltas: bool = self.video_inter_arrival_delta.compute_deltas( + packet_feedback.sent_packet.send_time, + packet_feedback.receive_time, + at_time, + packet_size.bytes() as usize, + &mut send_delta, + &mut recv_delta, + &mut size_delta, + ); + + self.video_delay_detector.update( + recv_delta.ms_float(), + send_delta.ms_float(), + packet_feedback.sent_packet.send_time.ms(), + packet_feedback.receive_time.ms(), + packet_size.bytes() as usize, + calculated_deltas, + ); + } + + fn maybe_update_estimate( + &mut self, + acked_bitrate: Option, + probe_bitrate: Option, + recovered_from_overuse: bool, + _in_alr: bool, + at_time: Timestamp, + ) -> DelayBasedBweResult { + let mut result = DelayBasedBweResult::default(); + + // Currently overusing the bandwidth. + if self.active_delay_detector_state() == BandwidthUsage::Overusing { + if let Some(acked_bitrate) = acked_bitrate { + if self + .rate_control + .time_to_reduce_further(at_time, acked_bitrate) + { + result.updated = self.update_estimate( + at_time, + Some(acked_bitrate), + &mut result.target_bitrate, + ); + } + } else if self.rate_control.valid_estimate() + && self.rate_control.initial_time_to_reduce_further(at_time) + { + // Overusing before we have a measured acknowledged bitrate. Reduce send + // rate by 50% every 200 ms. + // TODO(tschumim): Improve this and/or the acknowledged bitrate estimator + // so that we (almost) always have a bitrate estimate. + self.rate_control + .set_estimate(self.rate_control.latest_estimate() / 2, at_time); + result.updated = true; + result.probe = false; + result.target_bitrate = self.rate_control.latest_estimate(); + } + } else if let Some(probe_bitrate) = probe_bitrate { + result.probe = true; + result.updated = true; + self.rate_control.set_estimate(probe_bitrate, at_time); + result.target_bitrate = self.rate_control.latest_estimate(); + } else { + result.updated = + self.update_estimate(at_time, acked_bitrate, &mut result.target_bitrate); + result.recovered_from_overuse = recovered_from_overuse; + } + let detector_state: BandwidthUsage = self.active_delay_detector_state(); + if (result.updated && self.prev_bitrate != result.target_bitrate) + || detector_state != self.prev_state + { + let bitrate: DataRate = if result.updated { + result.target_bitrate + } else { + self.prev_bitrate + }; + + self.prev_bitrate = bitrate; + self.prev_state = detector_state; + } + + result.delay_detector_state = detector_state; + result + } + + // Updates the current remote rate estimate and returns true if a valid + // estimate exists. + fn update_estimate( + &mut self, + at_time: Timestamp, + acked_bitrate: Option, + target_rate: &mut DataRate, + ) -> bool { + let input = RateControlInput::new(self.active_delay_detector_state(), acked_bitrate); + *target_rate = self.rate_control.update(input, at_time); + self.rate_control.valid_estimate() + } +} + +#[cfg(test)] +mod test { + use crate::gcc::api::transport::{PacedPacketInfo, SentPacket}; + use crate::gcc::{ProbeBitrateEstimator, RobustThroughputEstimator}; + + pub use super::*; + + const DEFAULT_SSRC: u32 = 0; + const MTU: usize = 1200; + const ACCEPTED_BITRATE_ERROR_BPS: u32 = 50000; + + // Number of packets needed before we have a valid estimate. + const NUM_INITIAL_PACKETS: i64 = 2; + + const INITIAL_PROBING_PACKETS: i64 = 5; + + pub struct TestBitrateObserver { + updated: bool, + latest_bitrate: u32, + } + + impl TestBitrateObserver { + pub fn new() -> Self { + Self { + updated: false, + latest_bitrate: 0, + } + } + + pub fn on_receive_bitrate_changed(&mut self, bitrate: u32) { + self.latest_bitrate = bitrate; + self.updated = true; + } + + pub fn reset(&mut self) { + self.updated = false; + } + + pub fn updated(&self) -> bool { + self.updated + } + + pub fn latest_bitrate(&self) -> u32 { + self.latest_bitrate + } + } + + pub struct RtpStream { + fps: i64, + bitrate_bps: i64, + next_rtp_time: i64, + } + + impl RtpStream { + const SEND_SIDE_OFFSET_US: i64 = 1000000; + + pub fn new(fps: i64, bitrate_bps: i64) -> Self { + assert!(fps > 0); + Self { + fps, + bitrate_bps, + next_rtp_time: 0, + } + } + + // Generates a new frame for this stream. If called too soon after the + // previous frame, no frame will be generated. The frame is split into + // packets. + pub fn generate_frame(&mut self, time_now_us: i64, packets: &mut Vec) -> i64 { + if time_now_us < self.next_rtp_time { + return self.next_rtp_time; + } + let bits_per_frame: usize = ((self.bitrate_bps + self.fps / 2) / self.fps) as usize; + let n_packets: usize = std::cmp::max((bits_per_frame + 4 * MTU) / (8 * MTU), 1); + let payload_size: usize = (bits_per_frame + 4 * n_packets) / (8 * n_packets); + for _ in 0..n_packets { + let mut packet = PacketResult::default(); + packet.sent_packet.send_time = + Timestamp::from_micros(time_now_us + Self::SEND_SIDE_OFFSET_US); + packet.sent_packet.size = DataSize::from_bytes(payload_size as _); + packets.push(packet); + } + self.next_rtp_time = time_now_us + (1000000 + self.fps / 2) / self.fps; + self.next_rtp_time + } + + // The send-side time when the next frame can be generated. + pub fn next_rtp_time(&self) -> i64 { + self.next_rtp_time + } + + pub fn set_bitrate_bps(&mut self, bitrate_bps: i64) { + assert!(bitrate_bps >= 0); + self.bitrate_bps = bitrate_bps; + } + + pub fn bitrate_bps(&self) -> i64 { + self.bitrate_bps + } + } + + pub struct StreamGenerator { + // Capacity of the simulated channel in bits per second. + capacity: i64, + // The time when the last packet arrived. + prev_arrival_time_us: i64, + // All streams being transmitted on this simulated channel. + streams: Vec, + } + + impl StreamGenerator { + pub fn new(capacity: i64, time_now: i64) -> Self { + Self { + capacity, + prev_arrival_time_us: time_now, + streams: Vec::new(), + } + } + + // Add a new stream. + pub fn add_stream(&mut self, stream: RtpStream) { + self.streams.push(stream); + } + + // Set the link capacity. + pub fn set_capacity_bps(&mut self, capacity_bps: i64) { + assert!(capacity_bps > 0); + self.capacity = capacity_bps; + } + + // Divides `bitrate_bps` among all streams. The allocated bitrate per stream + // is decided by the initial allocation ratios. + pub fn set_bitrate_bps(&mut self, bitrate_bps: i64) { + let mut total_bitrate_before: i64 = 0; + for stream in &self.streams { + total_bitrate_before += stream.bitrate_bps(); + } + let mut bitrate_before: i64 = 0; + let mut total_bitrate_after: i64 = 0; + for stream in &mut self.streams { + bitrate_before += stream.bitrate_bps(); + let bitrate_after: i64 = (bitrate_before * bitrate_bps + total_bitrate_before / 2) + / total_bitrate_before; + stream.set_bitrate_bps(bitrate_after - total_bitrate_after); + total_bitrate_after += stream.bitrate_bps(); + } + assert_eq!(bitrate_before, total_bitrate_before); + assert_eq!(total_bitrate_after, bitrate_bps); + } + + // TODO(holmer): Break out the channel simulation part from this class to make + // it possible to simulate different types of channels. + pub fn generate_frame(&mut self, time_now_us: i64, packets: &mut Vec) -> i64 { + assert!(packets.is_empty()); + assert!(self.capacity > 0); + let it = self + .streams + .iter_mut() + .min_by_key(|x| x.next_rtp_time()) + .unwrap(); + it.generate_frame(time_now_us, packets); + for packet in packets { + let capacity_bpus: i64 = self.capacity / 1000; + let required_network_time_us: i64 = (8 * 1000 * packet.sent_packet.size.bytes() + + capacity_bpus / 2) + / capacity_bpus; + self.prev_arrival_time_us = std::cmp::max( + time_now_us + required_network_time_us, + self.prev_arrival_time_us + required_network_time_us, + ); + packet.receive_time = Timestamp::from_micros(self.prev_arrival_time_us); + } + let it = self + .streams + .iter() + .min_by_key(|x| x.next_rtp_time()) + .unwrap(); + it.next_rtp_time().max(time_now_us) + } + } + + pub struct DelayBasedBweTest { + clock: Timestamp, // Time at the receiver. + bitrate_observer: TestBitrateObserver, + acknowledged_bitrate_estimator: RobustThroughputEstimator, + probe_bitrate_estimator: ProbeBitrateEstimator, + bitrate_estimator: DelayBasedBwe, + stream_generator: StreamGenerator, + arrival_time_offset_ms: i64, + first_update: bool, + } + + impl DelayBasedBweTest { + pub fn new() -> Self { + let field_trials = FieldTrials::default(); + let clock = Timestamp::from_micros(100000000); + + Self { + acknowledged_bitrate_estimator: RobustThroughputEstimator::default(), + probe_bitrate_estimator: ProbeBitrateEstimator::default(), + bitrate_estimator: DelayBasedBwe::new(&field_trials), + stream_generator: StreamGenerator::new( + 1000000, // Capacity. + clock.us(), + ), + arrival_time_offset_ms: 0, + first_update: true, + clock, + bitrate_observer: TestBitrateObserver::new(), + } + } + + pub fn add_default_stream(&mut self) { + self.stream_generator.add_stream(RtpStream::new(30, 300000)); + } + + // Helpers to insert a single packet into the delay-based BWE. + fn incoming_feedback( + &mut self, + arrival_time: Timestamp, + send_time: Timestamp, + payload_size: usize, + pacing_info: PacedPacketInfo, + ) { + let receive_time = arrival_time + TimeDelta::from_millis(self.arrival_time_offset_ms); + + let packet = PacketResult { + receive_time, + ambiguous_receive_time: false, + sent_packet: SentPacket { + send_time, + size: DataSize::from_bytes(payload_size as _), + pacing_info, + ..Default::default() + }, + }; + + if packet.sent_packet.pacing_info.probe_cluster_id != PacedPacketInfo::NOT_APROBE { + self.probe_bitrate_estimator + .handle_probe_and_estimate_bitrate(&packet); + } + + let msg = TransportPacketsFeedback { + feedback_time: Timestamp::from_millis(self.clock.ms()), + packet_feedbacks: vec![packet], + ..Default::default() + }; + let sorted = msg.sorted_by_receive_time(); + self.acknowledged_bitrate_estimator + .incoming_packet_feedback(&sorted); + let result = self.bitrate_estimator.incoming_packet_feedback_vector( + &msg, + &sorted, + self.acknowledged_bitrate_estimator.bitrate(), + self.probe_bitrate_estimator + .fetch_and_reset_last_estimated_bitrate(), + /*in_alr*/ false, + ); + if result.updated { + self.bitrate_observer + .on_receive_bitrate_changed(result.target_bitrate.bps() as _); + } + } + + // Generates a frame of packets belonging to a stream at a given bitrate and + // with a given ssrc. The stream is pushed through a very simple simulated + // network, and is then given to the receive-side bandwidth estimator. + // Returns true if an over-use was seen, false otherwise. + // The StreamGenerator::updated() should be used to check for any changes in + // target bitrate after the call to this function. + fn generate_and_process_frame(&mut self, _ssrc: u32, bitrate_bps: u32) -> bool { + self.stream_generator.set_bitrate_bps(bitrate_bps as _); + let mut packets: Vec = Vec::new(); + + let next_time_us: i64 = self + .stream_generator + .generate_frame(self.clock.us(), &mut packets); + if packets.is_empty() { + return false; + } + + let mut overuse: bool = false; + self.bitrate_observer.reset(); + self.clock = packets.last().unwrap().receive_time; + for packet in &mut packets { + assert!(packet.receive_time.ms() + self.arrival_time_offset_ms >= 0); + packet.receive_time += TimeDelta::from_millis(self.arrival_time_offset_ms); + + if packet.sent_packet.pacing_info.probe_cluster_id != PacedPacketInfo::NOT_APROBE { + self.probe_bitrate_estimator + .handle_probe_and_estimate_bitrate(packet); + } + } + + self.acknowledged_bitrate_estimator + .incoming_packet_feedback(&packets); + let msg = TransportPacketsFeedback { + packet_feedbacks: packets, + feedback_time: self.clock, + ..Default::default() + }; + + let sorted = msg.sorted_by_receive_time(); + let result: DelayBasedBweResult = + self.bitrate_estimator.incoming_packet_feedback_vector( + &msg, + &sorted, + self.acknowledged_bitrate_estimator.bitrate(), + self.probe_bitrate_estimator + .fetch_and_reset_last_estimated_bitrate(), + /*in_alr*/ false, + ); + if result.updated { + self.bitrate_observer + .on_receive_bitrate_changed(result.target_bitrate.bps() as _); + if !self.first_update && result.target_bitrate.bps() < bitrate_bps as _ { + overuse = true; + } + self.first_update = false; + } + + self.clock = Timestamp::from_micros(next_time_us); + overuse + } + + // Run the bandwidth estimator with a stream of `number_of_frames` frames, or + // until it reaches `target_bitrate`. + // Can for instance be used to run the estimator for some time to get it + // into a steady state. + fn steady_state_run( + &mut self, + ssrc: u32, + number_of_frames: i64, + start_bitrate: u32, + min_bitrate: u32, + max_bitrate: u32, + target_bitrate: u32, + ) -> u32 { + let mut bitrate_bps: u32 = start_bitrate; + let mut bitrate_update_seen: bool = false; + // Produce `number_of_frames` frames and give them to the estimator. + for _ in 0..number_of_frames { + let overuse: bool = self.generate_and_process_frame(ssrc, bitrate_bps); + if overuse { + assert!(self.bitrate_observer.latest_bitrate() < max_bitrate); + assert!(self.bitrate_observer.latest_bitrate() > min_bitrate); + bitrate_bps = self.bitrate_observer.latest_bitrate(); + bitrate_update_seen = true; + } else if self.bitrate_observer.updated() { + bitrate_bps = self.bitrate_observer.latest_bitrate(); + self.bitrate_observer.reset(); + } + if bitrate_update_seen && bitrate_bps > target_bitrate { + break; + } + } + assert!(bitrate_update_seen); + bitrate_bps + } + + fn test_timestamp_grouping_test_helper(&mut self) { + const FRAMERATE: i64 = 50; // 50 fps to avoid rounding errors. + const FRAME_INTERVAL_MS: i64 = 1000 / FRAMERATE; + let mut send_time_ms: i64 = 0; + // Initial set of frames to increase the bitrate. 6 seconds to have enough + // time for the first estimate to be generated and for Process() to be called. + for _ in 0..=(6 * FRAMERATE) { + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms), + 1000, + Default::default(), + ); + + self.clock += TimeDelta::from_millis(FRAME_INTERVAL_MS as _); + send_time_ms += FRAME_INTERVAL_MS; + } + assert!(self.bitrate_observer.updated()); + assert!(self.bitrate_observer.latest_bitrate() >= 400000); + + // Insert batches of frames which were sent very close in time. Also simulate + // capacity over-use to see that we back off correctly. + const TIMESTAMP_GROUP_LENGTH: i64 = 15; + for _ in 0..100 { + for _ in 0..TIMESTAMP_GROUP_LENGTH { + // Insert `kTimestampGroupLength` frames with just 1 timestamp ticks in + // between. Should be treated as part of the same group by the estimator. + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms), + 100, + Default::default(), + ); + self.clock += + TimeDelta::from_millis(FRAME_INTERVAL_MS / TIMESTAMP_GROUP_LENGTH); + send_time_ms += 1; + } + // Increase time until next batch to simulate over-use. + self.clock += TimeDelta::from_millis(10); + send_time_ms += FRAME_INTERVAL_MS - TIMESTAMP_GROUP_LENGTH; + } + assert!(self.bitrate_observer.updated()); + // Should have reduced the estimate. + assert!(self.bitrate_observer.latest_bitrate() < 400000); + } + + fn test_wrapping_helper(&mut self, silence_time_s: i64) { + const FRAMERATE: i64 = 100; + const FRAME_INTERVAL_MS: i64 = 1000 / FRAMERATE; + let mut send_time_ms: i64 = 0; + + for _ in 0..3000 { + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms), + 1000, + Default::default(), + ); + self.clock += TimeDelta::from_millis(FRAME_INTERVAL_MS); + send_time_ms += FRAME_INTERVAL_MS; + } + let mut bitrate_before: DataRate = DataRate::zero(); + let mut ssrcs = Vec::new(); + self.bitrate_estimator + .latest_estimate(&mut ssrcs, &mut bitrate_before); + + self.clock += TimeDelta::from_millis(silence_time_s * 1000); + send_time_ms += silence_time_s * 1000; + + for _ in 0..24 { + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms), + 1000, + Default::default(), + ); + self.clock += TimeDelta::from_millis(2 * FRAME_INTERVAL_MS); + send_time_ms += FRAME_INTERVAL_MS; + } + let mut bitrate_after: DataRate = DataRate::zero(); + self.bitrate_estimator + .latest_estimate(&mut ssrcs, &mut bitrate_after); + assert!(bitrate_after < bitrate_before); + } + + fn initial_behavior_test_helper(&mut self, expected_converge_bitrate: u32) { + const FRAMERATE: i64 = 50; // 50 fps to avoid rounding errors. + const FRAME_INTERVAL_MS: i64 = 1000 / FRAMERATE; + const PACING_INFO: PacedPacketInfo = PacedPacketInfo::new(0, 5, 5000); + let mut bitrate: DataRate = DataRate::zero(); + let mut send_time_ms: i64 = 0; + let mut ssrcs: Vec = Vec::new(); + assert!( + !self + .bitrate_estimator + .latest_estimate(&mut ssrcs, &mut bitrate) + ); + assert_eq!(0, ssrcs.len()); + self.clock += TimeDelta::from_millis(1000); + assert!( + !self + .bitrate_estimator + .latest_estimate(&mut ssrcs, &mut bitrate) + ); + assert!(!self.bitrate_observer.updated()); + self.bitrate_observer.reset(); + self.clock += TimeDelta::from_millis(1000); + // Inserting packets for 5 seconds to get a valid estimate. + for i in 0..(5 * FRAMERATE + 1 + NUM_INITIAL_PACKETS) { + // NOTE!!! If the following line is moved under the if case then this test + // wont work on windows realease bots. + let pacing_info: PacedPacketInfo = if i < INITIAL_PROBING_PACKETS { + PACING_INFO + } else { + PacedPacketInfo::default() + }; + + if i == NUM_INITIAL_PACKETS { + assert!( + !self + .bitrate_estimator + .latest_estimate(&mut ssrcs, &mut bitrate) + ); + assert_eq!(0, ssrcs.len()); + assert!(!self.bitrate_observer.updated()); + self.bitrate_observer.reset(); + } + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms), + MTU, + pacing_info, + ); + self.clock += TimeDelta::from_millis(1000 / FRAMERATE); + send_time_ms += FRAME_INTERVAL_MS; + } + assert!( + self.bitrate_estimator + .latest_estimate(&mut ssrcs, &mut bitrate) + ); + assert_eq!(1, ssrcs.len()); + assert_eq!(&DEFAULT_SSRC, ssrcs.first().unwrap()); + assert_relative_eq!( + expected_converge_bitrate as f64, + bitrate.bps_float(), + epsilon = ACCEPTED_BITRATE_ERROR_BPS as f64 + ); + assert!(self.bitrate_observer.updated()); + self.bitrate_observer.reset(); + assert_eq!(self.bitrate_observer.latest_bitrate() as i64, bitrate.bps()); + } + + fn rate_increase_reordering_test_helper(&mut self, expected_bitrate_bps: u32) { + const FRAMERATE: i64 = 50; // 50 fps to avoid rounding errors. + const FRAME_INTERVAL_MS: i64 = 1000 / FRAMERATE; + const PACING_INFO: PacedPacketInfo = PacedPacketInfo::new(0, 5, 5000); + let mut send_time_ms: i64 = 0; + // Inserting packets for five seconds to get a valid estimate. + for i in 0..(5 * FRAMERATE + 1 + NUM_INITIAL_PACKETS) { + // NOTE!!! If the following line is moved under the if case then this test + // wont work on windows realease bots. + let pacing_info: PacedPacketInfo = if i < INITIAL_PROBING_PACKETS { + PACING_INFO + } else { + PacedPacketInfo::default() + }; + + // TODO(sprang): Remove this hack once the single stream estimator is gone, + // as it doesn't do anything in Process(). + if i == NUM_INITIAL_PACKETS { + // Process after we have enough frames to get a valid input rate estimate. + + assert!(!self.bitrate_observer.updated()); // No valid estimate. + } + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms), + MTU, + pacing_info, + ); + self.clock += TimeDelta::from_millis(FRAME_INTERVAL_MS); + send_time_ms += FRAME_INTERVAL_MS; + } + assert!(self.bitrate_observer.updated()); + assert_relative_eq!( + expected_bitrate_bps as f64, + self.bitrate_observer.latest_bitrate() as f64, + epsilon = ACCEPTED_BITRATE_ERROR_BPS as f64 + ); + for _ in 0..10 { + self.clock += TimeDelta::from_millis(2 * FRAME_INTERVAL_MS); + send_time_ms += 2 * FRAME_INTERVAL_MS; + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms), + 1000, + Default::default(), + ); + self.incoming_feedback( + self.clock, + Timestamp::from_millis(send_time_ms - FRAME_INTERVAL_MS), + 1000, + Default::default(), + ); + } + assert!(self.bitrate_observer.updated()); + assert_relative_eq!( + expected_bitrate_bps as f64, + self.bitrate_observer.latest_bitrate() as f64, + epsilon = ACCEPTED_BITRATE_ERROR_BPS as f64 + ); + } + fn rate_increase_rtp_timestamps_test_helper(&mut self, expected_iterations: i64) { + // This threshold corresponds approximately to increasing linearly with + // bitrate(i) = 1.04 * bitrate(i-1) + 1000 + // until bitrate(i) > 500000, with bitrate(1) ~= 30000. + let mut bitrate_bps: u32 = 30000; + let mut iterations: i64 = 0; + self.add_default_stream(); + // Feed the estimator with a stream of packets and verify that it reaches + // 500 kbps at the expected time. + while bitrate_bps < 500000 { + let overuse: bool = self.generate_and_process_frame(DEFAULT_SSRC, bitrate_bps); + if overuse { + assert!(self.bitrate_observer.latest_bitrate() > bitrate_bps); + bitrate_bps = self.bitrate_observer.latest_bitrate(); + self.bitrate_observer.reset(); + } else if self.bitrate_observer.updated() { + bitrate_bps = self.bitrate_observer.latest_bitrate(); + self.bitrate_observer.reset(); + } + iterations += 1; + } + assert_eq!(expected_iterations, iterations); + } + fn capacity_drop_test_helper( + &mut self, + number_of_streams: i64, + _wrap_time_stamp: bool, + expected_bitrate_drop_delta: u32, + receiver_clock_offset_change_ms: i64, + ) { + const FRAMERATE: i64 = 30; + const START_BITRATE: i64 = 900000; + const MIN_EXPECTED_BITRATE: i64 = 800000; + const MAX_EXPECTED_BITRATE: i64 = 1100000; + const INITIAL_CAPACITY_BPS: u32 = 1000000; + const REDUCED_CAPACITY_BPS: u32 = 500000; + + let steady_state_time; + if number_of_streams <= 1 { + steady_state_time = 10; + self.add_default_stream(); + } else { + steady_state_time = 10 * number_of_streams; + let mut bitrate_sum: i64 = 0; + let bitrate_denom: i64 = number_of_streams * (number_of_streams - 1); + for i in 0..number_of_streams { + // First stream gets half available bitrate, while the rest share the + // remaining half i.e.: 1/2 = Sum[n/(N*(N-1))] for n=1..N-1 (rounded up) + let mut bitrate: i64 = START_BITRATE / 2; + if i > 0 { + bitrate = (START_BITRATE * i + bitrate_denom / 2) / bitrate_denom; + } + self.stream_generator + .add_stream(RtpStream::new(FRAMERATE, bitrate)); + bitrate_sum += bitrate; + } + assert_eq!(bitrate_sum, START_BITRATE); + } + + // Run in steady state to make the estimator converge. + self.stream_generator + .set_capacity_bps(INITIAL_CAPACITY_BPS as _); + let mut bitrate_bps: u32 = self.steady_state_run( + DEFAULT_SSRC, + steady_state_time * FRAMERATE, + START_BITRATE as _, + MIN_EXPECTED_BITRATE as _, + MAX_EXPECTED_BITRATE as _, + INITIAL_CAPACITY_BPS, + ); + assert_relative_eq!( + INITIAL_CAPACITY_BPS as f64, + bitrate_bps as f64, + epsilon = 180000.0 + ); + self.bitrate_observer.reset(); + + // Add an offset to make sure the BWE can handle it. + self.arrival_time_offset_ms += receiver_clock_offset_change_ms; + + // Reduce the capacity and verify the decrease time. + self.stream_generator + .set_capacity_bps(REDUCED_CAPACITY_BPS as _); + let overuse_start_time: i64 = self.clock.ms(); + let mut bitrate_drop_time: i64 = -1; + for _ in 0..(100 * number_of_streams) { + self.generate_and_process_frame(DEFAULT_SSRC, bitrate_bps); + if bitrate_drop_time == -1 + && self.bitrate_observer.latest_bitrate() <= REDUCED_CAPACITY_BPS + { + bitrate_drop_time = self.clock.ms(); + } + if self.bitrate_observer.updated() { + bitrate_bps = self.bitrate_observer.latest_bitrate(); + } + } + + assert_relative_eq!( + expected_bitrate_drop_delta as f64, + (bitrate_drop_time - overuse_start_time) as f64, + epsilon = 33.0 + ); + } + } + + const NUM_PROBES_CLUSTER0: i64 = 5; + const NUM_PROBES_CLUSTER1: i64 = 8; + const PACING_INFO0: PacedPacketInfo = PacedPacketInfo::new(0, NUM_PROBES_CLUSTER0, 2000); + const PACING_INFO1: PacedPacketInfo = PacedPacketInfo::new(1, NUM_PROBES_CLUSTER1, 4000); + const TARGET_UTILIZATION_FRACTION: f64 = 0.95; + + #[test] + fn probe_detection() { + let mut bwe = DelayBasedBweTest::new(); + + // First burst sent at 8 * 1000 / 10 = 800 kbps. + for _ in 0..NUM_PROBES_CLUSTER0 { + bwe.clock += TimeDelta::from_millis(10); + bwe.incoming_feedback(bwe.clock, bwe.clock, 1000, PACING_INFO0); + } + assert!(bwe.bitrate_observer.updated()); + + // Second burst sent at 8 * 1000 / 5 = 1600 kbps. + for _ in 0..NUM_PROBES_CLUSTER1 { + bwe.clock += TimeDelta::from_millis(5); + bwe.incoming_feedback(bwe.clock, bwe.clock, 1000, PACING_INFO1); + } + + assert!(bwe.bitrate_observer.updated()); + assert!(bwe.bitrate_observer.latest_bitrate() > 1500000); + } + + #[test] + fn probe_detection_non_paced_packets() { + let mut bwe = DelayBasedBweTest::new(); + + // First burst sent at 8 * 1000 / 10 = 800 kbps, but with every other packet + // not being paced which could mess things up. + for _ in 0..NUM_PROBES_CLUSTER0 { + bwe.clock += TimeDelta::from_millis(5); + + bwe.incoming_feedback(bwe.clock, bwe.clock, 1000, PACING_INFO0); + // Non-paced packet, arriving 5 ms after. + bwe.clock += TimeDelta::from_millis(5); + bwe.incoming_feedback(bwe.clock, bwe.clock, 100, PacedPacketInfo::default()); + } + + assert!(bwe.bitrate_observer.updated()); + assert!(bwe.bitrate_observer.latest_bitrate() > 800000); + } + + #[test] + fn probe_detection_faster_arrival() { + let mut bwe = DelayBasedBweTest::new(); + + // First burst sent at 8 * 1000 / 10 = 800 kbps. + // Arriving at 8 * 1000 / 5 = 1600 kbps. + let mut send_time_ms: Timestamp = Timestamp::zero(); + for _ in 0..NUM_PROBES_CLUSTER0 { + bwe.clock += TimeDelta::from_millis(1); + send_time_ms += TimeDelta::from_millis(10); + + bwe.incoming_feedback(bwe.clock, send_time_ms, 1000, PACING_INFO0); + } + + assert!(!bwe.bitrate_observer.updated()); + } + + #[test] + fn probe_detection_slower_arrival() { + let mut bwe = DelayBasedBweTest::new(); + + // First burst sent at 8 * 1000 / 5 = 1600 kbps. + // Arriving at 8 * 1000 / 7 = 1142 kbps. + // Since the receive rate is significantly below the send rate, we expect to + // use 95% of the estimated capacity. + let mut send_time_ms: Timestamp = Timestamp::zero(); + for _ in 0..NUM_PROBES_CLUSTER1 { + bwe.clock += TimeDelta::from_millis(7); + send_time_ms += TimeDelta::from_millis(5); + + bwe.incoming_feedback(bwe.clock, send_time_ms, 1000, PACING_INFO1); + } + + assert!(bwe.bitrate_observer.updated()); + assert_relative_eq!( + bwe.bitrate_observer.latest_bitrate() as f64, + { TARGET_UTILIZATION_FRACTION * 1140000.0 }, + epsilon = 10000.0 + ); + } + + #[test] + fn probe_detection_slower_arrival_high_bitrate() { + let mut bwe = DelayBasedBweTest::new(); + + // Burst sent at 8 * 1000 / 1 = 8000 kbps. + // Arriving at 8 * 1000 / 2 = 4000 kbps. + // Since the receive rate is significantly below the send rate, we expect to + // use 95% of the estimated capacity. + let mut send_time_ms: Timestamp = Timestamp::zero(); + for _ in 0..NUM_PROBES_CLUSTER1 { + bwe.clock += TimeDelta::from_millis(2); + send_time_ms += TimeDelta::from_millis(1); + + bwe.incoming_feedback(bwe.clock, send_time_ms, 1000, PACING_INFO1); + } + + assert!(bwe.bitrate_observer.updated()); + assert_relative_eq!( + bwe.bitrate_observer.latest_bitrate() as f64, + TARGET_UTILIZATION_FRACTION * 4000000.0, + epsilon = 10000.0 + ); + } + + #[test] + fn initial_behavior() { + let mut bwe = DelayBasedBweTest::new(); + bwe.initial_behavior_test_helper(730000); + } + + #[test] + fn initialize_result() { + let result = DelayBasedBweResult::default(); + assert_eq!(result.delay_detector_state, BandwidthUsage::Normal); + } + + #[test] + fn rate_increase_reordering() { + let mut bwe = DelayBasedBweTest::new(); + bwe.rate_increase_reordering_test_helper(730000); + } + #[test] + fn rate_increase_rtp_timestamps() { + let mut bwe = DelayBasedBweTest::new(); + bwe.rate_increase_rtp_timestamps_test_helper(617); + } + + #[test] + fn capacity_drop_one_stream() { + let mut bwe = DelayBasedBweTest::new(); + bwe.capacity_drop_test_helper(1, false, 500, 0); + } + + #[test] + fn capacity_drop_pos_offset_change() { + let mut bwe = DelayBasedBweTest::new(); + bwe.capacity_drop_test_helper(1, false, 867, 30000); + } + + #[test] + fn capacity_drop_neg_offset_change() { + let mut bwe = DelayBasedBweTest::new(); + bwe.capacity_drop_test_helper(1, false, 933, -30000); + } + + #[test] + fn capacity_drop_one_stream_wrap() { + let mut bwe = DelayBasedBweTest::new(); + bwe.capacity_drop_test_helper(1, true, 533, 0); + } + + #[test] + fn test_timestamp_grouping() { + let mut bwe = DelayBasedBweTest::new(); + bwe.test_timestamp_grouping_test_helper(); + } + + #[test] + fn test_short_timeout_and_wrap() { + let mut bwe = DelayBasedBweTest::new(); + // Simulate a client leaving and rejoining the call after 35 seconds. This + // will make abs send time wrap, so if streams aren't timed out properly + // the next 30 seconds of packets will be out of order. + bwe.test_wrapping_helper(35); + } + + #[test] + fn test_long_timeout_and_wrap() { + let mut bwe = DelayBasedBweTest::new(); + // Simulate a client leaving and rejoining the call after some multiple of + // 64 seconds later. This will cause a zero difference in abs send times due + // to the wrap, but a big difference in arrival time, if streams aren't + // properly timed out. + bwe.test_wrapping_helper(10 * 64); + } + + #[test] + fn test_initial_overuse() { + let mut bwe = DelayBasedBweTest::new(); + const START_BITRATE: DataRate = DataRate::from_kilobits_per_sec(300); + const INITIAL_CAPACITY: DataRate = DataRate::from_kilobits_per_sec(200); + const DUMMY_SSRC: u32 = 0; + // High FPS to ensure that we send a lot of packets in a short time. + const FPS: i64 = 90; + + bwe.stream_generator + .add_stream(RtpStream::new(FPS, START_BITRATE.bps())); + bwe.stream_generator + .set_capacity_bps(INITIAL_CAPACITY.bps()); + + // Needed to initialize the AimdRateControl. + bwe.bitrate_estimator.set_start_bitrate(START_BITRATE); + + // Produce 40 frames (in 1/3 second) and give them to the estimator. + let mut bitrate_bps: i64 = START_BITRATE.bps(); + let mut seen_overuse: bool = false; + for _ in 0..40 { + let overuse: bool = bwe.generate_and_process_frame(DUMMY_SSRC, bitrate_bps as _); + if overuse { + assert!(bwe.bitrate_observer.updated()); + assert!(bwe.bitrate_observer.latest_bitrate() as i64 <= INITIAL_CAPACITY.bps()); + assert!( + bwe.bitrate_observer.latest_bitrate() as f64 + > 0.8 * INITIAL_CAPACITY.bps_float() + ); + //bitrate_bps = bwe.bitrate_observer.latest_bitrate() as _; + seen_overuse = true; + break; + } else if bwe.bitrate_observer.updated() { + bitrate_bps = bwe.bitrate_observer.latest_bitrate() as _; + bwe.bitrate_observer.reset(); + } + } + assert!(seen_overuse); + assert!(bwe.bitrate_observer.latest_bitrate() as i64 <= INITIAL_CAPACITY.bps()); + assert!(bwe.bitrate_observer.latest_bitrate() as f64 > 0.8 * INITIAL_CAPACITY.bps_float()); + } + + #[test] + fn test_timestamp_precision_handling() { + let mut bwe = DelayBasedBweTest::new(); + // This test does some basic checks to make sure that timestamps with higher + // than millisecond precision are handled properly and do not cause any + // problems in the estimator. Specifically, previously reported in + // webrtc:14023 and described in more details there, the rounding to the + // nearest milliseconds caused discrepancy in the accumulated delay. This lead + // to false-positive overuse detection. + // Technical details of the test: + // Send times(ms): 0.000, 9.725, 20.000, 29.725, 40.000, 49.725, ... + // Recv times(ms): 0.500, 10.000, 20.500, 30.000, 40.500, 50.000, ... + // Send deltas(ms): 9.750, 10.250, 9.750, 10.250, 9.750, ... + // Recv deltas(ms): 9.500, 10.500, 9.500, 10.500, 9.500, ... + // There is no delay building up between the send times and the receive times, + // therefore this case should never lead to an overuse detection. However, if + // the time deltas were accidentally rounded to the nearest milliseconds, then + // all the send deltas would be equal to 10ms while some recv deltas would + // round up to 11ms which would lead in a false illusion of delay build up. + let mut last_bitrate: u32 = bwe.bitrate_observer.latest_bitrate(); + for _ in 0..1000 { + bwe.clock += TimeDelta::from_micros(500); + bwe.incoming_feedback( + bwe.clock, + bwe.clock - TimeDelta::from_micros(500), + 1000, + PacedPacketInfo::default(), + ); + bwe.clock += TimeDelta::from_micros(9500); + bwe.incoming_feedback( + bwe.clock, + bwe.clock - TimeDelta::from_micros(250), + 1000, + PacedPacketInfo::default(), + ); + bwe.clock += TimeDelta::from_micros(10000); + + // The bitrate should never decrease in this test. + assert!(last_bitrate <= bwe.bitrate_observer.latest_bitrate()); + last_bitrate = bwe.bitrate_observer.latest_bitrate(); + } + } +} // namespace webrtc diff --git a/rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs b/rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs new file mode 100644 index 00000000000..cfb4e72e67e --- /dev/null +++ b/rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::api::transport::BandwidthUsage; + +pub trait DelayIncreaseDetectorInterface { + // Update the detector with a new sample. The deltas should represent deltas + // between timestamp groups as defined by the InterArrival class. + fn update( + &mut self, + recv_delta_ms: f64, + send_delta_ms: f64, + send_time_ms: i64, + arrival_time_ms: i64, + packet_size: usize, + calculated_deltas: bool, + ); + + fn state(&self) -> BandwidthUsage; +} diff --git a/rs/moq-congestion/src/gcc/experiments/field_trials.rs b/rs/moq-congestion/src/gcc/experiments/field_trials.rs new file mode 100644 index 00000000000..c1ab33a1bd0 --- /dev/null +++ b/rs/moq-congestion/src/gcc/experiments/field_trials.rs @@ -0,0 +1,69 @@ +use super::CongestionWindowConfig; + +use crate::gcc::{ + AlrDetectorConfig, LossBasedBweV2Config, ProbeControllerConfig, + RobustThroughputEstimatorSettings, RttBasedBackoffConfig, TrendlineEstimatorSettings, + remote_bitrate_estimator::BweBackOffFactor, +}; + +/// The WebRTC field trials that still reach a code path here, as typed settings. +/// +/// Upstream parses each from a string keyed by the trial's name, given in each field's doc. The +/// defaults are upstream's when no trial string is set. +#[derive(Clone, Debug)] +pub struct FieldTrials { + /// WebRTC-Bwe-RobustThroughputEstimatorSettings + pub robust_throughput_estimator_settings: RobustThroughputEstimatorSettings, + + /// WebRTC-Bwe-ProbingConfiguration + pub probing_configuration: ProbeControllerConfig, + + /// WebRTC-AlrDetectorParameters + pub alr_detector_parameters: AlrDetectorConfig, + + /// WebRTC-AddPacingToCongestionWindowPushback + pub add_pacing_to_congestion_window_pushback: bool, + + /// WebRTC-CongestionWindow + pub congestion_window: CongestionWindowConfig, + + /// WebRTC-Bwe-LossBasedBweV2 + pub loss_based_bwe_v2: LossBasedBweV2Config, + + /// WebRTC-Bwe-MaxRttLimit + pub max_rtt_limit: RttBasedBackoffConfig, + + /// WebRTC-Bwe-TrendlineEstimatorSettings + pub trendline_estimator_settings: TrendlineEstimatorSettings, + + /// WebRTC-BweBackOffFactor + pub bwe_back_off_factor: BweBackOffFactor, + + /// WebRTC-DontIncreaseDelayBasedBweInAlr + pub no_bitrate_increase_in_alr: bool, + + /// WebRTC-Bwe-MinAllocAsLowerBound + pub min_alloc_as_lower_bound: bool, + + /// WebRTC-Bwe-LimitProbesLowerThanThroughputEstimate + pub limit_probes_lower_than_throughput_estimate: bool, +} + +impl Default for FieldTrials { + fn default() -> Self { + Self { + robust_throughput_estimator_settings: RobustThroughputEstimatorSettings::default(), + probing_configuration: ProbeControllerConfig::default(), + alr_detector_parameters: AlrDetectorConfig::default(), + add_pacing_to_congestion_window_pushback: false, + congestion_window: CongestionWindowConfig::default(), + loss_based_bwe_v2: LossBasedBweV2Config::default(), + max_rtt_limit: RttBasedBackoffConfig::default(), + trendline_estimator_settings: TrendlineEstimatorSettings::default(), + bwe_back_off_factor: BweBackOffFactor::default(), + no_bitrate_increase_in_alr: false, + min_alloc_as_lower_bound: true, + limit_probes_lower_than_throughput_estimate: true, + } + } +} diff --git a/rs/moq-congestion/src/gcc/experiments/mod.rs b/rs/moq-congestion/src/gcc/experiments/mod.rs new file mode 100644 index 00000000000..04e51c1d649 --- /dev/null +++ b/rs/moq-congestion/src/gcc/experiments/mod.rs @@ -0,0 +1,5 @@ +mod field_trials; +mod rate_control; + +pub use field_trials::*; +pub use rate_control::*; diff --git a/rs/moq-congestion/src/gcc/experiments/rate_control.rs b/rs/moq-congestion/src/gcc/experiments/rate_control.rs new file mode 100644 index 00000000000..30ba4f80f8d --- /dev/null +++ b/rs/moq-congestion/src/gcc/experiments/rate_control.rs @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! Upstream's `RateControlSettings` also carries the video encoder settings +//! (`WebRTC-VideoRateControl`), which no congestion controller reads; only the congestion window +//! remains. + +use crate::gcc::{api::units::DataSize, experiments::FieldTrials}; + +/// WebRTC-CongestionWindow +#[derive(Clone, Debug)] +pub struct CongestionWindowConfig { + pub queue_size_ms: i64, // QueueSize + pub min_bitrate_bps: i64, // MinBitrate + pub initial_data_window: Option, // InitWin + pub drop_frame_only: bool, // DropFrame +} + +impl Default for CongestionWindowConfig { + // QueueSize:350,MinBitrate:30000,DropFrame:true + fn default() -> Self { + Self { + queue_size_ms: 350, + min_bitrate_bps: 30000, + initial_data_window: None, + drop_frame_only: true, + } + } +} + +#[derive(Clone, Default)] +pub struct RateControlSettings { + congestion_window_config: CongestionWindowConfig, +} + +impl RateControlSettings { + pub fn new(field_trials: &FieldTrials) -> Self { + Self { + congestion_window_config: field_trials.congestion_window.clone(), + } + } + + /// When CongestionWindowPushback is enabled, the pacer is oblivious to + /// the congestion window. The relation between outstanding data and + /// the congestion window affects encoder allocations directly. + pub fn use_congestion_window(&self) -> bool { + self.congestion_window_config.queue_size_ms > 0 + } + + pub fn get_congestion_window_additional_time_ms(&self) -> i64 { + self.congestion_window_config.queue_size_ms + } + + pub fn use_congestion_window_pushback(&self) -> bool { + self.congestion_window_config.queue_size_ms > 0 + && self.congestion_window_config.min_bitrate_bps > 0 + } + + pub fn use_congestion_window_drop_frame_only(&self) -> bool { + self.congestion_window_config.drop_frame_only + } + + pub fn congestion_window_min_pushback_target_bitrate_bps(&self) -> u32 { + self.congestion_window_config.min_bitrate_bps as _ + } + + pub fn congestion_window_initial_data_window(&self) -> Option { + self.congestion_window_config.initial_data_window + } +} diff --git a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs new file mode 100644 index 00000000000..78a1e499781 --- /dev/null +++ b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs @@ -0,0 +1,970 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! Upstream's `NetworkControllerInterface` also takes route changes, REMB, RTCP loss reports, +//! received packets, runtime constraint changes, and a network state estimate. A QUIC sender has +//! none of them: a path change rebuilds the controller, and the constraints come once, at +//! construction. What remains is the network becoming available, the process timer, the RTT, +//! each sent packet, each ACK's feedback, and stream settings, which carry the session's probe +//! target as the allocation GoogCC probes toward. + +use std::collections::VecDeque; + +use crate::gcc::{ + AlrDetector, BandwidthLimitedCause, CongestionWindowPushbackController, DelayBasedBwe, + DelayBasedBweResult, LossBasedState, ProbeBitrateEstimator, ProbeController, + RobustThroughputEstimator, SendSideBandwidthEstimation, + api::{ + transport::{ + BandwidthUsage, NetworkAvailability, NetworkControlUpdate, PacedPacketInfo, + PacerConfig, ProbeClusterConfig, ProcessInterval, RoundTripTimeUpdate, SentPacket, + StreamsConfig, TargetRateConstraints, TargetTransferRate, TransportPacketsFeedback, + }, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + experiments::{FieldTrials, RateControlSettings}, + remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE, +}; + +/// Configuration sent to factory create function. The parameters here are +/// optional to use for a network controller implementation. +#[derive(Default, Debug, Clone)] +pub struct NetworkControllerConfig { + pub field_trials: FieldTrials, + + /// The initial constraints to start with. Note that the starting rate + /// has to be set initially to provide a starting state for the network + /// controller, even though the field is marked as optional. + pub constraints: TargetRateConstraints, + /// Initial stream specific configuration. + pub stream_based_config: StreamsConfig, + pub default_pacing_time_window: TimeDelta, +} + +/// Google Congestion Control, as WebRTC's `GoogCcNetworkController`. +#[derive(Clone)] +pub struct GoogCcNetworkController { + use_min_allocatable_as_lower_bound: bool, + limit_probes_lower_than_throughput_estimate: bool, + rate_control_settings: RateControlSettings, + + probe_controller: ProbeController, + congestion_window_pushback_controller: Option, + + bandwidth_estimation: SendSideBandwidthEstimation, + alr_detector: AlrDetector, + probe_bitrate_estimator: ProbeBitrateEstimator, + delay_based_bwe: DelayBasedBwe, + acknowledged_bitrate_estimator: RobustThroughputEstimator, + + initial_config: Option, + + min_target_rate: DataRate, + min_data_rate: DataRate, + max_data_rate: DataRate, + starting_rate: Option, + + first_packet_sent: bool, + first_transport_feedback_received: bool, + + feedback_max_rtts: VecDeque, + + last_loss_based_target_rate: DataRate, + last_pushback_target_rate: DataRate, + last_loss_base_state: LossBasedState, + + last_estimated_round_trip_time: TimeDelta, + + pacing_factor: Option, + pacing_time_window: TimeDelta, + min_total_allocated_bitrate: DataRate, + + previously_in_alr: bool, + last_is_bandwidth_limited: bool, + + current_data_window: Option, +} + +impl GoogCcNetworkController { + // Pacing-rate relative to our target send rate. + // Multiplicative factor that is applied to the target bitrate to calculate + // the number of bytes that can be transmitted per interval. + // Increasing this factor will result in lower delays in cases of bitrate + // overshoots from the encoder. + const DEFAULT_PACE_MULTIPLIER: f64 = 2.5; + // If send side bandwidth estimation is used, this pacing multiplier is used per + // default. + const DEFAULT_PACE_MULTIPLIER_WITH_SEND_SIDE_BWE: f64 = 1.1; + + // If the probe result is far below the current throughput estimate + // it's unlikely that the probe is accurate, so we don't want to drop too far. + // However, if we actually are overusing, we want to drop to something slightly + // below the current throughput estimate to drain the network queues. + const PROBE_DROP_THROUGHPUT_FRACTION: f64 = 0.85; + + pub fn new(config: NetworkControllerConfig) -> Self { + let field_trials = &config.field_trials; + let rate_control_settings = RateControlSettings::new(field_trials); + let starting_rate = config.constraints.starting_rate.expect("a starting rate"); + + let mut delay_based_bwe = DelayBasedBwe::new(field_trials); + delay_based_bwe.set_min_bitrate(CONGESTION_CONTROLLER_MIN_BITRATE); + + Self { + use_min_allocatable_as_lower_bound: field_trials.min_alloc_as_lower_bound, + limit_probes_lower_than_throughput_estimate: field_trials + .limit_probes_lower_than_throughput_estimate, + + probe_controller: ProbeController::new(field_trials.probing_configuration.clone()), + congestion_window_pushback_controller: rate_control_settings + .use_congestion_window_pushback() + .then(|| CongestionWindowPushbackController::new(field_trials)), + rate_control_settings, + bandwidth_estimation: SendSideBandwidthEstimation::new(field_trials), + alr_detector: AlrDetector::new(field_trials.alr_detector_parameters.clone()), + probe_bitrate_estimator: ProbeBitrateEstimator::default(), + delay_based_bwe, + acknowledged_bitrate_estimator: RobustThroughputEstimator::new( + field_trials.robust_throughput_estimator_settings.clone(), + ), + + min_target_rate: DataRate::zero(), + min_data_rate: DataRate::zero(), + max_data_rate: DataRate::plus_infinity(), + starting_rate: config.constraints.starting_rate, + first_packet_sent: false, + first_transport_feedback_received: false, + feedback_max_rtts: VecDeque::new(), + last_loss_based_target_rate: starting_rate, + last_pushback_target_rate: starting_rate, + last_loss_base_state: LossBasedState::DelayBasedEstimate, + last_estimated_round_trip_time: TimeDelta::plus_infinity(), + pacing_factor: config.stream_based_config.pacing_factor, + pacing_time_window: config.default_pacing_time_window, + min_total_allocated_bitrate: config + .stream_based_config + .min_total_allocated_bitrate + .unwrap_or(DataRate::zero()), + previously_in_alr: false, + last_is_bandwidth_limited: true, + current_data_window: None, + + initial_config: Some(config), + } + } + + pub fn on_network_availability(&mut self, msg: NetworkAvailability) -> NetworkControlUpdate { + NetworkControlUpdate { + probe_cluster_configs: self.probe_controller.on_network_availability(msg), + ..Default::default() + } + } + + pub fn on_streams_config(&mut self, msg: StreamsConfig) -> NetworkControlUpdate { + let mut update = NetworkControlUpdate::default(); + if let Some(requests_alr_probing) = msg.requests_alr_probing { + self.probe_controller + .enable_periodic_alr_probing(requests_alr_probing); + } + if let Some(max_total_allocated_bitrate) = msg.max_total_allocated_bitrate { + update.probe_cluster_configs = self + .probe_controller + .on_max_total_allocated_bitrate(max_total_allocated_bitrate, msg.at_time); + } + + let mut pacing_changed = false; + if let Some(pacing_factor) = msg.pacing_factor + && Some(pacing_factor) != self.pacing_factor + { + self.pacing_factor = Some(pacing_factor); + pacing_changed = true; + } + if let Some(min_total_allocated_bitrate) = msg.min_total_allocated_bitrate + && min_total_allocated_bitrate != self.min_total_allocated_bitrate + { + self.min_total_allocated_bitrate = min_total_allocated_bitrate; + pacing_changed = true; + + if self.use_min_allocatable_as_lower_bound { + self.clamp_constraints(); + self.delay_based_bwe.set_min_bitrate(self.min_data_rate); + self.bandwidth_estimation + .set_min_max_bitrate(self.min_data_rate, self.max_data_rate); + } + } + if pacing_changed { + update.pacer_config = Some(self.get_pacing_rates()); + } + update + } + + pub fn on_process_interval(&mut self, msg: ProcessInterval) -> NetworkControlUpdate { + let mut update = NetworkControlUpdate::default(); + if let Some(initial_config) = self.initial_config.take() { + update.probe_cluster_configs = self.reset_constraints(initial_config.constraints); + update.pacer_config = Some(self.get_pacing_rates()); + + if let Some(requests_alr_probing) = + initial_config.stream_based_config.requests_alr_probing + { + self.probe_controller + .enable_periodic_alr_probing(requests_alr_probing); + } + self.probe_controller.enable_repeated_initial_probing( + initial_config + .stream_based_config + .enable_repeated_initial_probing, + ); + if let Some(total_bitrate) = initial_config + .stream_based_config + .max_total_allocated_bitrate + { + let mut probes = self + .probe_controller + .on_max_total_allocated_bitrate(total_bitrate, msg.at_time); + update.probe_cluster_configs.append(&mut probes); + } + } + + if let (Some(congestion_window_pushback_controller), Some(pacer_queue)) = ( + &mut self.congestion_window_pushback_controller, + msg.pacer_queue, + ) { + congestion_window_pushback_controller.update_pacing_queue(pacer_queue.bytes()) + }; + self.bandwidth_estimation.update_estimate(msg.at_time); + // Avoid ALR probing before the first transport feedback is received in + // order to avoid ALR probing if receive side BWE is used. + self.probe_controller + .set_alr_start_time(if self.first_transport_feedback_received { + self.alr_detector + .get_application_limited_region_start_time() + } else { + None + }); + + let mut probes = self.probe_controller.process(msg.at_time); + update.probe_cluster_configs.append(&mut probes); + + if self.rate_control_settings.use_congestion_window() && !self.feedback_max_rtts.is_empty() + { + self.update_congestion_window_size(); + } + match ( + &mut self.congestion_window_pushback_controller, + self.current_data_window, + ) { + (Some(congestion_window_pushback_controller), Some(current_data_window)) => { + congestion_window_pushback_controller.set_data_window(current_data_window) + } + _ => update.congestion_window = self.current_data_window, + }; + + self.maybe_trigger_on_network_changed(&mut update, msg.at_time); + update + } + + pub fn on_round_trip_time_update(&mut self, msg: RoundTripTimeUpdate) { + debug_assert!(!msg.round_trip_time.is_zero()); + self.delay_based_bwe.on_rtt_update(msg.round_trip_time); + self.bandwidth_estimation.update_rtt(msg.round_trip_time); + } + + pub fn on_sent_packet(&mut self, sent_packet: SentPacket) -> NetworkControlUpdate { + self.alr_detector + .on_bytes_sent(sent_packet.size, sent_packet.send_time); + + if !self.first_packet_sent { + self.first_packet_sent = true; + // Initialize feedback time to send time to allow estimation of RTT until + // first feedback is received. + self.bandwidth_estimation + .update_propagation_rtt(sent_packet.send_time, TimeDelta::zero()); + } + self.bandwidth_estimation.on_sent_packet(&sent_packet); + + if let Some(congestion_window_pushback_controller) = + &mut self.congestion_window_pushback_controller + { + congestion_window_pushback_controller + .update_outstanding_data(sent_packet.data_in_flight.bytes()); + let mut update = NetworkControlUpdate::default(); + self.maybe_trigger_on_network_changed(&mut update, sent_packet.send_time); + update + } else { + NetworkControlUpdate::default() + } + } + + pub fn on_transport_packets_feedback( + &mut self, + report: &TransportPacketsFeedback, + ) -> NetworkControlUpdate { + if report.packet_feedbacks.is_empty() { + // TODO(bugs.webrtc.org/10125): Design a better mechanism to safe-guard + // against building very large network queues. + return NetworkControlUpdate::default(); + } + // Send side BWE is used. + self.first_transport_feedback_received = true; + + if let Some(congestion_window_pushback_controller) = + &mut self.congestion_window_pushback_controller + { + congestion_window_pushback_controller + .update_outstanding_data(report.data_in_flight.bytes()); + } + let mut max_feedback_rtt: TimeDelta = TimeDelta::minus_infinity(); + let mut min_propagation_rtt: TimeDelta = TimeDelta::plus_infinity(); + let max_recv_time = report + .received_with_send_info() + .map(|x| x.receive_time) + .max() + .unwrap_or(Timestamp::minus_infinity()); + + for feedback in report.received_with_send_info() { + let feedback_rtt: TimeDelta = report.feedback_time - feedback.sent_packet.send_time; + let min_pending_time: TimeDelta = max_recv_time - feedback.receive_time; + let propagation_rtt: TimeDelta = feedback_rtt - min_pending_time; + max_feedback_rtt = std::cmp::max(max_feedback_rtt, feedback_rtt); + min_propagation_rtt = std::cmp::min(min_propagation_rtt, propagation_rtt); + } + + if max_feedback_rtt.is_finite() { + self.feedback_max_rtts.push_back(max_feedback_rtt.ms()); + const MAX_FEEDBACK_RTT_WINDOW: usize = 32; + if self.feedback_max_rtts.len() > MAX_FEEDBACK_RTT_WINDOW { + self.feedback_max_rtts.pop_front(); + } + // TODO(srte): Use time since last unacknowledged packet. + self.bandwidth_estimation + .update_propagation_rtt(report.feedback_time, min_propagation_rtt); + } + + let alr_start_time: Option = self + .alr_detector + .get_application_limited_region_start_time(); + if self.previously_in_alr && alr_start_time.is_none() { + self.probe_controller + .set_alr_ended_time(report.feedback_time); + } + self.previously_in_alr = alr_start_time.is_some(); + let sorted = report.sorted_by_receive_time(); + self.acknowledged_bitrate_estimator + .incoming_packet_feedback(&sorted); + let acknowledged_bitrate = self.acknowledged_bitrate_estimator.bitrate(); + self.bandwidth_estimation + .set_acknowledged_rate(acknowledged_bitrate); + for feedback in &sorted { + if feedback.sent_packet.pacing_info.probe_cluster_id != PacedPacketInfo::NOT_APROBE { + self.probe_bitrate_estimator + .handle_probe_and_estimate_bitrate(feedback); + } + } + + let mut probe_bitrate: Option = self + .probe_bitrate_estimator + .fetch_and_reset_last_estimated_bitrate(); + + if let (Some(probe), Some(acknowledged)) = (probe_bitrate, acknowledged_bitrate) + && self.limit_probes_lower_than_throughput_estimate + { + // Limit the backoff to something slightly below the acknowledged + // bitrate. ("Slightly below" because we want to drain the queues + // if we are actually overusing.) + // The acknowledged bitrate shouldn't normally be higher than the delay + // based estimate, but it could happen e.g. due to packet bursts or + // encoder overshoot. We use std::cmp::min to ensure that a probe result + // below the current BWE never causes an increase. + let limit: DataRate = std::cmp::min( + self.delay_based_bwe.last_estimate(), + acknowledged * Self::PROBE_DROP_THROUGHPUT_FRACTION, + ); + probe_bitrate = Some(std::cmp::max(probe, limit)); + } + + let mut update = NetworkControlUpdate::default(); + + let result: DelayBasedBweResult = self.delay_based_bwe.incoming_packet_feedback_vector( + report, + &sorted, + acknowledged_bitrate, + probe_bitrate, + alr_start_time.is_some(), + ); + + if result.updated { + if result.probe { + self.bandwidth_estimation + .set_send_bitrate(result.target_bitrate, report.feedback_time); + } + // Since SetSendBitrate now resets the delay-based estimate, we have to + // call UpdateDelayBasedEstimate after SetSendBitrate. + self.bandwidth_estimation + .update_delay_based_estimate(report.feedback_time, result.target_bitrate); + } + self.bandwidth_estimation.update_loss_based_estimator( + report, + result.delay_detector_state, + probe_bitrate, + alr_start_time.is_some(), + ); + if result.updated { + // Update the estimate in the ProbeController, in case we want to probe. + self.maybe_trigger_on_network_changed(&mut update, report.feedback_time); + } + + if result.recovered_from_overuse { + self.probe_controller.set_alr_start_time(alr_start_time); + let mut probes = self.probe_controller.request_probe(report.feedback_time); + update.probe_cluster_configs.append(&mut probes); + } + + // No valid RTT could be because send-side BWE isn't used, in which case + // we don't try to limit the outstanding packets. + if self.rate_control_settings.use_congestion_window() && max_feedback_rtt.is_finite() { + self.update_congestion_window_size(); + } + match ( + &mut self.congestion_window_pushback_controller, + self.current_data_window, + ) { + (Some(congestion_window_pushback_controller), Some(current_data_window)) => { + congestion_window_pushback_controller.set_data_window(current_data_window) + } + _ => update.congestion_window = self.current_data_window, + }; + + update + } + + /// The data window the congestion window settings derive from the target rate and the RTT, + /// whether or not the pushback controller is the one enforcing it. + pub fn congestion_window(&self) -> Option { + self.current_data_window + } + + fn reset_constraints( + &mut self, + new_constraints: TargetRateConstraints, + ) -> Vec { + self.min_target_rate = new_constraints.min_data_rate.unwrap_or(DataRate::zero()); + self.max_data_rate = new_constraints + .max_data_rate + .unwrap_or(DataRate::plus_infinity()); + self.starting_rate = new_constraints.starting_rate; + self.clamp_constraints(); + + self.bandwidth_estimation.set_bitrates( + self.starting_rate, + self.min_data_rate, + self.max_data_rate, + new_constraints.at_time, + ); + + if let Some(starting_rate) = self.starting_rate { + self.delay_based_bwe.set_start_bitrate(starting_rate); + } + self.delay_based_bwe.set_min_bitrate(self.min_data_rate); + + self.probe_controller.set_bitrates( + self.min_data_rate, + self.starting_rate.unwrap_or(DataRate::zero()), + self.max_data_rate, + new_constraints.at_time, + ) + } + + fn clamp_constraints(&mut self) { + // TODO(holmer): We should make sure the default bitrates are set to 10 kbps, + // and that we don't try to set the min bitrate to 0 from any applications. + // The congestion controller should allow a min bitrate of 0. + self.min_data_rate = std::cmp::max(self.min_target_rate, CONGESTION_CONTROLLER_MIN_BITRATE); + if self.use_min_allocatable_as_lower_bound { + self.min_data_rate = + std::cmp::max(self.min_data_rate, self.min_total_allocated_bitrate); + } + if self.max_data_rate < self.min_data_rate { + tracing::warn!( + max = ?self.max_data_rate, + min = ?self.min_data_rate, + "max bitrate smaller than min bitrate" + ); + self.max_data_rate = self.min_data_rate; + } + if let Some(starting_rate) = self.starting_rate + && starting_rate < self.min_data_rate + { + tracing::warn!( + start = ?starting_rate, + min = ?self.min_data_rate, + "start bitrate smaller than min bitrate" + ); + self.starting_rate = Some(self.min_data_rate); + } + } + + fn maybe_trigger_on_network_changed( + &mut self, + update: &mut NetworkControlUpdate, + at_time: Timestamp, + ) { + let round_trip_time: TimeDelta = self.bandwidth_estimation.round_trip_time(); + let loss_based_target_rate: DataRate = self.bandwidth_estimation.target_rate(); + let loss_based_state: LossBasedState = self.bandwidth_estimation.loss_based_state(); + let mut pushback_target_rate: DataRate = loss_based_target_rate; + + if let Some(congestion_window_pushback_controller) = + &mut self.congestion_window_pushback_controller + { + let mut pushback_rate: i64 = congestion_window_pushback_controller + .update_target_bitrate(loss_based_target_rate.bps() as _) + as _; + pushback_rate = self + .bandwidth_estimation + .get_min_bitrate() + .max(pushback_rate); + pushback_target_rate = DataRate::from_bits_per_sec(pushback_rate); + } + + let is_bandwidth_limited = self + .alr_detector + .get_application_limited_region_start_time() + .is_none(); + + if (loss_based_target_rate != self.last_loss_based_target_rate) + || (loss_based_state != self.last_loss_base_state) + || (round_trip_time != self.last_estimated_round_trip_time) + || (pushback_target_rate != self.last_pushback_target_rate) + || (is_bandwidth_limited != self.last_is_bandwidth_limited) + { + self.last_loss_based_target_rate = loss_based_target_rate; + self.last_pushback_target_rate = pushback_target_rate; + self.last_estimated_round_trip_time = round_trip_time; + self.last_loss_base_state = loss_based_state; + self.last_is_bandwidth_limited = is_bandwidth_limited; + + self.alr_detector + .set_estimated_bitrate(loss_based_target_rate); + + // With DropFrame, the target stays at the loss-based rate and the pushback reaches + // the encoder as `cwnd_reduce_ratio`, a frame drop ratio, which no consumer reads. + let target_rate = if self + .rate_control_settings + .use_congestion_window_drop_frame_only() + { + loss_based_target_rate + } else { + pushback_target_rate + }; + update.target_rate = Some(TargetTransferRate { target_rate }); + + let mut probes = self.probe_controller.set_estimated_bitrate( + loss_based_target_rate, + get_bandwidth_limited_cause( + self.bandwidth_estimation.loss_based_state(), + self.bandwidth_estimation.is_rtt_above_limit(), + self.delay_based_bwe.last_state(), + ), + at_time, + ); + update.probe_cluster_configs.append(&mut probes); + update.pacer_config = Some(self.get_pacing_rates()); + tracing::trace!( + at = at_time.ms(), + pushback_target_bps = self.last_pushback_target_rate.bps(), + estimate_bps = loss_based_target_rate.bps(), + "bwe" + ); + } + } + + fn get_pacing_rates(&self) -> PacerConfig { + // Pacing rate is based on target rate before congestion window pushback, + // because we don't want to build queues in the pacer when pushback occurs. + let pacing_rate = self + .min_total_allocated_bitrate + .max(self.last_loss_based_target_rate) + * self + .pacing_factor + .unwrap_or(if self.first_transport_feedback_received { + Self::DEFAULT_PACE_MULTIPLIER_WITH_SEND_SIDE_BWE + } else { + Self::DEFAULT_PACE_MULTIPLIER + }); + PacerConfig::create(pacing_rate, self.pacing_time_window) + } + + fn update_congestion_window_size(&mut self) { + let min_feedback_max_rtt: TimeDelta = + TimeDelta::from_millis(*self.feedback_max_rtts.iter().min().expect("a feedback RTT")); + + const MIN_CWND: DataSize = DataSize::from_bytes(2 * 1500); + let time_window: TimeDelta = min_feedback_max_rtt + + TimeDelta::from_millis( + self.rate_control_settings + .get_congestion_window_additional_time_ms(), + ); + + let mut data_window: DataSize = self.last_loss_based_target_rate * time_window; + if let Some(current_data_window) = self.current_data_window { + data_window = std::cmp::max(MIN_CWND, (data_window + current_data_window) / 2); + } else { + data_window = std::cmp::max(MIN_CWND, data_window); + } + self.current_data_window = Some(data_window); + } +} + +fn get_bandwidth_limited_cause( + loss_based_state: LossBasedState, + is_rtt_above_limit: bool, + bandwidth_usage: BandwidthUsage, +) -> BandwidthLimitedCause { + if bandwidth_usage == BandwidthUsage::Overusing || bandwidth_usage == BandwidthUsage::Underusing + { + return BandwidthLimitedCause::DelayBasedLimitedDelayIncreased; + } else if is_rtt_above_limit { + return BandwidthLimitedCause::RttBasedBackOffHighRtt; + } + match loss_based_state { + // Probes may not be sent in this state. + LossBasedState::Decreasing => BandwidthLimitedCause::LossLimitedBwe, + // Probes may not be sent in this state. + LossBasedState::IncreaseUsingPadding => BandwidthLimitedCause::LossLimitedBwe, + // Probes may be sent in this state. + LossBasedState::Increasing => BandwidthLimitedCause::LossLimitedBweIncreasing, + LossBasedState::DelayBasedEstimate => BandwidthLimitedCause::DelayBasedLimited, + } +} + +#[cfg(test)] +mod test { + use crate::gcc::api::transport::PacketResult; + + use super::*; + + const INITIAL_BITRATE_KBPS: i64 = 60; + const INITIAL_BITRATE: DataRate = DataRate::from_kilobits_per_sec(INITIAL_BITRATE_KBPS); + const DEFAULT_PACING_RATE: f64 = 2.5; + const DEFAULT_PACING_RATE_SEND_SIDE_BWE: f64 = 1.1; + + fn create_packet_result( + arrival_time: Timestamp, + send_time: Timestamp, + payload_size: i64, + pacing_info: PacedPacketInfo, + ) -> PacketResult { + PacketResult { + sent_packet: SentPacket { + send_time, + size: DataSize::from_bytes(payload_size), + pacing_info, + ..Default::default() + }, + receive_time: arrival_time, + ambiguous_receive_time: false, + } + } + + // Simulate sending packets and receiving transport feedback during + // `runtime_ms`, then return the final target birate. + fn packet_transmission_and_feedback_block( + controller: &mut GoogCcNetworkController, + runtime_ms: i64, + delay: i64, + current_time: &mut Timestamp, + ) -> Option { + let mut target_bitrate: Option = None; + let mut delay_buildup: i64 = 0; + let start_time_ms: i64 = current_time.ms(); + while current_time.ms() - start_time_ms < runtime_ms { + const PAYLOAD_SIZE: i64 = 1000; + let packet: PacketResult = create_packet_result( + *current_time + TimeDelta::from_millis(delay_buildup), + *current_time, + PAYLOAD_SIZE, + PacedPacketInfo::default(), + ); + delay_buildup += delay; + let update = controller.on_sent_packet(packet.sent_packet); + if let Some(target_rate) = update.target_rate { + target_bitrate = Some(target_rate.target_rate); + } + let feedback = TransportPacketsFeedback { + feedback_time: packet.receive_time, + packet_feedbacks: vec![packet], + ..Default::default() + }; + let update = controller.on_transport_packets_feedback(&feedback); + if let Some(target_rate) = update.target_rate { + target_bitrate = Some(target_rate.target_rate); + } + *current_time += TimeDelta::from_millis(50); + let update = controller.on_process_interval(ProcessInterval { + at_time: *current_time, + ..Default::default() + }); + if let Some(target_rate) = update.target_rate { + target_bitrate = Some(target_rate.target_rate); + } + } + target_bitrate + } + + // Create transport packets feedback with a built-up delay. + fn create_transport_packets_feedback( + per_packet_network_delay: TimeDelta, + one_way_delay: TimeDelta, + send_time: Timestamp, + ) -> TransportPacketsFeedback { + let mut delay_buildup: TimeDelta = one_way_delay; + const FEEDBACK_SIZE: i64 = 3; + const PAYLOAD_SIZE: i64 = 1000; + let mut feedback = TransportPacketsFeedback::default(); + for _ in 0..FEEDBACK_SIZE { + let packet: PacketResult = create_packet_result( + /*arrival_time=*/ send_time + delay_buildup, + send_time, + PAYLOAD_SIZE, + PacedPacketInfo::default(), + ); + delay_buildup += per_packet_network_delay; + feedback.feedback_time = packet.receive_time + one_way_delay; + feedback.packet_feedbacks.push(packet); + } + feedback + } + + fn create_controller(stream_based_config: StreamsConfig) -> GoogCcNetworkController { + GoogCcNetworkController::new(NetworkControllerConfig { + constraints: TargetRateConstraints { + at_time: Timestamp::zero(), + min_data_rate: Some(DataRate::from_kilobits_per_sec(0)), + max_data_rate: Some(DataRate::from_kilobits_per_sec(5 * INITIAL_BITRATE_KBPS)), + starting_rate: Some(DataRate::from_kilobits_per_sec(INITIAL_BITRATE_KBPS)), + }, + stream_based_config, + default_pacing_time_window: PacerConfig::DEFAULT_TIME_INTERVAL, + ..Default::default() + }) + } + + fn available(controller: &mut GoogCcNetworkController, at_time: Timestamp) { + let _ = controller.on_network_availability(NetworkAvailability { + at_time, + network_available: true, + }); + } + + #[test] + fn initialize_target_rate_on_first_process_interval_after_network_available() { + let mut controller = create_controller(StreamsConfig::default()); + available(&mut controller, Timestamp::from_millis(123456)); + let update = controller.on_process_interval(ProcessInterval { + at_time: Timestamp::from_millis(123456), + ..Default::default() + }); + + assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE); + assert_eq!( + update.pacer_config.unwrap().data_rate(), + INITIAL_BITRATE * DEFAULT_PACING_RATE + ); + assert_eq!( + update.probe_cluster_configs[0].target_data_rate, + INITIAL_BITRATE * 3 + ); + assert_eq!( + update.probe_cluster_configs[1].target_data_rate, + INITIAL_BITRATE * 5 + ); + } + + #[test] + fn change_default_pacing_factor_after_first_feedback() { + let mut controller = create_controller(StreamsConfig::default()); + available(&mut controller, Timestamp::from_millis(123456)); + let update = controller.on_process_interval(ProcessInterval { + at_time: Timestamp::from_millis(123456), + ..Default::default() + }); + assert_eq!( + update.pacer_config.unwrap().data_rate(), + INITIAL_BITRATE * DEFAULT_PACING_RATE + ); + + let feedback_time = Timestamp::from_millis(123457); + let feedback = TransportPacketsFeedback { + feedback_time, + packet_feedbacks: vec![create_packet_result( + feedback_time, + feedback_time, + 100, + PacedPacketInfo::default(), + )], + ..Default::default() + }; + let update = controller.on_transport_packets_feedback(&feedback); + let pacer_config = update.pacer_config.expect("a pacer config"); + assert!(pacer_config.data_rate() >= INITIAL_BITRATE * DEFAULT_PACING_RATE_SEND_SIDE_BWE); + assert!( + pacer_config.data_rate() + < (INITIAL_BITRATE + DataRate::from_kilobits_per_sec(5)) + * DEFAULT_PACING_RATE_SEND_SIDE_BWE + ); + } + + #[test] + fn respects_configured_pacing_factor() { + let mut controller = create_controller(StreamsConfig { + pacing_factor: Some(10.0), + ..Default::default() + }); + available(&mut controller, Timestamp::from_millis(123456)); + let update = controller.on_process_interval(ProcessInterval { + at_time: Timestamp::from_millis(123456), + ..Default::default() + }); + assert_eq!( + update.pacer_config.unwrap().data_rate(), + INITIAL_BITRATE * 10.0 + ); + + let feedback_time = Timestamp::from_millis(123457); + let feedback = TransportPacketsFeedback { + feedback_time, + packet_feedbacks: vec![create_packet_result( + feedback_time, + feedback_time, + 100, + PacedPacketInfo::default(), + )], + ..Default::default() + }; + let update = controller.on_transport_packets_feedback(&feedback); + let pacer_config = update.pacer_config.expect("a pacer config"); + assert!(pacer_config.data_rate() >= INITIAL_BITRATE * 10.0); + assert!( + pacer_config.data_rate() + < (INITIAL_BITRATE + DataRate::from_kilobits_per_sec(5)) * 10.0 + ); + } + + #[test] + fn updates_delay_based_estimate() { + let mut controller = create_controller(StreamsConfig::default()); + const RUN_TIME_MS: i64 = 6000; + let mut current_time: Timestamp = Timestamp::from_millis(123); + available(&mut controller, current_time); + + // The test must run and insert packets/feedback long enough that the + // BWE computes a valid estimate. This is first done in an environment which + // simulates no bandwidth limitation, and therefore not built-up delay. + let target_bitrate_before_delay = packet_transmission_and_feedback_block( + &mut controller, + RUN_TIME_MS, + 0, + &mut current_time, + ) + .expect("an estimate"); + + // Repeat, but this time with a building delay, and make sure that the + // estimation is adjusted downwards. + let target_bitrate_after_delay = packet_transmission_and_feedback_block( + &mut controller, + RUN_TIME_MS, + 50, + &mut current_time, + ) + .expect("an estimate"); + assert!(target_bitrate_after_delay < target_bitrate_before_delay); + } + + #[test] + fn calculates_rtt_from_transport_feedback() { + let mut controller = create_controller(StreamsConfig::default()); + let current_time: Timestamp = Timestamp::from_millis(123); + let one_way_delay: TimeDelta = TimeDelta::from_millis(10); + + let feedback: TransportPacketsFeedback = create_transport_packets_feedback( + /*per_packet_network_delay=*/ TimeDelta::from_millis(50), + one_way_delay, + /*send_time=*/ current_time, + ); + controller.on_transport_packets_feedback(&feedback); + // The propagation RTT discounts each packet's wait behind the newest one received. + assert!(!controller.bandwidth_estimation.is_rtt_above_limit()); + assert_eq!( + controller.feedback_max_rtts.back(), + Some(&(2 * one_way_delay.ms() + 100)) + ); + } + + #[test] + fn triggers_target_rate_update_when_bandwidth_limited_changes() { + let mut controller = create_controller(StreamsConfig::default()); + let mut current_time = Timestamp::from_millis(123456); + available(&mut controller, current_time); + let update = controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + assert!(update.target_rate.is_some()); + assert!(controller.last_is_bandwidth_limited); + + // Send packet to initialize ALR detector + let mut sent_packet = SentPacket { + send_time: current_time, + size: DataSize::from_bytes(1000), + ..Default::default() + }; + let _ = controller.on_sent_packet(sent_packet); + + // Wait 10 seconds without sending any packets (this will make budget ratio + // accumulate and enter ALR) + current_time += TimeDelta::from_seconds(10); + // Send a tiny packet to trigger AlrDetector state change evaluation + sent_packet.send_time = current_time; + sent_packet.size = DataSize::from_bytes(1); + let update = controller.on_sent_packet(sent_packet); + let alr_detected = update.target_rate.is_some() + || controller + .on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }) + .target_rate + .is_some(); + assert!(alr_detected); + assert!(!controller.last_is_bandwidth_limited); + + // Now send a large burst of packets to leave ALR. + let mut bandwidth_limited_detected = false; + for _ in 0..50 { + current_time += TimeDelta::from_millis(1); + sent_packet.send_time = current_time; + sent_packet.size = DataSize::from_bytes(1000); + let update = controller.on_sent_packet(sent_packet); + bandwidth_limited_detected |= update.target_rate.is_some(); + } + if !bandwidth_limited_detected { + bandwidth_limited_detected = controller + .on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }) + .target_rate + .is_some(); + } + assert!(bandwidth_limited_detected); + assert!(controller.last_is_bandwidth_limited); + } +} diff --git a/rs/moq-congestion/src/gcc/inter_arrival_delta.rs b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs new file mode 100644 index 00000000000..11026b60b1e --- /dev/null +++ b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs @@ -0,0 +1,191 @@ +/* + * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::api::units::{TimeDelta, Timestamp}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct SendTimeGroup { + size: usize, + first_send_time: Timestamp, + send_time: Timestamp, + first_arrival: Timestamp, + complete_time: Timestamp, + last_system_time: Timestamp, +} + +impl Default for SendTimeGroup { + fn default() -> Self { + Self { + size: 0, + first_send_time: Timestamp::minus_infinity(), + send_time: Timestamp::minus_infinity(), + first_arrival: Timestamp::minus_infinity(), + complete_time: Timestamp::minus_infinity(), + last_system_time: Timestamp::minus_infinity(), + } + } +} +impl SendTimeGroup { + pub fn is_first_packet(&self) -> bool { + self.complete_time.is_infinite() + } +} + +// Helper class to compute the inter-arrival time delta and the size delta +// between two send bursts. This code is branched from +// modules/remote_bitrate_estimator/inter_arrival. +#[derive(Clone)] +pub struct InterArrivalDelta { + send_time_group_length: TimeDelta, + current_timestamp_group: SendTimeGroup, + prev_timestamp_group: SendTimeGroup, + num_consecutive_reordered_packets: i64, +} + +impl InterArrivalDelta { + const REORDERED_RESET_THRESHOLD: i64 = 3; + const ARRIVAL_TIME_OFFSET_THRESHOLD: TimeDelta = TimeDelta::from_seconds(3); + const BURST_DELTA_THRESHOLD: TimeDelta = TimeDelta::from_millis(5); + const MAX_BURST_DURATION: TimeDelta = TimeDelta::from_millis(100); + + pub fn new(send_time_group_length: TimeDelta) -> Self { + Self { + send_time_group_length, + current_timestamp_group: SendTimeGroup::default(), + prev_timestamp_group: SendTimeGroup::default(), + num_consecutive_reordered_packets: 0, + } + } + + // This function returns true if a delta was computed, or false if the current + // group is still incomplete or if only one group has been completed. + // `send_time` is the send time. + // `arrival_time` is the time at which the packet arrived. + // `packet_size` is the size of the packet. + // `timestamp_delta` (output) is the computed send time delta. + // `arrival_time_delta` (output) is the computed arrival-time delta. + // `packet_size_delta` (output) is the computed size delta. + #[allow( + clippy::too_many_arguments, + reason = "upstream's ComputeDeltas, with its outputs as arguments" + )] + pub fn compute_deltas( + &mut self, + send_time: Timestamp, + arrival_time: Timestamp, + system_time: Timestamp, + packet_size: usize, + send_time_delta: &mut TimeDelta, + arrival_time_delta: &mut TimeDelta, + packet_size_delta: &mut i64, + ) -> bool { + let mut calculated_deltas: bool = false; + if self.current_timestamp_group.is_first_packet() { + // We don't have enough data to update the filter, so we store it until we + // have two frames of data to process. + self.current_timestamp_group.send_time = send_time; + self.current_timestamp_group.first_send_time = send_time; + self.current_timestamp_group.first_arrival = arrival_time; + } else if self.current_timestamp_group.first_send_time > send_time { + // Reordered packet. + return false; + } else if self.new_timestamp_group(arrival_time, send_time) { + // First packet of a later send burst, the previous packets sample is ready. + if self.prev_timestamp_group.complete_time.is_finite() { + *send_time_delta = + self.current_timestamp_group.send_time - self.prev_timestamp_group.send_time; + *arrival_time_delta = self.current_timestamp_group.complete_time + - self.prev_timestamp_group.complete_time; + + let system_time_delta: TimeDelta = self.current_timestamp_group.last_system_time + - self.prev_timestamp_group.last_system_time; + + if *arrival_time_delta - system_time_delta >= Self::ARRIVAL_TIME_OFFSET_THRESHOLD { + tracing::warn!( + "The arrival time clock offset has changed (diff = {} ms), resetting.", + arrival_time_delta.ms() - system_time_delta.ms() + ); + self.reset(); + return false; + } + if *arrival_time_delta < TimeDelta::zero() { + // The group of packets has been reordered since receiving its local + // arrival timestamp. + self.num_consecutive_reordered_packets += 1; + if self.num_consecutive_reordered_packets >= Self::REORDERED_RESET_THRESHOLD { + tracing::warn!( + "Packets between send burst arrived out of order, resetting: arrival_time_delta_ms={}, send_time_delta_ms={}", + arrival_time_delta.ms(), + send_time_delta.ms() + ); + self.reset(); + } + return false; + } else { + self.num_consecutive_reordered_packets = 0; + } + *packet_size_delta = (self.current_timestamp_group.size) as i64 + - (self.prev_timestamp_group.size) as i64; + calculated_deltas = true; + } + self.prev_timestamp_group = self.current_timestamp_group; + // The new timestamp is now the current frame. + self.current_timestamp_group.first_send_time = send_time; + self.current_timestamp_group.send_time = send_time; + self.current_timestamp_group.first_arrival = arrival_time; + self.current_timestamp_group.size = 0; + } else { + self.current_timestamp_group.send_time = + std::cmp::max(self.current_timestamp_group.send_time, send_time); + } + // Accumulate the frame size. + self.current_timestamp_group.size += packet_size; + self.current_timestamp_group.complete_time = arrival_time; + self.current_timestamp_group.last_system_time = system_time; + + calculated_deltas + } + + // Returns true if the last packet was the end of the current batch and the + // packet with `send_time` is the first of a new batch. + fn new_timestamp_group(&self, arrival_time: Timestamp, send_time: Timestamp) -> bool { + if self.current_timestamp_group.is_first_packet() + || self.belongs_to_burst(arrival_time, send_time) + { + false + } else { + send_time - self.current_timestamp_group.first_send_time > self.send_time_group_length + } + } + + fn belongs_to_burst(&self, arrival_time: Timestamp, send_time: Timestamp) -> bool { + assert!(self.current_timestamp_group.complete_time.is_finite()); + let arrival_time_delta: TimeDelta = + arrival_time - self.current_timestamp_group.complete_time; + let send_time_delta: TimeDelta = send_time - self.current_timestamp_group.send_time; + if send_time_delta.is_zero() { + return true; + } + let propagation_delta: TimeDelta = arrival_time_delta - send_time_delta; + if propagation_delta < TimeDelta::zero() + && arrival_time_delta <= Self::BURST_DELTA_THRESHOLD + && arrival_time - self.current_timestamp_group.first_arrival < Self::MAX_BURST_DURATION + { + return true; + } + false + } + + fn reset(&mut self) { + self.num_consecutive_reordered_packets = 0; + self.current_timestamp_group = SendTimeGroup::default(); + self.prev_timestamp_group = SendTimeGroup::default(); + } +} diff --git a/rs/moq-congestion/src/gcc/link_capacity_estimator.rs b/rs/moq-congestion/src/gcc/link_capacity_estimator.rs new file mode 100644 index 00000000000..73547343c04 --- /dev/null +++ b/rs/moq-congestion/src/gcc/link_capacity_estimator.rs @@ -0,0 +1,100 @@ +/* + * Copyright 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::api::units::DataRate; + +#[derive(Clone)] +pub struct LinkCapacityEstimator { + estimate_kbps: Option, + deviation_kbps: f64, +} + +impl Default for LinkCapacityEstimator { + fn default() -> Self { + Self::new() + } +} + +impl LinkCapacityEstimator { + pub fn new() -> Self { + Self { + estimate_kbps: None, + deviation_kbps: 0.4, + } + } + pub fn upper_bound(&self) -> DataRate { + if let Some(estimate_kbps) = self.estimate_kbps { + DataRate::from_kilobits_per_sec_float( + estimate_kbps + 3.0 * self.deviation_estimate_kbps(estimate_kbps), + ) + } else { + DataRate::infinity() + } + } + + pub fn lower_bound(&self) -> DataRate { + if let Some(estimate_kbps) = self.estimate_kbps { + DataRate::from_kilobits_per_sec_float( + (estimate_kbps - 3.0 * self.deviation_estimate_kbps(estimate_kbps)).max(0.0), + ) + } else { + DataRate::zero() + } + } + + pub fn reset(&mut self) { + self.estimate_kbps.take(); + } + + pub fn on_overuse_detected(&mut self, acknowledged_rate: DataRate) { + self.update(acknowledged_rate, 0.05); + } + + /* unused + pub fn on_probe_rate(&mut self, probe_rate: DataRate) { + self.update(probe_rate, 0.5); + } + */ + + pub fn has_estimate(&self) -> bool { + self.estimate_kbps.is_some() + } + + pub fn estimate(&self) -> DataRate { + DataRate::from_kilobits_per_sec_float(self.estimate_kbps.unwrap_or_default()) + } + + fn update(&mut self, capacity_sample: DataRate, alpha: f64) { + let sample_kbps: f64 = capacity_sample.kbps_float(); + let estimate_kbps = if let Some(estimate_kbps) = self.estimate_kbps { + (1.0 - alpha) * estimate_kbps + alpha * sample_kbps + } else { + sample_kbps + }; + + // Estimate the variance of the link capacity estimate and normalize the + // variance with the link capacity estimate. + let norm: f64 = estimate_kbps.max(1.0); + let error_kbps: f64 = estimate_kbps - sample_kbps; + self.deviation_kbps = + (1.0 - alpha) * self.deviation_kbps + alpha * error_kbps * error_kbps / norm; + // 0.4 ~= 14 kbit/s at 500 kbit/s + // 2.5f ~= 35 kbit/s at 500 kbit/s + self.deviation_kbps = self.deviation_kbps.clamp(0.4, 2.5); + self.estimate_kbps = Some(estimate_kbps); + } + + fn deviation_estimate_kbps(&self, estimate_kbps: f64) -> f64 { + // Calculate the max bit rate std dev given the normalized + // variance and the current throughput bitrate. The standard deviation will + // only be used if self.estimate_kbps has a value. + (self.deviation_kbps * estimate_kbps).sqrt() + } +} diff --git a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs new file mode 100644 index 00000000000..92f971fa3a0 --- /dev/null +++ b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs @@ -0,0 +1,3683 @@ +/* + * Copyright 2021 The WebRTC project authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::{ + api::{ + transport::PacketResult, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE, +}; + +// State of the loss based estimate, which can be either increasing/decreasing +// when network is loss limited, or equal to the delay based estimate. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum LossBasedState { + Increasing = 0, + // TODO(bugs.webrtc.org/12707): Remove one of the increasing states once we + // have decided if padding is usefull for ramping up when BWE is loss + // limited. + IncreaseUsingPadding = 1, + Decreasing = 2, + DelayBasedEstimate = 3, +} +#[derive(Clone, Debug, Copy)] +pub struct LossBasedBweV2Result { + pub bandwidth_estimate: DataRate, + // State is used by goog_cc, which later sends probe requests to probe + // controller if state is Increasing. + pub state: LossBasedState, +} + +impl Default for LossBasedBweV2Result { + fn default() -> Self { + Self { + bandwidth_estimate: DataRate::zero(), + state: LossBasedState::DelayBasedEstimate, + } + } +} + +#[derive(Clone, Debug)] +struct ChannelParameters { + pub inherent_loss: f64, + pub loss_limited_bandwidth: DataRate, +} + +impl Default for ChannelParameters { + fn default() -> Self { + Self { + inherent_loss: 0.0, + loss_limited_bandwidth: DataRate::minus_infinity(), + } + } +} + +#[derive(Clone, Debug)] +pub struct LossBasedBweV2Config { + pub enabled: bool, + pub bw_rampup_upper_bound_factor: f64, + pub bw_rampup_upper_bound_in_hold_factor: f64, + pub bw_rampup_upper_bound_hold_threshold: f64, + pub bw_rampup_accel_max_factor: f64, + pub bw_rampup_accel_maxout_time: TimeDelta, + pub candidate_factors: Vec, + pub higher_bw_bias_factor: f64, + pub higher_log_bw_bias_factor: f64, + pub inherent_loss_lower_bound: f64, + pub loss_threshold_of_high_bandwidth_preference: f64, + pub bandwidth_preference_smoothing_factor: f64, + pub inherent_loss_upper_bound_bw_balance: DataRate, + pub inherent_loss_upper_bound_offset: f64, + pub initial_inherent_loss_estimate: f64, + pub newton_iterations: usize, + pub newton_step_size: f64, + pub acked_rate_candidate: bool, + pub delay_based_candidate: bool, + pub upper_bound_candidate_in_alr: bool, + pub observation_duration_lower_bound: TimeDelta, + pub observation_window_size: usize, + pub sending_rate_smoothing_factor: f64, + pub instant_upper_bound_temporal_weight_factor: f64, + pub instant_upper_bound_bw_balance: DataRate, + pub instant_upper_bound_loss_offset: f64, + pub temporal_weight_factor: f64, + pub bw_backoff_lower_bound_factor: f64, + pub max_increase_factor: f64, + pub delayed_increase_window: TimeDelta, + pub not_increase_if_inherent_loss_less_than_average_loss: bool, + pub not_use_acked_rate_in_alr: bool, + pub use_in_start_phase: bool, + pub min_num_observations: usize, + pub lower_bound_by_acked_rate_factor: f64, + pub hold_duration_factor: f64, + pub use_byte_loss_rate: bool, + pub padding_duration: TimeDelta, + pub bound_best_candidate: bool, + pub median_sending_rate_factor: f64, +} + +impl LossBasedBweV2Config { + pub fn is_valid(&self) -> bool { + let mut valid = true; + + if self.bw_rampup_upper_bound_factor <= 1.0 { + tracing::warn!( + "The bandwidth rampup upper bound factor must be greater than 1: {}", + self.bw_rampup_upper_bound_factor + ); + valid = false; + } + if self.bw_rampup_upper_bound_in_hold_factor <= 1.0 { + tracing::warn!( + "The bandwidth rampup upper bound factor in hold must be greater than 1: {}", + self.bw_rampup_upper_bound_in_hold_factor + ); + valid = false; + } + if self.bw_rampup_upper_bound_hold_threshold < 0.0 { + tracing::warn!( + "The bandwidth rampup hold threshold must be non-negative: {}", + self.bw_rampup_upper_bound_hold_threshold + ); + valid = false; + } + if self.bw_rampup_accel_max_factor < 0.0 { + tracing::warn!( + "The rampup acceleration max factor must be non-negative: {}", + self.bw_rampup_accel_max_factor + ); + valid = false; + } + if self.bw_rampup_accel_maxout_time <= TimeDelta::zero() { + tracing::warn!( + "The rampup acceleration maxout time must be above zero: {:?}", + self.bw_rampup_accel_maxout_time + ); + valid = false; + } + for candidate_factor in self.candidate_factors.iter() { + if *candidate_factor <= 0.0 { + tracing::warn!( + "All candidate factors must be greater than zero: {}", + candidate_factor + ); + valid = false; + } + } + + // Ensure that the configuration allows generation of at least one candidate + // other than the current estimate. + if !self.acked_rate_candidate + && !self.delay_based_candidate + && !self.candidate_factors.iter().any(|cf| *cf != 1.0) + { + tracing::warn!( + "The configuration does not allow generating candidates. Specify a candidate factor other than 1.0, allow the acknowledged rate to be a candidate, and/or allow the delay based estimate to be a candidate." + ); + valid = false; + } + + if self.higher_bw_bias_factor < 0.0 { + tracing::warn!( + "The higher bandwidth bias factor must be non-negative: {}", + self.higher_bw_bias_factor + ); + valid = false; + } + if self.inherent_loss_lower_bound < 0.0 || self.inherent_loss_lower_bound >= 1.0 { + tracing::warn!( + "The inherent loss lower bound must be in [0, 1): {}", + self.inherent_loss_lower_bound + ); + valid = false; + } + if self.loss_threshold_of_high_bandwidth_preference < 0.0 + || self.loss_threshold_of_high_bandwidth_preference >= 1.0 + { + tracing::warn!( + "The loss threshold of high bandwidth preference must be in [0, 1): {}", + self.loss_threshold_of_high_bandwidth_preference + ); + valid = false; + } + if self.bandwidth_preference_smoothing_factor <= 0.0 + || self.bandwidth_preference_smoothing_factor > 1.0 + { + tracing::warn!( + "The bandwidth preference smoothing factor must be in (0, 1]: {}", + self.bandwidth_preference_smoothing_factor + ); + valid = false; + } + if self.inherent_loss_upper_bound_bw_balance <= DataRate::zero() { + tracing::warn!( + "The inherent loss upper bound bandwidth balance must be positive: {:?}", + self.inherent_loss_upper_bound_bw_balance + ); + valid = false; + } + if self.inherent_loss_upper_bound_offset < self.inherent_loss_lower_bound + || self.inherent_loss_upper_bound_offset >= 1.0 + { + tracing::warn!( + "The inherent loss upper bound must be greater than or equal to the inherent loss lower bound, which is {}, and less than 1: {}", + self.inherent_loss_lower_bound, + self.inherent_loss_upper_bound_offset + ); + valid = false; + } + if self.initial_inherent_loss_estimate < 0.0 || self.initial_inherent_loss_estimate >= 1.0 { + tracing::warn!( + "The initial inherent loss estimate must be in [0, 1): {}", + self.initial_inherent_loss_estimate + ); + valid = false; + } + if self.newton_iterations == 0 { + tracing::warn!( + "The number of Newton iterations must be positive: {}", + self.newton_iterations + ); + valid = false; + } + if self.newton_step_size <= 0.0 { + tracing::warn!( + "The Newton step size must be positive: {}", + self.newton_step_size + ); + valid = false; + } + if self.observation_duration_lower_bound <= TimeDelta::zero() { + tracing::warn!( + "The observation duration lower bound must be positive: {}", + self.observation_duration_lower_bound.ms() + ); + valid = false; + } + if self.observation_window_size < 2 { + tracing::warn!( + "The observation window size must be at least 2: {}", + self.observation_window_size + ); + valid = false; + } + if self.sending_rate_smoothing_factor < 0.0 || self.sending_rate_smoothing_factor >= 1.0 { + tracing::warn!( + "The sending rate smoothing factor must be in [0, 1): {}", + self.sending_rate_smoothing_factor + ); + valid = false; + } + if self.instant_upper_bound_temporal_weight_factor <= 0.0 + || self.instant_upper_bound_temporal_weight_factor > 1.0 + { + tracing::warn!( + "The instant upper bound temporal weight factor must be in (0, 1]: {}", + self.instant_upper_bound_temporal_weight_factor + ); + valid = false; + } + if self.instant_upper_bound_bw_balance <= DataRate::zero() { + tracing::warn!( + "The instant upper bound bandwidth balance must be positive: {:?}", + self.instant_upper_bound_bw_balance + ); + valid = false; + } + if self.instant_upper_bound_loss_offset < 0.0 || self.instant_upper_bound_loss_offset >= 1.0 + { + tracing::warn!( + "The instant upper bound loss offset must be in [0, 1): {}", + self.instant_upper_bound_loss_offset + ); + valid = false; + } + if self.temporal_weight_factor <= 0.0 || self.temporal_weight_factor > 1.0 { + tracing::warn!( + "The temporal weight factor must be in (0, 1]: {}", + self.temporal_weight_factor + ); + valid = false; + } + if self.bw_backoff_lower_bound_factor > 1.0 { + tracing::warn!( + "The bandwidth backoff lower bound factor must not be greater than 1: {}", + self.bw_backoff_lower_bound_factor + ); + valid = false; + } + if self.max_increase_factor <= 0.0 { + tracing::warn!( + "The maximum increase factor must be positive: {}", + self.max_increase_factor + ); + valid = false; + } + if self.delayed_increase_window <= TimeDelta::zero() { + tracing::warn!( + "The delayed increase window must be positive: {:?}", + self.delayed_increase_window + ); + valid = false; + } + if self.min_num_observations == 0 { + tracing::warn!( + "The min number of observations must be positive: {}", + self.min_num_observations + ); + valid = false; + } + if self.lower_bound_by_acked_rate_factor < 0.0 { + tracing::warn!( + "The estimate lower bound by acknowledged rate factor must be non-negative: {}", + self.lower_bound_by_acked_rate_factor + ); + valid = false; + } + valid + } +} + +impl Default for LossBasedBweV2Config { + fn default() -> Self { + Self { + enabled: true, // Enabled + bw_rampup_upper_bound_factor: 1.5, // BwRampupUpperBoundFactor + bw_rampup_upper_bound_in_hold_factor: 1.2, // BwRampupUpperBoundInHoldFactor + bw_rampup_upper_bound_hold_threshold: 1.3, // BwRampupUpperBoundHoldThreshold + bw_rampup_accel_max_factor: 0.0, // BwRampupAccelMaxFactor + bw_rampup_accel_maxout_time: TimeDelta::from_seconds(60), // BwRampupAccelMaxoutTime + candidate_factors: vec![1.02, 1.0, 0.95], // CandidateFactors + higher_bw_bias_factor: 0.0002, // HigherBwBiasFactor + higher_log_bw_bias_factor: 0.02, // HigherLogBwBiasFactor + inherent_loss_lower_bound: 1.0e-3, // InherentLossLowerBound + loss_threshold_of_high_bandwidth_preference: 0.2, // LossThresholdOfHighBandwidthPreference + bandwidth_preference_smoothing_factor: 0.002, // BandwidthPreferenceSmoothingFactor + inherent_loss_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(100), // InherentLossUpperBoundBwBalance + inherent_loss_upper_bound_offset: 0.05, // InherentLossUpperBoundOffset + initial_inherent_loss_estimate: 0.01, // InitialInherentLossEstimate + newton_iterations: 1, // NewtonIterations + newton_step_size: 0.75, // NewtonStepSize + acked_rate_candidate: true, // AckedRateCandidate + delay_based_candidate: true, // DelayBasedCandidate + upper_bound_candidate_in_alr: false, // UpperBoundCandidateInAlr + observation_duration_lower_bound: TimeDelta::from_millis(250), // ObservationDurationLowerBound + observation_window_size: 15, // ObservationWindowSize + sending_rate_smoothing_factor: 0.0, // SendingRateSmoothingFactor + instant_upper_bound_temporal_weight_factor: 0.9, // InstantUpperBoundTemporalWeightFactor + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(100), // InstantUpperBoundBwBalance + instant_upper_bound_loss_offset: 0.05, // InstantUpperBoundLossOffset + temporal_weight_factor: 0.9, // TemporalWeightFactor + bw_backoff_lower_bound_factor: 1.0, // BwBackoffLowerBoundFactor + max_increase_factor: 1.3, // MaxIncreaseFactor + delayed_increase_window: TimeDelta::from_millis(300), // DelayedIncreaseWindow + not_increase_if_inherent_loss_less_than_average_loss: true, // NotIncreaseIfInherentLossLessThanAverageLoss + not_use_acked_rate_in_alr: true, // NotUseAckedRateInAlr + use_in_start_phase: true, // UseInStartPhase + min_num_observations: 3, // MinNumObservations + lower_bound_by_acked_rate_factor: 1.0, // LowerBoundByAckedRateFactor + hold_duration_factor: 2.0, // HoldDurationFactor + use_byte_loss_rate: true, // UseByteLossRate + padding_duration: TimeDelta::from_seconds(2), // PaddingDuration + bound_best_candidate: true, // BoundBest + median_sending_rate_factor: 2.0, // MedianSendingRateFactor + } + } +} + +#[derive(Default)] +struct Derivatives { + pub first: f64, + pub second: f64, +} + +#[derive(Debug, Clone)] +struct Observation { + pub num_packets: usize, + pub num_lost_packets: usize, + pub num_received_packets: usize, + pub sending_rate: DataRate, + pub size: DataSize, + pub lost_size: DataSize, + pub id: i64, +} + +impl Default for Observation { + fn default() -> Self { + Self { + num_packets: 0, + num_lost_packets: 0, + num_received_packets: 0, + sending_rate: DataRate::minus_infinity(), + size: DataSize::zero(), + lost_size: DataSize::zero(), + id: -1, + } + } +} + +impl Observation { + pub fn is_initialized(&self) -> bool { + self.id != -1 + } +} + +// Upstream keys lost packets by sequence number, so a packet one feedback reports lost and a +// later one received counts once. QUIC reports each packet once, acknowledged or lost. +#[derive(Clone)] +struct PartialObservation { + pub num_packets: usize, + pub num_lost_packets: usize, + pub lost_size: DataSize, + pub size: DataSize, +} + +impl Default for PartialObservation { + fn default() -> Self { + Self { + num_packets: 0, + num_lost_packets: 0, + lost_size: DataSize::zero(), + size: DataSize::zero(), + } + } +} + +#[derive(Clone)] +struct PaddingInfo { + pub padding_rate: DataRate, + pub padding_timestamp: Timestamp, +} + +impl Default for PaddingInfo { + fn default() -> Self { + Self { + padding_rate: DataRate::minus_infinity(), + padding_timestamp: Timestamp::minus_infinity(), + } + } +} + +#[derive(Clone)] +struct HoldInfo { + timestamp: Timestamp, + duration: TimeDelta, + rate: DataRate, +} + +impl Default for HoldInfo { + fn default() -> Self { + Self { + timestamp: Timestamp::minus_infinity(), + duration: TimeDelta::zero(), + rate: DataRate::plus_infinity(), + } + } +} + +#[derive(Clone)] +pub struct LossBasedBweV2 { + acknowledged_bitrate: Option, + config: LossBasedBweV2Config, + current_best_estimate: ChannelParameters, + num_observations: usize, + observations: Vec, + partial_observation: PartialObservation, + last_send_time_most_recent_observation: Timestamp, + last_time_estimate_reduced: Timestamp, + cached_instant_upper_bound: Option, + cached_instant_lower_bound: Option, + instant_upper_bound_temporal_weights: Vec, + temporal_weights: Vec, + recovering_after_loss_timestamp: Timestamp, + bandwidth_limit_in_current_window: DataRate, + min_bitrate: DataRate, + max_bitrate: DataRate, + delay_based_estimate: DataRate, + loss_based_result: LossBasedBweV2Result, + last_hold_info: HoldInfo, + last_padding_info: PaddingInfo, + average_reported_loss_ratio: f64, +} + +impl LossBasedBweV2 { + const INIT_HOLD_DURATION: TimeDelta = TimeDelta::from_millis(300); + const MAX_HOLD_DURATION: TimeDelta = TimeDelta::from_seconds(60); + + pub fn new(config: LossBasedBweV2Config) -> Self { + let mut this = Self { + acknowledged_bitrate: None, + current_best_estimate: ChannelParameters::default(), + num_observations: 0, + observations: Vec::new(), + partial_observation: PartialObservation::default(), + last_send_time_most_recent_observation: Timestamp::plus_infinity(), + last_time_estimate_reduced: Timestamp::minus_infinity(), + cached_instant_upper_bound: None, + cached_instant_lower_bound: None, + temporal_weights: Vec::new(), + instant_upper_bound_temporal_weights: Vec::new(), + recovering_after_loss_timestamp: Timestamp::minus_infinity(), + bandwidth_limit_in_current_window: DataRate::plus_infinity(), + min_bitrate: DataRate::from_kilobits_per_sec(1), + max_bitrate: DataRate::plus_infinity(), + delay_based_estimate: DataRate::plus_infinity(), + loss_based_result: LossBasedBweV2Result::default(), + last_hold_info: HoldInfo::default(), + last_padding_info: PaddingInfo::default(), + average_reported_loss_ratio: 0.0, + config, + }; + + if !this.config.enabled { + tracing::debug!( + "The configuration does not specify that the estimator should be enabled, disabling it." + ); + return this; + } + + if !this.config.is_valid() { + tracing::warn!("The configuration is invalid, disabling the estimator."); + this.config.enabled = false; + return this; + } + + this.current_best_estimate.inherent_loss = this.config.initial_inherent_loss_estimate; + this.observations + .resize_with(this.config.observation_window_size, Observation::default); + + for i in 0..this.config.observation_window_size { + this.temporal_weights + .push(this.config.temporal_weight_factor.powf(i as _)); + + this.instant_upper_bound_temporal_weights.push( + this.config + .instant_upper_bound_temporal_weight_factor + .powf(i as _), + ); + } + + this.last_hold_info.duration = Self::INIT_HOLD_DURATION; + + this + } + + pub fn is_enabled(&self) -> bool { + self.config.enabled + } + // Returns true iff a BWE can be calculated, i.e., the estimator has been + // initialized with a BWE and then has received enough `PacketResult`s. + pub fn is_ready(&self) -> bool { + self.is_enabled() + && self + .current_best_estimate + .loss_limited_bandwidth + .is_finite() + && self.num_observations >= self.config.min_num_observations + } + + // Returns true if loss based BWE is ready to be used in the start phase. + pub fn ready_to_use_in_start_phase(&self) -> bool { + self.is_ready() && self.config.use_in_start_phase + } + + // Returns `DataRate::plus_infinity` if no BWE can be calculated. + pub fn get_loss_based_result(&self) -> LossBasedBweV2Result { + if !self.is_ready() { + if !self.is_enabled() { + tracing::warn!("The estimator must be enabled before it can be used."); + } else { + if !self + .current_best_estimate + .loss_limited_bandwidth + .is_finite() + { + tracing::warn!("The estimator must be initialized before it can be used."); + } + if self.num_observations <= self.config.min_num_observations { + tracing::warn!( + "The estimator must receive enough loss statistics before it can be used." + ); + } + } + return LossBasedBweV2Result { + bandwidth_estimate: if self.delay_based_estimate.is_finite() { + self.delay_based_estimate + } else { + DataRate::plus_infinity() + }, + state: LossBasedState::DelayBasedEstimate, + }; + } + self.loss_based_result + } + + pub fn set_acknowledged_bitrate(&mut self, acknowledged_bitrate: DataRate) { + if acknowledged_bitrate.is_finite() { + self.acknowledged_bitrate = Some(acknowledged_bitrate); + self.calculate_instant_lower_bound(); + } else { + tracing::warn!( + "The acknowledged bitrate must be finite: {:?}", + acknowledged_bitrate + ); + } + } + pub fn set_min_max_bitrate(&mut self, min_bitrate: DataRate, max_bitrate: DataRate) { + if min_bitrate.is_finite() { + self.min_bitrate = min_bitrate; + self.calculate_instant_lower_bound(); + } else { + tracing::warn!("The min bitrate must be finite: {:?}", min_bitrate); + } + + if max_bitrate.is_finite() { + self.max_bitrate = max_bitrate; + } else { + tracing::warn!("The max bitrate must be finite: {:?}", max_bitrate); + } + } + pub fn update_bandwidth_estimate( + &mut self, + packet_results: &[PacketResult], + delay_based_estimate: DataRate, + in_alr: bool, + ) { + self.delay_based_estimate = delay_based_estimate; + if !self.is_enabled() { + tracing::warn!("The estimator must be enabled before it can be used."); + return; + } + + if packet_results.is_empty() { + tracing::debug!("The estimate cannot be updated without any loss statistics."); + return; + } + + if !self.push_back_observation(packet_results) { + return; + } + + if !self + .current_best_estimate + .loss_limited_bandwidth + .is_finite() + { + if !delay_based_estimate.is_finite() { + tracing::warn!( + "The delay based estimate must be finite: {:?}", + delay_based_estimate + ); + return; + } + self.current_best_estimate.loss_limited_bandwidth = delay_based_estimate; + self.loss_based_result = LossBasedBweV2Result { + bandwidth_estimate: delay_based_estimate, + state: LossBasedState::DelayBasedEstimate, + }; + } + + let mut best_candidate: ChannelParameters = self.current_best_estimate.clone(); + let mut objective_max: f64 = f64::MIN; + for mut candidate in self.get_candidates(in_alr) { + self.newtons_method_update(&mut candidate); + + let candidate_objective: f64 = self.get_objective(&candidate); + // Select the candidate with the highest objective or the one with the + // same objective but higher bandwidth if they are tied. + if candidate_objective > objective_max + || (candidate_objective == objective_max + && candidate.loss_limited_bandwidth > best_candidate.loss_limited_bandwidth) + { + objective_max = candidate_objective; + best_candidate = candidate; + } + } + if best_candidate.loss_limited_bandwidth < self.current_best_estimate.loss_limited_bandwidth + { + self.last_time_estimate_reduced = self.last_send_time_most_recent_observation; + } + + // Do not increase the estimate if the average loss is greater than current + // inherent loss. + if self.average_reported_loss_ratio > best_candidate.inherent_loss + && self + .config + .not_increase_if_inherent_loss_less_than_average_loss + && self.current_best_estimate.loss_limited_bandwidth + < best_candidate.loss_limited_bandwidth + { + best_candidate.loss_limited_bandwidth = + self.current_best_estimate.loss_limited_bandwidth; + } + + if self.is_in_loss_limited_state() { + // Bound the estimate increase if: + // 1. The estimate has been increased for less than + // `delayed_increase_window` ago, and + // 2. The best candidate is greater than bandwidth_limit_in_current_window. + if self.recovering_after_loss_timestamp.is_finite() + && self.recovering_after_loss_timestamp + self.config.delayed_increase_window + > self.last_send_time_most_recent_observation + && best_candidate.loss_limited_bandwidth > self.bandwidth_limit_in_current_window + { + best_candidate.loss_limited_bandwidth = self.bandwidth_limit_in_current_window; + } + + let increasing_when_loss_limited: bool = self.is_estimate_increasing_when_loss_limited( + /*old_estimate=*/ self.current_best_estimate.loss_limited_bandwidth, + /*new_estimate=*/ best_candidate.loss_limited_bandwidth, + ); + // Bound the best candidate by the acked bitrate. + if let Some(acknowledged_bitrate) = self.acknowledged_bitrate + && increasing_when_loss_limited + && acknowledged_bitrate.is_finite() + { + let mut rampup_factor: f64 = self.config.bw_rampup_upper_bound_factor; + if self.last_hold_info.rate.is_finite() + && acknowledged_bitrate + < self.config.bw_rampup_upper_bound_hold_threshold + * self.last_hold_info.rate + { + rampup_factor = self.config.bw_rampup_upper_bound_in_hold_factor; + } + + best_candidate.loss_limited_bandwidth = std::cmp::max( + self.current_best_estimate.loss_limited_bandwidth, + std::cmp::min( + best_candidate.loss_limited_bandwidth, + rampup_factor * (acknowledged_bitrate), + ), + ); + // Increase current estimate by at least 1kbps to make sure that the state + // will be switched to Increasing, thus padding is triggered. + if self.loss_based_result.state == LossBasedState::Decreasing + && best_candidate.loss_limited_bandwidth + == self.current_best_estimate.loss_limited_bandwidth + { + best_candidate.loss_limited_bandwidth = + self.current_best_estimate.loss_limited_bandwidth + + DataRate::from_bits_per_sec(1); + } + } + } + + let bounded_bandwidth_estimate: DataRate = if self.delay_based_estimate.is_finite() { + self.get_instant_lower_bound().max( + [ + best_candidate.loss_limited_bandwidth, + self.get_instant_upper_bound(), + self.delay_based_estimate, + ] + .iter() + .min() + .cloned() + .unwrap(), + ) + } else { + std::cmp::max( + self.get_instant_lower_bound(), + std::cmp::min( + best_candidate.loss_limited_bandwidth, + self.get_instant_upper_bound(), + ), + ) + }; + if self.config.bound_best_candidate + && bounded_bandwidth_estimate < best_candidate.loss_limited_bandwidth + { + // If network is lossy, cap the best estimate by the instant upper bound, + // e.g. 450kbps if loss rate is 50%. + // Otherwise, cap the estimate by the delay-based estimate or max_bitrate. + self.current_best_estimate.loss_limited_bandwidth = bounded_bandwidth_estimate; + self.current_best_estimate.inherent_loss = 0.0; + } else { + self.current_best_estimate = best_candidate; + if self.config.lower_bound_by_acked_rate_factor > 0.0 { + self.current_best_estimate.loss_limited_bandwidth = std::cmp::max( + self.current_best_estimate.loss_limited_bandwidth, + self.get_instant_lower_bound(), + ); + } + } + + if self.loss_based_result.state == LossBasedState::Decreasing + && self.last_hold_info.timestamp > self.last_send_time_most_recent_observation + && bounded_bandwidth_estimate < self.delay_based_estimate + { + // Ensure that acked rate is the lower bound of HOLD rate. + if self.config.lower_bound_by_acked_rate_factor > 0.0 { + self.last_hold_info.rate = + std::cmp::max(self.get_instant_lower_bound(), self.last_hold_info.rate); + } + // BWE is not allowed to increase above the HOLD rate. The purpose of + // HOLD is to not immediately ramp up BWE to a rate that may cause loss. + self.loss_based_result.bandwidth_estimate = + std::cmp::min(self.last_hold_info.rate, bounded_bandwidth_estimate); + return; + } + + if self.is_estimate_increasing_when_loss_limited( + /*old_estimate=*/ self.loss_based_result.bandwidth_estimate, + /*new_estimate=*/ bounded_bandwidth_estimate, + ) && self.can_keep_increasing_state(bounded_bandwidth_estimate) + && bounded_bandwidth_estimate < self.delay_based_estimate + && bounded_bandwidth_estimate < self.max_bitrate + { + if self.config.padding_duration > TimeDelta::zero() + && bounded_bandwidth_estimate > self.last_padding_info.padding_rate + { + // Start a new padding duration. + self.last_padding_info.padding_rate = bounded_bandwidth_estimate; + self.last_padding_info.padding_timestamp = + self.last_send_time_most_recent_observation; + } + self.loss_based_result.state = if self.config.padding_duration > TimeDelta::zero() { + LossBasedState::IncreaseUsingPadding + } else { + LossBasedState::Increasing + }; + } else if bounded_bandwidth_estimate < self.delay_based_estimate + && bounded_bandwidth_estimate < self.max_bitrate + { + if self.loss_based_result.state != LossBasedState::Decreasing + && self.config.hold_duration_factor > 0.0 + { + tracing::debug!( + "Switch to HOLD. Bounded BWE: {:?}, duration: {:?}", + bounded_bandwidth_estimate, + self.last_hold_info.duration + ); + self.last_hold_info = HoldInfo { + timestamp: self.last_send_time_most_recent_observation + + self.last_hold_info.duration, + duration: std::cmp::min( + Self::MAX_HOLD_DURATION, + self.last_hold_info.duration * self.config.hold_duration_factor, + ), + rate: bounded_bandwidth_estimate, + }; + } + + self.last_padding_info = PaddingInfo::default(); + self.loss_based_result.state = LossBasedState::Decreasing; + } else { + // Reset the HOLD info if delay based estimate works to avoid getting + // stuck in low bitrate. + self.last_hold_info = HoldInfo { + timestamp: Timestamp::minus_infinity(), + duration: Self::INIT_HOLD_DURATION, + rate: DataRate::plus_infinity(), + }; + self.last_padding_info = PaddingInfo::default(); + self.loss_based_result.state = LossBasedState::DelayBasedEstimate; + } + self.loss_based_result.bandwidth_estimate = bounded_bandwidth_estimate; + + if self.is_in_loss_limited_state() + && (self.recovering_after_loss_timestamp.is_infinite() + || self.recovering_after_loss_timestamp + self.config.delayed_increase_window + < self.last_send_time_most_recent_observation) + { + self.bandwidth_limit_in_current_window = std::cmp::max( + CONGESTION_CONTROLLER_MIN_BITRATE, + self.current_best_estimate.loss_limited_bandwidth * self.config.max_increase_factor, + ); + self.recovering_after_loss_timestamp = self.last_send_time_most_recent_observation; + } + } + // For unit testing only. + #[cfg(test)] + pub fn set_bandwidth_estimate(&mut self, bandwidth_estimate: DataRate) { + if bandwidth_estimate.is_finite() { + self.current_best_estimate.loss_limited_bandwidth = bandwidth_estimate; + self.loss_based_result = LossBasedBweV2Result { + bandwidth_estimate, + state: LossBasedState::DelayBasedEstimate, + }; + } else { + tracing::warn!( + "The bandwidth estimate must be finite: {:?}", + bandwidth_estimate + ); + } + } + + // Returns `0.0` if not enough loss statistics have been received. + fn update_average_reported_loss_ratio(&mut self) { + self.average_reported_loss_ratio = if self.config.use_byte_loss_rate { + self.calculate_average_reported_byte_loss_ratio() + } else { + self.calculate_average_reported_packet_loss_ratio() + }; + } + + fn calculate_average_reported_packet_loss_ratio(&self) -> f64 { + if self.num_observations == 0 { + return 0.0; + } + + let mut num_packets: f64 = 0.0; + let mut num_lost_packets: f64 = 0.0; + for observation in &self.observations { + if !observation.is_initialized() { + continue; + } + + let id: usize = observation.id.try_into().unwrap(); + let instant_temporal_weight: f64 = + self.instant_upper_bound_temporal_weights[(self.num_observations - 1) - id]; + num_packets += instant_temporal_weight * observation.num_packets as f64; + num_lost_packets += instant_temporal_weight * observation.num_lost_packets as f64; + } + + num_lost_packets / num_packets + } + // Calculates the average loss ratio over the last `observation_window_size` + // observations but skips the observation with min and max loss ratio in order + // to filter out loss spikes. + fn calculate_average_reported_byte_loss_ratio(&self) -> f64 { + if self.num_observations == 0 { + return 0.0; + } + + let mut total_bytes: DataSize = DataSize::zero(); + let mut lost_bytes: DataSize = DataSize::zero(); + let mut min_loss_rate: f64 = 1.0; + let mut max_loss_rate: f64 = 0.0; + let mut min_lost_bytes: DataSize = DataSize::zero(); + let mut max_lost_bytes: DataSize = DataSize::zero(); + let mut min_bytes_received: DataSize = DataSize::zero(); + let mut max_bytes_received: DataSize = DataSize::zero(); + let mut send_rate_of_max_loss_observation: DataRate = DataRate::zero(); + for observation in &self.observations { + if !observation.is_initialized() { + continue; + } + + let id: usize = observation.id.try_into().unwrap(); + let instant_temporal_weight: f64 = + self.instant_upper_bound_temporal_weights[(self.num_observations - 1) - id]; + total_bytes += instant_temporal_weight * observation.size; + lost_bytes += instant_temporal_weight * observation.lost_size; + + let loss_rate: f64 = if !observation.size.is_zero() { + observation.lost_size / observation.size + } else { + 0.0 + }; + if self.num_observations > 3 { + if loss_rate > max_loss_rate { + max_loss_rate = loss_rate; + max_lost_bytes = instant_temporal_weight * observation.lost_size; + max_bytes_received = instant_temporal_weight * observation.size; + send_rate_of_max_loss_observation = observation.sending_rate; + } + if loss_rate < min_loss_rate { + min_loss_rate = loss_rate; + min_lost_bytes = instant_temporal_weight * observation.lost_size; + min_bytes_received = instant_temporal_weight * observation.size; + } + } + } + if self.get_median_sending_rate() * self.config.median_sending_rate_factor + <= send_rate_of_max_loss_observation + { + // If the median sending rate is less than half of the sending rate of the + // observation with max loss rate, i.e. we suddenly send a lot of data, then + // the loss rate might not be due to a spike. + return lost_bytes / total_bytes; + } + + if total_bytes == max_bytes_received + min_bytes_received { + // It could happen if the observation window was 2. + return lost_bytes / total_bytes; + } + + (lost_bytes - min_lost_bytes - max_lost_bytes) + / (total_bytes - max_bytes_received - min_bytes_received) + } + + fn get_candidates(&self, in_alr: bool) -> Vec { + let best_estimate: ChannelParameters = self.current_best_estimate.clone(); + let mut bandwidths: Vec = Vec::new(); + for candidate_factor in &self.config.candidate_factors { + bandwidths.push(*candidate_factor * best_estimate.loss_limited_bandwidth); + } + + if let Some(acknowledged_bitrate) = self.acknowledged_bitrate + && self.config.acked_rate_candidate + && (!(self.config.not_use_acked_rate_in_alr && in_alr) + || (self.config.padding_duration > TimeDelta::zero() + && self.last_padding_info.padding_timestamp + self.config.padding_duration + >= self.last_send_time_most_recent_observation)) + { + bandwidths.push(acknowledged_bitrate * self.config.bw_backoff_lower_bound_factor); + } + + if self.delay_based_estimate.is_finite() + && self.config.delay_based_candidate + && self.delay_based_estimate > best_estimate.loss_limited_bandwidth + { + bandwidths.push(self.delay_based_estimate); + } + + if in_alr + && self.config.upper_bound_candidate_in_alr + && best_estimate.loss_limited_bandwidth > self.get_instant_upper_bound() + { + bandwidths.push(self.get_instant_upper_bound()); + } + + let candidate_bandwidth_upper_bound: DataRate = self.get_candidate_bandwidth_upper_bound(); + + let mut candidates: Vec = Vec::new(); + for bandwidth in &bandwidths { + let mut candidate: ChannelParameters = best_estimate.clone(); + candidate.loss_limited_bandwidth = std::cmp::min( + *bandwidth, + std::cmp::max( + best_estimate.loss_limited_bandwidth, + candidate_bandwidth_upper_bound, + ), + ); + candidate.inherent_loss = self.get_feasible_inherent_loss(&candidate); + candidates.push(candidate); + } + candidates + } + + fn get_candidate_bandwidth_upper_bound(&self) -> DataRate { + let mut candidate_bandwidth_upper_bound: DataRate = self.max_bitrate; + if self.is_in_loss_limited_state() && self.bandwidth_limit_in_current_window.is_finite() { + candidate_bandwidth_upper_bound = self.bandwidth_limit_in_current_window; + } + + let acknowledged_bitrate = match self.acknowledged_bitrate { + Some(rate) => rate, + None => return candidate_bandwidth_upper_bound, + }; + + if self.config.bw_rampup_accel_max_factor > 0.0 { + let time_since_bandwidth_reduced: TimeDelta = std::cmp::min( + self.config.bw_rampup_accel_maxout_time, + std::cmp::max( + TimeDelta::zero(), + self.last_send_time_most_recent_observation - self.last_time_estimate_reduced, + ), + ); + let rampup_acceleration: f64 = self.config.bw_rampup_accel_max_factor + * time_since_bandwidth_reduced + / self.config.bw_rampup_accel_maxout_time; + + candidate_bandwidth_upper_bound += rampup_acceleration * (acknowledged_bitrate); + } + candidate_bandwidth_upper_bound + } + + fn get_derivatives(&self, channel_parameters: &ChannelParameters) -> Derivatives { + let mut derivatives = Derivatives::default(); + + for observation in &self.observations { + if !observation.is_initialized() { + continue; + } + + let loss_probability: f64 = get_loss_probability( + channel_parameters.inherent_loss, + channel_parameters.loss_limited_bandwidth, + observation.sending_rate, + ); + + let id: usize = observation.id.try_into().unwrap(); + let temporal_weight: f64 = self.temporal_weights[(self.num_observations - 1) - id]; + if self.config.use_byte_loss_rate { + derivatives.first += temporal_weight + * ((to_kilo_bytes(observation.lost_size) / loss_probability) + - (to_kilo_bytes(observation.size - observation.lost_size) + / (1.0 - loss_probability))); + derivatives.second -= temporal_weight + * ((to_kilo_bytes(observation.lost_size) / loss_probability.powf(2.0)) + + (to_kilo_bytes(observation.size - observation.lost_size) + / (1.0 - loss_probability).powf(2.0))); + } else { + derivatives.first += temporal_weight + * ((observation.num_lost_packets as f64 / loss_probability) + - (observation.num_received_packets as f64 / (1.0 - loss_probability))); + derivatives.second -= temporal_weight + * ((observation.num_lost_packets as f64 / loss_probability.powf(2.0)) + + (observation.num_received_packets as f64 + / (1.0 - loss_probability).powf(2.0))); + } + } + + if derivatives.second >= 0.0 { + tracing::error!( + "The second derivative is mathematically guaranteed to be negative but is {}.", + derivatives.second + ); + derivatives.second = -1.0e-6; + } + + derivatives + } + + fn get_feasible_inherent_loss(&self, channel_parameters: &ChannelParameters) -> f64 { + channel_parameters + .inherent_loss + .max(self.config.inherent_loss_lower_bound) + .min(self.get_inherent_loss_upper_bound(channel_parameters.loss_limited_bandwidth)) + } + + fn get_inherent_loss_upper_bound(&self, bandwidth: DataRate) -> f64 { + if bandwidth.is_zero() { + return 1.0; + } + + let inherent_loss_upper_bound: f64 = self.config.inherent_loss_upper_bound_offset + + self.config.inherent_loss_upper_bound_bw_balance / bandwidth; + inherent_loss_upper_bound.min(1.0) + } + + fn adjust_bias_factor(&self, loss_rate: f64, bias_factor: f64) -> f64 { + bias_factor * (self.config.loss_threshold_of_high_bandwidth_preference - loss_rate) + / (self.config.bandwidth_preference_smoothing_factor + + (self.config.loss_threshold_of_high_bandwidth_preference - loss_rate).abs()) + } + + fn get_high_bandwidth_bias(&self, bandwidth: DataRate) -> f64 { + if bandwidth.is_finite() { + return self.adjust_bias_factor( + self.average_reported_loss_ratio, + self.config.higher_bw_bias_factor, + ) * bandwidth.kbps_float() + + self.adjust_bias_factor( + self.average_reported_loss_ratio, + self.config.higher_log_bw_bias_factor, + ) * (1.0 + bandwidth.kbps_float()).ln(); + } + 0.0 + } + + fn get_objective(&self, channel_parameters: &ChannelParameters) -> f64 { + let mut objective: f64 = 0.0; + + let high_bandwidth_bias: f64 = + self.get_high_bandwidth_bias(channel_parameters.loss_limited_bandwidth); + + for observation in &self.observations { + if !observation.is_initialized() { + continue; + } + + let loss_probability: f64 = get_loss_probability( + channel_parameters.inherent_loss, + channel_parameters.loss_limited_bandwidth, + observation.sending_rate, + ); + + let id: usize = observation.id.try_into().unwrap(); + let temporal_weight: f64 = self.temporal_weights[(self.num_observations - 1) - id]; + if self.config.use_byte_loss_rate { + objective += temporal_weight + * ((to_kilo_bytes(observation.lost_size) * loss_probability.ln()) + + (to_kilo_bytes(observation.size - observation.lost_size) + * (1.0 - loss_probability).ln())); + objective += + temporal_weight * high_bandwidth_bias * to_kilo_bytes(observation.size); + } else { + objective += temporal_weight + * ((observation.num_lost_packets as f64 * (loss_probability).ln()) + + (observation.num_received_packets as f64 + * (1.0 - loss_probability).ln())); + objective += temporal_weight * high_bandwidth_bias * observation.num_packets as f64; + } + } + + objective + } + + fn get_sending_rate(&self, instantaneous_sending_rate: DataRate) -> DataRate { + if self.num_observations == 0 { + return instantaneous_sending_rate; + } + + let most_recent_observation_idx: usize = + (self.num_observations - 1) % self.config.observation_window_size; + let most_recent_observation = &self.observations[most_recent_observation_idx]; + let sending_rate_previous_observation: DataRate = most_recent_observation.sending_rate; + + self.config.sending_rate_smoothing_factor * sending_rate_previous_observation + + (1.0 - self.config.sending_rate_smoothing_factor) * instantaneous_sending_rate + } + + fn get_instant_upper_bound(&self) -> DataRate { + self.cached_instant_upper_bound.unwrap_or(self.max_bitrate) + } + + fn calculate_instant_upper_bound(&mut self) { + let mut instant_limit: DataRate = self.max_bitrate; + if self.average_reported_loss_ratio > self.config.instant_upper_bound_loss_offset { + instant_limit = self.config.instant_upper_bound_bw_balance + / (self.average_reported_loss_ratio - self.config.instant_upper_bound_loss_offset); + } + + self.cached_instant_upper_bound = Some(instant_limit); + } + + fn get_instant_lower_bound(&self) -> DataRate { + self.cached_instant_lower_bound.unwrap_or(DataRate::zero()) + } + + fn calculate_instant_lower_bound(&mut self) { + let mut instance_lower_bound: DataRate = DataRate::zero(); + + if let Some(acknowledged_bitrate) = self.acknowledged_bitrate + && self.config.lower_bound_by_acked_rate_factor > 0.0 + { + instance_lower_bound = + self.config.lower_bound_by_acked_rate_factor * acknowledged_bitrate; + } + + if self.min_bitrate.is_finite() { + instance_lower_bound = std::cmp::max(instance_lower_bound, self.min_bitrate); + } + self.cached_instant_lower_bound = Some(instance_lower_bound); + } + + fn newtons_method_update(&self, channel_parameters: &mut ChannelParameters) { + if self.num_observations == 0 { + return; + } + + for _ in 0..self.config.newton_iterations { + let derivatives: Derivatives = self.get_derivatives(channel_parameters); + channel_parameters.inherent_loss -= + self.config.newton_step_size * derivatives.first / derivatives.second; + channel_parameters.inherent_loss = self.get_feasible_inherent_loss(channel_parameters); + } + } + + // Returns false if no observation was created. + fn push_back_observation(&mut self, packet_results: &[PacketResult]) -> bool { + if packet_results.is_empty() { + return false; + } + + self.partial_observation.num_packets += packet_results.len(); + let mut last_send_time: Timestamp = Timestamp::minus_infinity(); + let mut first_send_time: Timestamp = Timestamp::plus_infinity(); + for packet in packet_results { + if !packet.is_received() { + self.partial_observation.num_lost_packets += 1; + self.partial_observation.lost_size += packet.sent_packet.size; + } + self.partial_observation.size += packet.sent_packet.size; + last_send_time = std::cmp::max(last_send_time, packet.sent_packet.send_time); + first_send_time = std::cmp::min(first_send_time, packet.sent_packet.send_time); + } + + // This is the first packet report we have received. + if !self.last_send_time_most_recent_observation.is_finite() { + self.last_send_time_most_recent_observation = first_send_time; + } + + let observation_duration: TimeDelta = + last_send_time - self.last_send_time_most_recent_observation; + // Too small to be meaningful. + // To consider: what if it is too long?, i.e. we did not receive any packets + // for a long time, then all the packets we received are too old. + if observation_duration <= TimeDelta::zero() + || observation_duration < self.config.observation_duration_lower_bound + { + return false; + } + + self.last_send_time_most_recent_observation = last_send_time; + + let mut observation = Observation::default(); + observation.num_packets = self.partial_observation.num_packets; + observation.num_lost_packets = self.partial_observation.num_lost_packets; + observation.num_received_packets = observation.num_packets - observation.num_lost_packets; + observation.sending_rate = + self.get_sending_rate(self.partial_observation.size / observation_duration); + observation.lost_size = self.partial_observation.lost_size; + observation.size = self.partial_observation.size; + observation.id = self.num_observations as _; + self.observations[self.num_observations % self.config.observation_window_size] = + observation; + self.num_observations += 1; + + self.partial_observation = PartialObservation::default(); + self.update_average_reported_loss_ratio(); + self.calculate_instant_upper_bound(); + true + } + + fn is_estimate_increasing_when_loss_limited( + &mut self, + old_estimate: DataRate, + new_estimate: DataRate, + ) -> bool { + (old_estimate < new_estimate + || (old_estimate == new_estimate + && (self.loss_based_result.state == LossBasedState::Increasing + || self.loss_based_result.state == LossBasedState::IncreaseUsingPadding))) + && self.is_in_loss_limited_state() + } + + fn is_in_loss_limited_state(&self) -> bool { + self.loss_based_result.state != LossBasedState::DelayBasedEstimate + } + + fn can_keep_increasing_state(&self, estimate: DataRate) -> bool { + if self.config.padding_duration == TimeDelta::zero() + || self.loss_based_result.state != LossBasedState::IncreaseUsingPadding + { + return true; + } + + // Keep using the IncreaseUsingPadding if either the state has been + // IncreaseUsingPadding for less than PaddingDuration or the estimate + // increases. + self.last_padding_info.padding_timestamp + self.config.padding_duration + >= self.last_send_time_most_recent_observation + || self.last_padding_info.padding_rate < estimate + } + + fn get_median_sending_rate(&self) -> DataRate { + let mut sending_rates: Vec = Vec::new(); + for observation in &self.observations { + if !observation.is_initialized() + || !observation.sending_rate.is_finite() + || observation.sending_rate.is_zero() + { + continue; + } + sending_rates.push(observation.sending_rate); + } + if sending_rates.is_empty() { + return DataRate::zero(); + } + sending_rates.sort(); + if sending_rates.len().is_multiple_of(2) { + return (sending_rates[sending_rates.len() / 2 - 1] + + sending_rates[sending_rates.len() / 2]) + / 2; + } + sending_rates[sending_rates.len() / 2] + } +} + +fn to_kilo_bytes(datasize: DataSize) -> f64 { + datasize.bytes_float() / 1000.0 +} + +fn get_loss_probability( + mut inherent_loss: f64, + loss_limited_bandwidth: DataRate, + sending_rate: DataRate, +) -> f64 { + if !(0.0..=1.0).contains(&inherent_loss) { + tracing::warn!("The inherent loss must be in [0,1]: {}", inherent_loss); + inherent_loss = inherent_loss.clamp(0.0, 1.0) + } + if !sending_rate.is_finite() { + tracing::warn!("The sending rate must be finite: {:?}", sending_rate); + } + if !loss_limited_bandwidth.is_finite() { + tracing::warn!( + "The loss limited bandwidth must be finite: {:?}", + loss_limited_bandwidth + ); + } + + let mut loss_probability: f64 = inherent_loss; + if sending_rate.is_finite() + && loss_limited_bandwidth.is_finite() + && (sending_rate > loss_limited_bandwidth) + { + loss_probability += + (1.0 - inherent_loss) * (sending_rate - loss_limited_bandwidth) / sending_rate; + } + loss_probability.clamp(1.0e-6, 1.0 - 1.0e-6) +} + +#[cfg(test)] +mod test { + + use crate::gcc::api::transport::SentPacket; + + use super::*; + + const OBSERVATION_DURATION_LOWER_BOUND: TimeDelta = TimeDelta::from_millis(250); + const DELAYED_INCREASE_WINDOW: TimeDelta = TimeDelta::from_millis(300); + const MAX_INCREASE_FACTOR: f64 = 1.5; + const PACKET_SIZE: i64 = 15_000; + + fn config(enabled: bool, valid: bool) -> LossBasedBweV2Config { + LossBasedBweV2Config { + enabled, + bw_rampup_upper_bound_factor: if valid { 1.2 } else { 0.0 }, + candidate_factors: vec![1.1, 1.0, 0.95], + higher_bw_bias_factor: 0.01, + inherent_loss_lower_bound: 0.001, + inherent_loss_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(14), + inherent_loss_upper_bound_offset: 0.9, + initial_inherent_loss_estimate: 0.01, + newton_iterations: 2, + newton_step_size: 0.4, + observation_window_size: 15, + sending_rate_smoothing_factor: 0.01, + instant_upper_bound_temporal_weight_factor: 0.97, + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(90), + instant_upper_bound_loss_offset: 0.1, + temporal_weight_factor: 0.98, + min_num_observations: 1, + observation_duration_lower_bound: OBSERVATION_DURATION_LOWER_BOUND, + max_increase_factor: MAX_INCREASE_FACTOR, + delayed_increase_window: DELAYED_INCREASE_WINDOW, + ..Default::default() + } + } + + fn short_observation_config() -> LossBasedBweV2Config { + LossBasedBweV2Config { + min_num_observations: 1, + observation_window_size: 2, + ..Default::default() + } + } + + #[derive(Default)] + struct LossBasedBweV2Test {} + + impl LossBasedBweV2Test { + fn create_packet_results_with_received_packets( + &mut self, + first_packet_timestamp: Timestamp, + ) -> Vec { + let mut enough_feedback = vec![PacketResult::default(); 2]; + enough_feedback[0].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + enough_feedback[1].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + enough_feedback[0].sent_packet.send_time = first_packet_timestamp; + enough_feedback[1].sent_packet.send_time = + first_packet_timestamp + OBSERVATION_DURATION_LOWER_BOUND; + enough_feedback[0].receive_time = + first_packet_timestamp + OBSERVATION_DURATION_LOWER_BOUND; + enough_feedback[1].receive_time = + first_packet_timestamp + 2 * OBSERVATION_DURATION_LOWER_BOUND; + enough_feedback + } + + fn create_packet_results_with_10p_packet_loss_rate( + &mut self, + first_packet_timestamp: Timestamp, + lost_packet_size: DataSize, + ) -> Vec { + let mut enough_feedback = vec![PacketResult::default(); 10]; + for (i, packet) in enough_feedback.iter_mut().enumerate() { + packet.sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + packet.sent_packet.send_time = + first_packet_timestamp + (i) as i64 * OBSERVATION_DURATION_LOWER_BOUND; + packet.receive_time = + first_packet_timestamp + (i + 1) as i64 * OBSERVATION_DURATION_LOWER_BOUND; + } + enough_feedback[9].receive_time = Timestamp::plus_infinity(); + enough_feedback[9].sent_packet.size = lost_packet_size; + enough_feedback + } + + fn create_packet_results_with_50p_packet_loss_rate( + &mut self, + first_packet_timestamp: Timestamp, + ) -> Vec { + let mut enough_feedback = vec![PacketResult::default(); 2]; + enough_feedback[0].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + enough_feedback[1].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + enough_feedback[0].sent_packet.send_time = first_packet_timestamp; + enough_feedback[1].sent_packet.send_time = + first_packet_timestamp + OBSERVATION_DURATION_LOWER_BOUND; + enough_feedback[0].receive_time = + first_packet_timestamp + OBSERVATION_DURATION_LOWER_BOUND; + enough_feedback[1].receive_time = Timestamp::plus_infinity(); + enough_feedback + } + + fn create_packet_results_with_100p_loss_rate( + &mut self, + first_packet_timestamp: Timestamp, + num_packets: usize, /* =2 */ + ) -> Vec { + let mut enough_feedback = Vec::with_capacity(num_packets); + + for i in 0..(num_packets - 1) { + enough_feedback.push(PacketResult { + sent_packet: SentPacket { + size: DataSize::from_bytes(PACKET_SIZE), + send_time: first_packet_timestamp + TimeDelta::from_millis(i as i64 * 10), + ..Default::default() + }, + receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, + }); + } + + enough_feedback.push(PacketResult { + sent_packet: SentPacket { + size: DataSize::from_bytes(PACKET_SIZE), + send_time: first_packet_timestamp + OBSERVATION_DURATION_LOWER_BOUND, + ..Default::default() + }, + receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, + }); + + enough_feedback + } + } + + #[test] + fn enabled_when_given_valid_configuration_values() { + let config = config(true, true); + let loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + assert!(loss_based_bandwidth_estimator.is_enabled()); + } + + #[test] + fn disabled_when_given_disabled_configuration() { + let config = config(false, true); + let loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + assert!(!loss_based_bandwidth_estimator.is_enabled()); + } + + #[test] + fn disabled_when_given_non_valid_configuration_values() { + let config = config(true, false); + let loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + assert!(!loss_based_bandwidth_estimator.is_enabled()); + } + + #[test] + fn disabled_when_given_non_positive_candidate_factor() { + let config = LossBasedBweV2Config { + candidate_factors: vec![-1.3, 1.1], + ..Default::default() + }; + let loss_based_bandwidth_estimator_1 = LossBasedBweV2::new(config); + assert!(!loss_based_bandwidth_estimator_1.is_enabled()); + + let config = LossBasedBweV2Config { + candidate_factors: vec![0.0, 1.1], + ..Default::default() + }; + let loss_based_bandwidth_estimator_2 = LossBasedBweV2::new(config); + assert!(!loss_based_bandwidth_estimator_2.is_enabled()); + } + + #[test] + fn disabled_when_given_configuration_that_does_not_allow_generating_candidates() { + let config = LossBasedBweV2Config { + candidate_factors: vec![1.0], + acked_rate_candidate: false, + delay_based_candidate: false, + ..Default::default() + }; + let loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + assert!(!loss_based_bandwidth_estimator.is_enabled()); + } + + #[test] + fn returns_delay_based_estimate_when_disabled() { + let config = config(false, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + /*packet_results=*/ &[], + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(100), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(100) + ); + } + + #[test] + fn returns_delay_based_estimate_when_when_given_non_valid_configuration_values() { + let config = config(true, false); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + /*packet_results=*/ &[], + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(100), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(100) + ); + } + + #[test] + fn bandwidth_estimate_given_initialization_and_then_feedback() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback: Vec = test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert!(loss_based_bandwidth_estimator.is_ready()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_finite() + ); + } + + #[test] + fn no_bandwidth_estimate_given_no_initialization() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback: Vec = test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert!(!loss_based_bandwidth_estimator.is_ready()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_plus_infinity() + ); + } + + #[test] + fn no_bandwidth_estimate_given_not_enough_feedback() { + // Create packet results where the observation duration is less than the lower + // bound. + let mut not_enough_feedback = [PacketResult::default(); 2]; + not_enough_feedback[0].sent_packet.size = DataSize::from_bytes(15_000); + not_enough_feedback[1].sent_packet.size = DataSize::from_bytes(15_000); + not_enough_feedback[0].sent_packet.send_time = Timestamp::zero(); + not_enough_feedback[1].sent_packet.send_time = + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND / 2; + not_enough_feedback[0].receive_time = + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND / 2; + not_enough_feedback[1].receive_time = Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND; + + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + assert!(!loss_based_bandwidth_estimator.is_ready()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_plus_infinity() + ); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + ¬_enough_feedback, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert!(!loss_based_bandwidth_estimator.is_ready()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_plus_infinity() + ); + } + + #[test] + fn set_value_is_the_estimate_until_additional_feedback_has_been_received() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + 2 * OBSERVATION_DURATION_LOWER_BOUND, + ); + + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert_ne!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(600) + ); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(600) + ); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert_ne!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(600) + ); + } + + #[test] + fn set_acknowledged_bitrate_only_affects_the_bwe_when_additional_feedback_is_given() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + 2 * OBSERVATION_DURATION_LOWER_BOUND, + ); + + let config = config(true, true); + let mut loss_based_bandwidth_estimator_1 = LossBasedBweV2::new(config.clone()); + let mut loss_based_bandwidth_estimator_2 = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator_1 + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator_2 + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator_1.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + loss_based_bandwidth_estimator_2.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert_eq!( + loss_based_bandwidth_estimator_1 + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(660) + ); + + loss_based_bandwidth_estimator_1 + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(900)); + + assert_eq!( + loss_based_bandwidth_estimator_1 + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(660) + ); + + loss_based_bandwidth_estimator_1.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + loss_based_bandwidth_estimator_2.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert_ne!( + loss_based_bandwidth_estimator_1 + .get_loss_based_result() + .bandwidth_estimate, + loss_based_bandwidth_estimator_2 + .get_loss_based_result() + .bandwidth_estimate + ); + } + + #[test] + fn bandwidth_estimate_is_capped_to_be_tcp_fair_given_too_high_loss_rate() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_no_received_packets: Vec = test + .create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_no_received_packets, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(100) + ); + } + + // When network is normal, estimate can increase but never be higher than + // the delay based estimate. + #[test] + fn bandwidth_estimate_capped_by_delay_based_estimate_when_network_normal() { + let mut test = LossBasedBweV2Test::default(); + // Create two packet results, network is in normal state, 100% packets are + // received, and no delay increase. + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + 2 * OBSERVATION_DURATION_LOWER_BOUND, + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + // If the delay based estimate is infinity, then loss based estimate increases + // and not bounded by delay based estimate. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + > DataRate::from_kilobits_per_sec(600) + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(500), + /*in_alr=*/ false, + ); + // If the delay based estimate is not infinity, then loss based estimate is + // bounded by delay based estimate. + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(500) + ); + } + + // When loss based bwe receives a strong signal of overusing and an increase in + // loss rate, it should acked bitrate for emegency backoff. + #[test] + fn use_acked_bitrate_for_emegency_back_off() { + let mut test = LossBasedBweV2Test::default(); + // Create two packet results, first packet has 50% loss rate, second packet + // has 100% loss rate. + let enough_feedback_1: Vec = test + .create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + 2 * OBSERVATION_DURATION_LOWER_BOUND, + 2, + ); + + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + let acked_bitrate: DataRate = DataRate::from_kilobits_per_sec(300); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(acked_bitrate); + // Update estimate when network is overusing, and 50% loss rate. + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + // Update estimate again when network is continuously overusing, and 100% + // loss rate. + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + // The estimate bitrate now is backed off based on acked bitrate. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + <= acked_bitrate + ); + } + + // When receiving the same packet feedback, loss based bwe ignores the feedback + // and returns the current estimate. + #[test] + fn no_bwe_change_if_observation_duration_unchanged() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(300)); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + let estimate_1: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + // Use the same feedback and check if the estimate is unchanged. + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + let estimate_2: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + assert_eq!(estimate_2, estimate_1); + } + + // When receiving feedback of packets that were sent within an observation + // duration, and network is in the normal state, loss based bwe returns the + // current estimate. + #[test] + fn no_bwe_change_if_observation_duration_is_small_and_network_normal() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND - TimeDelta::from_millis(1), + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + let estimate_1: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + let estimate_2: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + assert_eq!(estimate_2, estimate_1); + } + + // When receiving feedback of packets that were sent within an observation + // duration, and network is in the underusing state, loss based bwe returns the + // current estimate. + #[test] + fn no_bwe_increase_if_observation_duration_is_small_and_network_underusing() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND - TimeDelta::from_millis(1), + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + let estimate_1: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + let estimate_2: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + assert!(estimate_2 <= estimate_1); + } + + #[test] + fn increase_to_delay_based_estimate_if_no_loss_or_delay_increase() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + 2 * OBSERVATION_DURATION_LOWER_BOUND, + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + delay_based_estimate + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + delay_based_estimate + ); + } + + #[test] + fn increase_by_hold_factor_after_loss_based_bwe_backs_off() { + let mut test = LossBasedBweV2Test::default(); + let config = short_observation_config(); + + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + let acked_rate: DataRate = DataRate::from_kilobits_per_sec(100); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(acked_rate); + + // Create some loss to create the loss limited scenario. + let enough_feedback_1: Vec = test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + let result_at_loss: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + + // Network recovers after loss. + let mut feedback_count: i64 = 2; + while loss_based_bandwidth_estimator.get_loss_based_result().state + != LossBasedState::IncreaseUsingPadding + { + assert!(feedback_count < 100, "never reached IncreaseUsingPadding"); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_count, + ), + delay_based_estimate, + /*in_alr=*/ false, + ); + feedback_count += 1; + } + + let result_after_recovery: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + // BwRampupUpperBoundInHoldFactor is 1.2. + assert_eq!( + result_after_recovery.bandwidth_estimate, + result_at_loss.bandwidth_estimate * 1.2 + ); + } + + #[test] + fn loss_based_state_is_delay_based_estimate_after_network_recovering() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + candidate_factors: vec![100.0, 1.0, 0.5], + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10000), + max_increase_factor: 100.0, + not_increase_if_inherent_loss_less_than_average_loss: false, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(600); + let acked_rate: DataRate = DataRate::from_kilobits_per_sec(300); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(acked_rate); + + // Create some loss to create the loss limited scenario. + let enough_feedback_1: Vec = test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + + // Network recovers after loss. + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + ); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::DelayBasedEstimate + ); + + // Network recovers continuing. + let enough_feedback_3: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * 2, + ); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_3, + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::DelayBasedEstimate + ); + } + + #[test] + fn loss_based_state_is_not_delay_based_estimate_if_delay_based_estimate_infinite() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + candidate_factors: vec![100.0, 1.0, 0.5], + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10000), + max_increase_factor: 100.0, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + // Create some loss to create the loss limited scenario. + let enough_feedback_1: Vec = test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + + // Network recovers after loss. + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + ); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_ne!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::DelayBasedEstimate + ); + } + + // After loss based bwe backs off, the next estimate is capped by + // a factor of acked bitrate. + #[test] + fn increase_by_factor_of_acked_bitrate_after_loss_based_bwe_backs_off() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + loss_threshold_of_high_bandwidth_preference: 0.99, + bw_rampup_upper_bound_factor: 1.2, + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10000), + ..short_observation_config() + }; + let enough_feedback_1: Vec = test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(300)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + let mut result: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + let estimate_1: DataRate = result.bandwidth_estimate; + assert!(estimate_1.kbps() < 600); + + loss_based_bandwidth_estimator.set_acknowledged_bitrate(estimate_1 * 0.9); + + let mut feedback_count: i64 = 1; + while result.state != LossBasedState::IncreaseUsingPadding { + assert!(feedback_count < 100, "never reached IncreaseUsingPadding"); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + feedback_count * OBSERVATION_DURATION_LOWER_BOUND, + ), + delay_based_estimate, + /*in_alr=*/ false, + ); + feedback_count += 1; + result = loss_based_bandwidth_estimator.get_loss_based_result(); + } + assert_eq!(result.state, LossBasedState::IncreaseUsingPadding); + + // The estimate is capped by acked_bitrate * BwRampupUpperBoundFactor. + assert_eq!(result.bandwidth_estimate, estimate_1 * 0.9 * 1.2); + + // But if acked bitrate decreases, BWE does not decrease when there is no + // loss. + loss_based_bandwidth_estimator.set_acknowledged_bitrate(estimate_1 * 0.9); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + feedback_count * OBSERVATION_DURATION_LOWER_BOUND, + ), + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + result.bandwidth_estimate + ); + } + + // Ensure that the state can switch to Increase even when the bandwidth is + // bounded by acked bitrate. + #[test] + fn ensure_increase_even_if_acked_bitrate_bound() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + loss_threshold_of_high_bandwidth_preference: 0.99, + bw_rampup_upper_bound_factor: 1.2, + // Set InstantUpperBoundBwBalance high to disable InstantUpperBound cap. + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10000), + ..short_observation_config() + }; + let enough_feedback_1: Vec = test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(300)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + let mut result: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + let estimate_1: DataRate = result.bandwidth_estimate; + assert!(estimate_1.kbps() < 600); + + // Set a low acked bitrate. + loss_based_bandwidth_estimator.set_acknowledged_bitrate(estimate_1 / 2); + + let mut feedback_count: i64 = 1; + while result.state != LossBasedState::IncreaseUsingPadding { + assert!(feedback_count < 100, "never reached IncreaseUsingPadding"); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + feedback_count * OBSERVATION_DURATION_LOWER_BOUND, + ), + delay_based_estimate, + /*in_alr=*/ false, + ); + feedback_count += 1; + result = loss_based_bandwidth_estimator.get_loss_based_result(); + } + + assert_eq!(result.state, LossBasedState::IncreaseUsingPadding); + // The estimate increases by 1kbps. + assert_eq!( + result.bandwidth_estimate, + estimate_1 + DataRate::from_bits_per_sec(1) + ); + } + + // After loss based bwe backs off, the estimate is bounded during the delayed + // window. + #[test] + fn estimate_bitrate_is_bounded_during_delayed_window_after_loss_based_bwe_backs_off() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + DELAYED_INCREASE_WINDOW - TimeDelta::from_millis(2), + ); + let enough_feedback_3: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + DELAYED_INCREASE_WINDOW - TimeDelta::from_millis(1), + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(300)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + // Increase the acknowledged bitrate to make sure that the estimate is not + // capped too low. + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(5000)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + delay_based_estimate, + /*in_alr=*/ false, + ); + + // The estimate is capped by current_estimate * MaxIncreaseFactor because + // it recently backed off. + let estimate_2: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_3, + delay_based_estimate, + /*in_alr=*/ false, + ); + // The latest estimate is the same as the previous estimate since the sent + // packets were sent within the DelayedIncreaseWindow. + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + estimate_2 + ); + } + + // The estimate is not bounded after the delayed increase window. + #[test] + fn keep_increasing_estimate_after_delayed_increase_window() { + let mut test = LossBasedBweV2Test::default(); + let enough_feedback_1: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_2: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + DELAYED_INCREASE_WINDOW - TimeDelta::from_millis(1), + ); + let enough_feedback_3: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + DELAYED_INCREASE_WINDOW + TimeDelta::from_millis(1), + ); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(300)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + // Increase the acknowledged bitrate to make sure that the estimate is not + // capped too low. + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(5000)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_2, + delay_based_estimate, + /*in_alr=*/ false, + ); + + // The estimate is capped by current_estimate * MaxIncreaseFactor because it + // recently backed off. + let estimate_2: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_3, + delay_based_estimate, + /*in_alr=*/ false, + ); + // The estimate can continue increasing after the DelayedIncreaseWindow. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + >= estimate_2 + ); + } + + #[test] + fn not_increase_if_inherent_loss_less_than_average_loss() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + candidate_factors: vec![1.25], + not_increase_if_inherent_loss_less_than_average_loss: true, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + let enough_feedback_10p_loss_1: Vec = test + .create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + DataSize::from_bytes(PACKET_SIZE), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_10p_loss_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + let enough_feedback_10p_loss_2: Vec = test + .create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + DataSize::from_bytes(PACKET_SIZE), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_10p_loss_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + // Do not increase the bitrate because inherent loss is less than average loss + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(600) + ); + } + + #[test] + fn select_high_bandwidth_candidate_if_loss_rate_is_less_than_threshold() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + loss_threshold_of_high_bandwidth_preference: 0.20, + not_increase_if_inherent_loss_less_than_average_loss: false, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + let enough_feedback_10p_loss_1: Vec = test + .create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + DataSize::from_bytes(PACKET_SIZE), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_10p_loss_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + + let enough_feedback_10p_loss_2: Vec = test + .create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + DataSize::from_bytes(PACKET_SIZE), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_10p_loss_2, + delay_based_estimate, + /*in_alr=*/ false, + ); + + // Because LossThresholdOfHighBandwidthPreference is 20%, the average loss is + // 10%, bandwidth estimate should increase. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + > DataRate::from_kilobits_per_sec(600) + ); + } + + #[test] + fn select_low_bandwidth_candidate_if_loss_rate_is_is_higher_than_threshold() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + loss_threshold_of_high_bandwidth_preference: 0.05, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + let enough_feedback_10p_loss_1: Vec = test + .create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + DataSize::from_bytes(PACKET_SIZE), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_10p_loss_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + + let enough_feedback_10p_loss_2: Vec = test + .create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + DataSize::from_bytes(PACKET_SIZE), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_10p_loss_2, + delay_based_estimate, + /*in_alr=*/ false, + ); + + // Because LossThresholdOfHighBandwidthPreference is 5%, the average loss is + // 10%, bandwidth estimate should decrease. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + < DataRate::from_kilobits_per_sec(600) + ); + } + + #[test] + fn estimate_is_not_higher_than_max_bitrate() { + let mut test = LossBasedBweV2Test::default(); + let config = config(true, true); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000), + ); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(1000)); + let enough_feedback: Vec = test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + <= DataRate::from_kilobits_per_sec(1000) + ); + } + + #[test] + fn not_back_off_to_acked_rate_in_alr() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(100), + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000000), + ); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + let acked_rate: DataRate = DataRate::from_kilobits_per_sec(100); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(acked_rate); + let enough_feedback_100p_loss_1: Vec = test + .create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_100p_loss_1, + delay_based_estimate, + /*in_alr=*/ true, + ); + + // Make sure that the estimate decreases but higher than acked rate. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + > acked_rate + ); + + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + < DataRate::from_kilobits_per_sec(600) + ); + } + + #[test] + fn back_off_to_acked_rate_if_not_in_alr() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(100), + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000000), + ); + let delay_based_estimate: DataRate = DataRate::from_kilobits_per_sec(5000); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + + let acked_rate: DataRate = DataRate::from_kilobits_per_sec(100); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(acked_rate); + let enough_feedback_100p_loss_1: Vec = test + .create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_100p_loss_1, + delay_based_estimate, + /*in_alr=*/ false, + ); + + // Make sure that the estimate decreases but higher than acked rate. + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + acked_rate + ); + } + + #[test] + fn not_ready_to_use_in_start_phase() { + let config = LossBasedBweV2Config { + use_in_start_phase: true, + ..short_observation_config() + }; + let loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + // Make sure that the estimator is not ready to use in start phase because of + // lacking TWCC feedback. + assert!(!loss_based_bandwidth_estimator.ready_to_use_in_start_phase()); + } + + #[test] + fn ready_to_use_in_start_phase() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + use_in_start_phase: true, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + let enough_feedback: Vec = test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback, + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(600), + /*in_alr=*/ false, + ); + assert!(loss_based_bandwidth_estimator.ready_to_use_in_start_phase()); + } + + #[test] + fn bound_estimate_by_acked_rate() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + lower_bound_by_acked_rate_factor: 1.0, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000000), + ); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(500)); + + let enough_feedback_100p_loss_1: Vec = test + .create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_100p_loss_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(500) + ); + } + + #[test] + fn not_bound_estimate_by_acked_rate() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + lower_bound_by_acked_rate_factor: 0.0, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000000), + ); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(600)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(500)); + + let enough_feedback_100p_loss_1: Vec = test + .create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_100p_loss_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + < DataRate::from_kilobits_per_sec(500) + ); + } + + #[test] + fn has_decrease_state_because_of_upper_bound() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + candidate_factors: vec![1.0], + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10), + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000000), + ); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(500)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(100)); + + let enough_feedback_10p_loss_1: Vec = test + .create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + DataSize::from_bytes(PACKET_SIZE), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_10p_loss_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + // Verify that the instant upper bound decreases the estimate, and state is + // updated to Decreasing. + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(200) + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + } + + #[test] + fn has_increase_state_because_of_lower_bound() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + candidate_factors: vec![1.0], + lower_bound_by_acked_rate_factor: 10.0, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000000), + ); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(500)); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(1)); + + // Network has a high loss to create a loss scenario. + let enough_feedback_50p_loss_1: Vec = test + .create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_50p_loss_1, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + + // Network still has a high loss, but better acked rate. + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(200)); + let enough_feedback_50p_loss_2: Vec = test + .create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * 2, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_50p_loss_2, + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + // Verify that the instant lower bound increases the estimate, and state is + // updated to IncreaseUsingPadding. + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(200) * 10 + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::IncreaseUsingPadding + ); + } + + #[test] + fn estimate_increase_slowly_from_instant_upper_bound_in_alr_if_field_trial() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + upper_bound_candidate_in_alr: true, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(1000)); + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(150)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + let result_after_loss: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + assert_eq!(result_after_loss.state, LossBasedState::Decreasing); + + for feedback_count in 1..=3 { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + feedback_count * OBSERVATION_DURATION_LOWER_BOUND, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + } + // Expect less than 100% increase. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + < 2 * result_after_loss.bandwidth_estimate + ); + } + + #[test] + fn has_delay_based_state_if_loss_based_bwe_is_max() { + let mut test = LossBasedBweV2Test::default(); + let config = short_observation_config(); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_min_max_bitrate( + /*min_bitrate=*/ DataRate::from_kilobits_per_sec(10), + /*max_bitrate=*/ DataRate::from_kilobits_per_sec(1000), + ); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + /*feedback = */ + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(2000), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::DelayBasedEstimate + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(1000) + ); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + /*feedback=*/ + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + ), + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(2000), + /*in_alr=*/ false, + ); + let mut result: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + assert_eq!(result.state, LossBasedState::Decreasing); + assert!(result.bandwidth_estimate < DataRate::from_kilobits_per_sec(1000)); + + // Eventually the estimator recovers to delay based state. + let mut feedback_count: i64 = 2; + while result.state != LossBasedState::DelayBasedEstimate { + assert!(feedback_count < 100, "never reached DelayBasedEstimate"); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + /*feedback = */ + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + feedback_count * OBSERVATION_DURATION_LOWER_BOUND, + ), + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(2000), + /*in_alr=*/ false, + ); + feedback_count += 1; + result = loss_based_bandwidth_estimator.get_loss_based_result(); + } + assert_eq!(result.state, LossBasedState::DelayBasedEstimate); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(1000) + ); + } + + #[test] + fn increase_using_padding_state_if_field_trial() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + padding_duration: TimeDelta::from_millis(1000), + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(2500)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + + let mut feedback_count: i64 = 1; + while loss_based_bandwidth_estimator.get_loss_based_result().state + != LossBasedState::IncreaseUsingPadding + { + assert!(feedback_count < 100, "never reached IncreaseUsingPadding"); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_count, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + feedback_count += 1; + } + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::IncreaseUsingPadding + ); + } + + #[test] + fn best_candidate_resets_to_upper_bound_in_field_trial() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + padding_duration: TimeDelta::from_millis(1000), + bound_best_candidate: true, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(2500)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + let result_after_loss: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + assert_eq!(result_after_loss.state, LossBasedState::Decreasing); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + 2 * OBSERVATION_DURATION_LOWER_BOUND, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + // After a BWE decrease due to large loss, BWE is expected to ramp up slowly + // and follow the acked bitrate. + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::IncreaseUsingPadding + ); + assert_relative_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .kbps_float(), + result_after_loss.bandwidth_estimate.kbps_float(), + epsilon = 100.0 + ); + } + + #[test] + fn decrease_to_acked_candidate_if_padding_in_alr() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + padding_duration: TimeDelta::from_millis(1000), + // Set InstantUpperBoundBwBalance high to disable InstantUpperBound cap. + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10000), + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(1000)); + let mut feedback_id: i64 = 0; + while loss_based_bandwidth_estimator.get_loss_based_result().state + != LossBasedState::Decreasing + { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_id, + 2, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + feedback_id += 1; + } + + while loss_based_bandwidth_estimator.get_loss_based_result().state + != LossBasedState::IncreaseUsingPadding + { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_id, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + feedback_id += 1; + } + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + > DataRate::from_kilobits_per_sec(900) + ); + + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(100)); + // Padding is sent now, create some lost packets. + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_id, + 2, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ true, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(100) + ); + } + + #[test] + fn decrease_after_padding() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + padding_duration: TimeDelta::from_millis(1000), + bw_rampup_upper_bound_factor: 2.0, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(2500)); + let mut acknowledged_bitrate: DataRate = DataRate::from_kilobits_per_sec(51); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(acknowledged_bitrate); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + acknowledged_bitrate + ); + + acknowledged_bitrate = DataRate::from_kilobits_per_sec(26); + loss_based_bandwidth_estimator.set_acknowledged_bitrate(acknowledged_bitrate); + let mut feedback_id: i64 = 1; + while loss_based_bandwidth_estimator.get_loss_based_result().state + != LossBasedState::IncreaseUsingPadding + { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_id, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + feedback_id += 1; + } + + let estimate_increased: Timestamp = + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_id; + // The state is IncreaseUsingPadding for a while without changing the + // estimate, which is limited by 2 * acked rate. + while loss_based_bandwidth_estimator.get_loss_based_result().state + == LossBasedState::IncreaseUsingPadding + { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * feedback_id, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + feedback_id += 1; + } + + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + let start_decreasing: Timestamp = + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * (feedback_id - 1); + assert_eq!( + start_decreasing - estimate_increased, + TimeDelta::from_seconds(1) + ); + } + + #[test] + fn hold_rate_not_lower_than_acked_rate() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + hold_duration_factor: 10.0, + lower_bound_by_acked_rate_factor: 1.0, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(2500)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + + // During the hold duration, hold rate is not lower than the acked rate. + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(1000)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(1000) + ); + } + + #[test] + fn estimate_not_lower_than_acked_rate() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + lower_bound_by_acked_rate_factor: 1.0, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(2500)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + 2, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + < DataRate::from_kilobits_per_sec(1000) + ); + + loss_based_bandwidth_estimator + .set_acknowledged_bitrate(DataRate::from_kilobits_per_sec(1000)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + 2, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DataRate::from_kilobits_per_sec(1000) + ); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * 2, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * 3, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + + // Verify that the estimate recovers from the acked rate. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + > DataRate::from_kilobits_per_sec(1000) + ); + } + + #[test] + fn end_hold_duration_if_delay_based_estimate_works() { + let mut test = LossBasedBweV2Test::default(); + let config = short_observation_config(); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator + .set_bandwidth_estimate(DataRate::from_kilobits_per_sec(2500)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_50p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + let estimate: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + ), + /*delay_based_estimate=*/ estimate + DataRate::from_kilobits_per_sec(10), + /*in_alr=*/ false, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * 2, + ), + /*delay_based_estimate=*/ estimate + DataRate::from_kilobits_per_sec(10), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::DelayBasedEstimate + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + estimate + DataRate::from_kilobits_per_sec(10) + ); + } + + #[test] + fn use_byte_loss_rate() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + use_byte_loss_rate: true, + ..short_observation_config() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DataRate::from_kilobits_per_sec(500)); + // Create packet feedback having 10% packet loss but more than 50% byte loss. + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_10p_packet_loss_rate( + /*first_packet_timestamp=*/ Timestamp::zero(), + /*lost_packet_size=*/ DataSize::from_bytes(PACKET_SIZE * 20), + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + // The estimate is bounded by the instant upper bound due to high loss. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + < DataRate::from_kilobits_per_sec(160) + ); + } + + #[test] + fn use_byte_loss_rate_ignore_loss_spike() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + use_byte_loss_rate: true, + observation_window_size: 5, + ..Default::default() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + const DELAY_BASED_ESTIMATE: DataRate = DataRate::from_kilobits_per_sec(500); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DELAY_BASED_ESTIMATE); + + // Fill the observation window. + for i in 0..5 { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + i * OBSERVATION_DURATION_LOWER_BOUND, + ), + DELAY_BASED_ESTIMATE, + /*in_alr=*/ false, + ); + } + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + 5 * OBSERVATION_DURATION_LOWER_BOUND, + 2, + ), + DELAY_BASED_ESTIMATE, + /*in_alr=*/ false, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + 6 * OBSERVATION_DURATION_LOWER_BOUND, + ), + DELAY_BASED_ESTIMATE, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::DelayBasedEstimate + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + DELAY_BASED_ESTIMATE + ); + + // But if more loss happen in a new observation, BWE back down. + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + 7 * OBSERVATION_DURATION_LOWER_BOUND, + 2, + ), + DELAY_BASED_ESTIMATE, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + < DELAY_BASED_ESTIMATE + ); + } + + #[test] + fn use_byte_loss_rate_does_not_ignore_loss_spike_on_send_burst() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + use_byte_loss_rate: true, + observation_window_size: 5, + ..Default::default() + }; + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + const DELAY_BASED_ESTIMATE: DataRate = DataRate::from_kilobits_per_sec(500); + loss_based_bandwidth_estimator.set_bandwidth_estimate(DELAY_BASED_ESTIMATE); + + // Fill the observation window. + for i in 0..5 { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + i * OBSERVATION_DURATION_LOWER_BOUND, + ), + DELAY_BASED_ESTIMATE, + /*in_alr=*/ false, + ); + } + + // If the loss happens when increasing sending rate, then + // the BWE should back down. + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + 5 * OBSERVATION_DURATION_LOWER_BOUND, + /*num_packets=*/ 5, + ), + DELAY_BASED_ESTIMATE, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + <= DELAY_BASED_ESTIMATE + ); + } + + #[test] + fn select_higher_candidate_if_having_same_objective() { + let mut test = LossBasedBweV2Test::default(); + let config = short_observation_config(); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + const START_BITRATE: DataRate = DataRate::from_kilobits_per_sec(1000); + loss_based_bandwidth_estimator.set_bandwidth_estimate(START_BITRATE); + let enough_feedback_01: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ); + let enough_feedback_02: Vec = test + .create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_01, + START_BITRATE, + /*in_alr=*/ false, + ); + let delay_based_estimate = + DataRate::from_bytes_per_sec_float(START_BITRATE.bytes_per_sec() as f64 * 1.02 + 1.0); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_02, + delay_based_estimate, + /*in_alr=*/ false, + ); + + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + delay_based_estimate + ); + } +} // namespace diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs new file mode 100644 index 00000000000..fd777386cac --- /dev/null +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -0,0 +1,540 @@ +//! libwebrtc's Google Congestion Control (GoogCC), driven by QUIC receive timestamps. +//! +//! The submodules are a Rust port of WebRTC's `modules/congestion_controller/goog_cc`, with the +//! parts of `api/transport`, `api/units`, `modules/pacing` and `modules/remote_bitrate_estimator` +//! it uses. They began as [goog_cc](https://github.com/kixelated/goog_cc) 0.1.4, a port of WebRTC +//! [da2a3d6c4d](https://webrtc.googlesource.com/src/+/da2a3d6c4d) (2024-12-09), and are refreshed +//! to WebRTC [556f7eec27](https://webrtc.googlesource.com/src/+/556f7eec27) (2026-11-01). They keep +//! WebRTC's BSD license, in `LICENSE` beside them, with its `PATENTS` grant and `AUTHORS`. +//! +//! Each file follows its upstream counterpart, so a refresh diffs WebRTC between the recorded +//! revision and a newer one and applies the changes file by file. What a QUIC sender has no source +//! for is gone, and each file's own docs say what: RTCP receiver reports and REMB, route changes, +//! the network state estimate, separate audio detection, RTP sequence numbers, ECN feedback, +//! padding, and the `AcknowledgedBitrateEstimator` that a disabled-by-default field trial selects. +//! Field trials are typed settings with upstream's defaults (`FieldTrials`). +//! +//! [`Gcc`] adapts it to [`Controller`]: each ACK becomes transport feedback carrying the peer's +//! receive times and each loss a feedback of lost packets, GoogCC's process timer runs from the +//! controller's calls, and a port of WebRTC's `BitrateProber` paces its probe clusters. + +/// `approx::assert_relative_eq!`: within `epsilon`, or within a relative `f64::EPSILON`. +#[cfg(test)] +macro_rules! assert_relative_eq { + ($a:expr, $b:expr $(,)?) => { + assert_relative_eq!($a, $b, epsilon = f64::EPSILON) + }; + ($a:expr, $b:expr, epsilon = $e:expr $(,)?) => {{ + let (a, b, e): (f64, f64, f64) = ($a.into(), $b.into(), $e.into()); + assert!( + (a - b).abs() <= e || (a - b).abs() <= a.abs().max(b.abs()) * f64::EPSILON, + "{a} is not within {e} of {b}" + ); + }}; +} + +mod api; +mod experiments; +mod pacing; +mod remote_bitrate_estimator; + +mod alr_detector; +mod congestion_window_pushback_controller; +mod delay_based_bwe; +mod delay_increase_detector_interface; +mod goog_cc_network_control; +mod inter_arrival_delta; +mod link_capacity_estimator; +mod loss_based_bwe_v2; +mod probe_bitrate_estimator; +mod probe_controller; +mod robust_throughput_estimator; +mod send_side_bandwidth_estimation; +mod trendline_estimator; + +use alr_detector::*; +use congestion_window_pushback_controller::*; +use delay_based_bwe::*; +use delay_increase_detector_interface::*; +use inter_arrival_delta::*; +use link_capacity_estimator::*; +use loss_based_bwe_v2::*; +use probe_bitrate_estimator::*; +use probe_controller::*; +use robust_throughput_estimator::*; +use send_side_bandwidth_estimation::*; +use trendline_estimator::*; + +use goog_cc_network_control::{GoogCcNetworkController, NetworkControllerConfig}; + +#[cfg(test)] +mod tests; + +#[cfg(test)] +mod bench; + +use std::{collections::VecDeque, num::NonZeroU16, sync::Arc, time::Duration}; + +use kio::clock::Instant; + +use crate::{ + Ack, Ce, Controller, Factory, Init, Loss, Metrics, Packet, Sent, + pacer::{self, Pacer, Rate}, +}; +use api::{ + transport::{ + NetworkAvailability, NetworkControlUpdate, PacedPacketInfo, PacerConfig, PacketResult, + ProcessInterval, RoundTripTimeUpdate, SentPacket, StreamsConfig, TargetRateConstraints, + TransportPacketsFeedback, + }, + units::{DataRate, DataSize, TimeDelta, Timestamp}, +}; +use pacing::BitrateProber; + +/// How often GoogCC expects its process timer, as `GetProcessInterval` +const PROCESS_INTERVAL: Duration = Duration::from_millis(25); +/// How far a mapped peer receive time may sit from the local clock: a peer's clock drifts far +/// less over a connection, and the bound keeps a hostile time from overflowing GoogCC's units. +const RECEIVE_SKEW: TimeDelta = TimeDelta::from_seconds(3600); +/// The smallest window GoogCC's congestion window settles on, `kMinCwnd` +const MIN_WINDOW: u64 = 2 * 1500; +/// How long a probe cluster's settings are kept for its packets' feedback, past the +/// `ProbeBitrateEstimator`'s own one second of history +const CLUSTER_HISTORY: TimeDelta = TimeDelta::from_seconds(2); + +/// The rate GoogCC starts at, whose initial probes go out at three and six times it: 1 Mbit/s, +/// rather than WebRTC's 300 kbit/s. +/// +/// WebRTC's encoder starts at GoogCC's rate, but a transport's sender writes at its own. From +/// 300 kbit/s the initial probing took several round trips to reach a few Mbit/s while 2 Mbit/s +/// of media queued behind the pacer. Draining that backlog once a probe found the path queued it +/// at the bottleneck, and the delay-based estimate cut the rate to 85% of the throughput +/// acknowledged so far, from a sender still starting up. Held above the estimate, the media kept +/// GoogCC out of the application-limited region, so no probe followed, and the estimate climbed +/// back at 8% a second. Only the 5 Mbit/s ceiling `ProbeController` keeps without a maximum rate +/// hid this: the probe it capped happened to restore the estimate after the cut. +const INITIAL_RATE: DataRate = DataRate::from_kilobits_per_sec(1000); +/// The most GoogCC estimates or probes when the transport sets no pacing limit, the 1 Gbit/s +/// `SendSideBandwidthEstimation` already defaults to. Without one, `ProbeController` caps every +/// probe at 5 Mbit/s, so a probe target above that could never be reached. +const MAX_RATE: DataRate = DataRate::from_kilobits_per_sec(1_000_000); + +/// Configuration for the [`Gcc`] congestion controller, which has no settings yet. +#[derive(Debug, Clone, Default)] +pub struct Config { + _private: (), +} + +impl Factory for Config { + fn build(self: Arc, init: Init) -> Box { + Box::new(Gcc::new(init)) + } +} + +/// A probe cluster's settings, kept for its packets' feedback +#[derive(Debug, Clone, Copy)] +struct Cluster { + info: PacedPacketInfo, + /// When its last packet was sent + sent: Timestamp, +} + +/// libwebrtc's Google Congestion Control, fed by receive timestamps. +/// +/// The window is GoogCC's congestion window (its target rate times the round trip plus 350ms), +/// the pacing rate its pacer's, and the bandwidth estimate its target rate. Each probe cluster +/// GoogCC requests paces at its rate while it runs and reports what it still needs as +/// [`Metrics::probe_inflight`], so an application-limited sender can fill it. +#[derive(Clone)] +pub struct Gcc { + cc: GoogCcNetworkController, + prober: BitrateProber, + pacer: Pacer, + mtu: u16, + /// The pacing rate GoogCC last configured, in bytes per second + pacing_rate: u64, + /// The target rate GoogCC last reported, in bytes per second + target_rate: u64, + /// The window until GoogCC computes one from feedback + initial_window: u64, + in_flight: u64, + /// Whether an ACK has arrived, which makes the network available to GoogCC and starts its + /// initial probes: before then a probe would only pace the handshake, which waits a round + /// trip between flights + available: bool, + next_process: Instant, + /// The local time, in microseconds, a peer receive time of zero maps to, fixed by the first + /// receive time the peer reports + receive_basis: Option, + /// Recently sent probe clusters, oldest first + clusters: VecDeque, + /// A probe target not yet handed to GoogCC, in bytes per second, which waits for a call that + /// knows the time + probe_target: Option, + /// Reused for each ACK's and loss's feedback + feedback: TransportPacketsFeedback, +} + +impl Gcc { + fn new(init: Init) -> Self { + let now = timestamp(init.now); + let starting_rate = INITIAL_RATE; + let cc = GoogCcNetworkController::new(NetworkControllerConfig { + constraints: TargetRateConstraints { + at_time: now, + min_data_rate: None, + max_data_rate: Some(init.max_pacing_rate.map_or(MAX_RATE, |rate| { + DataRate::from_bytes_per_sec(rate.min(i64::MAX as u64) as i64) + })), + starting_rate: Some(starting_rate), + }, + stream_based_config: StreamsConfig { + // Periodic probing while application-limited, so a sender held below its target + // finds the capacity to resume a stopped rendition + requests_alr_probing: Some(true), + ..Default::default() + }, + default_pacing_time_window: PacerConfig::DEFAULT_TIME_INTERVAL, + ..Default::default() + }); + let window_time = TimeDelta::from_micros(init.rtt.as_micros().min(i64::MAX as u128) as i64) + + TimeDelta::from_millis(experiments::CongestionWindowConfig::default().queue_size_ms); + let initial_window = ((starting_rate * window_time).bytes().max(0) as u64).max(MIN_WINDOW); + let mut gcc = Self { + cc, + prober: BitrateProber::default(), + pacer: Pacer::new( + init.rtt, + initial_window, + init.mtu, + init.max_pacing_rate, + init.now, + ), + mtu: init.mtu, + pacing_rate: starting_rate.bytes_per_sec() as u64, + target_rate: starting_rate.bytes_per_sec() as u64, + initial_window, + in_flight: 0, + available: false, + next_process: init.now, + receive_basis: None, + clusters: VecDeque::new(), + probe_target: None, + feedback: TransportPacketsFeedback::default(), + }; + gcc.process(init.now); + gcc + } + + /// Hands GoogCC a new probe target, then runs its process timer if it is due. + fn process(&mut self, now: Instant) { + if let Some(target) = self.probe_target.take() { + // The session's target is the rate its streams would send at, which GoogCC probes + // toward as soon as it exceeds the estimate, capping its probes at twice the target + let update = self.cc.on_streams_config(StreamsConfig { + at_time: timestamp(now), + max_total_allocated_bitrate: Some(DataRate::from_bytes_per_sec( + target.min(i64::MAX as u64) as i64, + )), + ..Default::default() + }); + self.apply(update); + } + if now < self.next_process { + return; + } + self.next_process = now + PROCESS_INTERVAL; + let update = self.cc.on_process_interval(ProcessInterval { + at_time: timestamp(now), + pacer_queue: None, + }); + self.apply(update); + } + + fn apply(&mut self, update: NetworkControlUpdate) { + if let Some(target) = update.target_rate { + self.target_rate = target.target_rate.bytes_per_sec().max(0) as u64; + } + if let Some(pacer) = update.pacer_config { + self.pacing_rate = (pacer.data_rate().bytes_per_sec().max(1)) as u64; + } + for cluster in &update.probe_cluster_configs { + self.prober.create_probe_cluster(cluster); + } + } + + /// Maps a peer receive time onto the local clock. + /// + /// The first one the peer reports lands at the local time its ACK arrived, so the mapped + /// times carry the forward delay of that packet as a constant offset, which GoogCC's + /// gradients ignore. + fn receive_time(&mut self, received: Duration, now: Instant) -> Timestamp { + let now = timestamp(now).us(); + // In i128, where a peer's time, at most 2^64 seconds, cannot overflow + let received = received.as_micros() as i128; + let basis = *self + .receive_basis + .get_or_insert_with(|| i128::from(now) - received); + let skew = i128::from(RECEIVE_SKEW.us()); + let mapped = (received + basis).clamp(i128::from(now) - skew, i128::from(now) + skew); + Timestamp::from_micros(mapped as i64) + } + + /// The feedback for `packet`, acknowledged at `now`. + /// + /// Until the peer reports a receive time, the ACK's arrival stands in for each packet's, so + /// GoogCC runs on the round-trip delay. Once it reports one, a packet without one, as when + /// the ACK carrying its time was lost, still counts as received for the loss-based estimate, + /// but not toward delay, rather than mix the two clocks. + fn acked(&mut self, packet: &crate::Acked, now: Instant) -> PacketResult { + let (receive_time, ambiguous_receive_time) = match packet.received { + Some(received) => (self.receive_time(received, now), false), + None => (timestamp(now), self.receive_basis.is_some()), + }; + PacketResult { + sent_packet: SentPacket { + send_time: timestamp(packet.sent), + size: DataSize::from_bytes(packet.bytes.into()), + pacing_info: self.cluster(packet.packet.probe_cluster), + data_in_flight: DataSize::zero(), + }, + receive_time, + ambiguous_receive_time, + } + } + + /// The pacing info of the probe cluster `id` was sent in. + fn cluster(&self, id: Option) -> PacedPacketInfo { + id.and_then(|id| { + self.clusters + .iter() + .rev() + .find(|cluster| short_id(cluster.info.probe_cluster_id) == Some(id)) + }) + .map_or_else(PacedPacketInfo::default, |cluster| cluster.info) + } + + fn feed(&mut self, now: Instant, in_flight: u64) { + if self.feedback.packet_feedbacks.is_empty() { + return; + } + self.feedback.feedback_time = timestamp(now); + self.feedback.data_in_flight = size(in_flight); + let update = self.cc.on_transport_packets_feedback(&self.feedback); + self.feedback.packet_feedbacks.clear(); + self.apply(update); + } + + /// The window GoogCC allows in flight. + fn congestion_window(&self) -> u64 { + let window = self + .cc + .congestion_window() + .map_or(self.initial_window, |window| window.bytes().max(0) as u64); + window.max(MIN_WINDOW) + } + + /// The bytes in flight up to which the current probe cluster may be filled. + /// + /// In whole packets: a cluster short of a packet's bytes still needs one more, and a sender + /// held to the bytes alone would leave it unfinished. + fn probe_inflight(&self) -> Option { + let (bytes, packets, _) = self.prober.remaining()?; + let mtu = u64::from(self.mtu); + let packets = u64::try_from(packets).unwrap_or(0) * mtu; + let bytes = u64::try_from(bytes.bytes()) + .unwrap_or(0) + .next_multiple_of(mtu); + Some(self.in_flight + bytes.max(packets)) + } +} + +impl Controller for Gcc { + fn on_sent(&mut self, sent: Sent) -> Packet { + self.process(sent.now); + let now = timestamp(sent.now); + self.in_flight = sent.in_flight; + self.pacer.on_transmit(sent.bytes); + let bytes = DataSize::from_bytes(sent.bytes.into()); + + // A large enough packet starts a pending probe cluster, as WebRTC's pacer starts one + // when media is queued, so the handshake's small last flight does not start a cluster + // only for the idle round trip after it to abandon + self.prober.on_incoming_packet(bytes); + let info = self.prober.current_cluster(now); + if info.is_some() { + self.prober.probe_sent(now, bytes); + } + let info = info.unwrap_or_default(); + let probe_cluster = short_id(info.probe_cluster_id); + if probe_cluster.is_some() { + match self.clusters.back_mut() { + Some(last) if last.info.probe_cluster_id == info.probe_cluster_id => { + last.sent = now; + } + _ => self.clusters.push_back(Cluster { info, sent: now }), + } + } + while self + .clusters + .front() + .is_some_and(|cluster| now - cluster.sent > CLUSTER_HISTORY) + { + self.clusters.pop_front(); + } + + let update = self.cc.on_sent_packet(SentPacket { + send_time: now, + size: bytes, + pacing_info: info, + data_in_flight: size(sent.in_flight), + }); + self.apply(update); + Packet { + probe_cluster, + ..Default::default() + } + } + + fn on_ack(&mut self, ack: Ack<'_>) { + self.process(ack.now); + self.in_flight = ack.in_flight; + if !self.available { + self.available = true; + let update = self.cc.on_network_availability(NetworkAvailability { + at_time: timestamp(ack.now), + network_available: true, + }); + self.apply(update); + } + if !ack.rtt.is_zero() { + self.cc.on_round_trip_time_update(RoundTripTimeUpdate { + round_trip_time: TimeDelta::from_micros( + ack.rtt.as_micros().min(i64::MAX as u128) as i64 + ), + }); + } + for packet in ack.packets { + let result = self.acked(packet, ack.now); + self.feedback.packet_feedbacks.push(result); + } + self.feed(ack.now, ack.in_flight); + } + + fn on_loss(&mut self, loss: Loss<'_>) { + self.process(loss.now); + self.in_flight = loss.in_flight; + for packet in loss.packets { + let result = PacketResult { + sent_packet: SentPacket { + send_time: timestamp(packet.sent), + size: DataSize::from_bytes(packet.bytes.into()), + pacing_info: self.cluster(packet.packet.probe_cluster), + data_in_flight: DataSize::zero(), + }, + receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, + }; + self.feedback.packet_feedbacks.push(result); + } + self.feed(loss.now, loss.in_flight); + } + + // GoogCC reacts to delay and loss; ECN is SCReAM's signal. + fn on_ce(&mut self, _ce: Ce) {} + + fn on_mtu_update(&mut self, new_mtu: u16) { + self.mtu = new_mtu; + } + + fn on_app_limited(&mut self, in_flight: u64) { + self.in_flight = in_flight; + self.prober.on_app_limited(); + } + + fn window(&self) -> u64 { + self.congestion_window() + } + + fn pace(&mut self, now: Instant, bytes: u64) -> Option { + self.process(now); + let at = timestamp(now); + if self.prober.current_cluster(at).is_some() { + let next = self.prober.next_probe_time(); + if next <= at { + return None; + } + return Some(now + Duration::from_micros((next - at).us().max(0) as u64)); + } + let rate = Rate { + window: self.congestion_window(), + pacing_rate: Some(self.pacing_rate), + }; + // A pacing rate makes the RTT irrelevant; the MTU only sizes the bursts. + self.pacer.delay(Duration::ZERO, bytes, self.mtu, now, rate) + } + + /// The pacer's own burst budget, or a single packet while a probe cluster runs. + /// + /// A transport checks each datagram's pace while the one before it is still being built, so + /// a batch longer than the budget releases a packet the budget never pays for. Under + /// 2 Mbit/s the budget is one packet, and batching two would pace at twice the rate. + fn burst(&self) -> Option { + let mtu = u64::from(self.mtu); + if self.prober.remaining().is_some() { + return Some(mtu); + } + Some(pacer::rate_capacity(self.pacing_rate, self.mtu).max(mtu)) + } + + fn metrics(&self) -> Metrics { + Metrics { + congestion_window: self.congestion_window(), + ssthresh: None, + pacing_rate: Some(self.pacing_rate), + bandwidth_estimate: Some(self.target_rate), + probe_inflight: self.probe_inflight(), + } + } + + fn probes(&self) -> bool { + true + } + + /// GoogCC reads a probe at the slower of its send and receive rates, so a cluster held below + /// its rate reads no higher, and its size bounds what the duplicates filling it cost. + fn paces_probe(&self) -> bool { + true + } + + fn on_probe_target(&mut self, bytes_per_second: u64) { + self.probe_target = Some(bytes_per_second); + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +fn timestamp(instant: Instant) -> Timestamp { + let micros = instant.duration_since(Instant::ZERO).as_micros(); + Timestamp::from_micros(micros.min(i64::MAX as u128 / 2) as i64) +} + +fn size(bytes: u64) -> DataSize { + DataSize::from_bytes(bytes.min(i64::MAX as u64 / 2) as i64) +} + +/// The probe cluster id a [`Packet`] stores, in sixteen bits without zero. GoogCC's ids, which +/// count up from one, wrap within the lower half, and the prober's restart ids within the upper +/// half, so a restarted cluster never shares a short id with one of GoogCC's. +fn short_id(id: i32) -> Option { + const HALF: u32 = 1 << 15; + let id = u32::try_from(id).ok().filter(|&id| id > 0)?; + let restart_base = BitrateProber::RESTART_ID_BASE as u32; + let short = match id.checked_sub(restart_base) { + Some(restart) => HALF | (restart % HALF), + None => (id - 1) % (HALF - 1) + 1, + }; + NonZeroU16::new(short as u16) +} diff --git a/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs b/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs new file mode 100644 index 00000000000..89263d1a3be --- /dev/null +++ b/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs @@ -0,0 +1,399 @@ +/* + * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! WebRTC's pacer sends each probe cluster GoogCC asks for, padding with old media when it has no +//! new. Here the cluster paces the controller instead: its packets leave at the cluster's rate, +//! and while it runs the transport may fill it with duplicates of data in flight (QUIC's native +//! probing). The `WebRTC-Bwe-ProbingBehavior` settings keep their defaults. + +use std::collections::VecDeque; + +use crate::gcc::api::{ + transport::{PacedPacketInfo, ProbeClusterConfig}, + units::{DataRate, DataSize, TimeDelta, Timestamp}, +}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum ProbingState { + // Probing will not be triggered in this state at all times. + Disabled, + // Probing is enabled and ready to trigger on the first packet arrival if + // there is a probe cluster. + Inactive, + // Probe cluster is filled with the set of data rates to be probed and + // probes are being sent. + Active, +} + +#[derive(Debug, Clone)] +struct ProbeCluster { + pace_info: PacedPacketInfo, + send_bitrate: DataRate, + + sent_probes: i64, + sent_bytes: i64, + min_probe_delta: TimeDelta, + requested_at: Timestamp, + started_at: Timestamp, +} + +/// Paces the probe clusters GoogCC requests, as WebRTC's `BitrateProber`. +#[derive(Debug, Clone)] +pub struct BitrateProber { + probing_state: ProbingState, + + // Probe bitrate per packet. These are used to compute the delta relative to + // the previous probe packet based on the size and time when that packet was + // sent. + clusters: VecDeque, + + // Time the next probe should be sent when in kActive state. + next_probe_time: Timestamp, + + max_probe_delay: TimeDelta, + min_packet_size: DataSize, + + /// Clusters restarted so far, numbering their new ids + restarts: i32, +} + +impl Default for BitrateProber { + fn default() -> Self { + Self { + probing_state: ProbingState::Inactive, + clusters: VecDeque::new(), + next_probe_time: Timestamp::plus_infinity(), + max_probe_delay: TimeDelta::from_millis(10), + min_packet_size: DataSize::from_bytes(200), + restarts: 0, + } + } +} + +impl BitrateProber { + const PROBE_CLUSTER_TIMEOUT: TimeDelta = TimeDelta::from_seconds(5); + const MAX_PENDING_PROBE_CLUSTERS: usize = 5; + /// Restarted clusters take ids from here up, above any GoogCC's ProbeController counts to + pub const RESTART_ID_BASE: i32 = 1 << 30; + + // Returns true if the prober is in a probing session, i.e., it currently + // wants packets to be sent out according to the time returned by + // TimeUntilNextProbe(). + #[cfg(test)] + pub fn is_probing(&self) -> bool { + self.probing_state == ProbingState::Active + } + + // Initializes a new probing session if the prober is allowed to probe. Does + // not initialize the prober unless the packet size is large enough to probe + // with. + pub fn on_incoming_packet(&mut self, packet_size: DataSize) { + self.maybe_set_active_state(packet_size); + } + + pub fn create_probe_cluster(&mut self, cluster_config: &ProbeClusterConfig) { + debug_assert!(self.probing_state != ProbingState::Disabled); + debug_assert!(cluster_config.min_probe_delta > TimeDelta::zero()); + + while let Some(front) = self.clusters.front() { + if cluster_config.at_time - front.requested_at > Self::PROBE_CLUSTER_TIMEOUT + || self.clusters.len() > Self::MAX_PENDING_PROBE_CLUSTERS + { + self.clusters.pop_front(); + } else { + break; + } + } + + let cluster = ProbeCluster { + pace_info: PacedPacketInfo::new( + cluster_config.id, + cluster_config.target_probe_count as i64, + (cluster_config.target_data_rate * cluster_config.target_duration).bytes(), + ), + send_bitrate: cluster_config.target_data_rate, + sent_probes: 0, + sent_bytes: 0, + min_probe_delta: cluster_config.min_probe_delta, + requested_at: cluster_config.at_time, + started_at: Timestamp::plus_infinity(), + }; + tracing::debug!( + bitrate = ?cluster.send_bitrate, + min_bytes = cluster.pace_info.probe_cluster_min_bytes, + min_probes = cluster.pace_info.probe_cluster_min_probes, + "probe cluster" + ); + self.clusters.push_back(cluster); + + self.maybe_set_active_state(DataSize::zero()); + } + + // Returns the time at which the next probe should be sent to get accurate + // probing. If probing is not desired at this time, Timestamp::PlusInfinity() + // will be returned. + pub fn next_probe_time(&self) -> Timestamp { + // Probing is not active or probing is already complete. + if self.probing_state != ProbingState::Active || self.clusters.is_empty() { + return Timestamp::plus_infinity(); + } + self.next_probe_time + } + + // Information about the current probing cluster. + pub fn current_cluster(&mut self, now: Timestamp) -> Option { + if self.clusters.is_empty() || self.probing_state != ProbingState::Active { + return None; + } + + if self.next_probe_time.is_finite() && now - self.next_probe_time > self.max_probe_delay { + tracing::debug!( + next = ?self.next_probe_time, + ?now, + "probe delay too high, discarding probe cluster" + ); + self.clusters.pop_front(); + if self.clusters.is_empty() { + self.probing_state = ProbingState::Inactive; + return None; + } + } + + self.clusters.front().map(|cluster| cluster.pace_info) + } + + /// The bytes and packets the next cluster still needs, at its rate, whether or not a packet + /// has started it. + pub fn remaining(&self) -> Option<(DataSize, i64, DataRate)> { + if self.probing_state == ProbingState::Disabled { + return None; + } + let cluster = self.clusters.front()?; + Some(( + DataSize::from_bytes( + (cluster.pace_info.probe_cluster_min_bytes - cluster.sent_bytes).max(0), + ), + (cluster.pace_info.probe_cluster_min_probes - cluster.sent_probes).max(0), + cluster.send_bitrate, + )) + } + + // Returns the minimum number of bytes that the prober recommends for + // the next probe, or zero if not probing. A probe can consist of multiple + // packets that are sent back to back. + pub fn recommended_min_probe_size(&self) -> DataSize { + match self.clusters.front() { + Some(cluster) => cluster.send_bitrate * cluster.min_probe_delta, + None => DataSize::zero(), + } + } + + // Called to report to the prober that a probe has been sent. In case of + // multiple packets per probe, this call would be made at the end of sending + // the last packet in probe. `size` is the total size of all packets in probe. + pub fn probe_sent(&mut self, now: Timestamp, size: DataSize) { + debug_assert!(self.probing_state == ProbingState::Active); + debug_assert!(!size.is_zero()); + + let Some(cluster) = self.clusters.front_mut() else { + return; + }; + if cluster.sent_probes == 0 { + debug_assert!(cluster.started_at.is_infinite()); + cluster.started_at = now; + } + cluster.sent_bytes += size.bytes(); + cluster.sent_probes += 1; + // Compute the time delta from the cluster start to ensure probe bitrate stays + // close to the target bitrate. + self.next_probe_time = + cluster.started_at + DataSize::from_bytes(cluster.sent_bytes) / cluster.send_bitrate; + if cluster.sent_bytes >= cluster.pace_info.probe_cluster_min_bytes + && cluster.sent_probes >= cluster.pace_info.probe_cluster_min_probes + { + self.clusters.pop_front(); + } + if self.clusters.is_empty() { + self.probing_state = ProbingState::Inactive; + } + } + + /// The sender ran out of data to send. + /// + /// WebRTC's pacer pads a probe cluster when media runs out, so a gap means the pacer fell + /// behind and the cluster is dropped. Here only the transport's duplicates can pad it, so a + /// sender with nothing to send restarts the cluster with its next packet instead, under a new + /// id: the packets it already sent span the gap, and estimating from them would read the gap + /// as the path's rate. + pub fn on_app_limited(&mut self) { + if self.probing_state != ProbingState::Active { + return; + } + let Some(cluster) = self.clusters.front_mut() else { + return; + }; + if cluster.sent_probes == 0 { + return; + } + cluster.sent_probes = 0; + cluster.sent_bytes = 0; + cluster.started_at = Timestamp::plus_infinity(); + self.restarts = (self.restarts + 1) % Self::RESTART_ID_BASE; + cluster.pace_info.probe_cluster_id = Self::RESTART_ID_BASE + self.restarts; + self.probing_state = ProbingState::Inactive; + } + + fn maybe_set_active_state(&mut self, packet_size: DataSize) { + if self.ready_to_set_active_state(packet_size) { + self.next_probe_time = Timestamp::minus_infinity(); + self.probing_state = ProbingState::Active; + } + } + + fn ready_to_set_active_state(&self, packet_size: DataSize) -> bool { + if self.clusters.is_empty() { + return false; + } + match self.probing_state { + ProbingState::Disabled | ProbingState::Active => false, + // A "large enough" packet must be sent first, before a probe can be + // generated and sent. + ProbingState::Inactive => { + packet_size + >= std::cmp::min(self.recommended_min_probe_size(), self.min_packet_size) + } + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + fn cluster(at_time: Timestamp, rate: DataRate, id: i32) -> ProbeClusterConfig { + ProbeClusterConfig { + at_time, + target_data_rate: rate, + target_duration: TimeDelta::from_millis(15), + min_probe_delta: TimeDelta::from_millis(2), + target_probe_count: 5, + id, + } + } + + // A cluster starts on a large enough packet, paces at its rate, and ends once it sent both + // its packets and its bytes. + #[test] + fn verify_states_and_time_between_probes() { + let mut prober = BitrateProber::default(); + assert!(!prober.is_probing()); + + let mut now = Timestamp::zero(); + let start_time = now; + assert_eq!(prober.next_probe_time(), Timestamp::plus_infinity()); + + const TEST_BITRATE_1: DataRate = DataRate::from_kilobits_per_sec(900); + const TEST_BITRATE_2: DataRate = DataRate::from_kilobits_per_sec(1800); + const CLUSTER_SIZE: i64 = 5; + const PROBE_SIZE: DataSize = DataSize::from_bytes(1000); + const MIN_PROBE_DURATION: TimeDelta = TimeDelta::from_millis(15); + + prober.create_probe_cluster(&cluster(now, TEST_BITRATE_1, 0)); + prober.create_probe_cluster(&cluster(now, TEST_BITRATE_2, 1)); + assert!(!prober.is_probing()); + + prober.on_incoming_packet(PROBE_SIZE); + assert!(prober.is_probing()); + assert_eq!(0, prober.current_cluster(now).unwrap().probe_cluster_id); + + // First packet should probe as soon as possible. + assert_eq!(Timestamp::minus_infinity(), prober.next_probe_time()); + + for _ in 0..CLUSTER_SIZE { + now = std::cmp::max(now, prober.next_probe_time()); + assert_eq!(now, std::cmp::max(now, prober.next_probe_time())); + assert_eq!(0, prober.current_cluster(now).unwrap().probe_cluster_id); + prober.probe_sent(now, PROBE_SIZE); + } + + assert!(now - start_time >= MIN_PROBE_DURATION); + // Verify that the actual bitrate is withing 10% of the target. + let bitrate = PROBE_SIZE * (CLUSTER_SIZE - 1) / (now - start_time); + assert!(bitrate > TEST_BITRATE_1 * 0.9); + assert!(bitrate < TEST_BITRATE_1 * 1.1); + + now = std::cmp::max(now, prober.next_probe_time()); + let probe2_started = now; + + for _ in 0..CLUSTER_SIZE { + now = std::cmp::max(now, prober.next_probe_time()); + assert_eq!(1, prober.current_cluster(now).unwrap().probe_cluster_id); + prober.probe_sent(now, PROBE_SIZE); + } + + // Verify that the actual bitrate is withing 10% of the target. + let duration = now - probe2_started; + assert!(duration >= MIN_PROBE_DURATION); + let bitrate = (PROBE_SIZE * (CLUSTER_SIZE - 1)) / duration; + assert!(bitrate > TEST_BITRATE_2 * 0.9); + assert!(bitrate < TEST_BITRATE_2 * 1.1); + + assert_eq!(prober.next_probe_time(), Timestamp::plus_infinity()); + assert!(!prober.is_probing()); + } + + // A sender that falls more than the maximum probe delay behind abandons the cluster rather + // than pacing at its rate forever. + #[test] + fn discards_delayed_probes() { + let mut prober = BitrateProber::default(); + let mut now = Timestamp::zero(); + prober.create_probe_cluster(&cluster(now, DataRate::from_kilobits_per_sec(900), 0)); + prober.on_incoming_packet(DataSize::from_bytes(1000)); + assert!(prober.is_probing()); + prober.probe_sent(now, DataSize::from_bytes(1000)); + + now = prober.next_probe_time() + TimeDelta::from_millis(11); + assert!(prober.current_cluster(now).is_none()); + assert!(!prober.is_probing()); + } + + // Clusters requested more than five seconds before a new one are dropped unsent. + #[test] + fn discards_stale_clusters() { + let mut prober = BitrateProber::default(); + let rate = DataRate::from_kilobits_per_sec(900); + prober.create_probe_cluster(&cluster(Timestamp::zero(), rate, 0)); + prober.create_probe_cluster(&cluster(Timestamp::from_seconds(6), rate, 1)); + prober.on_incoming_packet(DataSize::from_bytes(1000)); + assert_eq!( + prober + .current_cluster(Timestamp::from_seconds(6)) + .unwrap() + .probe_cluster_id, + 1 + ); + } + + // Probing waits for a packet large enough to probe with. + #[test] + fn does_not_initialize_probing_for_small_packets() { + let mut prober = BitrateProber::default(); + prober.create_probe_cluster(&cluster( + Timestamp::zero(), + DataRate::from_kilobits_per_sec(900), + 0, + )); + prober.on_incoming_packet(DataSize::from_bytes(100)); + assert!(!prober.is_probing()); + prober.on_incoming_packet(DataSize::from_bytes(1000)); + assert!(prober.is_probing()); + } +} diff --git a/rs/moq-congestion/src/gcc/pacing/interval_budget.rs b/rs/moq-congestion/src/gcc/pacing/interval_budget.rs new file mode 100644 index 00000000000..a00423d8a6e --- /dev/null +++ b/rs/moq-congestion/src/gcc/pacing/interval_budget.rs @@ -0,0 +1,69 @@ +#[derive(Clone)] +pub struct IntervalBudget { + target_rate_kbps: i64, + max_bytes_in_budget: i64, + bytes_remaining: i64, + can_build_up_underuse: bool, +} + +impl IntervalBudget { + const WINDOW_MS: i64 = 500; + + pub fn new(initial_target_rate_kbps: i64, can_build_up_underuse: bool) -> Self { + let mut this = Self { + bytes_remaining: 0, + can_build_up_underuse, + target_rate_kbps: 0, + max_bytes_in_budget: 0, + }; + this.set_target_rate_kbps(initial_target_rate_kbps); + this + } + + pub fn set_target_rate_kbps(&mut self, target_rate_kbps: i64) { + self.target_rate_kbps = target_rate_kbps; + self.max_bytes_in_budget = (Self::WINDOW_MS * self.target_rate_kbps) / 8; + self.bytes_remaining = std::cmp::min( + std::cmp::max(-self.max_bytes_in_budget, self.bytes_remaining), + self.max_bytes_in_budget, + ); + } + + pub fn increase_budget(&mut self, delta_time_ms: i64) { + let bytes: i64 = self.target_rate_kbps * delta_time_ms / 8; + if self.bytes_remaining < 0 || self.can_build_up_underuse { + // We overused last interval, compensate this interval. + self.bytes_remaining = + std::cmp::min(self.bytes_remaining + bytes, self.max_bytes_in_budget); + } else { + // If we underused last interval we can't use it this interval. + self.bytes_remaining = std::cmp::min(bytes, self.max_bytes_in_budget); + } + } + + pub fn use_budget(&mut self, bytes: usize) { + self.bytes_remaining = std::cmp::max( + self.bytes_remaining - bytes as i64, + -self.max_bytes_in_budget, + ); + } + + /* unused + pub fn bytes_remaining(&self) -> usize { + self.bytes_remaining.try_into().unwrap_or(0) + } + */ + + pub fn budget_ratio(&self) -> f64 { + if self.max_bytes_in_budget == 0 { + return 0.0; + } + self.bytes_remaining as f64 / self.max_bytes_in_budget as f64 + } + + /* unused + pub fn target_rate_kbps(&self) -> i64 { + self.target_rate_kbps + } + */ +} diff --git a/rs/moq-congestion/src/gcc/pacing/mod.rs b/rs/moq-congestion/src/gcc/pacing/mod.rs new file mode 100644 index 00000000000..5606d7034da --- /dev/null +++ b/rs/moq-congestion/src/gcc/pacing/mod.rs @@ -0,0 +1,5 @@ +mod bitrate_prober; +mod interval_budget; + +pub use bitrate_prober::*; +pub use interval_budget::*; diff --git a/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs new file mode 100644 index 00000000000..a1c04584358 --- /dev/null +++ b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs @@ -0,0 +1,522 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use std::collections::HashMap; + +use crate::gcc::api::{ + transport::{PacedPacketInfo, PacketResult}, + units::{DataRate, DataSize, TimeDelta, Timestamp}, +}; + +#[derive(Clone)] +struct AggregatedCluster { + pub num_probes: i64, + pub first_send: Timestamp, + pub last_send: Timestamp, + pub first_receive: Timestamp, + pub last_receive: Timestamp, + pub size_last_send: DataSize, + pub size_first_receive: DataSize, + pub size_total: DataSize, +} + +impl Default for AggregatedCluster { + fn default() -> Self { + Self { + num_probes: 0, + first_send: Timestamp::plus_infinity(), + last_send: Timestamp::minus_infinity(), + first_receive: Timestamp::plus_infinity(), + last_receive: Timestamp::minus_infinity(), + size_last_send: DataSize::zero(), + size_first_receive: DataSize::zero(), + size_total: DataSize::zero(), + } + } +} + +#[derive(Default, Clone)] +pub struct ProbeBitrateEstimator { + clusters: HashMap, + estimated_data_rate: Option, +} + +impl ProbeBitrateEstimator { + // The minumum number of probes we need to receive feedback about in percent + // in order to have a valid estimate. + const MIN_RECEIVED_PROBES_RATIO: f64 = 0.80; + + // The minumum number of bytes we need to receive feedback about in percent + // in order to have a valid estimate. + const MIN_RECEIVED_BYTES_RATIO: f64 = 0.80; + + // The maximum |receive rate| / |send rate| ratio for a valid estimate. + const MAX_VALID_RATIO: f64 = 2.0; + + // The minimum |receive rate| / |send rate| ratio assuming that the link is + // not saturated, i.e. we assume that we will receive at least + // MinRatioForUnsaturatedLink * |send rate| if |send rate| is less than the + // link capacity. + const MIN_RATIO_FOR_UNSATURATED_LINK: f32 = 0.9; + + // The target utilization of the link. If we know true link capacity + // we'd like to send at 95% of that rate. + const TARGET_UTILIZATION_FRACTION: f32 = 0.95; + + // The maximum time period over which the cluster history is retained. + // This is also the maximum time period beyond which a probing burst is not + // expected to last. + const MAX_CLUSTER_HISTORY: TimeDelta = TimeDelta::from_seconds(1); + + // The maximum time interval between first and the last probe on a cluster + // on the sender side as well as the receive side. + const MAX_PROBE_INTERVAL: TimeDelta = TimeDelta::from_seconds(1); + + // Should be called for every probe packet we receive feedback about. + // Returns the estimated bitrate if the probe completes a valid cluster. + pub fn handle_probe_and_estimate_bitrate( + &mut self, + packet_feedback: &PacketResult, + ) -> Option { + let cluster_id: i32 = packet_feedback.sent_packet.pacing_info.probe_cluster_id; + assert_ne!(cluster_id, PacedPacketInfo::NOT_APROBE); + + self.erase_old_clusters(packet_feedback.receive_time); + + let cluster: &mut AggregatedCluster = self.clusters.entry(cluster_id).or_default(); + + if packet_feedback.sent_packet.send_time < cluster.first_send { + cluster.first_send = packet_feedback.sent_packet.send_time; + } + if packet_feedback.sent_packet.send_time > cluster.last_send { + cluster.last_send = packet_feedback.sent_packet.send_time; + cluster.size_last_send = packet_feedback.sent_packet.size; + } + if packet_feedback.receive_time < cluster.first_receive { + cluster.first_receive = packet_feedback.receive_time; + cluster.size_first_receive = packet_feedback.sent_packet.size; + } + if packet_feedback.receive_time > cluster.last_receive { + cluster.last_receive = packet_feedback.receive_time; + } + cluster.size_total += packet_feedback.sent_packet.size; + cluster.num_probes += 1; + + assert!( + packet_feedback + .sent_packet + .pacing_info + .probe_cluster_min_probes + > 0 + ); + assert!( + packet_feedback + .sent_packet + .pacing_info + .probe_cluster_min_bytes + > 0 + ); + + let min_probes: i64 = (packet_feedback + .sent_packet + .pacing_info + .probe_cluster_min_probes as f64 + * Self::MIN_RECEIVED_PROBES_RATIO) as i64; + let min_size: DataSize = DataSize::from_bytes( + packet_feedback + .sent_packet + .pacing_info + .probe_cluster_min_bytes as _, + ) * Self::MIN_RECEIVED_BYTES_RATIO; + if cluster.num_probes < min_probes || cluster.size_total < min_size { + return None; + } + + let send_interval: TimeDelta = cluster.last_send - cluster.first_send; + let receive_interval: TimeDelta = cluster.last_receive - cluster.first_receive; + + if send_interval <= TimeDelta::zero() + || send_interval > Self::MAX_PROBE_INTERVAL + || receive_interval <= TimeDelta::zero() + || receive_interval > Self::MAX_PROBE_INTERVAL + { + tracing::debug!( + "Probing unsuccessful, invalid send/receive interval [cluster id: {}] [send interval: {:?}] [receive interval: {:?}]", + cluster_id, + send_interval, + receive_interval + ); + return None; + } + // Since the `send_interval` does not include the time it takes to actually + // send the last packet the size of the last sent packet should not be + // included when calculating the send bitrate. + assert!(cluster.size_total > cluster.size_last_send); + let send_size: DataSize = cluster.size_total - cluster.size_last_send; + let send_rate: DataRate = send_size / send_interval; + + // Since the `receive_interval` does not include the time it takes to + // actually receive the first packet the size of the first received packet + // should not be included when calculating the receive bitrate. + assert!(cluster.size_total > cluster.size_first_receive); + let receive_size: DataSize = cluster.size_total - cluster.size_first_receive; + let receive_rate: DataRate = receive_size / receive_interval; + + let ratio: f64 = receive_rate / send_rate; + if ratio > Self::MAX_VALID_RATIO { + tracing::debug!( + "Probing unsuccessful, receive/send ratio too high [cluster id: {}] [send: {:?}/{:?} = {:?}] [receive: {:?}/{:?} = {:?}] [ratio: {:?} > {:?}]", + cluster_id, + send_size, + send_interval, + send_rate, + receive_size, + receive_interval, + receive_rate, + ratio, + Self::MAX_VALID_RATIO + ); + return None; + } + + tracing::debug!( + "Probing successful [cluster id: {}] [send: {:?}/{:?} = {:?}] [receive: {:?}/{:?} = {:?}]", + cluster_id, + send_size, + send_interval, + send_rate, + receive_size, + receive_interval, + receive_rate + ); + + let mut res: DataRate = std::cmp::min(send_rate, receive_rate); + // If we're receiving at significantly lower bitrate than we were sending at, + // it suggests that we've found the true capacity of the link. In this case, + // set the target bitrate slightly lower to not immediately overuse. + if receive_rate < Self::MIN_RATIO_FOR_UNSATURATED_LINK * send_rate { + assert!(send_rate > receive_rate); + res = Self::TARGET_UTILIZATION_FRACTION * receive_rate; + } + self.estimated_data_rate = Some(res); + self.estimated_data_rate + } + + pub fn fetch_and_reset_last_estimated_bitrate(&mut self) -> Option { + let estimated_data_rate: Option = self.estimated_data_rate; + self.estimated_data_rate.take(); + estimated_data_rate + } + + // Erases old cluster data that was seen before `timestamp`. + fn erase_old_clusters(&mut self, timestamp: Timestamp) { + self.clusters + .retain(|_, cluster| cluster.last_receive + Self::MAX_CLUSTER_HISTORY >= timestamp); + } +} + +#[cfg(test)] +mod test { + use super::*; + + const DEFAULT_MIN_PROBES: i64 = 5; + const DEFAULT_MIN_BYTES: i64 = 5000; + const TARGET_UTILIZATION_FRACTION: f64 = 0.95; + + #[derive(Default)] + struct TestProbeBitrateEstimator { + measured_data_rate: Option, + probe_bitrate_estimator: ProbeBitrateEstimator, + } + + impl TestProbeBitrateEstimator { + // TODO(philipel): Use PacedPacketInfo when ProbeBitrateEstimator is rewritten + // to use that information. + fn add_packet_feedback( + &mut self, + probe_cluster_id: i32, + size_bytes: usize, + send_time_ms: i64, + arrival_time_ms: i64, + min_probes: i64, + min_bytes: i64, + ) { + const REFERENCE_TIME: Timestamp = Timestamp::from_seconds(1000); + let mut feedback: PacketResult = PacketResult::default(); + feedback.sent_packet.send_time = REFERENCE_TIME + TimeDelta::from_millis(send_time_ms); + feedback.sent_packet.size = DataSize::from_bytes(size_bytes as _); + feedback.sent_packet.pacing_info = + PacedPacketInfo::new(probe_cluster_id, min_probes, min_bytes); + feedback.receive_time = REFERENCE_TIME + TimeDelta::from_millis(arrival_time_ms); + self.measured_data_rate = self + .probe_bitrate_estimator + .handle_probe_and_estimate_bitrate(&feedback); + } + } + + #[test] + fn one_cluster() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 20, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 30, 40, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + 800000.0, + epsilon = 10.0 + ); + } + + #[test] + fn one_cluster_too_few_probes() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 2000, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 2000, 10, 20, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 2000, 20, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + + assert!(test.measured_data_rate.is_none()); + } + + #[test] + fn one_cluster_too_few_bytes() { + let mut test = TestProbeBitrateEstimator::default(); + const MIN_BYTES: i64 = 6000; + test.add_packet_feedback(0, 800, 0, 10, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 800, 10, 20, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 800, 20, 30, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 800, 30, 40, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 800, 40, 50, DEFAULT_MIN_PROBES, MIN_BYTES); + + assert!(test.measured_data_rate.is_none()); + } + + #[test] + fn small_cluster() { + let mut test = TestProbeBitrateEstimator::default(); + const MIN_BYTES: i64 = 1000; + test.add_packet_feedback(0, 150, 0, 10, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 150, 10, 20, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 150, 20, 30, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 150, 30, 40, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 150, 40, 50, DEFAULT_MIN_PROBES, MIN_BYTES); + test.add_packet_feedback(0, 150, 50, 60, DEFAULT_MIN_PROBES, MIN_BYTES); + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + 120000.0, + epsilon = 10.0 + ); + } + + #[test] + fn large_cluster() { + let mut test = TestProbeBitrateEstimator::default(); + const MIN_PROBES: i64 = 30; + const MIN_BYTES: i64 = 312500; + + for send_time in 0..25 { + let receive_time = send_time + 5; + test.add_packet_feedback(0, 12500, send_time, receive_time, MIN_PROBES, MIN_BYTES); + } + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + 100000000.0, + epsilon = 10.0 + ); + } + + #[test] + fn fast_receive() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 15, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 35, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 30, 40, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + 800000.0, + epsilon = 10.0 + ); + } + + #[test] + fn too_fast_receive() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 19, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 22, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 25, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 40, 27, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + + assert!(test.measured_data_rate.is_none()); + } + + #[test] + fn slow_receive() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 40, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 70, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 30, 85, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + // Expected send rate = 800 kbps, expected receive rate = 320 kbps. + + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + TARGET_UTILIZATION_FRACTION * 320000.0, + epsilon = 10.0 + ); + } + + #[test] + fn burst_receive() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 50, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 50, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 50, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 40, 50, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + + assert!(test.measured_data_rate.is_none()); + } + + #[test] + fn multiple_clusters() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 20, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 40, 60, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + // Expected send rate = 600 kbps, expected receive rate = 480 kbps. + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + TARGET_UTILIZATION_FRACTION * 480000.0, + epsilon = 10.0 + ); + + test.add_packet_feedback(0, 1000, 50, 60, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + // Expected send rate = 640 kbps, expected receive rate = 640 kbps. + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + 640000.0, + epsilon = 10.0 + ); + + test.add_packet_feedback(1, 1000, 60, 70, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(1, 1000, 65, 77, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(1, 1000, 70, 84, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(1, 1000, 75, 90, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + // Expected send rate = 1600 kbps, expected receive rate = 1200 kbps. + + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + TARGET_UTILIZATION_FRACTION * 1200000.0, + epsilon = 10.0 + ); + } + + #[test] + fn ignore_old_clusters() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 20, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + + test.add_packet_feedback(1, 1000, 60, 70, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(1, 1000, 65, 77, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(1, 1000, 70, 84, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(1, 1000, 75, 90, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + // Expected send rate = 1600 kbps, expected receive rate = 1200 kbps. + + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + TARGET_UTILIZATION_FRACTION * 1200000.0, + epsilon = 10.0 + ); + + // Coming in 6s later + test.add_packet_feedback( + 0, + 1000, + 40 + 6000, + 60 + 6000, + DEFAULT_MIN_PROBES, + DEFAULT_MIN_BYTES, + ); + + assert!(test.measured_data_rate.is_none()); + } + + #[test] + fn ignore_size_last_send_packet() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 20, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 30, 40, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1500, 40, 50, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + // Expected send rate = 800 kbps, expected receive rate = 900 kbps. + + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + 800000.0, + epsilon = 10.0 + ); + } + + #[test] + fn ignore_size_first_receive_packet() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1500, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 20, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 30, 40, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + // Expected send rate = 933 kbps, expected receive rate = 800 kbps. + + assert_relative_eq!( + test.measured_data_rate.unwrap().bps_float(), + TARGET_UTILIZATION_FRACTION * 800000.0, + epsilon = 10.0 + ); + } + + #[test] + fn no_last_estimated_bitrate_bps() { + let mut test = TestProbeBitrateEstimator::default(); + assert!( + test.probe_bitrate_estimator + .fetch_and_reset_last_estimated_bitrate() + .is_none() + ); + } + + #[test] + fn fetch_last_estimated_bitrate_bps() { + let mut test = TestProbeBitrateEstimator::default(); + test.add_packet_feedback(0, 1000, 0, 10, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 10, 20, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 20, 30, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + test.add_packet_feedback(0, 1000, 30, 40, DEFAULT_MIN_PROBES, DEFAULT_MIN_BYTES); + + let estimated_bitrate = test + .probe_bitrate_estimator + .fetch_and_reset_last_estimated_bitrate(); + assert!(estimated_bitrate.is_some()); + assert_relative_eq!( + estimated_bitrate.unwrap().bps_float(), + 800000.0, + epsilon = 10.0 + ); + assert!( + test.probe_bitrate_estimator + .fetch_and_reset_last_estimated_bitrate() + .is_none() + ); + } +} // namespace webrtc diff --git a/rs/moq-congestion/src/gcc/probe_controller.rs b/rs/moq-congestion/src/gcc/probe_controller.rs new file mode 100644 index 00000000000..63f15692e7e --- /dev/null +++ b/rs/moq-congestion/src/gcc/probe_controller.rs @@ -0,0 +1,2077 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::api::{ + transport::{NetworkAvailability, ProbeClusterConfig}, + units::{DataRate, TimeDelta, Timestamp}, +}; + +// NOTE: These have been renamed to match the field trials. +// WebRTC-Bwe-ProbingConfiguration +#[derive(Debug, Clone)] +pub struct ProbeControllerConfig { + // These parameters configure the initial probes. First we send one or two + // probes of sizes p1 * self.start_bitrate and p2 * self.start_bitrate. + // Then whenever we get a bitrate estimate of at least further_probe_threshold + // times the size of the last sent probe we'll send another one of size + // step_size times the new estimate. + pub p1: f64, + pub p2: f64, + pub step_size: f64, + pub further_probe_threshold: f64, + pub abort_further: bool, + // Duration of time from the first initial probe where repeated initial probes + // are sent if repeated initial probing is enabled. + pub initial_probing: TimeDelta, + // The minimum probing duration of an individual probe during + // the repeated_initial_probing_time_period. + pub initial_probe_duration: TimeDelta, + // Delta time between sent bursts of packets in a probe during + // the repeated_initial_probing_time_period. + pub initial_min_probe_delta: TimeDelta, + // Configures how often we send ALR probes and how big they are. + pub alr_interval: TimeDelta, + pub alr_scale: f64, + + // Configures the probes emitted by changed to the allocated bitrate. + pub probe_max_allocation: bool, + pub probe_max_allocation_without_alr: bool, + pub alloc_p1: f64, + pub alloc_p2: f64, + pub alloc_current_bwe_limit: f64, + + // The minimum number probing packets used. + pub min_probe_packets_sent: i32, + // The minimum probing duration. + pub min_probe_duration: TimeDelta, + // Delta time between sent bursts of packets in a probe. + pub min_probe_delta: TimeDelta, + pub loss_limited_scale: f64, + // Don't send a probe if min(estimate, network state estimate) is larger than + // this fraction of the set max or max allocated bitrate. + pub skip_if_est_larger_than_fraction_of_max: f64, + // Scale factor of the max allocated bitrate. Used when deciding if a probe + // can be skiped due to that the estimate is already high enough. + pub skip_max_allocated_scale: f64, +} + +impl Default for ProbeControllerConfig { + fn default() -> Self { + Self { + p1: 3.0, + p2: 6.0, + step_size: 2.0, + further_probe_threshold: 0.7, + abort_further: false, + initial_probing: TimeDelta::from_seconds(5), + initial_probe_duration: TimeDelta::from_millis(100), + initial_min_probe_delta: TimeDelta::from_millis(20), + alr_interval: TimeDelta::from_seconds(5), + alr_scale: 2.0, + probe_max_allocation: true, + probe_max_allocation_without_alr: true, + alloc_p1: 1.0, + alloc_p2: 2.0, + alloc_current_bwe_limit: 2.0, + min_probe_packets_sent: 5, + min_probe_duration: TimeDelta::from_millis(15), + min_probe_delta: TimeDelta::from_millis(2), + loss_limited_scale: 1.5, + skip_if_est_larger_than_fraction_of_max: 0.0, + skip_max_allocated_scale: 1.0, + } + } +} + +// Reason that bandwidth estimate is limited. Bandwidth estimate can be limited +// by either delay based bwe, or loss based bwe when it increases/decreases the +// estimate. +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum BandwidthLimitedCause { + LossLimitedBweIncreasing = 0, + LossLimitedBwe = 1, + DelayBasedLimited = 2, + DelayBasedLimitedDelayIncreased = 3, + RttBasedBackOffHighRtt = 4, +} + +#[derive(Clone)] +enum State { + // Initial state where no probing has been triggered yet. + Init, + // Waiting for probing results to continue further probing. + WaitingForProbingResult, + // Probing is complete. + ProbingComplete, +} + +// This class controls initiation of probing to estimate initial channel +// capacity. There is also support for probing during a session when max +// bitrate is adjusted by an application. +#[derive(Clone)] +pub struct ProbeController { + network_available: bool, + repeated_initial_probing_enabled: bool, + last_allowed_repeated_initial_probe: Timestamp, + bandwidth_limited_cause: BandwidthLimitedCause, + state: State, + min_bitrate_to_probe_further: DataRate, + time_last_probing_initiated: Timestamp, + estimated_bitrate: DataRate, + start_bitrate: DataRate, + max_bitrate: DataRate, + last_bwe_drop_probing_time: Timestamp, + alr_start_time: Option, + alr_end_time: Option, + enable_periodic_alr_probing: bool, + time_of_last_large_drop: Timestamp, + bitrate_before_last_large_drop: DataRate, + max_total_allocated_bitrate: DataRate, + + in_rapid_recovery_experiment: bool, + next_probe_cluster_id: i32, + + config: ProbeControllerConfig, +} + +impl Default for ProbeController { + fn default() -> Self { + let mut this = Self { + network_available: false, + state: State::Init, + min_bitrate_to_probe_further: DataRate::plus_infinity(), + time_last_probing_initiated: Timestamp::minus_infinity(), + estimated_bitrate: DataRate::zero(), + start_bitrate: DataRate::zero(), + max_bitrate: DataRate::plus_infinity(), + last_bwe_drop_probing_time: Timestamp::zero(), + alr_start_time: None, + alr_end_time: None, + enable_periodic_alr_probing: false, + time_of_last_large_drop: Timestamp::minus_infinity(), + bitrate_before_last_large_drop: DataRate::zero(), + max_total_allocated_bitrate: DataRate::zero(), + in_rapid_recovery_experiment: false, + next_probe_cluster_id: 1, + config: ProbeControllerConfig::default(), + repeated_initial_probing_enabled: false, + last_allowed_repeated_initial_probe: Timestamp::minus_infinity(), + bandwidth_limited_cause: BandwidthLimitedCause::DelayBasedLimited, + }; + this.reset(Timestamp::zero()); + this + } +} + +impl ProbeController { + // the measured results back. + const MAX_WAITING_TIME_FOR_PROBING_RESULT: TimeDelta = TimeDelta::from_seconds(1); + + // Default probing bitrate limit. Applied only when the application didn't + // specify max bitrate. + const DEFAULT_MAX_PROBING_BITRATE: DataRate = DataRate::from_kilobits_per_sec(5000); + + // If the bitrate drops to a factor `kBitrateDropThreshold` or lower + // and we recover within `kBitrateDropTimeoutMs`, then we'll send + // a probe at a fraction `kProbeFractionAfterDrop` of the original bitrate. + const BITRATE_DROP_THRESHOLD: f64 = 0.66; + const BITRATE_DROP_TIMEOUT: TimeDelta = TimeDelta::from_seconds(5); + const PROBE_FRACTION_AFTER_DROP: f64 = 0.85; + + // Timeout for probing after leaving ALR. If the bitrate drops significantly, + // (as determined by the delay based estimator) and we leave ALR, then we will + // send a probe if we recover within `kLeftAlrTimeoutMs` ms. + const ALR_ENDED_TIMEOUT: TimeDelta = TimeDelta::from_seconds(3); + + // The expected uncertainty of probe result (as a fraction of the target probe + // This is a limit on how often probing can be done when there is a BW + // drop detected in ALR. + const MIN_TIME_BETWEEN_ALR_PROBES: TimeDelta = TimeDelta::from_seconds(5); + + // bitrate). Used to avoid probing if the probe bitrate is close to our current + // estimate. + const PROBE_UNCERTAINTY: f64 = 0.05; + + pub fn new(config: ProbeControllerConfig) -> Self { + Self { + config, + ..Default::default() + } + } + + pub fn set_bitrates( + &mut self, + min_bitrate: DataRate, + start_bitrate: DataRate, + max_bitrate: DataRate, + at_time: Timestamp, + ) -> Vec { + if start_bitrate > DataRate::zero() { + self.start_bitrate = start_bitrate; + self.estimated_bitrate = start_bitrate; + } else if self.start_bitrate.is_zero() { + self.start_bitrate = min_bitrate; + } + + // The reason we use the variable `old_max_bitrate_pbs` is because we + // need to set `max_bitrate_` before we call InitiateProbing. + let old_max_bitrate: DataRate = self.max_bitrate; + self.max_bitrate = if max_bitrate.is_finite() { + max_bitrate + } else { + Self::DEFAULT_MAX_PROBING_BITRATE + }; + + match self.state { + State::Init => { + if self.network_available { + return self.initiate_exponential_probing(at_time); + } + } + State::WaitingForProbingResult => (), + State::ProbingComplete => { + // If the new max bitrate is higher than both the old max bitrate and the + // estimate then initiate probing. + if !self.estimated_bitrate.is_zero() + && old_max_bitrate < self.max_bitrate + && self.estimated_bitrate < self.max_bitrate + { + return self.initiate_probing(at_time, &[self.max_bitrate], false); + } + } + } + vec![] + } + + // The total bitrate, as opposed to the max bitrate, is the sum of the + // configured bitrates for all active streams. + + pub fn on_max_total_allocated_bitrate( + &mut self, + max_total_allocated_bitrate: DataRate, + at_time: Timestamp, + ) -> Vec { + let in_alr: bool = self.alr_start_time.is_some(); + let allow_allocation_probe: bool = in_alr || self.config.probe_max_allocation_without_alr; + if self.config.probe_max_allocation + && matches!(self.state, State::ProbingComplete) + && max_total_allocated_bitrate > self.max_total_allocated_bitrate + && self.estimated_bitrate < self.max_bitrate + && self.estimated_bitrate < max_total_allocated_bitrate + && allow_allocation_probe + { + self.max_total_allocated_bitrate = max_total_allocated_bitrate; + + if self.config.alloc_p1 == 0.0 { + return vec![]; + } + + let mut first_probe_rate: DataRate = max_total_allocated_bitrate * self.config.alloc_p1; + let current_bwe_limit: DataRate = + self.config.alloc_current_bwe_limit * self.estimated_bitrate; + let mut limited_by_current_bwe: bool = current_bwe_limit < first_probe_rate; + if limited_by_current_bwe { + first_probe_rate = current_bwe_limit; + } + + let mut probes: Vec = vec![first_probe_rate]; + if self.config.alloc_p2 > 0.0 && !limited_by_current_bwe { + let mut second_probe_rate: DataRate = + max_total_allocated_bitrate * self.config.alloc_p2; + limited_by_current_bwe = current_bwe_limit < second_probe_rate; + if limited_by_current_bwe { + second_probe_rate = current_bwe_limit; + } + if second_probe_rate > first_probe_rate { + probes.push(second_probe_rate); + } + } + + let allow_further_probing: bool = limited_by_current_bwe; + + return self.initiate_probing(at_time, &probes, allow_further_probing); + } + if !max_total_allocated_bitrate.is_zero() { + self.last_allowed_repeated_initial_probe = at_time; + } + + self.max_total_allocated_bitrate = max_total_allocated_bitrate; + vec![] + } + + pub fn on_network_availability(&mut self, msg: NetworkAvailability) -> Vec { + self.network_available = msg.network_available; + + if !self.network_available && matches!(self.state, State::WaitingForProbingResult) { + self.state = State::ProbingComplete; + self.min_bitrate_to_probe_further = DataRate::plus_infinity(); + } + + if self.network_available + && matches!(self.state, State::Init) + && !self.start_bitrate.is_zero() + { + return self.initiate_exponential_probing(msg.at_time); + } + vec![] + } + + pub fn set_estimated_bitrate( + &mut self, + bitrate: DataRate, + bandwidth_limited_cause: BandwidthLimitedCause, + at_time: Timestamp, + ) -> Vec { + self.bandwidth_limited_cause = bandwidth_limited_cause; + if bitrate < Self::BITRATE_DROP_THRESHOLD * self.estimated_bitrate { + self.time_of_last_large_drop = at_time; + self.bitrate_before_last_large_drop = self.estimated_bitrate; + } + self.estimated_bitrate = bitrate; + + if matches!(self.state, State::WaitingForProbingResult) { + // Continue probing if probing results indicate channel has greater + // capacity unless we already reached the needed bitrate. + if self.config.abort_further + && (bitrate > self.max_bitrate + || (!self.max_total_allocated_bitrate.is_zero() + && bitrate > 2 * self.max_total_allocated_bitrate)) + { + // No need to continue probing. + self.min_bitrate_to_probe_further = DataRate::plus_infinity(); + } + tracing::debug!( + "Measured bitrate: {:?} Minimum to probe further: {:?}", + bitrate, + self.min_bitrate_to_probe_further, + ); + + if bitrate > self.min_bitrate_to_probe_further { + return self.initiate_probing(at_time, &[self.config.step_size * bitrate], true); + } + } + vec![] + } + + pub fn enable_periodic_alr_probing(&mut self, enable: bool) { + self.enable_periodic_alr_probing = enable; + } + + // Probes are sent periodically every 1s during the first 5s after the network + // becomes available or until OnMaxTotalAllocatedBitrate is invoked with a + // none zero max_total_allocated_bitrate (there are active streams being + // sent.) Probe rate is up to max configured bitrate configured via + // SetBitrates. + pub fn enable_repeated_initial_probing(&mut self, enable: bool) { + self.repeated_initial_probing_enabled = enable; + } + + pub fn set_alr_start_time(&mut self, alr_start_time: Option) { + self.alr_start_time = alr_start_time; + } + pub fn set_alr_ended_time(&mut self, alr_end_time: Timestamp) { + self.alr_end_time = Some(alr_end_time); + } + + pub fn request_probe(&mut self, at_time: Timestamp) -> Vec { + // Called once we have returned to normal state after a large drop in + // estimated bandwidth. The current response is to initiate a single probe + // session (if not already probing) at the previous bitrate. + // + // If the probe session fails, the assumption is that this drop was a + // real one from a competing flow or a network change. + let in_alr: bool = self.alr_start_time.is_some(); + let alr_ended_recently: bool = match self.alr_end_time { + Some(alr_end_time) => at_time - alr_end_time < Self::ALR_ENDED_TIMEOUT, + _ => false, + }; + + if (in_alr || alr_ended_recently || self.in_rapid_recovery_experiment) + && matches!(self.state, State::ProbingComplete) + { + let suggested_probe: DataRate = + Self::PROBE_FRACTION_AFTER_DROP * self.bitrate_before_last_large_drop; + let min_expected_probe_result: DataRate = + (1.0 - Self::PROBE_UNCERTAINTY) * suggested_probe; + let time_since_drop: TimeDelta = at_time - self.time_of_last_large_drop; + let time_since_probe: TimeDelta = at_time - self.last_bwe_drop_probing_time; + if min_expected_probe_result > self.estimated_bitrate + && time_since_drop < Self::BITRATE_DROP_TIMEOUT + && time_since_probe > Self::MIN_TIME_BETWEEN_ALR_PROBES + { + tracing::debug!("Detected big bandwidth drop, start probing."); + // Track how often we probe in response to bandwidth drop in ALR. + self.last_bwe_drop_probing_time = at_time; + return self.initiate_probing(at_time, &[suggested_probe], false); + } + } + vec![] + } + + // Resets the ProbeController to a state equivalent to as if it was just + // created EXCEPT for configuration settings like + // `enable_periodic_alr_probing_` `network_available_` and + // `max_total_allocated_bitrate_`. + pub fn reset(&mut self, at_time: Timestamp) { + self.bandwidth_limited_cause = BandwidthLimitedCause::DelayBasedLimited; + self.state = State::Init; + self.min_bitrate_to_probe_further = DataRate::plus_infinity(); + self.time_last_probing_initiated = Timestamp::zero(); + self.estimated_bitrate = DataRate::zero(); + self.start_bitrate = DataRate::zero(); + self.max_bitrate = Self::DEFAULT_MAX_PROBING_BITRATE; + let now: Timestamp = at_time; + self.last_bwe_drop_probing_time = now; + self.alr_end_time.take(); + self.time_of_last_large_drop = now; + self.bitrate_before_last_large_drop = DataRate::zero(); + } + + pub fn process(&mut self, at_time: Timestamp) -> Vec { + if at_time - self.time_last_probing_initiated > Self::MAX_WAITING_TIME_FOR_PROBING_RESULT + && matches!(self.state, State::WaitingForProbingResult) + { + tracing::debug!("WaitingForProbingResult: timeout"); + self.update_state(State::ProbingComplete); + } + if self.estimated_bitrate.is_zero() || !matches!(self.state, State::ProbingComplete) { + return vec![]; + } + if self.time_for_next_repeated_initial_probe(at_time) { + return self.initiate_probing( + at_time, + &[self.estimated_bitrate * self.config.p1], + true, + ); + } + if self.time_for_alr_probe(at_time) { + return self.initiate_probing( + at_time, + &[self.estimated_bitrate * self.config.alr_scale], + true, + ); + } + vec![] + } + + fn update_state(&mut self, new_state: State) { + match new_state { + State::Init => { + self.state = State::Init; + } + State::WaitingForProbingResult => { + self.state = State::WaitingForProbingResult; + } + State::ProbingComplete => { + self.state = State::ProbingComplete; + self.min_bitrate_to_probe_further = DataRate::plus_infinity(); + } + } + } + fn initiate_exponential_probing(&mut self, at_time: Timestamp) -> Vec { + assert!(self.network_available); + assert!(matches!(self.state, State::Init)); + assert!(self.start_bitrate > DataRate::zero()); + + // When probing at 1.8 Mbps ( 6x 300), this represents a threshold of + // 1.2 Mbps to continue probing. + let mut probes: Vec = vec![self.config.p1 * self.start_bitrate]; + if self.config.p2 > 0.0 { + probes.push(self.config.p2 * self.start_bitrate); + } + + if self.repeated_initial_probing_enabled && self.max_total_allocated_bitrate.is_zero() { + self.last_allowed_repeated_initial_probe = at_time + self.config.initial_probing; + tracing::debug!( + "Repeated initial probing enabled, last allowed probe: {:?} now: {:?}", + self.last_allowed_repeated_initial_probe, + at_time + ); + } + + self.initiate_probing(at_time, &probes, true) + } + fn initiate_probing( + &mut self, + now: Timestamp, + bitrates_to_probe: &[DataRate], + mut probe_further: bool, + ) -> Vec { + if self.config.skip_if_est_larger_than_fraction_of_max > 0.0 { + let max_probe_rate: DataRate = if self.max_total_allocated_bitrate.is_zero() { + self.max_bitrate + } else { + std::cmp::min( + self.config.skip_max_allocated_scale * self.max_total_allocated_bitrate, + self.max_bitrate, + ) + }; + if self.estimated_bitrate + > self.config.skip_if_est_larger_than_fraction_of_max * max_probe_rate + { + self.update_state(State::ProbingComplete); + return vec![]; + } + } + + let mut max_probe_bitrate: DataRate = self.max_bitrate; + if self.max_total_allocated_bitrate > DataRate::zero() { + // If a max allocated bitrate has been configured, allow probing up to 2x + // that rate. This allows some overhead to account for bursty streams, + // which otherwise would have to ramp up when the overshoot is already in + // progress. + // It also avoids minor quality reduction caused by probes often being + // received at slightly less than the target probe bitrate. + max_probe_bitrate = + std::cmp::min(max_probe_bitrate, self.max_total_allocated_bitrate * 2); + } + + match self.bandwidth_limited_cause { + BandwidthLimitedCause::RttBasedBackOffHighRtt + | BandwidthLimitedCause::DelayBasedLimitedDelayIncreased + | BandwidthLimitedCause::LossLimitedBwe => { + tracing::debug!( + "Not sending probe in bandwidth limited state. {:?}", + self.bandwidth_limited_cause + ); + return vec![]; + } + BandwidthLimitedCause::LossLimitedBweIncreasing => { + max_probe_bitrate = std::cmp::min( + max_probe_bitrate, + self.estimated_bitrate * self.config.loss_limited_scale, + ); + } + _ => (), + }; + + let mut pending_probes: Vec = vec![]; + for mut bitrate in bitrates_to_probe.iter().cloned() { + assert!(!bitrate.is_zero()); + if bitrate >= max_probe_bitrate { + bitrate = max_probe_bitrate; + probe_further = false; + } + pending_probes.push(self.create_probe_cluster_config(now, bitrate)); + } + self.time_last_probing_initiated = now; + if probe_further { + self.update_state(State::WaitingForProbingResult); + // Dont expect probe results to be larger than a fraction of the actual + // probe rate. + self.min_bitrate_to_probe_further = pending_probes.last().unwrap().target_data_rate + * self.config.further_probe_threshold; + } else { + self.update_state(State::ProbingComplete); + } + pending_probes + } + fn time_for_alr_probe(&self, at_time: Timestamp) -> bool { + if let Some(alr_start_time) = self.alr_start_time + && self.enable_periodic_alr_probing + { + let next_probe_time: Timestamp = + std::cmp::max(alr_start_time, self.time_last_probing_initiated) + + self.config.alr_interval; + return at_time >= next_probe_time; + } + false + } + fn time_for_next_repeated_initial_probe(&self, at_time: Timestamp) -> bool { + if !matches!(self.state, State::WaitingForProbingResult) + && self.last_allowed_repeated_initial_probe > at_time + { + let next_probe_time: Timestamp = + self.time_last_probing_initiated + Self::MAX_WAITING_TIME_FOR_PROBING_RESULT; + if at_time >= next_probe_time { + return true; + } + } + false + } + fn create_probe_cluster_config( + &mut self, + at_time: Timestamp, + bitrate: DataRate, + ) -> ProbeClusterConfig { + let mut config = ProbeClusterConfig { + id: self.next_probe_cluster_id, + at_time, + target_data_rate: bitrate, + target_probe_count: self.config.min_probe_packets_sent, + ..Default::default() + }; + + self.next_probe_cluster_id += 1; + + if at_time < self.last_allowed_repeated_initial_probe { + config.target_duration = self.config.initial_probe_duration; + config.min_probe_delta = self.config.initial_min_probe_delta; + } else { + config.target_duration = self.config.min_probe_duration; + config.min_probe_delta = self.config.min_probe_delta; + } + + config + } +} + +#[cfg(test)] +mod test { + use super::*; + const MIN_BITRATE: DataRate = DataRate::from_bits_per_sec(100); + const START_BITRATE: DataRate = DataRate::from_bits_per_sec(300); + const MAX_BITRATE: DataRate = DataRate::from_bits_per_sec(10000); + const MBPS_MULTIPLIER: DataRate = DataRate::from_kilobits_per_sec(1000); + + const EXPONENTIAL_PROBING_TIMEOUT: TimeDelta = TimeDelta::from_seconds(5); + + const ALR_PROBE_INTERVAL: TimeDelta = TimeDelta::from_seconds(5); + const ALR_ENDED_TIMEOUT: TimeDelta = TimeDelta::from_seconds(3); + const BITRATE_DROP_TIMEOUT: TimeDelta = TimeDelta::from_seconds(5); + + #[test] + fn initiates_probing_after_set_bitrates() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(probes.len() >= 2); + } + + #[test] + fn initiates_probing_when_network_available() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + + let probes: Vec = + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(probes.is_empty()); + let probes = probe_controller.on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }); + assert!(probes.len() >= 2); + } + + #[test] + fn sets_default_target_duration_and_target_probe_count() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes: Vec = + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(probes.len() >= 2); + + assert_eq!(probes[0].target_duration, TimeDelta::from_millis(15)); + assert_eq!(probes[0].target_probe_count, 5); + } + + #[test] + fn field_trials_override_default_target_duration_and_target_probe_count() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + min_probe_packets_sent: 2, + min_probe_duration: TimeDelta::from_millis(123), + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes: Vec = + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(probes.len() >= 2); + + assert_eq!(probes[0].target_duration, TimeDelta::from_millis(123)); + assert_eq!(probes[0].target_probe_count, 2); + } + + #[test] + fn probe_only_when_network_is_up() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + probe_controller.on_network_availability(NetworkAvailability { + at_time: clock, + network_available: false, + }); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(probes.is_empty()); + let probes = probe_controller.on_network_availability(NetworkAvailability { + at_time: clock, + network_available: true, + }); + assert!(probes.len() >= 2); + } + + #[test] + fn can_configure_initial_probe_rate_factor() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + p1: 2.0, + p2: 3.0, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert_eq!(probes.len(), 2); + assert_eq!(probes[0].target_data_rate, START_BITRATE * 2); + assert_eq!(probes[1].target_data_rate, START_BITRATE * 3); + } + + #[test] + fn disable_second_initial_probe_if_rate_factorzero() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + p1: 2.0, + p2: 0.0, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, START_BITRATE * 2); + } + + #[test] + fn initiates_probing_on_max_bitrate_increase() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + // Long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; + probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + probe_controller.process(clock); + let probes = probe_controller.set_bitrates( + MIN_BITRATE, + START_BITRATE, + MAX_BITRATE + DataRate::from_bits_per_sec(100), + clock, + ); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps(), MAX_BITRATE.bps() + 100); + } + + #[test] + fn probes_in_alr_when_allocation_probing_outside_alr_is_disabled() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + probe_max_allocation_without_alr: false, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.set_estimated_bitrate( + MAX_BITRATE - DataRate::from_bits_per_sec(1), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + // Probe when in alr. + probe_controller.set_alr_start_time(Some(clock)); + let probes = probe_controller + .on_max_total_allocated_bitrate(MAX_BITRATE + DataRate::from_bits_per_sec(1), clock); + assert_eq!(probes.len(), 2); + assert_eq!(probes[0].target_data_rate, MAX_BITRATE); + } + + #[test] + fn probes_on_max_allocated_bitrate_increase_outside_alr_by_default() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + const TRANSPORT_MAX_BITRATE: DataRate = DataRate::from_kilobits_per_sec(10000); + const ESTIMATED_AFTER_INITIAL_PROBING: DataRate = DataRate::from_kilobits_per_sec(300); + const SCREEN_SHARE_MAX_BITRATE: DataRate = DataRate::from_kilobits_per_sec(3500); + + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, TRANSPORT_MAX_BITRATE, clock); + probe_controller.set_estimated_bitrate( + ESTIMATED_AFTER_INITIAL_PROBING, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + probe_controller.set_alr_start_time(None); + let probes = + probe_controller.on_max_total_allocated_bitrate(SCREEN_SHARE_MAX_BITRATE, clock); + assert_eq!(probes.len(), 1); + assert_eq!( + probes[0].target_data_rate, + 2 * ESTIMATED_AFTER_INITIAL_PROBING + ); + } + + #[test] + fn allocation_probing_outside_alr_can_be_disabled() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + probe_max_allocation_without_alr: false, + ..Default::default() + }); + const TRANSPORT_MAX_BITRATE: DataRate = DataRate::from_kilobits_per_sec(10000); + const ESTIMATED_AFTER_INITIAL_PROBING: DataRate = DataRate::from_kilobits_per_sec(300); + const SCREEN_SHARE_MAX_BITRATE: DataRate = DataRate::from_kilobits_per_sec(3500); + + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, TRANSPORT_MAX_BITRATE, clock); + probe_controller.set_estimated_bitrate( + ESTIMATED_AFTER_INITIAL_PROBING, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + probe_controller.set_alr_start_time(None); + assert!( + probe_controller + .on_max_total_allocated_bitrate(SCREEN_SHARE_MAX_BITRATE, clock) + .is_empty() + ); + } + + #[test] + fn does_not_probe_when_max_allocated_bitrate_decreases() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + // Set the initial allocation while initial probing is still in progress. + assert!( + probe_controller + .on_max_total_allocated_bitrate(MAX_BITRATE, clock) + .is_empty() + ); + probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + clock += EXPONENTIAL_PROBING_TIMEOUT; + assert!(probe_controller.process(clock).is_empty()); + + // A lower allocation should update the limit without triggering a probe. + assert!( + probe_controller + .on_max_total_allocated_bitrate(MAX_BITRATE / 2, clock) + .is_empty() + ); + + // A subsequent increase from the lower allocation should trigger probing. + let probes = probe_controller.on_max_total_allocated_bitrate(MAX_BITRATE * 3 / 4i64, clock); + assert!(!probes.is_empty()); + } + + #[test] + fn probes_on_max_allocated_bitrate_limited_by_current_bwe() { + let mut clock: Timestamp = Timestamp::zero(); + + assert!(MAX_BITRATE > 1.5 * START_BITRATE); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + // Probe when in alr. + probe_controller.set_alr_start_time(Some(clock)); + let probes = probe_controller.on_max_total_allocated_bitrate(MAX_BITRATE, clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, 2.0 * START_BITRATE); + + // Continue probing if probe succeeds. + let probes = probe_controller.set_estimated_bitrate( + 1.5 * START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert_eq!(probes.len(), 1); + assert!(probes[0].target_data_rate > 1.5 * START_BITRATE); + } + + #[test] + fn can_disable_probing_on_max_total_allocated_bitrate_increase() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + probe_max_allocation: false, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.set_estimated_bitrate( + MAX_BITRATE - DataRate::from_bits_per_sec(1), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + probe_controller.set_alr_start_time(Some(clock)); + + // Do no probe, since probe_max_allocation:false. + probe_controller.set_alr_start_time(Some(clock)); + let probes = probe_controller + .on_max_total_allocated_bitrate(MAX_BITRATE + DataRate::from_bits_per_sec(1), clock); + assert!(probes.is_empty()); + } + + #[test] + fn initiates_probing_on_max_bitrate_increase_at_max_bitrate() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + // Long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; + probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + probe_controller.process(clock); + probe_controller.set_estimated_bitrate( + MAX_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + let probes = probe_controller.set_bitrates( + MIN_BITRATE, + START_BITRATE, + MAX_BITRATE + DataRate::from_bits_per_sec(100), + clock, + ); + assert_eq!(probes.len(), 1); + assert_eq!( + probes[0].target_data_rate, + MAX_BITRATE + DataRate::from_bits_per_sec(100) + ); + } + + #[test] + fn test_exponential_probing() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + + // Repeated probe should only be sent when estimated bitrate climbs above + // 0.7 * 6 * StartBitrate = 1260. + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1000), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1800), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps(), 2 * 1800); + } + + #[test] + fn exponential_probing_stop_if_max_bitrate_low() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + abort_further: true, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + // Repeated probe normally is sent when estimated bitrate climbs above + // 0.7 * 6 * StartBitrate = 1260. But since max bitrate is low, expect + // exponential probing to stop. + let probes = probe_controller.set_bitrates( + MIN_BITRATE, + START_BITRATE, + /*max_bitrate=*/ START_BITRATE, + clock, + ); + assert!(probes.is_empty()); + + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1800), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + } + + #[test] + fn exponential_probing_stop_if_max_allocated_bitrate_low() { + let clock = Timestamp::zero(); + let mut probe_controller: ProbeController = ProbeController::new(ProbeControllerConfig { + abort_further: true, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + // Repeated probe normally is sent when estimated bitrate climbs above + // 0.7 * 6 * StartBitrate = 1260. But since allocated bitrate i slow, expect + // exponential probing to stop. + let probes = probe_controller.on_max_total_allocated_bitrate(START_BITRATE, clock); + assert!(probes.is_empty()); + + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1800), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + } + + #[test] + fn initial_probing_to_low_max_allocatedbitrate() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + // Repeated probe is sent when estimated bitrate climbs above + // 0.7 * 6 * StartBitrate = 1260. + let probes = probe_controller.on_max_total_allocated_bitrate(START_BITRATE, clock); + assert!(probes.is_empty()); + + // If the inital probe result is received, a new probe is sent at 2x the + // needed max bitrate. + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1800), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps(), 2 * START_BITRATE.bps()); + } + + #[test] + fn initial_probing_timeout() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + // Advance far enough to cause a time out in waiting for probing result. + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1800), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + } + + #[test] + fn repeated_initial_probing_sends_new_probe_after_timeout() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + probe_controller.enable_repeated_initial_probing(true); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + let start_time: Timestamp = clock; + let mut last_probe_time: Timestamp = clock; + while clock < start_time + TimeDelta::from_seconds(5) { + clock += TimeDelta::from_millis(100); + let probes = probe_controller.process(clock); + if !probes.is_empty() { + // Expect a probe every second. + assert_eq!(clock - last_probe_time, TimeDelta::from_seconds_float(1.1)); + assert_eq!(probes[0].min_probe_delta, TimeDelta::from_millis(20)); + assert_eq!(probes[0].target_duration, TimeDelta::from_millis(100)); + last_probe_time = clock; + } else { + assert!(clock - last_probe_time < TimeDelta::from_seconds_float(1.1)); + } + } + clock += TimeDelta::from_seconds(1); + // After 5s, repeated initial probing stops. + assert!(probe_controller.process(clock).is_empty()); + } + + #[test] + fn repeated_initial_probing_stop_if_max_allocated_bitrate_set() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + probe_controller.enable_repeated_initial_probing(true); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + clock += TimeDelta::from_millis(1100); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + probe_controller.on_max_total_allocated_bitrate(MIN_BITRATE, clock); + clock += TimeDelta::from_millis(1100); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } + + #[test] + fn request_probe_in_alr() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(probes.len() >= 2); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + probe_controller.set_alr_start_time(Some(clock)); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + probe_controller.process(clock); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(250), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + let probes = probe_controller.request_probe(clock); + + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps_float(), 0.85 * 500.0); + } + + #[test] + fn request_probe_when_alr_ended_recently() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert_eq!(probes.len(), 2); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + probe_controller.set_alr_start_time(None); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + probe_controller.process(clock); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(250), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + probe_controller.set_alr_ended_time(clock); + clock += ALR_ENDED_TIMEOUT - TimeDelta::from_millis(1); + let probes = probe_controller.request_probe(clock); + + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps_float(), 0.85 * 500.0); + } + + #[test] + fn request_probe_when_alr_not_ended_recently() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert_eq!(probes.len(), 2); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + probe_controller.set_alr_start_time(None); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + probe_controller.process(clock); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(250), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + probe_controller.set_alr_ended_time(clock); + clock += ALR_ENDED_TIMEOUT + TimeDelta::from_millis(1); + let probes = probe_controller.request_probe(clock); + assert!(probes.is_empty()); + } + + #[test] + fn request_probe_when_bwe_drop_not_recent() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert_eq!(probes.len(), 2); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + probe_controller.set_alr_start_time(Some(clock)); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + probe_controller.process(clock); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(250), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + clock += BITRATE_DROP_TIMEOUT + TimeDelta::from_millis(1); + let probes = probe_controller.request_probe(clock); + assert!(probes.is_empty()); + } + + #[test] + fn periodic_probing() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.enable_periodic_alr_probing(true); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert_eq!(probes.len(), 2); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + let start_time: Timestamp = clock; + + // Expect the controller to send a new probe after 5s has passed. + probe_controller.set_alr_start_time(Some(start_time)); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps(), 1000); + + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + // The following probe should be sent at 10s into ALR. + probe_controller.set_alr_start_time(Some(start_time)); + clock += TimeDelta::from_seconds(4); + probe_controller.process(clock); + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + probe_controller.set_alr_start_time(Some(start_time)); + clock += TimeDelta::from_seconds(1); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + } + + #[test] + fn periodic_probing_after_reset() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let alr_start_time: Timestamp = clock; + + probe_controller.set_alr_start_time(Some(alr_start_time)); + probe_controller.enable_periodic_alr_probing(true); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.reset(clock); + + clock += TimeDelta::from_seconds(10); + let probes = probe_controller.process(clock); + // Since bitrates are not yet set, no probe is sent event though we are in ALR + // mode. + assert!(probes.is_empty()); + + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert_eq!(probes.len(), 2); + + // Make sure we use `kStartBitrateBps` as the estimated bitrate + // until SetEstimatedBitrate is called with an updated estimate. + clock += TimeDelta::from_seconds(10); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, START_BITRATE * 2); + } + + #[test] + fn no_probes_when_transport_is_not_writable() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + probe_controller.enable_periodic_alr_probing(true); + + let probes: Vec = + probe_controller.on_network_availability(NetworkAvailability { + network_available: false, + ..Default::default() + }); + assert!(probes.is_empty()); + assert!( + probe_controller + .set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock) + .is_empty() + ); + clock += TimeDelta::from_seconds(10); + assert!(probe_controller.process(clock).is_empty()); + + // Controller is reset after a network route change. + // But, a probe should not be sent since the transport is not writable. + // Transport is not writable until after DTLS negotiation completes. + // However, the bitrate constraints may change. + probe_controller.reset(clock); + assert!( + probe_controller + .set_bitrates(2 * MIN_BITRATE, 2 * START_BITRATE, 2 * MAX_BITRATE, clock) + .is_empty() + ); + clock += TimeDelta::from_seconds(10); + assert!(probe_controller.process(clock).is_empty()); + } + + #[test] + fn test_exponential_probing_overflow() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates( + MIN_BITRATE, + 10 * MBPS_MULTIPLIER, + 100 * MBPS_MULTIPLIER, + clock, + ); + // Verify that probe bitrate is capped at the specified max bitrate. + let probes = probe_controller.set_estimated_bitrate( + 60 * MBPS_MULTIPLIER, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, 100 * MBPS_MULTIPLIER); + // Verify that repeated probes aren't sent. + let probes = probe_controller.set_estimated_bitrate( + 100 * MBPS_MULTIPLIER, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + } + + #[test] + fn test_allocated_bitrate_cap() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates( + MIN_BITRATE, + 10 * MBPS_MULTIPLIER, + 100 * MBPS_MULTIPLIER, + clock, + ); + + // Configure ALR for periodic probing. + probe_controller.enable_periodic_alr_probing(true); + let alr_start_time: Timestamp = clock; + probe_controller.set_alr_start_time(Some(alr_start_time)); + + let estimated_bitrate: DataRate = 10 * MBPS_MULTIPLIER; + probe_controller.set_estimated_bitrate( + estimated_bitrate, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + // Set a max allocated bitrate below the current estimate. + let max_allocated: DataRate = estimated_bitrate - 1 * MBPS_MULTIPLIER; + let probes = probe_controller.on_max_total_allocated_bitrate(max_allocated, clock); + assert!(probes.is_empty()); // No probe since lower than current max. + + // Probes such as ALR capped at 2x the max allocation limit. + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, 2 * max_allocated); + + // Remove allocation limit. + assert!( + probe_controller + .on_max_total_allocated_bitrate(DataRate::zero(), clock) + .is_empty() + ); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, estimated_bitrate * 2); + } + + #[test] + fn configurable_probing_field_trial() { + let mut clock = Timestamp::zero(); + + // "WebRTC-Bwe-ProbingConfiguration/p1:2,p2:5,step_size:3,further_probe_threshold:0.8,alloc_p1:2,alloc_current_bwe_limit:1000.0,alloc_p2,min_probe_packets_sent:2/" + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + p1: 2.0, + p2: 5.0, + step_size: 3.0, + further_probe_threshold: 0.8, + alloc_p1: 2.0, + alloc_current_bwe_limit: 1000.0, + alloc_p2: 0.0, + min_probe_packets_sent: 2, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + + let probes = probe_controller.set_bitrates( + MIN_BITRATE, + START_BITRATE, + DataRate::from_kilobits_per_sec(5000), + clock, + ); + assert_eq!(probes.len(), 2); + assert_eq!(probes[0].target_data_rate.bps(), 600); + assert_eq!(probes[0].target_probe_count, 2); + assert_eq!(probes[1].target_data_rate.bps(), 1500); + assert_eq!(probes[1].target_probe_count, 2); + + // Repeated probe should only be sent when estimated bitrate climbs above + // 0.8 * 5 * StartBitrateBps = 1200. + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1100), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert_eq!(probes.len(), 0); + + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(1250), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps(), 3 * 1250); + + clock += TimeDelta::from_seconds(5); + probe_controller.process(clock); + + probe_controller.set_alr_start_time(Some(clock)); + let probes = probe_controller + .on_max_total_allocated_bitrate(DataRate::from_kilobits_per_sec(200), clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate.bps(), 400_000); + } + + #[test] + fn limit_alr_probe_when_loss_based_bwe_limited() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.enable_periodic_alr_probing(true); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + // Expect the controller to send a new probe after 5s has passed. + probe_controller.set_alr_start_time(Some(clock)); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + + probe_controller.set_estimated_bitrate( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::LossLimitedBweIncreasing, + clock, + ); + clock += TimeDelta::from_seconds(6); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!( + probes[0].target_data_rate, + 1.5 * DataRate::from_bits_per_sec(500) + ); + + probe_controller.set_estimated_bitrate( + 1.5 * DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + clock += TimeDelta::from_seconds(6); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + assert!(probes[0].target_data_rate > 1.5 * 1.5 * DataRate::from_bits_per_sec(500)); + } + + #[test] + fn probe_in_alr_if_loss_based_increasing() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.enable_periodic_alr_probing(true); + probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::LossLimitedBweIncreasing, + clock, + ); + + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + // Probe when in alr. + probe_controller.set_alr_start_time(Some(clock)); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, 1.5 * START_BITRATE); + } + + #[test] + fn not_probe_when_in_alr_if_loss_based_decreases() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.enable_periodic_alr_probing(true); + probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::LossLimitedBwe, + clock, + ); + + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + // Not probe in alr when loss based estimate decreases. + probe_controller.set_alr_start_time(Some(clock)); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } + + #[test] + fn not_probe_if_loss_based_increasing_outside_alr() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + probe_controller.enable_periodic_alr_probing(true); + probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::LossLimitedBweIncreasing, + clock, + ); + + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + probe_controller.set_alr_start_time(None); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } + + #[test] + fn max_allocated_bitrate_not_reset() { + let clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + probe_controller.on_max_total_allocated_bitrate(START_BITRATE / 4, clock); + probe_controller.reset(clock); + + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + assert_eq!(probes[0].target_data_rate, START_BITRATE / 4 * 2); + } + + #[test] + fn skip_alr_probe_if_estimate_larger_than_max_probe() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + skip_if_est_larger_than_fraction_of_max: 0.9, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.enable_periodic_alr_probing(true); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + let probes = probe_controller.set_estimated_bitrate( + MAX_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + probe_controller.set_alr_start_time(Some(clock)); + clock += TimeDelta::from_seconds(10); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + // But if the max rate increase, A new probe is sent. + let probes = + probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, 2 * MAX_BITRATE, clock); + assert!(!probes.is_empty()); + } + + #[test] + fn skip_alr_probe_if_estimate_larger_than_fraction_of_max_allocated() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + skip_if_est_larger_than_fraction_of_max: 1.0, + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + probe_controller.enable_periodic_alr_probing(true); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + probe_controller.set_estimated_bitrate( + MAX_BITRATE / 2, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + + clock += TimeDelta::from_seconds(10); + probe_controller.set_alr_start_time(Some(clock)); + let probes = probe_controller.on_max_total_allocated_bitrate(MAX_BITRATE / 2, clock); + // No probes since total allocated is not higher than the current estimate. + assert!(probes.is_empty()); + clock += TimeDelta::from_seconds(2); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + // But if the max allocated increase, A new probe is sent. + let probes = probe_controller.on_max_total_allocated_bitrate( + MAX_BITRATE / 2 + DataRate::from_bits_per_sec(1), + clock, + ); + assert!(!probes.is_empty()); + } + + #[test] + fn dont_probe_if_delay_increased() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + // Need to wait at least one second before process can trigger a new probe. + clock += TimeDelta::from_millis(1100); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + let probes = probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::DelayBasedLimitedDelayIncreased, + clock, + ); + assert!(probes.is_empty()); + + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } + + #[test] + fn dont_probe_if_high_rtt() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + ..Default::default() + }); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); + + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + // Need to wait at least one second before process can trigger a new probe. + clock += TimeDelta::from_millis(1100); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + let probes = probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::RttBasedBackOffHighRtt, + clock, + ); + assert!(probes.is_empty()); + + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } +} diff --git a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/aimd_rate_control.rs b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/aimd_rate_control.rs new file mode 100644 index 00000000000..5256dfe195b --- /dev/null +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/aimd_rate_control.rs @@ -0,0 +1,641 @@ +/* + * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::{ + LinkCapacityEstimator, + api::{ + transport::BandwidthUsage, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + experiments::FieldTrials, + remote_bitrate_estimator::BITRATE_WINDOW, +}; + +use super::{CONGESTION_CONTROLLER_MIN_BITRATE, RateControlInput}; + +// WebRTC-BweBackOffFactor +#[derive(Debug, Clone)] +pub struct BweBackOffFactor { + pub backoff_factor: f64, // Enabled-* +} + +impl Default for BweBackOffFactor { + fn default() -> Self { + Self { + backoff_factor: Self::DEFAULT_BACKOFF_FACTOR, + } + } +} + +impl BweBackOffFactor { + const DEFAULT_BACKOFF_FACTOR: f64 = 0.85; + + pub fn validate(&mut self) { + if self.backoff_factor >= 1.0 { + tracing::warn!("Back-off factor must be less than 1."); + } else if self.backoff_factor <= 0.0 { + tracing::warn!("Back-off factor must be greater than 0."); + } else { + return; + } + + tracing::warn!( + "Failed to parse parameters for AimdRateControl experiment from field trial string. Using default." + ); + self.backoff_factor = Self::DEFAULT_BACKOFF_FACTOR + } +} + +#[derive(Debug, PartialEq, Clone, Copy)] +enum RateControlState { + Hold, + Increase, + Decrease, +} + +// A rate control implementation based on additive increases of +// bitrate when no over-use is detected and multiplicative decreases when +// over-uses are detected. When we think the available bandwidth has changes or +// is unknown, we will switch to a "slow-start mode" where we increase +// multiplicatively. +#[derive(Clone)] +pub struct AimdRateControl { + min_configured_bitrate: DataRate, + current_bitrate: DataRate, + latest_estimated_throughput: DataRate, + link_capacity: LinkCapacityEstimator, + rate_control_state: RateControlState, + time_last_bitrate_change: Timestamp, + time_last_bitrate_decrease: Timestamp, + time_first_throughput_estimate: Timestamp, + bitrate_is_initialized: bool, + beta: f64, + in_alr: bool, + rtt: TimeDelta, + send_side: bool, + // Allow the delay based estimate to only increase as long as application + // limited region (alr) is not detected. + no_bitrate_increase_in_alr: bool, + last_decrease: Option, +} + +impl Default for AimdRateControl { + fn default() -> Self { + let max_configured_bitrate = DataRate::from_kilobits_per_sec(30000); + Self { + min_configured_bitrate: CONGESTION_CONTROLLER_MIN_BITRATE, + current_bitrate: max_configured_bitrate, + latest_estimated_throughput: max_configured_bitrate, + link_capacity: LinkCapacityEstimator::new(), + rate_control_state: RateControlState::Hold, + time_last_bitrate_change: Timestamp::minus_infinity(), + time_last_bitrate_decrease: Timestamp::minus_infinity(), + time_first_throughput_estimate: Timestamp::minus_infinity(), + bitrate_is_initialized: false, + beta: Self::DEFAULT_BACKOFF_FACTOR, + in_alr: false, + rtt: Self::DEFAULT_RTT, + send_side: false, + no_bitrate_increase_in_alr: false, + last_decrease: None, + } + } +} + +impl AimdRateControl { + const DEFAULT_RTT: TimeDelta = TimeDelta::from_millis(200); + const DEFAULT_BACKOFF_FACTOR: f64 = 0.85; + + pub fn new(field_trials: &FieldTrials, send_side: bool) -> Self { + let mut backoff = field_trials.bwe_back_off_factor.clone(); + backoff.validate(); + tracing::debug!( + "Using aimd rate control with back off factor {}", + backoff.backoff_factor + ); + + Self { + send_side, + beta: backoff.backoff_factor, + no_bitrate_increase_in_alr: field_trials.no_bitrate_increase_in_alr, + ..Default::default() + } + } + + // Returns true if the target bitrate has been initialized. This happens + // either if it has been explicitly set via SetStartBitrate/SetEstimate, or if + // we have measured a throughput. + pub fn valid_estimate(&self) -> bool { + self.bitrate_is_initialized + } + + pub fn set_start_bitrate(&mut self, start_bitrate: DataRate) { + self.current_bitrate = start_bitrate; + self.latest_estimated_throughput = self.current_bitrate; + self.bitrate_is_initialized = true; + } + + pub fn set_min_bitrate(&mut self, min_bitrate: DataRate) { + self.min_configured_bitrate = min_bitrate; + self.current_bitrate = self.current_bitrate.max(min_bitrate); + } + + /* unused + pub fn get_feedback_interval(&self) -> TimeDelta { + // Estimate how often we can send RTCP if we allocate up to 5% of bandwidth + // to feedback. + const RTCP_SIZE: DataSize = DataSize::from_bytes(80); + let rtcp_bitrate: DataRate = self.current_bitrate * 0.05; + let interval: TimeDelta = RTCP_SIZE / rtcp_bitrate; + const MIN_FEEDBACK_INTERVAL: TimeDelta = TimeDelta::from_millis(200); + const MAX_FEEDBACK_INTERVAL: TimeDelta = TimeDelta::from_millis(1000); + interval.clamp(MIN_FEEDBACK_INTERVAL, MAX_FEEDBACK_INTERVAL) + } + */ + + // Returns true if the bitrate estimate hasn't been changed for more than + // an RTT, or if the estimated_throughput is less than half of the current + // estimate. Should be used to decide if we should reduce the rate further + // when over-using. + pub fn time_to_reduce_further( + &mut self, + at_time: Timestamp, + estimated_throughput: DataRate, + ) -> bool { + let bitrate_reduction_interval: TimeDelta = self + .rtt + .clamp(TimeDelta::from_millis(10), TimeDelta::from_millis(200)); + if at_time - self.time_last_bitrate_change >= bitrate_reduction_interval { + return true; + } + if self.valid_estimate() { + // TODO(terelius/holmer): Investigate consequences of increasing + // the threshold to 0.95 * LatestEstimate(). + let threshold: DataRate = 0.5 * self.latest_estimate(); + return estimated_throughput < threshold; + } + false + } + // As above. To be used if overusing before we have measured a throughput. + pub fn initial_time_to_reduce_further(&mut self, at_time: Timestamp) -> bool { + self.valid_estimate() + && self.time_to_reduce_further( + at_time, + self.latest_estimate() / 2 - DataRate::from_bits_per_sec(1), + ) + } + + pub fn latest_estimate(&self) -> DataRate { + self.current_bitrate + } + pub fn set_rtt(&mut self, rtt: TimeDelta) { + self.rtt = rtt; + } + pub fn update(&mut self, input: RateControlInput, at_time: Timestamp) -> DataRate { + // Set the initial bit rate value to what we're receiving the first half + // second. + // TODO(bugs.webrtc.org/9379): The comment above doesn't match to the code. + if !self.bitrate_is_initialized { + const INITIALIZATION_TIME: TimeDelta = TimeDelta::from_seconds(5); + assert!(BITRATE_WINDOW <= INITIALIZATION_TIME); + + if let Some(estimated_throughput) = input.estimated_throughput { + if self.time_first_throughput_estimate.is_infinite() { + self.time_first_throughput_estimate = at_time; + } else if at_time - self.time_first_throughput_estimate > INITIALIZATION_TIME { + self.current_bitrate = estimated_throughput; + self.bitrate_is_initialized = true; + } + } + } + + self.change_bitrate(input, at_time); + self.current_bitrate + } + + pub fn set_in_application_limited_region(&mut self, in_alr: bool) { + self.in_alr = in_alr; + } + + pub fn set_estimate(&mut self, bitrate: DataRate, at_time: Timestamp) { + self.bitrate_is_initialized = true; + let prev_bitrate: DataRate = self.current_bitrate; + self.current_bitrate = self.clamp_bitrate(bitrate); + self.time_last_bitrate_change = at_time; + if self.current_bitrate < prev_bitrate { + self.time_last_bitrate_decrease = at_time; + } + } + + // Returns the increase rate when used bandwidth is near the link capacity. + pub fn get_near_max_increase_rate_bps_per_second(&self) -> f64 { + assert!(!self.current_bitrate.is_zero()); + let frame_interval: TimeDelta = TimeDelta::from_seconds_float(1.0 / 30.0); + let frame_size: DataSize = self.current_bitrate * frame_interval; + const PACKET_SIZE: DataSize = DataSize::from_bytes(1200); + let packets_per_frame: f64 = (frame_size / PACKET_SIZE).ceil(); + let avg_packet_size: DataSize = frame_size / packets_per_frame; + + // Approximate the over-use estimator delay to 100 ms. + let mut response_time: TimeDelta = self.rtt + TimeDelta::from_millis(100); + + response_time *= 2; + let increase_rate_bps_per_second: f64 = (avg_packet_size / response_time).bps_float(); + const MIN_INCREASE_RATE_BPS_PER_SECOND: f64 = 4000.0; + increase_rate_bps_per_second.max(MIN_INCREASE_RATE_BPS_PER_SECOND) + } + // Update the target bitrate based on, among other things, the current rate + // control state, the current target bitrate and the estimated throughput. + // When in the "increase" state the bitrate will be increased either + // additively or multiplicatively depending on the rate control region. When + // in the "decrease" state the bitrate will be decreased to slightly below the + // current throughput. When in the "hold" state the bitrate will be kept + // constant to allow built up queues to drain. + fn change_bitrate(&mut self, input: RateControlInput, at_time: Timestamp) { + let mut new_bitrate: Option = None; + let estimated_throughput: DataRate = input + .estimated_throughput + .unwrap_or(self.latest_estimated_throughput); + if let Some(estimated_throughput) = input.estimated_throughput { + self.latest_estimated_throughput = estimated_throughput; + } + + // An over-use should always trigger us to reduce the bitrate, even though + // we have not yet established our first estimate. By acting on the over-use, + // we will end up with a valid estimate. + if !self.bitrate_is_initialized && input.bw_state != BandwidthUsage::Overusing { + return; + } + + self.change_state(input, at_time); + + match self.rate_control_state { + RateControlState::Hold => (), + RateControlState::Increase => { + if estimated_throughput > self.link_capacity.upper_bound() { + self.link_capacity.reset(); + } + + // We limit the new bitrate based on the troughput to avoid unlimited + // bitrate increases. We allow a bit more lag at very low rates to not too + // easily get stuck if the encoder produces uneven outputs. + let mut increase_limit: DataRate = + 1.5 * estimated_throughput + DataRate::from_kilobits_per_sec(10); + if self.send_side && self.in_alr && self.no_bitrate_increase_in_alr { + // Do not increase the delay based estimate in alr since the estimator + // will not be able to get transport feedback necessary to detect if + // the new estimate is correct. + // If we have previously increased above the limit (for instance due to + // probing), we don't allow further changes. + increase_limit = self.current_bitrate; + } + + if self.current_bitrate < increase_limit { + let increased_bitrate: DataRate = if self.link_capacity.has_estimate() { + // The link_capacity estimate is reset if the measured throughput + // is too far from the estimate. We can therefore assume that our + // target rate is reasonably close to link capacity and use additive + // increase. + let additive_increase: DataRate = + self.additive_rate_increase(at_time, self.time_last_bitrate_change); + self.current_bitrate + additive_increase + } else { + // If we don't have an estimate of the link capacity, use faster ramp + // up to discover the capacity. + let multiplicative_increase: DataRate = self.multiplicative_rate_increase( + at_time, + self.time_last_bitrate_change, + self.current_bitrate, + ); + self.current_bitrate + multiplicative_increase + }; + new_bitrate = Some(increased_bitrate.min(increase_limit)); + } + self.time_last_bitrate_change = at_time; + } + RateControlState::Decrease => { + // Set bit rate to something slightly lower than the measured throughput + // to get rid of any self-induced delay. + let mut decreased_bitrate: DataRate = estimated_throughput * self.beta; + if decreased_bitrate > DataRate::from_kilobits_per_sec(5) { + decreased_bitrate -= DataRate::from_kilobits_per_sec(5); + } + + if decreased_bitrate > self.current_bitrate { + // TODO(terelius): The link_capacity estimate may be based on old + // throughput measurements. Relying on them may lead to unnecessary + // BWE drops. + if self.link_capacity.has_estimate() { + decreased_bitrate = self.beta * self.link_capacity.estimate(); + } + } + // Avoid increasing the rate when over-using. + if decreased_bitrate < self.current_bitrate { + new_bitrate = Some(decreased_bitrate); + } + + if self.bitrate_is_initialized && estimated_throughput < self.current_bitrate { + if let Some(new_bitrate) = new_bitrate { + self.last_decrease = Some(self.current_bitrate - new_bitrate); + } else { + self.last_decrease = Some(DataRate::zero()); + } + } + if estimated_throughput < self.link_capacity.lower_bound() { + // The current throughput is far from the estimated link capacity. Clear + // the estimate to allow an immediate update in OnOveruseDetected. + self.link_capacity.reset(); + } + + self.bitrate_is_initialized = true; + self.link_capacity.on_overuse_detected(estimated_throughput); + // Stay on hold until the pipes are cleared. + self.rate_control_state = RateControlState::Hold; + self.time_last_bitrate_change = at_time; + self.time_last_bitrate_decrease = at_time; + } + }; + + self.current_bitrate = self.clamp_bitrate(new_bitrate.unwrap_or(self.current_bitrate)); + } + + fn clamp_bitrate(&self, mut new_bitrate: DataRate) -> DataRate { + new_bitrate = new_bitrate.max(self.min_configured_bitrate); + new_bitrate + } + + fn multiplicative_rate_increase( + &self, + at_time: Timestamp, + last_time: Timestamp, + current_bitrate: DataRate, + ) -> DataRate { + let mut alpha: f64 = 1.08; + if last_time.is_finite() { + let time_since_last_update = at_time - last_time; + alpha = alpha.powf(time_since_last_update.seconds_float().min(1.0)); + } + let multiplicative_increase: DataRate = + (current_bitrate * (alpha - 1.0)).max(DataRate::from_bits_per_sec(1000)); + multiplicative_increase + } + fn additive_rate_increase(&self, at_time: Timestamp, last_time: Timestamp) -> DataRate { + let time_period_seconds: f64 = (at_time - last_time).seconds_float(); + let data_rate_increase_bps: f64 = + self.get_near_max_increase_rate_bps_per_second() * time_period_seconds; + DataRate::from_bits_per_sec_float(data_rate_increase_bps) + } + fn change_state(&mut self, input: RateControlInput, at_time: Timestamp) { + match input.bw_state { + BandwidthUsage::Normal => { + if self.rate_control_state == RateControlState::Hold { + self.time_last_bitrate_change = at_time; + self.rate_control_state = RateControlState::Increase; + } + } + BandwidthUsage::Overusing => { + if self.rate_control_state != RateControlState::Decrease { + self.rate_control_state = RateControlState::Decrease; + } + } + BandwidthUsage::Underusing => { + self.rate_control_state = RateControlState::Hold; + } + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + const INITIAL_TIME: Timestamp = Timestamp::from_millis(123_456); + + // After an overuse, we back off to 85% to the received bitrate. + const FRACTION_AFTER_OVERUSE: f64 = 0.85; + + #[test] + fn min_near_max_increase_rate_on_low_bandwith() { + let mut aimd_rate_control = AimdRateControl::default(); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(30_000), INITIAL_TIME); + assert_eq!( + aimd_rate_control.get_near_max_increase_rate_bps_per_second(), + 4_000.0 + ); + } + + #[test] + fn near_max_increase_rate_is_5kbps_on_90kbps_and_200ms_rtt() { + let mut aimd_rate_control = AimdRateControl::default(); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(90_000), INITIAL_TIME); + assert_eq!( + aimd_rate_control.get_near_max_increase_rate_bps_per_second(), + 5_000.0 + ); + } + + #[test] + fn near_max_increase_rate_is_5kbps_on_60kbps_and_100ms_rtt() { + let mut aimd_rate_control = AimdRateControl::default(); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(60_000), INITIAL_TIME); + aimd_rate_control.set_rtt(TimeDelta::from_millis(100)); + assert_eq!( + aimd_rate_control.get_near_max_increase_rate_bps_per_second(), + 5_000.0 + ); + } + + #[test] + fn get_increase_rate() { + let mut aimd_rate_control = AimdRateControl::default(); + const BITRATE: DataRate = DataRate::from_bits_per_sec(300_000); + aimd_rate_control.set_estimate(BITRATE, INITIAL_TIME); + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Overusing, Some(BITRATE)), + INITIAL_TIME, + ); + assert!( + (aimd_rate_control.get_near_max_increase_rate_bps_per_second() - 14_000.0).abs() + <= 1_000.0, + ); + } + + #[test] + fn bwe_limited_by_acked_bitrate() { + let mut aimd_rate_control = AimdRateControl::default(); + const ACKED_BITRATE: DataRate = DataRate::from_bits_per_sec(10_000); + let mut now: Timestamp = INITIAL_TIME; + aimd_rate_control.set_estimate(ACKED_BITRATE, now); + while now - INITIAL_TIME < TimeDelta::from_seconds(20) { + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Normal, Some(ACKED_BITRATE)), + now, + ); + now += TimeDelta::from_millis(100); + } + assert!(aimd_rate_control.valid_estimate()); + assert_eq!( + aimd_rate_control.latest_estimate(), + 1.5 * ACKED_BITRATE + DataRate::from_bits_per_sec(10_000) + ); + } + + #[test] + fn bwe_not_limited_by_decreasing_acked_bitrate() { + let mut aimd_rate_control = AimdRateControl::default(); + const ACKED_BITRATE: DataRate = DataRate::from_bits_per_sec(10_000); + let mut now: Timestamp = INITIAL_TIME; + aimd_rate_control.set_estimate(ACKED_BITRATE, now); + while now - INITIAL_TIME < TimeDelta::from_seconds(20) { + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Normal, Some(ACKED_BITRATE)), + now, + ); + now += TimeDelta::from_millis(100); + } + assert!(aimd_rate_control.valid_estimate()); + // If the acked bitrate decreases the BWE shouldn't be reduced to 1.5x + // what's being acked, but also shouldn't get to increase more. + let prev_estimate: DataRate = aimd_rate_control.latest_estimate(); + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Normal, Some(ACKED_BITRATE / 2)), + now, + ); + let new_estimate: DataRate = aimd_rate_control.latest_estimate(); + assert_eq!(new_estimate, prev_estimate); + assert!( + (new_estimate.bps() + - (1.5f64 * ACKED_BITRATE + DataRate::from_bits_per_sec(10_000)).bps()) + .abs() + < 2_000 + ); + } + + #[test] + fn typical_drop() { + let mut aimd_rate_control = AimdRateControl::default(); + // The rate increase at 216 kbps should be 12 kbps. + const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(264_000); + const UPDATED_BITRATE: DataRate = DataRate::from_bits_per_sec(216_000); + let acked_bitrate: DataRate = + (UPDATED_BITRATE + DataRate::from_bits_per_sec(5_000)) / FRACTION_AFTER_OVERUSE; + let mut now: Timestamp = INITIAL_TIME; + aimd_rate_control.set_estimate(INITIAL_BITRATE, now); + now += TimeDelta::from_millis(100); + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Overusing, Some(acked_bitrate)), + now, + ); + assert_eq!(aimd_rate_control.latest_estimate(), UPDATED_BITRATE); + assert_eq!( + aimd_rate_control.get_near_max_increase_rate_bps_per_second(), + 12_000.0 + ); + } + + #[test] + fn sending_rate_bounded_when_throughput_not_estimated() { + let mut aimd_rate_control = AimdRateControl::default(); + const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(123_000); + let mut now: Timestamp = INITIAL_TIME; + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Normal, Some(INITIAL_BITRATE)), + now, + ); + // AimdRateControl sets the initial bit rate to what it receives after + // five seconds has passed. + // TODO(bugs.webrtc.org/9379): The comment in the AimdRateControl does not + // match the constant. + const INITIALIZATION_TIME: TimeDelta = TimeDelta::from_seconds(5); + now += INITIALIZATION_TIME + TimeDelta::from_millis(1); + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Normal, Some(INITIAL_BITRATE)), + now, + ); + for _ in 0..100 { + aimd_rate_control.update(RateControlInput::new(BandwidthUsage::Normal, None), now); + now += TimeDelta::from_millis(100); + } + assert!( + aimd_rate_control.latest_estimate() + <= INITIAL_BITRATE * 1.5 + DataRate::from_bits_per_sec(10_000) + ); + } + + #[test] + fn estimate_does_not_increase_in_alr() { + // When alr is detected, the delay based estimator is not allowed to increase + // bwe since there will be no feedback from the network if the new estimate + // is correct. + // WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/ + let field_trials = FieldTrials { + no_bitrate_increase_in_alr: true, + ..Default::default() + }; + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + let mut now: Timestamp = INITIAL_TIME; + const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(123_000); + aimd_rate_control.set_estimate(INITIAL_BITRATE, now); + aimd_rate_control.set_in_application_limited_region(true); + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Normal, Some(INITIAL_BITRATE)), + now, + ); + assert_eq!(aimd_rate_control.latest_estimate(), INITIAL_BITRATE); + + for _ in 0..100 { + aimd_rate_control.update(RateControlInput::new(BandwidthUsage::Normal, None), now); + now += TimeDelta::from_millis(100); + } + assert_eq!(aimd_rate_control.latest_estimate(), INITIAL_BITRATE); + } + + #[test] + fn set_estimate_increase_bwe_in_alr() { + let field_trials = FieldTrials { + no_bitrate_increase_in_alr: true, + ..Default::default() + }; + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + + const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(123_000); + aimd_rate_control.set_estimate(INITIAL_BITRATE, INITIAL_TIME); + aimd_rate_control.set_in_application_limited_region(true); + assert_eq!(aimd_rate_control.latest_estimate(), INITIAL_BITRATE); + aimd_rate_control.set_estimate(2 * INITIAL_BITRATE, INITIAL_TIME); + assert_eq!(aimd_rate_control.latest_estimate(), 2 * INITIAL_BITRATE); + } + + #[test] + fn estimate_increase_while_not_in_alr() { + // Allow the estimate to increase as long as alr is not detected to ensure + // tha BWE can not get stuck at a certain bitrate. + // WebRTC-DontIncreaseDelayBasedBweInAlr/Enabled/ + let field_trials = FieldTrials { + no_bitrate_increase_in_alr: true, + ..Default::default() + }; + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + aimd_rate_control.no_bitrate_increase_in_alr = true; + let mut now: Timestamp = INITIAL_TIME; + const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(123_000); + aimd_rate_control.set_estimate(INITIAL_BITRATE, now); + aimd_rate_control.set_in_application_limited_region(false); + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Normal, Some(INITIAL_BITRATE)), + now, + ); + for _ in 0..100 { + aimd_rate_control.update(RateControlInput::new(BandwidthUsage::Normal, None), now); + now += TimeDelta::from_millis(100); + } + assert!(aimd_rate_control.latest_estimate() > INITIAL_BITRATE); + } +} diff --git a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/bwe_defines.rs b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/bwe_defines.rs new file mode 100644 index 00000000000..5c2a2a977ed --- /dev/null +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/bwe_defines.rs @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use crate::gcc::api::{ + transport::BandwidthUsage, + units::{DataRate, TimeDelta}, +}; + +pub const CONGESTION_CONTROLLER_MIN_BITRATE: DataRate = DataRate::from_bits_per_sec(5_000); +pub const BITRATE_WINDOW: TimeDelta = TimeDelta::from_seconds(1); + +/* unused +pub enum BweNames { + ReceiverNoExtension = 0, + ReceiverTOffset = 1, + ReceiverAbsSendTime = 2, + SendSideTransportSeqNum = 3, + BweNamesMax = 4, +} +*/ + +pub struct RateControlInput { + pub bw_state: BandwidthUsage, + pub estimated_throughput: Option, +} + +impl RateControlInput { + pub fn new(bw_state: BandwidthUsage, estimated_throughput: Option) -> Self { + RateControlInput { + bw_state, + estimated_throughput, + } + } +} diff --git a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs new file mode 100644 index 00000000000..b49f9aa6183 --- /dev/null +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs @@ -0,0 +1,5 @@ +mod aimd_rate_control; +mod bwe_defines; + +pub use aimd_rate_control::*; +pub use bwe_defines::*; diff --git a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs new file mode 100644 index 00000000000..411cffa0b11 --- /dev/null +++ b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs @@ -0,0 +1,800 @@ +/* + * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +//! Upstream also keeps the older `AcknowledgedBitrateEstimator`, selected when this estimator's +//! field trial is disabled; it is enabled by default, so the older one is gone. QUIC tracks every +//! packet it sends, so no data goes unacknowledged outside the feedback, and `unacked_weight` with +//! it. + +use std::collections::VecDeque; + +use crate::gcc::api::{ + transport::PacketResult, + units::{DataRate, DataSize, TimeDelta, Timestamp}, +}; + +// WebRTC-Bwe-RobustThroughputEstimatorSettings +#[derive(Debug, Clone)] +pub struct RobustThroughputEstimatorSettings { + // The estimator keeps the smallest window containing at least + // `window_packets` and at least the packets received during the last + // `min_window_duration` milliseconds. + // (This means that it may store more than `window_packets` at high bitrates, + // and a longer duration than `min_window_duration` at low bitrates.) + // However, if will never store more than MaxPackets (for performance + // reasons), and never longer than max_window_duration (to avoid very old + // packets influencing the estimate for example when sending is paused). + pub window_packets: usize, + pub max_window_packets: usize, + pub window_duration: TimeDelta, + pub max_window_duration: TimeDelta, + + // The estimator window requires at least `required_packets` packets + // to produce an estimate. + pub required_packets: usize, +} + +impl Default for RobustThroughputEstimatorSettings { + fn default() -> Self { + Self { + window_packets: 20, + max_window_packets: 500, + window_duration: TimeDelta::from_seconds(1), + max_window_duration: TimeDelta::from_seconds(5), + required_packets: 10, + } + } +} + +impl RobustThroughputEstimatorSettings { + pub fn validate(&mut self) { + if self.window_packets < 10 || 1000 < self.window_packets { + tracing::warn!("Window size must be between 10 and 1000 packets"); + self.window_packets = 20; + } + if self.max_window_packets < 10 || 1000 < self.max_window_packets { + tracing::warn!("Max window size must be between 10 and 1000 packets"); + self.max_window_packets = 500; + } + self.max_window_packets = self.max_window_packets.max(self.window_packets); + + if self.required_packets < 10 || 1000 < self.required_packets { + tracing::warn!( + "Required number of initial packets must be between 10 and 1000 packets" + ); + self.required_packets = 10; + } + self.required_packets = self.required_packets.min(self.window_packets); + + if self.window_duration < TimeDelta::from_millis(100) + || TimeDelta::from_millis(3000) < self.window_duration + { + tracing::warn!("Window duration must be between 100 and 3000 ms"); + self.window_duration = TimeDelta::from_millis(750); + } + if self.max_window_duration < TimeDelta::from_seconds(1) + || TimeDelta::from_seconds(15) < self.max_window_duration + { + tracing::warn!("Max window duration must be between 1 and 15 s"); + self.max_window_duration = TimeDelta::from_seconds(5); + } + self.window_duration = self.window_duration.min(self.max_window_duration); + } +} + +#[derive(Clone)] +pub struct RobustThroughputEstimator { + settings: RobustThroughputEstimatorSettings, + window: VecDeque, + latest_discarded_send_time: Timestamp, +} + +impl Default for RobustThroughputEstimator { + fn default() -> Self { + Self::new(RobustThroughputEstimatorSettings::default()) + } +} + +impl RobustThroughputEstimator { + pub fn new(mut settings: RobustThroughputEstimatorSettings) -> Self { + settings.validate(); + + Self { + settings, + window: VecDeque::new(), + latest_discarded_send_time: Timestamp::minus_infinity(), + } + } + + fn first_packet_outside_window(&self) -> bool { + if self.window.is_empty() { + return false; + } + if self.window.len() > self.settings.max_window_packets { + return true; + } + let current_window_duration: TimeDelta = + self.window.back().unwrap().receive_time - self.window.front().unwrap().receive_time; + if current_window_duration > self.settings.max_window_duration { + return true; + } + if self.window.len() > self.settings.window_packets + && current_window_duration > self.settings.window_duration + { + return true; + } + false + } + + pub fn incoming_packet_feedback(&mut self, packet_feedback: &[PacketResult]) { + //assert!(packet_feedback.is_sorted_by_key(|x| x.receive_time)); + for packet in packet_feedback { + // Ignore packets without valid send or receive times. + // (This should not happen in production since lost packets are filtered + // out before passing the feedback vector to the throughput estimator. + // However, explicitly handling this case makes the estimator more robust + // and avoids a hard-to-detect bad state.) + if packet.receive_time.is_infinite() || packet.sent_packet.send_time.is_infinite() { + continue; + } + + // Insert the new packet. + self.window.push_back(*packet); + // In most cases, receive timestamps should already be in order, but in the + // rare case where feedback packets have been reordered, we do some swaps to + // ensure that the window is sorted. + let mut i = self.window.len() - 1; + while i > 0 && self.window[i].receive_time < self.window[i - 1].receive_time { + self.window.swap(i, i - 1); + i -= 1; + } + + const MAX_REORDERING_TIME: TimeDelta = TimeDelta::from_seconds(1); + let receive_delta: TimeDelta = + self.window.back().unwrap().receive_time - packet.receive_time; + if receive_delta > MAX_REORDERING_TIME { + tracing::warn!( + "Severe packet re-ordering or timestamps offset changed: {:?}", + receive_delta + ); + self.window.clear(); + self.latest_discarded_send_time = Timestamp::minus_infinity(); + } + } + + // Remove old packets. + while self.first_packet_outside_window() { + self.latest_discarded_send_time = std::cmp::max( + self.latest_discarded_send_time, + self.window.front().unwrap().sent_packet.send_time, + ); + self.window.pop_front(); + } + } + + pub fn bitrate(&self) -> Option { + if self.window.is_empty() || self.window.len() < self.settings.required_packets { + return None; + } + + let mut largest_recv_gap = TimeDelta::zero(); + let mut second_largest_recv_gap = TimeDelta::zero(); + for i in 1..self.window.len() { + // Find receive time gaps. + let gap: TimeDelta = self.window[i].receive_time - self.window[i - 1].receive_time; + if gap > largest_recv_gap { + second_largest_recv_gap = largest_recv_gap; + largest_recv_gap = gap; + } else if gap > second_largest_recv_gap { + second_largest_recv_gap = gap; + } + } + + let mut first_send_time: Timestamp = Timestamp::plus_infinity(); + let mut last_send_time: Timestamp = Timestamp::minus_infinity(); + let mut first_recv_time: Timestamp = Timestamp::plus_infinity(); + let mut last_recv_time: Timestamp = Timestamp::minus_infinity(); + let mut recv_size: DataSize = DataSize::from_bytes(0); + let mut send_size: DataSize = DataSize::from_bytes(0); + let mut first_recv_size: DataSize = DataSize::from_bytes(0); + let mut last_send_size: DataSize = DataSize::from_bytes(0); + let mut num_sent_packets_in_window: usize = 0; + for packet in &self.window { + if packet.receive_time < first_recv_time { + first_recv_time = packet.receive_time; + first_recv_size = packet.sent_packet.size; + } + last_recv_time = std::cmp::max(last_recv_time, packet.receive_time); + recv_size += packet.sent_packet.size; + + if packet.sent_packet.send_time < self.latest_discarded_send_time { + // If we have dropped packets from the window that were sent after + // this packet, then this packet was reordered. Ignore it from + // the send rate computation (since the send time may be very far + // in the past, leading to underestimation of the send rate.) + // However, ignoring packets creates a risk that we end up without + // any packets left to compute a send rate. + continue; + } + if packet.sent_packet.send_time > last_send_time { + last_send_time = packet.sent_packet.send_time; + last_send_size = packet.sent_packet.size; + } + first_send_time = std::cmp::min(first_send_time, packet.sent_packet.send_time); + + send_size += packet.sent_packet.size; + num_sent_packets_in_window += 1; + } + + // Suppose a packet of size S is sent every T milliseconds. + // A window of N packets would contain N*S bytes, but the time difference + // between the first and the last packet would only be (N-1)*T. Thus, we + // need to remove the size of one packet to get the correct rate of S/T. + // Which packet to remove (if the packets have varying sizes), + // depends on the network model. + // Suppose that 2 packets with sizes s1 and s2, are received at times t1 + // and t2, respectively. If the packets were transmitted back to back over + // a bottleneck with rate capacity r, then we'd expect t2 = t1 + r * s2. + // Thus, r = (t2-t1) / s2, so the size of the first packet doesn't affect + // the difference between t1 and t2. + // Analoguously, if the first packet is sent at time t1 and the sender + // paces the packets at rate r, then the second packet can be sent at time + // t2 = t1 + r * s1. Thus, the send rate estimate r = (t2-t1) / s1 doesn't + // depend on the size of the last packet. + recv_size -= first_recv_size; + send_size -= last_send_size; + + // Remove the largest gap by replacing it by the second largest gap. + // This is to ensure that spurious "delay spikes" (i.e. when the + // network stops transmitting packets for a short period, followed + // by a burst of delayed packets), don't cause the estimate to drop. + // This could cause an overestimation, which we guard against by + // never returning an estimate above the send rate. + assert!(first_recv_time.is_finite()); + assert!(last_recv_time.is_finite()); + let mut recv_duration: TimeDelta = + (last_recv_time - first_recv_time) - largest_recv_gap + second_largest_recv_gap; + recv_duration = std::cmp::max(recv_duration, TimeDelta::from_millis(1)); + + if num_sent_packets_in_window < self.settings.required_packets { + // Too few send times to calculate a reliable send rate. + return Some(recv_size / recv_duration); + } + + assert!(first_send_time.is_finite()); + assert!(last_send_time.is_finite()); + let mut send_duration: TimeDelta = last_send_time - first_send_time; + send_duration = std::cmp::max(send_duration, TimeDelta::from_millis(1)); + + Some(std::cmp::min( + send_size / send_duration, + recv_size / recv_duration, + )) + } +} + +#[cfg(test)] +mod test { + use crate::gcc::api::transport::SentPacket; + + use super::*; + + struct FeedbackGenerator { + send_clock: Timestamp, + recv_clock: Timestamp, + } + + impl Default for FeedbackGenerator { + fn default() -> Self { + Self { + send_clock: Timestamp::from_millis(100000), + recv_clock: Timestamp::from_millis(10000), + } + } + } + + impl FeedbackGenerator { + fn create_feedback( + &mut self, + number_of_packets: usize, + packet_size: DataSize, + send_rate: DataRate, + recv_rate: DataRate, + ) -> Vec { + let mut packet_feedback = Vec::with_capacity(number_of_packets); + + for _ in 0..number_of_packets { + self.recv_clock += packet_size / recv_rate; + packet_feedback.push(PacketResult { + sent_packet: SentPacket { + send_time: self.send_clock, + size: packet_size, + ..Default::default() + }, + receive_time: self.recv_clock, + ambiguous_receive_time: false, + }); + self.send_clock += packet_size / send_rate; + } + + packet_feedback + } + + fn current_receive_clock(&self) -> Timestamp { + self.recv_clock + } + + fn advance_receive_clock(&mut self, delta: TimeDelta) { + self.recv_clock += delta; + } + + fn advance_send_clock(&mut self, delta: TimeDelta) { + self.send_clock += delta; + } + } + + #[test] + fn initial_estimate() { + let mut feedback_generator = FeedbackGenerator::default(); + + let mut throughput_estimator = RobustThroughputEstimator::default(); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + // No estimate until the estimator has enough data. + let packet_feedback: Vec = + feedback_generator.create_feedback(9, DataSize::from_bytes(1000), send_rate, recv_rate); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + assert!(throughput_estimator.bitrate().is_none()); + + // Estimate once `required_packets` packets have been received. + let packet_feedback = + feedback_generator.create_feedback(1, DataSize::from_bytes(1000), send_rate, recv_rate); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + + // Estimate remains stable when send and receive rates are stable. + let packet_feedback = feedback_generator.create_feedback( + 15, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + } + + #[test] + fn estimate_adapts() { + let mut feedback_generator = FeedbackGenerator::default(); + let mut throughput_estimator = RobustThroughputEstimator::default(); + + // 1 second, 800kbps, estimate is stable. + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + for _ in 0..10 { + let packet_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + } + + // 1 second, 1600kbps, estimate increases + let send_rate = DataRate::from_bytes_per_sec(200000); + let recv_rate = DataRate::from_bytes_per_sec(200000); + for _ in 0..20 { + let packet_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert!(throughput.unwrap() >= DataRate::from_bytes_per_sec(100000)); + assert!(throughput.unwrap() <= send_rate); + } + + // 1 second, 1600kbps, estimate is stable + for _ in 0..20 { + let packet_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + } + + // 1 second, 400kbps, estimate decreases + let send_rate = DataRate::from_bytes_per_sec(50000); + let recv_rate = DataRate::from_bytes_per_sec(50000); + for _ in 0..5 { + let packet_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert!(throughput.unwrap() <= DataRate::from_bytes_per_sec(200000)); + assert!(throughput.unwrap() >= send_rate); + } + + // 1 second, 400kbps, estimate is stable + let send_rate = DataRate::from_bytes_per_sec(50000); + let recv_rate = DataRate::from_bytes_per_sec(50000); + for _ in 0..5 { + let packet_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + } + } + + #[test] + fn capped_by_receive_rate() { + let mut feedback_generator = FeedbackGenerator::default(); + let mut throughput_estimator = RobustThroughputEstimator::default(); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(25000); + + let packet_feedback: Vec = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + recv_rate.bytes_per_sec_float(), + epsilon = 0.05 * recv_rate.bytes_per_sec_float() + ); // Allow 5% error + } + + #[test] + fn capped_by_send_rate() { + let mut feedback_generator = FeedbackGenerator::default(); + let mut throughput_estimator = RobustThroughputEstimator::default(); + let send_rate: DataRate = DataRate::from_bytes_per_sec(50000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + let packet_feedback: Vec = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + send_rate.bytes_per_sec_float(), + epsilon = 0.05 * send_rate.bytes_per_sec_float() + ); // Allow 5% error + } + + #[test] + fn delay_spike() { + let mut feedback_generator = FeedbackGenerator::default(); + // This test uses a 500ms window to amplify the effect + // of a delay spike. + let settings = RobustThroughputEstimatorSettings { + window_duration: TimeDelta::from_millis(500), + ..Default::default() + }; + let mut throughput_estimator = RobustThroughputEstimator::new(settings); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + let packet_feedback: Vec = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + + // Delay spike. 25 packets sent, but none received. + feedback_generator.advance_receive_clock(TimeDelta::from_millis(250)); + + // Deliver all of the packets during the next 50 ms. (During this time, + // we'll have sent an additional 5 packets, so we need to receive 30 + // packets at 1000 bytes each in 50 ms, i.e. 600000 bytes per second). + let recv_rate = DataRate::from_bytes_per_sec(600000); + // Estimate should not drop. + for _ in 0..30 { + let packet_feedback = feedback_generator.create_feedback( + 1, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + send_rate.bytes_per_sec_float(), + epsilon = 0.05 * send_rate.bytes_per_sec_float() + ); // Allow 5% error + } + + // Delivery at normal rate. When the packets received before the gap + // has left the estimator's window, the receive rate will be high, but the + // estimate should be capped by the send rate. + let recv_rate = DataRate::from_bytes_per_sec(100000); + for _ in 0..20 { + let packet_feedback = feedback_generator.create_feedback( + 5, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + send_rate.bytes_per_sec_float(), + epsilon = 0.05 * send_rate.bytes_per_sec_float() + ); // Allow 5% error + } + } + + #[test] + fn high_loss() { + let mut feedback_generator = FeedbackGenerator::default(); + let mut throughput_estimator = RobustThroughputEstimator::default(); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + let mut packet_feedback: Vec = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + + // 50% loss + for packet in packet_feedback.iter_mut().skip(1).step_by(2) { + packet.receive_time = Timestamp::plus_infinity(); + } + + packet_feedback.sort_by_key(|x| x.receive_time); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + send_rate.bytes_per_sec_float() / 2.0, + epsilon = 0.05 * send_rate.bytes_per_sec_float() / 2.0 + ); // Allow 5% error + } + + #[test] + fn reordered_feedback() { + let mut feedback_generator = FeedbackGenerator::default(); + let mut throughput_estimator = RobustThroughputEstimator::default(); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + let packet_feedback: Vec = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + + let delayed_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + let packet_feedback = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + + // Since we're missing some feedback, it's expected that the + // estimate will drop. + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert!(throughput.unwrap() < send_rate); + + // But it should completely recover as soon as we get the feedback. + throughput_estimator.incoming_packet_feedback(&delayed_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + + // It should then remain stable (as if the feedbacks weren't reordered.) + for _ in 0..10 { + let packet_feedback = feedback_generator.create_feedback( + 15, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + } + } + + #[test] + fn deep_reordering() { + let mut feedback_generator = FeedbackGenerator::default(); + // This test uses a 500ms window to amplify the + // effect of reordering. + let settings = RobustThroughputEstimatorSettings { + window_duration: TimeDelta::from_millis(500), + ..Default::default() + }; + let mut throughput_estimator = RobustThroughputEstimator::new(settings); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + let mut delayed_packets: Vec = + feedback_generator.create_feedback(1, DataSize::from_bytes(1000), send_rate, recv_rate); + + for _ in 0..10 { + let packet_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + } + + // Delayed packet arrives ~1 second after it should have. + // Since the window is 500 ms, the delayed packet was sent ~500 + // ms before the second oldest packet. However, the send rate + // should not drop. + delayed_packets.first_mut().unwrap().receive_time = + feedback_generator.current_receive_clock(); + throughput_estimator.incoming_packet_feedback(&delayed_packets); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + send_rate.bytes_per_sec_float(), + epsilon = 0.05 * send_rate.bytes_per_sec_float() + ); // Allow 5% error + + // Thoughput should stay stable. + for _ in 0..10 { + let packet_feedback: Vec = feedback_generator.create_feedback( + 10, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + send_rate.bytes_per_sec_float(), + epsilon = 0.05 * send_rate.bytes_per_sec_float() + ); // Allow 5% error + } + } + + #[test] + fn resets_if_receive_clock_change_backwards() { + let mut feedback_generator = FeedbackGenerator::default(); + let mut throughput_estimator = RobustThroughputEstimator::default(); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + let packet_feedback: Vec = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + assert_eq!(throughput_estimator.bitrate().unwrap(), send_rate); + + feedback_generator.advance_receive_clock(TimeDelta::from_seconds(-2)); + let send_rate = DataRate::from_bytes_per_sec(200000); + let recv_rate = DataRate::from_bytes_per_sec(200000); + let packet_feedback = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + assert_eq!(throughput_estimator.bitrate().unwrap(), send_rate); + } + + #[test] + fn stream_paused_and_resumed() { + let mut feedback_generator = FeedbackGenerator::default(); + let mut throughput_estimator = RobustThroughputEstimator::default(); + let send_rate: DataRate = DataRate::from_bytes_per_sec(100000); + let recv_rate: DataRate = DataRate::from_bytes_per_sec(100000); + + let packet_feedback: Vec = feedback_generator.create_feedback( + 20, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_some()); + let expected_bytes_per_sec: f64 = 100.0 * 1000.0; + assert_relative_eq!( + throughput.unwrap().bytes_per_sec_float(), + expected_bytes_per_sec, + epsilon = 0.05 * expected_bytes_per_sec + ); // Allow 5% error + + // No packets sent or feedback received for 60s. + feedback_generator.advance_send_clock(TimeDelta::from_seconds(60)); + feedback_generator.advance_receive_clock(TimeDelta::from_seconds(60)); + + // Resume sending packets at the same rate as before. The estimate + // will initially be invalid, due to lack of recent data. + let packet_feedback = + feedback_generator.create_feedback(5, DataSize::from_bytes(1000), send_rate, recv_rate); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert!(throughput.is_none()); + + // But be back to the normal level once we have enough data. + for _ in 0..4 { + let packet_feedback = feedback_generator.create_feedback( + 5, + DataSize::from_bytes(1000), + send_rate, + recv_rate, + ); + throughput_estimator.incoming_packet_feedback(&packet_feedback); + let throughput = throughput_estimator.bitrate(); + assert_eq!(throughput.unwrap(), send_rate); + } + } +} // namespace webrtc diff --git a/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs b/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs new file mode 100644 index 00000000000..52593f949e2 --- /dev/null +++ b/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs @@ -0,0 +1,445 @@ +/* + * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + * + * FEC and NACK added bitrate is handled outside class + */ + +//! QUIC carries no RTCP, so the receiver-report loss path (`UpdatePacketsLost`, the classic +//! 2%/10% loss controller and its min-bitrate history) and the REMB receiver limit are gone. The +//! start phase upstream ends two seconds after the first receiver report, so here it never does: +//! the delay-based estimate is trusted until the loss-based estimator is ready. + +use crate::gcc::remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE; +use crate::gcc::{ + LossBasedBweV2, LossBasedState, + api::{ + transport::{BandwidthUsage, SentPacket, TransportPacketsFeedback}, + units::{DataRate, TimeDelta, Timestamp}, + }, + experiments::FieldTrials, +}; + +// WebRTC-Bwe-MaxRttLimit +#[derive(Clone, Debug)] +pub struct RttBasedBackoffConfig { + disabled: bool, // Disabled + configured_limit: TimeDelta, // limit + drop_fraction: f64, // fraction + drop_interval: TimeDelta, // interval, + bandwidth_floor: DataRate, // floor +} + +impl Default for RttBasedBackoffConfig { + fn default() -> Self { + Self { + disabled: false, + configured_limit: TimeDelta::from_seconds(3), + drop_fraction: 0.8, + drop_interval: TimeDelta::from_seconds(1), + bandwidth_floor: DataRate::from_kilobits_per_sec(5), + } + } +} + +#[derive(Clone)] +pub struct RttBasedBackoff { + pub drop_fraction: f64, + pub drop_interval: TimeDelta, + pub bandwidth_floor: DataRate, + pub rtt_limit: TimeDelta, + pub last_propagation_rtt_update: Timestamp, + pub last_propagation_rtt: TimeDelta, + pub last_packet_sent: Timestamp, +} + +impl RttBasedBackoff { + pub fn new(config: &RttBasedBackoffConfig) -> Self { + Self { + drop_fraction: config.drop_fraction, + drop_interval: config.drop_interval, + bandwidth_floor: config.bandwidth_floor, + rtt_limit: if !config.disabled { + config.configured_limit + } else { + TimeDelta::plus_infinity() + }, + last_propagation_rtt_update: Timestamp::plus_infinity(), + last_propagation_rtt: TimeDelta::zero(), + last_packet_sent: Timestamp::minus_infinity(), + } + } + + pub fn update_propagation_rtt(&mut self, at_time: Timestamp, propagation_rtt: TimeDelta) { + self.last_propagation_rtt_update = at_time; + self.last_propagation_rtt = propagation_rtt; + } + pub fn is_rtt_above_limit(&self) -> bool { + self.corrected_rtt() > self.rtt_limit + } + + fn corrected_rtt(&self) -> TimeDelta { + // Avoid timeout when no packets are being sent. + let timeout_correction: TimeDelta = std::cmp::max( + self.last_packet_sent - self.last_propagation_rtt_update, + TimeDelta::zero(), + ); + timeout_correction + self.last_propagation_rtt + } +} + +#[derive(Clone)] +pub struct SendSideBandwidthEstimation { + rtt_backoff: RttBasedBackoff, + + current_target: DataRate, + min_bitrate_configured: DataRate, + max_bitrate_configured: DataRate, + last_low_bitrate_log: Timestamp, + + last_round_trip_time: TimeDelta, + + delay_based_limit: DataRate, + time_last_decrease: Timestamp, + loss_based_bandwidth_estimator_v2: LossBasedBweV2, + loss_based_state: LossBasedState, +} + +impl Default for SendSideBandwidthEstimation { + fn default() -> Self { + Self::new(&FieldTrials::default()) + } +} + +impl SendSideBandwidthEstimation { + const DEFAULT_MAX_BITRATE: DataRate = DataRate::from_bits_per_sec(1000000000); + const LOW_BITRATE_LOG_PERIOD: TimeDelta = TimeDelta::from_millis(10000); + + pub fn new(field_trials: &FieldTrials) -> Self { + let mut loss_based_bandwidth_estimator_v2 = + LossBasedBweV2::new(field_trials.loss_based_bwe_v2.clone()); + if loss_based_bandwidth_estimator_v2.is_enabled() { + loss_based_bandwidth_estimator_v2 + .set_min_max_bitrate(CONGESTION_CONTROLLER_MIN_BITRATE, Self::DEFAULT_MAX_BITRATE); + } + + Self { + rtt_backoff: RttBasedBackoff::new(&field_trials.max_rtt_limit), + current_target: DataRate::zero(), + min_bitrate_configured: CONGESTION_CONTROLLER_MIN_BITRATE, + max_bitrate_configured: Self::DEFAULT_MAX_BITRATE, + last_low_bitrate_log: Timestamp::minus_infinity(), + last_round_trip_time: TimeDelta::zero(), + delay_based_limit: DataRate::plus_infinity(), + time_last_decrease: Timestamp::minus_infinity(), + loss_based_bandwidth_estimator_v2, + loss_based_state: LossBasedState::DelayBasedEstimate, + } + } + + pub fn target_rate(&self) -> DataRate { + std::cmp::max(self.min_bitrate_configured, self.current_target) + } + pub fn loss_based_state(&self) -> LossBasedState { + self.loss_based_state + } + // Return whether the current rtt is higher than the rtt limited configured in + // RttBasedBackoff. + pub fn is_rtt_above_limit(&self) -> bool { + self.rtt_backoff.is_rtt_above_limit() + } + + pub fn round_trip_time(&self) -> TimeDelta { + self.last_round_trip_time + } + + // Call periodically to update estimate. + pub fn update_estimate(&mut self, at_time: Timestamp) { + if self.rtt_backoff.is_rtt_above_limit() { + if at_time - self.time_last_decrease >= self.rtt_backoff.drop_interval + && self.current_target > self.rtt_backoff.bandwidth_floor + { + self.time_last_decrease = at_time; + let new_bitrate: DataRate = std::cmp::max( + self.current_target * self.rtt_backoff.drop_fraction, + self.rtt_backoff.bandwidth_floor, + ); + self.update_target_bitrate(new_bitrate, at_time); + return; + } + // TODO(srte): This is likely redundant in most cases. + self.apply_target_limits(at_time); + return; + } + + // We trust the delay-based estimate until the loss-based estimator is ready, to allow + // startup bitrate probing. + if !self + .loss_based_bandwidth_estimator_v2 + .ready_to_use_in_start_phase() + && self.delay_based_limit.is_finite() + && self.delay_based_limit > self.current_target + { + self.update_target_bitrate(self.delay_based_limit, at_time); + return; + } + + if self.loss_based_bandwidth_estimator_v2_ready_for_use() { + let result = self + .loss_based_bandwidth_estimator_v2 + .get_loss_based_result(); + self.loss_based_state = result.state; + self.update_target_bitrate(result.bandwidth_estimate, at_time); + return; + } + + // TODO(srte): This is likely redundant in most cases. + self.apply_target_limits(at_time); + } + pub fn on_sent_packet(&mut self, sent_packet: &SentPacket) { + // Only feedback-triggering packets will be reported here. + self.rtt_backoff.last_packet_sent = sent_packet.send_time; + } + pub fn update_propagation_rtt(&mut self, at_time: Timestamp, propagation_rtt: TimeDelta) { + self.rtt_backoff + .update_propagation_rtt(at_time, propagation_rtt); + } + // Call when a new delay-based estimate is available. + pub fn update_delay_based_estimate(&mut self, at_time: Timestamp, bitrate: DataRate) { + // TODO(srte): Ensure caller passes plus_infinity, not zero, to represent no + // limitation. + self.delay_based_limit = if bitrate.is_zero() { + DataRate::plus_infinity() + } else { + bitrate + }; + self.apply_target_limits(at_time); + } + pub fn update_rtt(&mut self, rtt: TimeDelta) { + if rtt > TimeDelta::zero() { + self.last_round_trip_time = rtt; + } + } + pub fn set_bitrates( + &mut self, + send_bitrate: Option, + min_bitrate: DataRate, + max_bitrate: DataRate, + at_time: Timestamp, + ) { + self.set_min_max_bitrate(min_bitrate, max_bitrate); + if let Some(send_bitrate) = send_bitrate { + self.set_send_bitrate(send_bitrate, at_time); + } + } + pub fn set_send_bitrate(&mut self, bitrate: DataRate, at_time: Timestamp) { + debug_assert!(bitrate > DataRate::zero()); + // Reset to avoid being capped by the estimate. + self.delay_based_limit = DataRate::plus_infinity(); + self.update_target_bitrate(bitrate, at_time); + } + pub fn set_min_max_bitrate(&mut self, min_bitrate: DataRate, max_bitrate: DataRate) { + self.min_bitrate_configured = std::cmp::max(min_bitrate, CONGESTION_CONTROLLER_MIN_BITRATE); + if max_bitrate > DataRate::zero() && max_bitrate.is_finite() { + self.max_bitrate_configured = std::cmp::max(self.min_bitrate_configured, max_bitrate); + } else { + self.max_bitrate_configured = Self::DEFAULT_MAX_BITRATE; + } + self.loss_based_bandwidth_estimator_v2 + .set_min_max_bitrate(self.min_bitrate_configured, self.max_bitrate_configured); + } + + pub fn get_min_bitrate(&self) -> i64 { + self.min_bitrate_configured.bps() + } + + pub fn set_acknowledged_rate(&mut self, acknowledged_rate: Option) { + let Some(acknowledged_rate) = acknowledged_rate else { + return; + }; + if self.loss_based_bandwidth_estimator_v2.is_enabled() { + self.loss_based_bandwidth_estimator_v2 + .set_acknowledged_bitrate(acknowledged_rate); + } + } + pub fn update_loss_based_estimator( + &mut self, + report: &TransportPacketsFeedback, + _delay_detector_state: BandwidthUsage, + _probe_bitrate: Option, + in_alr: bool, + ) { + if self.loss_based_bandwidth_estimator_v2.is_enabled() { + self.loss_based_bandwidth_estimator_v2 + .update_bandwidth_estimate( + &report.packet_feedbacks, + self.delay_based_limit, + in_alr, + ); + self.update_estimate(report.feedback_time); + } + } + + // Gets the upper limit for the target bitrate. This is the minimum of the + // delay based limit and the configured max. + fn get_upper_limit(&self) -> DataRate { + std::cmp::min(self.delay_based_limit, self.max_bitrate_configured) + } + // Prints a warning if `bitrate` if sufficiently long time has past since last + // warning. + fn maybe_log_low_bitrate_warning(&mut self, bitrate: DataRate, at_time: Timestamp) { + if at_time - self.last_low_bitrate_log > Self::LOW_BITRATE_LOG_PERIOD { + tracing::debug!( + "Estimated available bandwidth {:?} is below configured min bitrate {:?}.", + bitrate, + self.min_bitrate_configured + ); + self.last_low_bitrate_log = at_time; + } + } + + // Cap `bitrate` to [self.min_bitrate_configured, max_bitrate_configured_] and + // set `current_bitrate_` to the capped value. + fn update_target_bitrate(&mut self, mut new_bitrate: DataRate, at_time: Timestamp) { + new_bitrate = std::cmp::min(new_bitrate, self.get_upper_limit()); + if new_bitrate < self.min_bitrate_configured { + self.maybe_log_low_bitrate_warning(new_bitrate, at_time); + new_bitrate = self.min_bitrate_configured; + } + self.current_target = new_bitrate; + } + // Applies lower and upper bounds to the current target rate. + // TODO(srte): This seems to be called even when limits haven't changed, that + // should be cleaned up. + fn apply_target_limits(&mut self, at_time: Timestamp) { + self.update_target_bitrate(self.current_target, at_time); + } + + fn loss_based_bandwidth_estimator_v2_ready_for_use(&self) -> bool { + self.loss_based_bandwidth_estimator_v2.is_ready() + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn initial_delay_based_bwe_with_probing() { + let mut bwe = SendSideBandwidthEstimation::default(); + let now = Timestamp::from_millis(0); + bwe.set_min_max_bitrate( + DataRate::from_bits_per_sec(100000), + DataRate::from_bits_per_sec(1500000), + ); + bwe.set_send_bitrate(DataRate::from_bits_per_sec(200000), now); + bwe.update_rtt(TimeDelta::from_millis(50)); + + // The delay-based estimate applies at once while the loss-based estimator has no + // observations. + bwe.update_delay_based_estimate(now, DataRate::from_bits_per_sec(1000000)); + bwe.update_estimate(now); + assert_eq!(1000000, bwe.target_rate().bps()); + } + + #[test] + fn setting_send_bitrate_overrides_delay_based_estimate() { + let mut bwe = SendSideBandwidthEstimation::default(); + const MIN_BITRATE_BPS: i64 = 10000; + const MAX_BITRATE_BPS: i64 = 10000000; + const INITIAL_BITRATE_BPS: i64 = 300000; + const DELAY_BASED_BITRATE_BPS: i64 = 350000; + const FORCED_HIGH_BITRATE: i64 = 2500000; + + let now_ms: i64 = 0; + + bwe.set_min_max_bitrate( + DataRate::from_bits_per_sec(MIN_BITRATE_BPS), + DataRate::from_bits_per_sec(MAX_BITRATE_BPS), + ); + bwe.set_send_bitrate( + DataRate::from_bits_per_sec(INITIAL_BITRATE_BPS), + Timestamp::from_millis(now_ms), + ); + + bwe.update_delay_based_estimate( + Timestamp::from_millis(now_ms), + DataRate::from_bits_per_sec(DELAY_BASED_BITRATE_BPS), + ); + bwe.update_estimate(Timestamp::from_millis(now_ms)); + assert!(bwe.target_rate().bps() >= INITIAL_BITRATE_BPS); + assert!(bwe.target_rate().bps() <= DELAY_BASED_BITRATE_BPS); + + bwe.set_send_bitrate( + DataRate::from_bits_per_sec(FORCED_HIGH_BITRATE), + Timestamp::from_millis(now_ms), + ); + assert_eq!(bwe.target_rate().bps(), FORCED_HIGH_BITRATE); + } + + #[test] + fn default_enabled() { + let rtt_backoff = RttBasedBackoff::new(&RttBasedBackoffConfig::default()); + assert!(rtt_backoff.rtt_limit.is_finite()); + } + + #[test] + fn can_be_disabled() { + let config = RttBasedBackoffConfig { + disabled: true, + ..Default::default() + }; + + let rtt_backoff = RttBasedBackoff::new(&config); + assert!(rtt_backoff.rtt_limit.is_plus_infinity()); + } + + #[test] + fn rtt_is_above_limit_if_rtt_greater_than_limit() { + let mut bwe = SendSideBandwidthEstimation::default(); + const MIN_BITRATE_BPS: i64 = 10000; + const MAX_BITRATE_BPS: i64 = 10000000; + const INITIAL_BITRATE_BPS: i64 = 300000; + let now_ms: i64 = 0; + bwe.set_min_max_bitrate( + DataRate::from_bits_per_sec(MIN_BITRATE_BPS), + DataRate::from_bits_per_sec(MAX_BITRATE_BPS), + ); + bwe.set_send_bitrate( + DataRate::from_bits_per_sec(INITIAL_BITRATE_BPS), + Timestamp::from_millis(now_ms), + ); + bwe.update_propagation_rtt( + /*at_time=*/ Timestamp::from_millis(now_ms), + /*propagation_rtt=*/ TimeDelta::from_millis(5000), + ); + assert!(bwe.is_rtt_above_limit()); + } + + #[test] + fn rtt_is_below_limit_if_rtt_less_than_limit() { + let mut bwe = SendSideBandwidthEstimation::default(); + const MIN_BITRATE_BPS: i64 = 10000; + const MAX_BITRATE_BPS: i64 = 10000000; + const INITIAL_BITRATE_BPS: i64 = 300000; + let now_ms: i64 = 0; + bwe.set_min_max_bitrate( + DataRate::from_bits_per_sec(MIN_BITRATE_BPS), + DataRate::from_bits_per_sec(MAX_BITRATE_BPS), + ); + bwe.set_send_bitrate( + DataRate::from_bits_per_sec(INITIAL_BITRATE_BPS), + Timestamp::from_millis(now_ms), + ); + bwe.update_propagation_rtt( + /*at_time=*/ Timestamp::from_millis(now_ms), + /*propagation_rtt=*/ TimeDelta::from_millis(1000), + ); + assert!(!bwe.is_rtt_above_limit()); + } +} diff --git a/rs/moq-congestion/src/gcc/tests.rs b/rs/moq-congestion/src/gcc/tests.rs new file mode 100644 index 00000000000..e3b87ff6e14 --- /dev/null +++ b/rs/moq-congestion/src/gcc/tests.rs @@ -0,0 +1,486 @@ +//! [`Gcc`] over a simulated uplink on mocked time: a FIFO bottleneck with a rate and a queue +//! bounded in time, a fixed propagation delay each way, and a receiver that acknowledges what +//! arrived each millisecond with its receive times. + +use std::collections::VecDeque; + +use super::*; +use crate::{Acked, Lost}; + +const MTU: u16 = 1200; +/// One-way propagation delay, each way +const DELAY: Duration = Duration::from_millis(20); +/// How long after sending a dropped packet the sender declares it lost without three later +/// packets acknowledged, standing in for QUIC's time threshold and probe timeout +const LOSS_TIMEOUT: Duration = Duration::from_secs(1); + +/// A packet between the sender and its ACK +struct Flight { + sent: Instant, + bytes: u16, + packet: Packet, + /// When it reaches the receiver, or `None` if the bottleneck dropped it + arrival: Option, +} + +/// What the sender has to send +#[derive(Clone, Copy)] +enum Source { + /// Always more than the controller allows + Bulk, + /// Media at this many bytes per second, in 30 frames each second + Media(u64), +} + +struct Sim { + gcc: Gcc, + epoch: Instant, + now: Instant, + /// The bottleneck's rate in bytes per second + rate: u64, + /// The longest a packet waits in the bottleneck before it is dropped + queue: Duration, + /// When the bottleneck finishes serving what it holds + busy_until: Instant, + flight: VecDeque, + in_flight: u64, + source: Source, + /// Media bytes written and not yet sent + backlog: u64, + next_frame: Instant, + /// Whether the sender fills the controller's probe with duplicates, as moq-quic's native + /// probing does + fill_probes: bool, + /// Whether the receiver reports receive times + timestamps: bool, + /// When each delivered packet reached the bottleneck, and its queueing delay there + queued: Vec<(Duration, Duration)>, +} + +impl Sim { + fn new(rate: u64, source: Source) -> Self { + let epoch = Instant::ZERO + Duration::from_secs(1000); + let gcc = Gcc::new(Init { + now: epoch, + mtu: MTU, + rtt: Duration::from_millis(333), + max_pacing_rate: None, + }); + Self { + gcc, + epoch, + now: epoch, + rate, + queue: Duration::from_millis(500), + busy_until: epoch, + flight: VecDeque::new(), + in_flight: 0, + source, + backlog: 0, + next_frame: epoch, + fill_probes: false, + timestamps: true, + queued: Vec::new(), + } + } + + fn elapsed(&self) -> Duration { + self.now - self.epoch + } + + /// Runs until `until` since the start, in steps of a millisecond + fn run(&mut self, until: Duration) { + while self.elapsed() < until { + self.ack(); + self.write(); + self.send(); + self.now += Duration::from_millis(1); + } + } + + fn write(&mut self) { + if let Source::Media(rate) = self.source { + while self.next_frame <= self.now { + self.backlog += rate / 30; + self.next_frame += Duration::from_secs(1) / 30; + } + } + } + + fn send(&mut self) { + loop { + let duplicate = match self.source { + Source::Bulk => false, + Source::Media(_) if self.backlog > 0 => false, + Source::Media(_) => { + let probe = self.gcc.metrics().probe_inflight; + if !self.fill_probes + || probe.is_none_or(|probe| self.in_flight + u64::from(MTU) > probe) + { + self.gcc.on_app_limited(self.in_flight); + return; + } + true + } + }; + if self.in_flight + u64::from(MTU) > self.gcc.window() { + return; + } + if self.gcc.pace(self.now, MTU.into()).is_some() { + return; + } + if duplicate { + self.gcc.on_probe_filled(self.in_flight); + } else { + self.backlog = self.backlog.saturating_sub(MTU.into()); + } + self.in_flight += u64::from(MTU); + let packet = self.gcc.on_sent(Sent { + now: self.now, + bytes: MTU, + in_flight: self.in_flight, + }); + let start = self.busy_until.max(self.now); + let wait = start - self.now; + let arrival = (wait <= self.queue).then(|| { + self.busy_until = + start + Duration::from_secs_f64(f64::from(MTU) / self.rate as f64); + self.queued.push((self.elapsed(), wait)); + self.busy_until + DELAY + }); + self.flight.push_back(Flight { + sent: self.now, + bytes: MTU, + packet, + arrival, + }); + } + } + + /// Acknowledges what arrived a propagation delay ago, and declares lost a packet the + /// bottleneck dropped once three later packets were acknowledged or [`LOSS_TIMEOUT`] passed + fn ack(&mut self) { + let acked_by = |arrival: Instant| arrival + DELAY; + let mut acked = Vec::new(); + let mut lost = Vec::new(); + while let Some(front) = self.flight.front() { + match front.arrival { + Some(arrival) if acked_by(arrival) <= self.now => {} + Some(_) => break, + None => { + let acked_later = self + .flight + .iter() + .skip(1) + .take(3) + .filter(|f| f.arrival.is_some_and(|a| acked_by(a) <= self.now)) + .count(); + if acked_later < 3 && self.now < front.sent + LOSS_TIMEOUT { + break; + } + } + } + let flight = self.flight.pop_front().unwrap(); + self.in_flight -= u64::from(flight.bytes); + match flight.arrival { + Some(arrival) => acked.push(Acked { + sent: flight.sent, + bytes: flight.bytes, + packet: flight.packet, + received: self.timestamps.then(|| arrival - self.epoch), + }), + None => lost.push(Lost { + sent: flight.sent, + bytes: flight.bytes, + packet: flight.packet, + new_burst: true, + }), + } + } + if !lost.is_empty() { + self.gcc.on_loss(Loss { + now: self.now, + packets: &lost, + in_flight: self.in_flight, + persistent: false, + }); + } + if !acked.is_empty() { + self.gcc.on_ack(Ack { + now: self.now, + packets: &acked, + in_flight: self.in_flight, + app_limited: false, + rtt: 2 * DELAY, + }); + } + } + + /// The bandwidth estimate, in bytes per second + fn estimate(&self) -> u64 { + self.gcc.metrics().bandwidth_estimate.unwrap() + } + + /// The `p` quantile of the queueing delay of packets reaching the bottleneck since `from` + fn queue(&self, from: Duration, p: f64) -> Duration { + let mut delays: Vec = self + .queued + .iter() + .filter(|(at, _)| *at >= from) + .map(|&(_, delay)| delay) + .collect(); + delays.sort_unstable(); + delays[((delays.len() - 1) as f64 * p) as usize] + } +} + +/// 2 Mbit/s in bytes per second +const LINK: u64 = 250_000; + +/// A bulk upload's initial probes find the link within a few seconds, and the delay-based +/// estimate then holds the queue near empty, below the link's rate +#[test] +fn finds_the_link_and_keeps_the_queue_short() { + let mut sim = Sim::new(LINK, Source::Bulk); + sim.run(Duration::from_secs(10)); + let estimate = sim.estimate(); + assert!( + (LINK / 2..=LINK * 11 / 10).contains(&estimate), + "estimate {estimate} B/s on a {LINK} B/s link" + ); + let queue = sim.queue(Duration::from_secs(5), 0.95); + assert!(queue < Duration::from_millis(50), "p95 queue {queue:?}"); +} + +/// When the link drops to a quarter, the queue's growth cuts the estimate to the new rate +/// within a couple of seconds, and the queue drains +#[test] +fn backs_off_a_capacity_drop() { + let mut sim = Sim::new(LINK, Source::Bulk); + sim.run(Duration::from_secs(10)); + sim.rate = LINK / 4; + sim.run(Duration::from_secs(12)); + let estimate = sim.estimate(); + assert!( + estimate <= LINK / 4 * 11 / 10, + "estimate {estimate} B/s 2s after the link fell to {} B/s", + LINK / 4 + ); + sim.run(Duration::from_secs(20)); + let queue = sim.queue(Duration::from_secs(17), 0.95); + assert!(queue < Duration::from_millis(50), "p95 queue {queue:?}"); +} + +/// Without receive timestamps the ACK's arrival stands in for each packet's, so GoogCC still +/// finds the link on the round-trip delay +#[test] +fn falls_back_to_ack_arrival() { + let mut sim = Sim::new(LINK, Source::Bulk); + sim.timestamps = false; + sim.run(Duration::from_secs(10)); + let estimate = sim.estimate(); + assert!( + (LINK / 2..=LINK * 11 / 10).contains(&estimate), + "estimate {estimate} B/s on a {LINK} B/s link" + ); +} + +/// A packet acknowledged without a receive time, once the peer reports some, reaches the +/// feedback as received, for the loss-based estimate's loss rate, but not among the packets whose +/// times the delay-based estimate and the RTT read +#[test] +fn counts_packets_acknowledged_without_times() { + let mut sim = Sim::new(LINK, Source::Bulk); + let untimed = |gcc: &mut Gcc, now| { + let packet = gcc.on_sent(Sent { + now, + bytes: MTU, + in_flight: MTU.into(), + }); + let acked = Acked { + sent: now, + bytes: MTU, + packet, + received: None, + }; + TransportPacketsFeedback { + packet_feedbacks: vec![gcc.acked(&acked, now)], + ..Default::default() + } + }; + + // Before any time is reported, the ACK's arrival is the packet's + let feedback = untimed(&mut sim.gcc, sim.now); + assert_eq!(feedback.received_with_send_info().count(), 1); + + sim.run(Duration::from_millis(500)); + let feedback = untimed(&mut sim.gcc, sim.now); + assert!(feedback.packet_feedbacks[0].is_received()); + assert_eq!(feedback.received_with_send_info().count(), 0); + assert!(feedback.sorted_by_receive_time().is_empty()); +} + +/// An application-limited sender gets probe clusters to fill: duplicates filling them carry the +/// estimate far past what the media alone reads from the same clusters +#[test] +fn filled_probes_raise_the_estimate() { + const MEDIA: u64 = 125_000; + let mut alone = Sim::new(10 * LINK, Source::Media(MEDIA)); + alone.run(Duration::from_secs(10)); + let mut filled = Sim::new(10 * LINK, Source::Media(MEDIA)); + filled.fill_probes = true; + filled.run(Duration::from_secs(10)); + assert!( + filled.estimate() >= 4 * alone.estimate(), + "estimate {} B/s filling the probes, {} B/s without", + filled.estimate(), + alone.estimate() + ); +} + +/// A probe target is the allocation GoogCC aims its probes at: no more than twice it, so filled +/// clusters raise the estimate to the target rather than to the link's rate, and the duplicates +/// cost no more than the target needs +#[test] +fn probes_aim_at_the_target() { + const MEDIA: u64 = 125_000; + /// 4 Mbit/s, a fifth of the link + const TARGET: u64 = 500_000; + let run = |target: Option| { + let mut sim = Sim::new(10 * LINK, Source::Media(MEDIA)); + sim.fill_probes = true; + if let Some(target) = target { + sim.gcc.on_probe_target(target); + } + sim.run(Duration::from_secs(10)); + sim.estimate() + }; + let aimed = run(Some(TARGET)); + let unaimed = run(None); + assert!( + (TARGET..=2 * TARGET).contains(&aimed), + "estimate {aimed} B/s probing toward {TARGET} B/s" + ); + assert!( + unaimed > 2 * TARGET, + "estimate {unaimed} B/s without a target" + ); +} + +/// A cluster short of a packet's bytes leaves room for a whole packet, which ends it, rather than +/// holding the sender to the bytes and leaving it unfinished +#[test] +fn probe_inflight_rounds_up_to_a_packet() { + let now = Instant::ZERO + Duration::from_secs(1000); + let mut gcc = Gcc::new(Init { + now, + mtu: MTU, + rtt: Duration::from_millis(100), + max_pacing_rate: None, + }); + // 3000 bytes, two and a half packets + gcc.prober + .create_probe_cluster(&api::transport::ProbeClusterConfig { + at_time: timestamp(now), + target_data_rate: DataRate::from_bytes_per_sec(200_000), + target_duration: TimeDelta::from_millis(15), + min_probe_delta: TimeDelta::from_millis(2), + target_probe_count: 1, + id: 1, + }); + let mut in_flight = 0; + for _ in 0..2 { + in_flight += u64::from(MTU); + gcc.on_sent(Sent { + now, + bytes: MTU, + in_flight, + }); + } + assert_eq!( + gcc.metrics().probe_inflight, + Some(in_flight + u64::from(MTU)) + ); + + in_flight += u64::from(MTU); + gcc.on_sent(Sent { + now, + bytes: MTU, + in_flight, + }); + assert_eq!(gcc.metrics().probe_inflight, None, "the cluster went on"); +} + +/// A restarted cluster's short id never matches one of GoogCC's, whatever either counter reached, +/// so a packet resolves to the cluster it was sent in +#[test] +fn restart_ids_never_alias_googcc_ids() { + let base = BitrateProber::RESTART_ID_BASE; + for googcc in [ + 1, + 2, + 16_384, + 16_385, + 32_767, + 32_768, + 65_535, + 65_536, + 1 << 20, + ] { + let short = short_id(googcc).unwrap().get(); + assert!(short < 1 << 15, "GoogCC id {googcc} took short id {short}"); + } + for restart in [0, 1, 16_383, 16_384, 32_767, 32_768, 65_535, (1 << 30) - 1] { + let short = short_id(base + restart).unwrap().get(); + assert!(short >= 1 << 15, "restart {restart} took short id {short}"); + } + assert_eq!(short_id(0), None); + assert_eq!(short_id(-1), None); +} + +/// Receive times a peer controls, however far off, regressing, or unreported, never panic or +/// leave the estimate outside GoogCC's bounds +#[test] +fn survives_hostile_receive_times() { + let mut now = Instant::ZERO + Duration::from_secs(1000); + let mut gcc = Gcc::new(Init { + now, + mtu: MTU, + rtt: Duration::from_millis(100), + max_pacing_rate: None, + }); + let times = [ + Some(Duration::ZERO), + Some(Duration::MAX), + Some(Duration::from_secs(u64::MAX / 2)), + None, + Some(Duration::from_secs(1)), + Some(Duration::ZERO), + Some(Duration::from_nanos(1)), + ]; + for round in 0..2000 { + now += Duration::from_millis(1); + let packet = gcc.on_sent(Sent { + now, + bytes: MTU, + in_flight: MTU.into(), + }); + let acked = [Acked { + sent: now, + bytes: MTU, + packet, + received: times[round % times.len()], + }]; + gcc.on_ack(Ack { + now, + packets: &acked, + in_flight: 0, + app_limited: false, + rtt: Duration::from_millis(100), + }); + let _ = gcc.pace(now, MTU.into()); + let metrics = gcc.metrics(); + assert!(metrics.bandwidth_estimate.unwrap() >= 5_000 / 8); + assert!(metrics.congestion_window >= MIN_WINDOW); + } +} diff --git a/rs/moq-congestion/src/gcc/trendline_estimator.rs b/rs/moq-congestion/src/gcc/trendline_estimator.rs new file mode 100644 index 00000000000..06de9322db1 --- /dev/null +++ b/rs/moq-congestion/src/gcc/trendline_estimator.rs @@ -0,0 +1,531 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +use std::collections::VecDeque; + +use crate::gcc::{DelayIncreaseDetectorInterface, api::transport::BandwidthUsage}; + +// WebRTC-Bwe-TrendlineEstimatorSettings +#[derive(Debug, Clone)] +pub struct TrendlineEstimatorSettings { + // Sort the packets in the window. Should be redundant, + // but then almost no cost. + pub enable_sort: bool, + + // Cap the trendline slope based on the minimum delay seen + // in the beginning_packets and end_packets respectively. + pub enable_cap: bool, + pub beginning_packets: usize, + pub end_packets: usize, + pub cap_uncertainty: f64, + + // Size (in packets) of the window. + pub window_size: usize, +} + +impl TrendlineEstimatorSettings { + const DEFAULT_TRENDLINE_WINDOW_SIZE: usize = 20; + + pub fn validate(&mut self) { + if self.window_size < 10 || 200 < self.window_size { + tracing::warn!("Window size must be between 10 and 200 packets"); + self.window_size = Self::DEFAULT_TRENDLINE_WINDOW_SIZE; + } + if self.enable_cap { + if self.beginning_packets < 1 + || self.end_packets < 1 + || self.beginning_packets > self.window_size + || self.end_packets > self.window_size + { + tracing::warn!( + "Size of beginning and end must be between 1 and {}", + self.window_size + ); + self.enable_cap = false; + self.beginning_packets = 0; + self.end_packets = 0; + self.cap_uncertainty = 0.0; + } + if self.beginning_packets + self.end_packets > self.window_size { + tracing::warn!("Size of beginning plus end can't exceed the window size"); + self.enable_cap = false; + self.beginning_packets = 0; + self.end_packets = 0; + self.cap_uncertainty = 0.0; + } + if self.cap_uncertainty < 0.0 || 0.025 < self.cap_uncertainty { + tracing::warn!("Cap uncertainty must be between 0 and 0.025"); + self.cap_uncertainty = 0.0; + } + } + } +} + +impl Default for TrendlineEstimatorSettings { + fn default() -> Self { + Self { + enable_sort: false, + enable_cap: false, + beginning_packets: 7, + end_packets: 7, + cap_uncertainty: 0.0, + window_size: 20, + } + } +} + +#[derive(Clone)] +pub struct TrendlineEstimator { + // Parameters. + settings: TrendlineEstimatorSettings, + smoothing_coef: f64, + threshold_gain: f64, + // Used by the existing threshold. + num_of_deltas: i64, + // Keep the arrival times small by using the change from the first packet. + first_arrival_time_ms: i64, + // Exponential backoff filtering. + accumulated_delay: f64, + smoothed_delay: f64, + // Linear least squares regression. + delay_hist: VecDeque, + + k_up: f64, + k_down: f64, + overusing_time_threshold: f64, + threshold: f64, + prev_modified_trend: f64, + last_update_ms: i64, + prev_trend: f64, + time_over_using: f64, + overuse_counter: i64, + hypothesis: BandwidthUsage, +} + +#[derive(Clone)] +struct PacketTiming { + pub arrival_time_ms: f64, + pub smoothed_delay_ms: f64, + pub raw_delay_ms: f64, +} + +impl Default for TrendlineEstimator { + fn default() -> Self { + Self::new(TrendlineEstimatorSettings::default()) + } +} + +impl DelayIncreaseDetectorInterface for TrendlineEstimator { + fn update( + &mut self, + recv_delta_ms: f64, + send_delta_ms: f64, + send_time_ms: i64, + arrival_time_ms: i64, + packet_size: usize, + calculated_deltas: bool, + ) { + if calculated_deltas { + self.update_trendline( + recv_delta_ms, + send_delta_ms, + send_time_ms, + arrival_time_ms, + packet_size, + ); + } + } + fn state(&self) -> BandwidthUsage { + self.hypothesis + } +} + +fn linear_fit_slope(packets: &VecDeque) -> Option { + assert!(packets.len() >= 2); + // Compute the "center of mass". + let mut sum_x: f64 = 0.0; + let mut sum_y: f64 = 0.0; + for packet in packets { + sum_x += packet.arrival_time_ms; + sum_y += packet.smoothed_delay_ms; + } + let x_avg: f64 = sum_x / packets.len() as f64; + let y_avg: f64 = sum_y / packets.len() as f64; + // Compute the slope k = \sum (x_i-x_avg)(y_i-y_avg) / \sum (x_i-x_avg)^2 + let mut numerator: f64 = 0.0; + let mut denominator: f64 = 0.0; + for packet in packets.iter() { + let x: f64 = packet.arrival_time_ms; + let y: f64 = packet.smoothed_delay_ms; + numerator += (x - x_avg) * (y - y_avg); + denominator += (x - x_avg) * (x - x_avg); + } + if denominator == 0.0 { + return None; + } + Some(numerator / denominator) +} + +fn compute_slope_cap( + packets: &VecDeque, + settings: &TrendlineEstimatorSettings, +) -> Option { + assert!(1 <= settings.beginning_packets && settings.beginning_packets < packets.len()); + assert!(1 <= settings.end_packets && settings.end_packets < packets.len()); + assert!(settings.beginning_packets + settings.end_packets <= packets.len()); + let mut early = &packets[0]; + for packet in packets.iter().take(settings.beginning_packets).skip(1) { + if packet.raw_delay_ms < early.raw_delay_ms { + early = packet; + } + } + let late_start: usize = packets.len() - settings.end_packets; + let mut late = &packets[late_start]; + for packet in packets.iter().skip(late_start + 1) { + if packet.raw_delay_ms < late.raw_delay_ms { + late = packet; + } + } + if late.arrival_time_ms - early.arrival_time_ms < 1.0 { + return None; + } + Some( + (late.raw_delay_ms - early.raw_delay_ms) / (late.arrival_time_ms - early.arrival_time_ms) + + settings.cap_uncertainty, + ) +} + +impl TrendlineEstimator { + const DEFAULT_TRENDLINE_SMOOTHING_COEFF: f64 = 0.9; + const DEFAULT_TRENDLINE_THRESHOLD_GAIN: f64 = 4.0; + const MAX_ADAPT_OFFSET_MS: f64 = 15.0; + const OVER_USING_TIME_THRESHOLD: f64 = 10.0; + const MIN_NUM_DELTAS: i64 = 60; + const DELTA_COUNTER_MAX: i64 = 1000; + + pub fn new(mut settings: TrendlineEstimatorSettings) -> Self { + settings.validate(); + + tracing::debug!( + "Using Trendline filter for delay change estimation with settings {:?} and no network state predictor", + settings + ); + Self { + settings, + smoothing_coef: Self::DEFAULT_TRENDLINE_SMOOTHING_COEFF, + threshold_gain: Self::DEFAULT_TRENDLINE_THRESHOLD_GAIN, + num_of_deltas: 0, + first_arrival_time_ms: -1, + accumulated_delay: 0.0, + smoothed_delay: 0.0, + delay_hist: VecDeque::new(), + k_up: 0.0087, + k_down: 0.039, + overusing_time_threshold: Self::OVER_USING_TIME_THRESHOLD, + threshold: 12.5, + prev_modified_trend: f64::NAN, + last_update_ms: -1, + prev_trend: 0.0, + time_over_using: -1.0, + overuse_counter: 0, + hypothesis: BandwidthUsage::Normal, + } + } + + fn update_trendline( + &mut self, + recv_delta_ms: f64, + send_delta_ms: f64, + _send_time_ms: i64, + arrival_time_ms: i64, + _packet_size: usize, + ) { + let delta_ms: f64 = recv_delta_ms - send_delta_ms; + self.num_of_deltas += 1; + self.num_of_deltas = self.num_of_deltas.min(Self::DELTA_COUNTER_MAX); + if self.first_arrival_time_ms == -1 { + self.first_arrival_time_ms = arrival_time_ms; + } + + // Exponential backoff filter. + self.accumulated_delay += delta_ms; + self.smoothed_delay = self.smoothing_coef * self.smoothed_delay + + (1.0 - self.smoothing_coef) * self.accumulated_delay; + + // Maintain packet window + self.delay_hist.push_back(PacketTiming { + arrival_time_ms: (arrival_time_ms - self.first_arrival_time_ms) as f64, + smoothed_delay_ms: self.smoothed_delay, + raw_delay_ms: self.accumulated_delay, + }); + if self.settings.enable_sort { + let mut i = self.delay_hist.len() - 1; + while i > 0 + && self.delay_hist[i].arrival_time_ms < self.delay_hist[i - 1].arrival_time_ms + { + self.delay_hist.swap(i, i - 1); + i -= 1; + } + } + if self.delay_hist.len() > self.settings.window_size { + self.delay_hist.pop_front(); + } + + // Simple linear regression. + let mut trend: f64 = self.prev_trend; + if self.delay_hist.len() == self.settings.window_size { + // Update self.trend if it is possible to fit a line to the data. The delay + // trend can be seen as an estimate of (send_rate - capacity)/capacity. + // 0 < trend < 1 . the delay increases, queues are filling up + // trend == 0 . the delay does not change + // trend < 0 . the delay decreases, queues are being emptied + trend = linear_fit_slope(&self.delay_hist).unwrap_or(trend); + if self.settings.enable_cap { + let cap = compute_slope_cap(&self.delay_hist, &self.settings); + // We only use the cap to filter out overuse detections, not + // to detect additional underuses. + if let Some(cap) = cap + && trend >= 0.0 + && trend > cap + { + trend = cap; + } + } + } + self.detect(trend, send_delta_ms, arrival_time_ms); + } + + fn detect(&mut self /* TrendlineEstimator */, trend: f64, ts_delta: f64, now_ms: i64) { + if self.num_of_deltas < 2 { + self.hypothesis = BandwidthUsage::Normal; + return; + } + let modified_trend: f64 = + self.num_of_deltas.min(Self::MIN_NUM_DELTAS) as f64 * trend * self.threshold_gain; + self.prev_modified_trend = modified_trend; + if modified_trend > self.threshold { + if self.time_over_using == -1.0 { + // Initialize the timer. Assume that we've been + // over-using half of the time since the previous + // sample. + self.time_over_using = ts_delta / 2.0; + } else { + // Increment timer + self.time_over_using += ts_delta; + } + self.overuse_counter += 1; + if self.time_over_using > self.overusing_time_threshold + && self.overuse_counter > 1 + && trend >= self.prev_trend + { + self.time_over_using = 0.0; + self.overuse_counter = 0; + self.hypothesis = BandwidthUsage::Overusing; + } + } else if modified_trend < -self.threshold { + self.time_over_using = -1.0; + self.overuse_counter = 0; + self.hypothesis = BandwidthUsage::Underusing; + } else { + self.time_over_using = -1.0; + self.overuse_counter = 0; + self.hypothesis = BandwidthUsage::Normal; + } + self.prev_trend = trend; + self.update_threshold(modified_trend, now_ms); + } + + fn update_threshold(&mut self /* TrendlineEstimator */, modified_trend: f64, now_ms: i64) { + if self.last_update_ms == -1 { + self.last_update_ms = now_ms; + } + + if modified_trend.abs() > self.threshold + Self::MAX_ADAPT_OFFSET_MS { + // Avoid adapting the threshold to big latency spikes, caused e.g., + // by a sudden capacity drop. + self.last_update_ms = now_ms; + return; + } + + let k: f64 = match modified_trend.abs() < self.threshold { + true => self.k_down, + false => self.k_up, + }; + const MAX_TIME_DELTA_MS: i64 = 100; + let time_delta_ms: i64 = std::cmp::min(now_ms - self.last_update_ms, MAX_TIME_DELTA_MS); + self.threshold += k * (modified_trend.abs() - self.threshold) * time_delta_ms as f64; + self.threshold = self.threshold.clamp(6.0, 600.0); + self.last_update_ms = now_ms; + } +} + +#[cfg(test)] +mod test { + use super::*; + + struct PacketTimeGenerator { + initial_clock: i64, + time_between_packets: f64, + packets: usize, + } + + impl PacketTimeGenerator { + pub fn new(initial_clock: i64, time_between_packets: f64) -> Self { + Self { + initial_clock, + time_between_packets, + packets: 0, + } + } + } + + impl Iterator for PacketTimeGenerator { + type Item = i64; + + fn next(&mut self) -> Option { + let value = + self.initial_clock + (self.time_between_packets * self.packets as f64) as i64; + self.packets += 1; + Some(value) + } + } + + struct TrendlineEstimatorTest { + send_times: Vec, + recv_times: Vec, + packet_sizes: Vec, + estimator: TrendlineEstimator, + count: usize, + } + + impl TrendlineEstimatorTest { + const PACKET_COUNT: usize = 25; + const PACKET_SIZE_BYTES: usize = 1200; + + pub fn new(send_times: PacketTimeGenerator, recv_times: PacketTimeGenerator) -> Self { + Self { + send_times: send_times.take(Self::PACKET_COUNT).collect(), + recv_times: recv_times.take(Self::PACKET_COUNT).collect(), + packet_sizes: vec![Self::PACKET_SIZE_BYTES; Self::PACKET_COUNT], + estimator: TrendlineEstimator::default(), + count: 1, + } + } + + fn run_test_until_state_change(&mut self) { + assert_eq!(self.send_times.len(), Self::PACKET_COUNT); + assert_eq!(self.recv_times.len(), Self::PACKET_COUNT); + assert_eq!(self.packet_sizes.len(), Self::PACKET_COUNT); + assert!(self.count >= 1); + assert!(self.count < Self::PACKET_COUNT); + + let initial_state = self.estimator.state(); + while self.count < Self::PACKET_COUNT { + let recv_delta: f64 = + (self.recv_times[self.count] - self.recv_times[self.count - 1]) as f64; + let send_delta: f64 = + (self.send_times[self.count] - self.send_times[self.count - 1]) as f64; + self.estimator.update( + recv_delta, + send_delta, + self.send_times[self.count], + self.recv_times[self.count], + self.packet_sizes[self.count], + true, + ); + if self.estimator.state() != initial_state { + return; + } + + self.count += 1; + } + } + } + + #[test] + fn normal() { + let send_time_generator = PacketTimeGenerator::new( + 123456789, /*initial clock*/ + 20.0, /*20 ms between sent packets*/ + ); + let recv_time_generator = PacketTimeGenerator::new( + 987654321, /*initial clock*/ + 20.0, /*delivered at the same pace*/ + ); + + let mut test = TrendlineEstimatorTest::new(send_time_generator, recv_time_generator); + + assert_eq!(test.estimator.state(), BandwidthUsage::Normal); + test.run_test_until_state_change(); + assert_eq!(test.estimator.state(), BandwidthUsage::Normal); + assert_eq!(test.count, TrendlineEstimatorTest::PACKET_COUNT); // All packets processed + } + + #[test] + fn overusing() { + let send_time_generator = PacketTimeGenerator::new( + 123456789, /*initial clock*/ + 20.0, /*20 ms between sent packets*/ + ); + let recv_time_generator = PacketTimeGenerator::new( + 987654321, /*initial clock*/ + 1.1 * 20.0, /*10% slower delivery*/ + ); + let mut test = TrendlineEstimatorTest::new(send_time_generator, recv_time_generator); + + assert_eq!(test.estimator.state(), BandwidthUsage::Normal); + test.run_test_until_state_change(); + assert_eq!(test.estimator.state(), BandwidthUsage::Overusing); + test.run_test_until_state_change(); + assert_eq!(test.estimator.state(), BandwidthUsage::Overusing); + assert_eq!(test.count, TrendlineEstimatorTest::PACKET_COUNT); // All packets processed + } + + #[test] + fn underusing() { + let send_time_generator = PacketTimeGenerator::new( + 123456789, /*initial clock*/ + 20.0, /*20 ms between sent packets*/ + ); + let recv_time_generator = PacketTimeGenerator::new( + 987654321, /*initial clock*/ + 0.85 * 20.0, /*15% faster delivery*/ + ); + let mut test = TrendlineEstimatorTest::new(send_time_generator, recv_time_generator); + + assert_eq!(test.estimator.state(), BandwidthUsage::Normal); + test.run_test_until_state_change(); + assert_eq!(test.estimator.state(), BandwidthUsage::Underusing); + test.run_test_until_state_change(); + assert_eq!(test.estimator.state(), BandwidthUsage::Underusing); + assert_eq!(test.count, TrendlineEstimatorTest::PACKET_COUNT); // All packets processed + } + + #[test] + fn includes_small_packets_by_default() { + let send_time_generator = PacketTimeGenerator::new( + 123456789, /*initial clock*/ + 20.0, /*20 ms between sent packets*/ + ); + let recv_time_generator = PacketTimeGenerator::new( + 987654321, /*initial clock*/ + 1.1 * 20.0, /*10% slower delivery*/ + ); + let mut test = TrendlineEstimatorTest::new(send_time_generator, recv_time_generator); + test.packet_sizes = vec![100; TrendlineEstimatorTest::PACKET_COUNT]; + + assert_eq!(test.estimator.state(), BandwidthUsage::Normal); + test.run_test_until_state_change(); + assert_eq!(test.estimator.state(), BandwidthUsage::Overusing); + test.run_test_until_state_change(); + assert_eq!(test.estimator.state(), BandwidthUsage::Overusing); + assert_eq!(test.count, TrendlineEstimatorTest::PACKET_COUNT); // All packets processed + } +} // namespace webrtc diff --git a/rs/moq-congestion/src/lib.rs b/rs/moq-congestion/src/lib.rs index a5dbc9422cd..381cbcffc4e 100644 --- a/rs/moq-congestion/src/lib.rs +++ b/rs/moq-congestion/src/lib.rs @@ -14,12 +14,14 @@ use std::sync::Arc; pub mod bbr3; /// CUBIC congestion control. pub mod cubic; +pub mod gcc; mod pacer; mod rate; mod unlimited; pub use bbr3::Bbr3; pub use cubic::Cubic; +pub use gcc::Gcc; pub use rate::Packet; pub use unlimited::Unlimited; @@ -128,6 +130,29 @@ pub trait Controller: Send + Sync { false } + /// Whether each probe is a cluster the controller sizes and paces itself, so a transport + /// filling [`Metrics::probe_inflight`] sends at the controller's [`Controller::pace`] + /// rather than holding its duplicates to the session's probe target + /// + /// A controller that probes by a gain on its estimate for as long as its cycle allows, as + /// BBR3 does, leaves the target to bound what its probes cost. One that reads a probe at the + /// rate it was sent, as GoogCC does, learns only that rate from a probe held below it, and + /// bounds the cost by the cluster's size. + fn paces_probe(&self) -> bool { + false + } + + /// The session's probe target, the rate in bytes per second the application wants to send, + /// or zero for none + /// + /// The transport reports it to each controller it builds and whenever it changes. A + /// controller that sizes its own probes, as GoogCC does, may aim them at it. + #[allow( + unused_variables, + reason = "a no-op default that keeps the parameter names for implementors" + )] + fn on_probe_target(&mut self, bytes_per_second: u64) {} + /// Duplicate the controller's state fn clone_box(&self) -> Box; } diff --git a/rs/moq-congestion/src/pacer.rs b/rs/moq-congestion/src/pacer.rs index b295b739b01..42675659f53 100644 --- a/rs/moq-congestion/src/pacer.rs +++ b/rs/moq-congestion/src/pacer.rs @@ -212,7 +212,7 @@ pub(crate) struct Rate { /// Burst intervals trade distributing datagrams over time against waking the connection up more /// often than user-space timer accuracy can service; overshooting one by more than 25% loses the /// tokens for the extra elapsed time. -fn rate_capacity(pacing_rate: u64, mtu: u16) -> u64 { +pub(crate) fn rate_capacity(pacing_rate: u64, mtu: u16) -> u64 { let mtu = u64::from(mtu); let bytes_in = |interval: Duration| ((pacing_rate as u128 * interval.as_nanos()) / 1_000_000_000) as u64; diff --git a/rs/moq-congestion/src/rate.rs b/rs/moq-congestion/src/rate.rs index abb57f61bba..8dd15e86773 100644 --- a/rs/moq-congestion/src/rate.rs +++ b/rs/moq-congestion/src/rate.rs @@ -6,6 +6,7 @@ //! use kio::clock::Instant; +use std::num::NonZeroU16; use std::time::Duration; use crate::{Ack, Sent}; @@ -30,6 +31,10 @@ pub struct Packet { pub(crate) tx_in_flight: u32, /// equivalent to P.is_app_limited: whether C.app_limited was set when the packet was sent. pub(crate) is_app_limited: bool, + /// The GCC probe cluster it was sent in, if any. Sixteen bits fit the padding after the + /// fields above. GoogCC's clusters and the prober's restarts each wrap within half of them, + /// far more than either starts in the two seconds a cluster is kept. + pub(crate) probe_cluster: Option, } /// An empty state, for a controller that keeps none. @@ -42,6 +47,7 @@ impl Default for Packet { lost: 0, tx_in_flight: 0, is_app_limited: false, + probe_cluster: None, } } } @@ -115,6 +121,7 @@ impl Sampler { lost: self.lost, tx_in_flight: sent.in_flight.try_into().unwrap_or(u32::MAX), is_app_limited: self.app_limited != 0, + probe_cluster: None, } } diff --git a/rs/moq-quic/README.md b/rs/moq-quic/README.md index 1f2df38cfda..c66f5453c90 100644 --- a/rs/moq-quic/README.md +++ b/rs/moq-quic/README.md @@ -2,7 +2,7 @@ MoQ's sans-IO QUIC state machine, a hard fork of [quinn-proto](https://github.com/quinn-rs/quinn). -BBR3, Cubic, and Unlimited live in [moq-congestion](../moq-congestion); this crate stores each packet's controller state and hands it back on acknowledgement or loss, and the controller paces the sends. +BBR3, Cubic, GCC, and Unlimited live in [moq-congestion](../moq-congestion); this crate stores each packet's controller state and hands it back on acknowledgement or loss, and the controller paces the sends. All credit for the code goes to the quinn developers; it stays under quinn's MIT or Apache-2.0 license. diff --git a/rs/moq-quic/src/connection/bandwidth_probe.rs b/rs/moq-quic/src/connection/bandwidth_probe.rs index 7e5fe79dda7..094de2bc47c 100644 --- a/rs/moq-quic/src/connection/bandwidth_probe.rs +++ b/rs/moq-quic/src/connection/bandwidth_probe.rs @@ -5,9 +5,10 @@ //! session that wants more than it sends, to resume a stopped rendition, would never find out //! the path has room. While the controller probes, this fills the pacing headroom it opens with //! duplicates of STREAM data already in flight, up to the session's target, so the probe's -//! samples measure the path. A duplicate needs no wire change, and repairs a loss of its -//! original. It is never retransmitted, and a reset stream's data is never sent again, so a -//! probe never sends past a group's expiry. +//! samples measure the path. A controller that paces its own probe cluster, as GoogCC does, gets +//! it filled at its pace instead, since it reads the cluster at the rate it was sent. A duplicate +//! needs no wire change, and repairs a loss of its original. It is never retransmitted, and a +//! reset stream's data is never sent again, so a probe never sends past a group's expiry. //! //! An attempt that ends short of the target found the path's ceiling, and another would only //! spend the same duplicates finding it again. So each miss in a row doubles how long the probe @@ -45,7 +46,8 @@ pub(super) struct BandwidthProbe { /// Until when the probe sits out after a miss resume: Option, /// Holds everything sent while probing to `target`, which the controller's probing gain - /// alone may reach past + /// alone may reach past, unless the controller paces its own probe + /// ([`Controller::paces_probe`](crate::congestion::Controller::paces_probe)) pacing: Bucket, /// The packet number to resume taking in-flight data from, so the duplicates spread over the /// whole flight, oldest first @@ -97,6 +99,11 @@ impl BandwidthProbe { } } + /// The rate the caller asked to probe up to, in bytes per second; zero when probing is off + pub(super) fn target(&self) -> u64 { + self.target + } + /// Probe up to `bytes_per_second`, retrying a changed target at once pub(super) fn set_target(&mut self, bytes_per_second: u64) { if bytes_per_second != self.target { @@ -194,8 +201,12 @@ impl Connection { } return Ready::Blocked(None); } + // A controller that paces its own probe cluster reads it at the rate it was sent, so the + // target would cap the reading; the cluster's size bounds the duplicates instead let mtu = self.path.current_mtu(); - if let Some(at) = self.bandwidth_probe.pacing.delay(target, bytes, mtu, now) { + if !self.path.congestion.paces_probe() + && let Some(at) = self.bandwidth_probe.pacing.delay(target, bytes, mtu, now) + { return Ready::Blocked(Some(at)); } let estimate = metrics.bandwidth_estimate.unwrap_or(0); diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index c7ce9b5a447..8327cb7028a 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1597,13 +1597,22 @@ impl Connection { /// fills the inflight it allows /// ([`probe_inflight`](crate::congestion::Metrics::probe_inflight)) with duplicates /// of STREAM data still in flight, sending no more than the target in all, so the probe - /// measures the path. Duplicates count against congestion control, as - /// [`PathStats::sent_bandwidth_probe_bytes`]. A reset stream's data is never sent again. An + /// measures the path. A controller that paces its own probe cluster + /// ([`Controller::paces_probe`]) gets it filled at its pace instead, and may aim its probes at + /// the target ([`Controller::on_probe_target`]). Duplicates count against congestion control, + /// as [`PathStats::sent_bandwidth_probe_bytes`]. A reset stream's data is never sent again. An /// attempt that misses the target sits out a few seconds before retrying it, and a changed /// target is retried at once. A controller that never probes ([`Controller::probes`]) /// ignores the target, and zero, the default, never probes. pub fn set_probe_target(&mut self, bytes_per_second: u64) { + if bytes_per_second == self.bandwidth_probe.target() { + return; + } self.bandwidth_probe.set_target(bytes_per_second); + self.path.congestion.on_probe_target(bytes_per_second); + if let Some((_, prev)) = &mut self.prev_path { + prev.congestion.on_probe_target(bytes_per_second); + } } /// Resets path-specific settings. @@ -1619,6 +1628,7 @@ impl Connection { #[cfg(test)] pub(crate) fn path_changed(&mut self, now: Instant) { self.path.reset(now, &self.config); + self.path.congestion.on_probe_target(self.bandwidth_probe.target()); // The rebuilt controller never sees state the old one gave packets still in flight. for space in SpaceId::iter() { for info in self.spaces[space].sent_packets.values_mut() { @@ -3370,6 +3380,7 @@ impl Connection { &self.config, ) }; + new_path.congestion.on_probe_target(self.bandwidth_probe.target()); new_path.challenge = Some(self.rng.random()); new_path.challenge_pending = true; let prev_pto = self.pto(SpaceId::Data); diff --git a/rs/moq-quic/src/tests/probe.rs b/rs/moq-quic/src/tests/probe.rs index ad1dbae270c..8ba3aeaabb4 100644 --- a/rs/moq-quic/src/tests/probe.rs +++ b/rs/moq-quic/src/tests/probe.rs @@ -13,7 +13,7 @@ use std::collections::HashMap; use super::*; -use crate::congestion::cubic; +use crate::congestion::{cubic, gcc}; /// The media's bitrate, 2 Mbit/s, which the probe has to see past const MEDIA: u64 = 250_000; @@ -87,6 +87,8 @@ struct Scenario { ack_threshold: Option, /// Bytes of bulk upload before the media starts, to learn the path's rate first warmup: usize, + /// Whether the media runs GCC, reading the receive timestamps it asks for, rather than BBR3 + gcc: bool, } impl Default for Scenario { @@ -104,6 +106,7 @@ impl Default for Scenario { receive_window: None, ack_threshold: None, warmup: 0, + gcc: false, } } } @@ -166,9 +169,9 @@ impl Outcome { .map(|sample| sample.at) } - /// How soon after `from` the estimate reaches the target, if it ever does - fn rise(&self, from: Duration) -> Option { - self.first(from..Duration::MAX, |estimate| estimate >= TARGET) + /// How soon after `from` the estimate reaches `target`, if it ever does + fn rise(&self, from: Duration, target: u64) -> Option { + self.first(from..Duration::MAX, |estimate| estimate >= target) .map(|reached| reached - from) } @@ -261,6 +264,10 @@ fn run(scenario: &Scenario) -> Outcome { }; transport.ack_frequency = Some(ack); } + if scenario.gcc { + transport.congestion = Arc::new(gcc::Config::default()); + transport.receive_timestamps = NonZeroU32::new(32); + } let mut config = client_config(); config.transport = transport; let (client_ch, server_ch) = pair.connect_with(config); @@ -602,20 +609,28 @@ fn compare(label: &str, scenario: Scenario, seeds: ops::RangeInclusive) -> /// Asserts the probing runs' estimate reaches the target within [`RISE`] of `from` on the mean fn rises(trials: &[Trial], from: Duration) { + let rise = reaches(trials, from, TARGET); + assert!( + rise <= RISE.as_secs_f64(), + "estimate reached {TARGET} B/s {rise:.1}s after {from:?} on the mean" + ); +} + +/// Asserts every probing run's estimate reaches `target` after `from`, returning how soon, in +/// seconds on the mean +fn reaches(trials: &[Trial], from: Duration, target: u64) -> f64 { let rises = trials.iter().map(|Trial { seed, probing, .. }| { - let rise = probing.rise(from).unwrap_or_else(|| { + let rise = probing.rise(from, target).unwrap_or_else(|| { panic!( - "seed {seed}: estimate {} B/s never reached {TARGET} B/s after {from:?}", + "seed {seed}: estimate {} B/s never reached {target} B/s after {from:?}", probing.samples.last().expect("sampled").estimate ) }); rise.as_secs_f64() }); let rise = rises.sum::() / trials.len() as f64; - assert!( - rise <= RISE.as_secs_f64(), - "estimate reached {TARGET} B/s {rise:.1}s after {from:?} on the mean" - ); + info!(target, rise, "estimate reached the target after {from:?} on the mean"); + rise } /// Asserts the estimate, probing or not, falls to within the ProbeBW_UP gain of `rate` within @@ -676,6 +691,48 @@ fn rises_to_target() { rises(&compare("rises", scenario, GATE), Duration::from_secs(5)); } +/// The same under GCC, at the target and at twice it. GCC aims its probes at the target, at +/// twice its estimate but no more than twice the target, and reads each at the slower of its send +/// and receive rates, so the duplicates fill each cluster at its pace. Once the target is above +/// its estimate, GCC probes from the application-limited region five seconds after it last +/// probed or entered that region, so this holds the rise to reaching the target, not to [`RISE`], +/// and reports how soon for the bake-off. +#[test] +#[ignore = "nightly: just rs quic-sim"] +fn rises_to_target_under_gcc() { + let _guard = subscribe(); + let opens = Duration::from_secs(5); + for target in [TARGET, 2 * TARGET] { + let scenario = Scenario { + latency: Duration::from_millis(50), + buffer: 170_000, + keyframe: 0, + rates: vec![(Duration::ZERO, 375_000), (opens, 1_500_000)], + duration: Duration::from_secs(20), + target, + gcc: true, + ..Scenario::default() + }; + let trials = compare(&format!("rises gcc {target}"), scenario, SEEDS); + for Trial { + seed, + probing, + baseline, + } in &trials + { + info!( + seed, + target, + rise = ?probing.rise(opens, target), + probing = probing.samples.last().expect("sampled").estimate, + baseline = baseline.samples.last().expect("sampled").estimate, + "estimate after the path opened up, in B/s" + ); + } + reaches(&trials, opens, target); + } +} + /// A path short of the target, behind a queue a second deep: each attempt that misses sits out /// past the longest BBR3 waits between its own probes, and the path opening up is still found #[test] @@ -703,7 +760,7 @@ fn backs_off_unreachable_target() { // The samples are 100ms apart assert!(gap > Duration::from_millis(2900), "attempts began at {starts:?}"); } - let rise = outcome.rise(opens).expect("reached the target"); + let rise = outcome.rise(opens, TARGET).expect("reached the target"); assert!(rise <= RISE, "reached the target {rise:?} after the path opened"); } @@ -837,6 +894,100 @@ fn probe_inflight_limits_only_an_attempt() { ); } +/// A controller that always probes, allowing [`Cluster::INFLIGHT`] in flight, and never limits +/// the window or the pace +#[derive(Clone)] +struct Cluster { + paces_probe: bool, +} + +impl Cluster { + const INFLIGHT: u64 = 20_000; +} + +impl Controller for Cluster { + fn on_sent(&mut self, _sent: congestion::Sent) -> congestion::Packet { + Default::default() + } + + fn on_ack(&mut self, _ack: congestion::Ack<'_>) {} + + fn on_loss(&mut self, _loss: congestion::Loss<'_>) {} + + fn on_ce(&mut self, _ce: congestion::Ce) {} + + fn on_mtu_update(&mut self, _new_mtu: u16) {} + + fn window(&self) -> u64 { + u64::MAX / 2 + } + + fn pace(&mut self, _now: Instant, _bytes: u64) -> Option { + None + } + + fn metrics(&self) -> congestion::Metrics { + congestion::Metrics { + congestion_window: self.window(), + probe_inflight: Some(Self::INFLIGHT), + ..Default::default() + } + } + + fn probes(&self) -> bool { + true + } + + fn paces_probe(&self) -> bool { + self.paces_probe + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Factory for Cluster { + fn build(self: Arc, _init: congestion::Init) -> Box { + Box::new((*self).clone()) + } +} + +/// A controller that paces its own probe gets the inflight it allows filled at its pace, past +/// what the target's pace would let leave at once; any other is held to the target +#[test] +fn paced_probe_fills_past_the_target() { + let _guard = subscribe(); + let sent = |paces_probe| { + let mut pair = Pair::default(); + pair.latency = Duration::from_millis(20); + let mut config = client_config(); + config.transport.congestion = Arc::new(Cluster { paces_probe }); + let (client_ch, _) = pair.connect_with(config); + pair.client_conn_mut(client_ch).set_probe_target(TARGET); + let id = pair + .client_streams(client_ch) + .open(Dir::Uni, Default::default()) + .unwrap(); + pair.client_send(client_ch, id).write(&mut &[0; 1400][..]).unwrap(); + pair.drive_client(); + let conn = pair.client_conn_mut(client_ch); + (conn.stats().path.sent_bandwidth_probe_bytes, conn.bytes_in_flight()) + }; + + let (paced, in_flight) = sent(true); + assert!( + in_flight + DEFAULT_MTU as u64 > Cluster::INFLIGHT, + "{in_flight} bytes in flight, short of the probe's {}", + Cluster::INFLIGHT + ); + let (held, _) = sent(false); + assert!( + held < paced, + "{held} probe bytes held to the target, {paced} paced by the controller" + ); +} + #[test] #[ignore = "nightly: just rs quic-sim"] fn steps_keyframes() {