From 33c2346c84896fc0b0ff2d5ab180694c4d744a41 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 03:18:41 -0800 Subject: [PATCH 01/15] chore(quest): claim quest/m2/quic-realtime/gcc.md Quest-Claim: 8b0af89e7be69f35 From 5286afb73037e46a82690ad9ee65a7e91e24fbf7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 03:23:04 -0800 Subject: [PATCH 02/15] chore(congestion): import goog_cc 0.1.4 verbatim Copied unchanged from kixelated/goog_cc at 3599b2a (WebRTC da2a3d6c4d, 2024-12-09), with its BSD LICENSE, PATENTS, and AUTHORS, so the following commits show the refresh and the trim as diffs. Not built yet. Co-Authored-By: Claude Opus 5.5 --- rs/moq-congestion/src/gcc/AUTHORS | 212 + rs/moq-congestion/src/gcc/LICENSE | 29 + rs/moq-congestion/src/gcc/PATENTS | 24 + .../src/gcc/acknowledged_bitrate_estimator.rs | 82 + ...cknowledged_bitrate_estimator_interface.rs | 22 + rs/moq-congestion/src/gcc/alr_detector.rs | 345 ++ rs/moq-congestion/src/gcc/api/mod.rs | 8 + .../src/gcc/api/network_control.rs | 110 + .../src/gcc/api/transport/bandwidth_usage.rs | 6 + .../src/gcc/api/transport/ecn_marking.rs | 35 + .../src/gcc/api/transport/mod.rs | 7 + .../src/gcc/api/transport/network_types.rs | 540 +++ .../src/gcc/api/units/data_rate.rs | 393 ++ .../src/gcc/api/units/data_size.rs | 167 + .../src/gcc/api/units/frequency.rs | 295 ++ .../src/gcc/api/units/is_newer.rs | 184 + rs/moq-congestion/src/gcc/api/units/mod.rs | 16 + .../src/gcc/api/units/time_delta.rs | 394 ++ .../src/gcc/api/units/timestamp.rs | 366 ++ .../src/gcc/api/units/unit_base.rs | 800 ++++ .../src/gcc/bitrate_estimator.rs | 218 + .../congestion_window_pushback_controller.rs | 186 + rs/moq-congestion/src/gcc/delay_based_bwe.rs | 1367 ++++++ .../gcc/delay_increase_detector_interface.rs | 27 + .../src/gcc/experiments/alr_experiment.rs | 27 + .../src/gcc/experiments/field_trials.rs | 130 + rs/moq-congestion/src/gcc/experiments/mod.rs | 7 + .../src/gcc/experiments/rate_control.rs | 168 + .../src/gcc/goog_cc_network_control.rs | 2174 +++++++++ .../src/gcc/inter_arrival_delta.rs | 183 + .../src/gcc/link_capacity_estimator.rs | 99 + .../gcc/loss_based_bandwidth_estimation.rs | 291 ++ .../src/gcc/loss_based_bwe_v2.rs | 3943 +++++++++++++++++ rs/moq-congestion/src/gcc/mod.rs | 85 + .../src/gcc/pacing/interval_budget.rs | 68 + rs/moq-congestion/src/gcc/pacing/mod.rs | 2 + .../src/gcc/probe_bitrate_estimator.rs | 499 +++ rs/moq-congestion/src/gcc/probe_controller.rs | 2579 +++++++++++ .../aimd_rate_control.rs | 919 ++++ .../remote_bitrate_estimator/bwe_defines.rs | 41 + .../remote_bitrate_estimator/inter_arrival.rs | 953 ++++ .../src/gcc/remote_bitrate_estimator/mod.rs | 7 + .../src/gcc/robust_throughput_estimator.rs | 849 ++++ .../src/gcc/send_side_bandwidth_estimation.rs | 1000 +++++ .../src/gcc/trendline_estimator.rs | 525 +++ 45 files changed, 20382 insertions(+) create mode 100644 rs/moq-congestion/src/gcc/AUTHORS create mode 100644 rs/moq-congestion/src/gcc/LICENSE create mode 100644 rs/moq-congestion/src/gcc/PATENTS create mode 100644 rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs create mode 100644 rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs create mode 100644 rs/moq-congestion/src/gcc/alr_detector.rs create mode 100644 rs/moq-congestion/src/gcc/api/mod.rs create mode 100644 rs/moq-congestion/src/gcc/api/network_control.rs create mode 100644 rs/moq-congestion/src/gcc/api/transport/bandwidth_usage.rs create mode 100644 rs/moq-congestion/src/gcc/api/transport/ecn_marking.rs create mode 100644 rs/moq-congestion/src/gcc/api/transport/mod.rs create mode 100644 rs/moq-congestion/src/gcc/api/transport/network_types.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/data_rate.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/data_size.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/frequency.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/is_newer.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/mod.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/time_delta.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/timestamp.rs create mode 100644 rs/moq-congestion/src/gcc/api/units/unit_base.rs create mode 100644 rs/moq-congestion/src/gcc/bitrate_estimator.rs create mode 100644 rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs create mode 100644 rs/moq-congestion/src/gcc/delay_based_bwe.rs create mode 100644 rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs create mode 100644 rs/moq-congestion/src/gcc/experiments/alr_experiment.rs create mode 100644 rs/moq-congestion/src/gcc/experiments/field_trials.rs create mode 100644 rs/moq-congestion/src/gcc/experiments/mod.rs create mode 100644 rs/moq-congestion/src/gcc/experiments/rate_control.rs create mode 100644 rs/moq-congestion/src/gcc/goog_cc_network_control.rs create mode 100644 rs/moq-congestion/src/gcc/inter_arrival_delta.rs create mode 100644 rs/moq-congestion/src/gcc/link_capacity_estimator.rs create mode 100644 rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs create mode 100644 rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs create mode 100644 rs/moq-congestion/src/gcc/mod.rs create mode 100644 rs/moq-congestion/src/gcc/pacing/interval_budget.rs create mode 100644 rs/moq-congestion/src/gcc/pacing/mod.rs create mode 100644 rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs create mode 100644 rs/moq-congestion/src/gcc/probe_controller.rs create mode 100644 rs/moq-congestion/src/gcc/remote_bitrate_estimator/aimd_rate_control.rs create mode 100644 rs/moq-congestion/src/gcc/remote_bitrate_estimator/bwe_defines.rs create mode 100644 rs/moq-congestion/src/gcc/remote_bitrate_estimator/inter_arrival.rs create mode 100644 rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs create mode 100644 rs/moq-congestion/src/gcc/robust_throughput_estimator.rs create mode 100644 rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs create mode 100644 rs/moq-congestion/src/gcc/trendline_estimator.rs 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/acknowledged_bitrate_estimator.rs b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs new file mode 100644 index 00000000000..b9f5e43acc3 --- /dev/null +++ b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2017 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::{ + api::{ + transport::PacketResult, + units::{DataRate, Timestamp}, + }, + experiments::FieldTrials, + AcknowledgedBitrateEstimatorInterface, BitrateEstimator, RobustThroughputEstimator, +}; + +pub struct AcknowledgedBitrateEstimator { + alr_ended_time: Option, + in_alr: bool, + bitrate_estimator: BitrateEstimator, +} + +impl AcknowledgedBitrateEstimatorInterface for AcknowledgedBitrateEstimator { + 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.iter() { + if let Some(alr_ended_time) = self.alr_ended_time { + if packet.sent_packet.send_time > alr_ended_time { + self.bitrate_estimator.expect_fast_rate_change(); + self.alr_ended_time = None; + } + } + let mut acknowledged_estimate = packet.sent_packet.size; + acknowledged_estimate += packet.sent_packet.prior_unacked_data; + self.bitrate_estimator + .update(packet.receive_time, acknowledged_estimate, self.in_alr); + } + } + + fn bitrate(&self) -> Option { + self.bitrate_estimator.bitrate() + } + + fn peek_rate(&self) -> Option { + self.bitrate_estimator.peek_rate() + } + + fn set_alr_ended_time(&mut self, alr_ended_time: Timestamp) { + self.alr_ended_time.replace(alr_ended_time); + } + + fn set_alr(&mut self, in_alr: bool) { + self.in_alr = in_alr; + } +} + +impl AcknowledgedBitrateEstimator { + pub fn new(bitrate_estimator: BitrateEstimator) -> Self { + Self { + alr_ended_time: None, + in_alr: false, + bitrate_estimator, + } + } + + pub fn create(field_trials: &FieldTrials) -> Box { + let simplified_estimator_settings = &field_trials.robust_throughput_estimator_settings; + if simplified_estimator_settings.enabled { + Box::new(RobustThroughputEstimator::new( + simplified_estimator_settings.clone(), + )) + } else { + Box::new(AcknowledgedBitrateEstimator::new(BitrateEstimator::new( + field_trials.bwe_throughput_window_config.clone(), + ))) + } + } +} diff --git a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs new file mode 100644 index 00000000000..eadcf74789b --- /dev/null +++ b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs @@ -0,0 +1,22 @@ +/* + * 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 crate::api::{ + transport::PacketResult, + units::{DataRate, Timestamp}, +}; + +pub trait AcknowledgedBitrateEstimatorInterface { + fn incoming_packet_feedback(&mut self, packet_feedback: &[PacketResult]); + fn bitrate(&self) -> Option; + fn peek_rate(&self) -> Option; + fn set_alr(&mut self, in_alr: bool); + fn set_alr_ended_time(&mut self, alr_ended_time: Timestamp); +} 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..fa87054c921 --- /dev/null +++ b/rs/moq-congestion/src/gcc/alr_detector.rs @@ -0,0 +1,345 @@ +/* + * 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::time::Instant; + +use crate::{experiments::AlrExperimentSettings, 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 { + // The ALR experiment seems hard-coded on. + // TODO Technically, WebRTC-AlrDetectorParameters could override these values. + let settings = AlrExperimentSettings::default(); + Self { + bandwidth_usage_ratio: settings.alr_bandwidth_usage_percent as f64 / 100.0, + start_budget_level_ratio: settings.alr_start_budget_level_percent as f64 / 100.0, + stop_budget_level_ratio: settings.alr_stop_budget_level_percent as f64 / 100.0, + } + } +} + +// 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. +pub struct AlrDetector { + conf: AlrDetectorConfig, + + last_send_time_ms: Option, + + alr_budget: IntervalBudget, + alr_started_time_ms: Option, + + // Replacement for rtc_time + start: Instant, +} + +impl Default for AlrDetector { + fn default() -> Self { + Self::new(AlrDetectorConfig::default()) + } +} + +impl AlrDetector { + pub fn new(conf: AlrDetectorConfig) -> Self { + Self { + conf, + last_send_time_ms: None, + alr_budget: IntervalBudget::new(0, true), + alr_started_time_ms: None, + start: Instant::now(), + } + } + + pub fn on_bytes_sent(&mut self, bytes_sent: usize, send_time_ms: i64) { + let last_send_time_ms = match self.last_send_time_ms { + Some(v) => v, + None => { + // Since the duration for sending the bytes is unknwon, return without + // updating alr state. + self.last_send_time_ms = Some(send_time_ms); + return; + } + }; + + let delta_time_ms = send_time_ms - last_send_time_ms; + self.last_send_time_ms = Some(send_time_ms); + + self.alr_budget.use_budget(bytes_sent); + self.alr_budget.increase_budget(delta_time_ms); + if self.alr_budget.budget_ratio() > self.conf.start_budget_level_ratio + && self.alr_started_time_ms.is_none() + { + self.alr_started_time_ms + .replace(self.start.elapsed().as_millis() as i64); + } else if self.alr_budget.budget_ratio() < self.conf.stop_budget_level_ratio + && self.alr_started_time_ms.is_some() + { + self.alr_started_time_ms.take(); + } + } + + // Set current estimated bandwidth. + pub fn set_estimated_bitrate(&mut self, bitrate_bps: i64) { + assert!(bitrate_bps > 0); + let target_rate_kbps: i64 = + (bitrate_bps as f64 * self.conf.bandwidth_usage_ratio / 1000.0) as i64; + self.alr_budget.set_target_rate_kbps(target_rate_kbps); + } + + // Returns time in milliseconds 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_ms + } +} + +#[cfg(test)] +mod test { + use super::*; + + 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( + (ESTIMATED_BITRATE_BPS * usage_percentage * TIME_STEP_MS / (8 * 100 * 1000)) + as usize, + *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( + (ESTIMATED_BITRATE_BPS * usage_percentage * remainder_ms / (8 * 100 * 1000)) + as usize, + *self.timestamp_ms, + ); + } + } + } + + #[test] + fn alr_detection() { + let mut timestamp_ms: i64 = 1000; + let mut alr_detector = AlrDetector::default(); + alr_detector.set_estimated_bitrate(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(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(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(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(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()) + ); + } +} 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..999a83d6b7d --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/mod.rs @@ -0,0 +1,8 @@ +/// A generic congestion control interface: [NetworkControllerInterface](network_control::NetworkControllerInterface). +pub mod network_control; + +/// Network structs and types. +pub mod transport; + +/// Some unit types, such as [DataSize](units::DataSize) and [DataRate](units::DataRate). +pub mod units; diff --git a/rs/moq-congestion/src/gcc/api/network_control.rs b/rs/moq-congestion/src/gcc/api/network_control.rs new file mode 100644 index 00000000000..00bf6858124 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/network_control.rs @@ -0,0 +1,110 @@ +/* + * 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::experiments::FieldTrials; + +use super::{transport::*, units::TimeDelta}; + +/* +pub trait TargetTransferRateObserver { + /// Called to indicate target transfer rate as well as giving information about + /// the current estimate of network parameters. + fn on_target_transfer_rate(target_transfer_rate: TargetTransferRate); + /// Called to provide updates to the expected target rate in case it changes + /// before the first call to OnTargetTransferRate. + fn on_start_rate_update(data_rate: DataRate); +} +*/ + +/// 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, these can be changed at any later + /// time by calls to OnTargetRateConstraints. 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, these are changed at any later time + /// by calls to OnStreamsConfig. + pub stream_based_config: StreamsConfig, +} + +/// NetworkControllerInterface is implemented by network controllers. A network +/// controller is a class that uses information about network state and traffic +/// to estimate network parameters such as round trip time and bandwidth. Network +/// controllers does not guarantee thread safety, the interface must be used in a +/// non-concurrent fashion. +pub trait NetworkControllerInterface { + /// Called when network availabilty changes. + fn on_network_availability(&mut self, msg: NetworkAvailability) -> NetworkControlUpdate; + /// Called when the receiving or sending endpoint changes address. + fn on_network_route_change(&mut self, msg: NetworkRouteChange) -> NetworkControlUpdate; + /// Called periodically with a periodicy as specified by + /// NetworkControllerFactoryInterface::GetProcessInterval. + fn on_process_interval(&mut self, msg: ProcessInterval) -> NetworkControlUpdate; + /// Called when remotely calculated bitrate is received. + fn on_remote_bitrate_report(&mut self, msg: RemoteBitrateReport) -> NetworkControlUpdate; + /// Called round trip time has been calculated by protocol specific mechanisms. + fn on_round_trip_time_update(&mut self, msg: RoundTripTimeUpdate) -> NetworkControlUpdate; + /// Called when a packet is sent on the network. + fn on_sent_packet(&mut self, msg: SentPacket) -> NetworkControlUpdate; + /// Called when a packet is received from the remote client. + fn on_received_packet(&mut self, msg: ReceivedPacket) -> NetworkControlUpdate; + /// Called when the stream specific configuration has been updated. + fn on_streams_config(&mut self, msg: StreamsConfig) -> NetworkControlUpdate; + /// Called when target transfer rate constraints has been changed. + fn on_target_rate_constraints(&mut self, msg: TargetRateConstraints) -> NetworkControlUpdate; + /// Called when a protocol specific calculation of packet loss has been made. + fn on_transport_loss_report(&mut self, msg: TransportLossReport) -> NetworkControlUpdate; + /// Called with per packet feedback regarding receive time. + fn on_transport_packets_feedback( + &mut self, + msg: TransportPacketsFeedback, + ) -> NetworkControlUpdate; + /// Called with network state estimate updates. + fn on_network_state_estimate(&mut self, msg: NetworkStateEstimate) -> NetworkControlUpdate; + + /// Returns the interval by which the network controller expects + /// OnProcessInterval calls. + fn get_process_interval() -> TimeDelta; +} + +/* +// NetworkControllerFactoryInterface is an interface for creating a network +// controller. +pub trait NetworkControllerFactoryInterface { + // Used to create a new network controller, requires an observer to be + // provided to handle callbacks. + fn create(config: NetworkControllerConfig) -> impl NetworkControllerInterface; +} +*/ + +// Under development, subject to change without notice. +/* Purposely removed because nothing implements it (except for secret Google stuff?) +pub trait NetworkStateEstimator { + // Gets the current best estimate according to the estimator. + fn GetCurrentEstimate(&self) -> Option; + // Called with per packet feedback regarding receive time. + // Used when the NetworkStateEstimator runs in the sending endpoint. + fn OnTransportPacketsFeedback(&mut self, event: &TransportPacketsFeedback); + // Called with per packet feedback regarding receive time. + // Used when the NetworkStateEstimator runs in the receiving endpoint. + fn OnReceivedPacket(&mut self, event: &PacketResult); + // Called when the receiving or sending endpoint changes address. + fn OnRouteChange(&mut self, event: &NetworkRouteChange); +} + +pub trait NetworkStateEstimatorFactory { + fn Create() -> impl NetworkStateEstimator; +} +*/ 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/ecn_marking.rs b/rs/moq-congestion/src/gcc/api/transport/ecn_marking.rs new file mode 100644 index 00000000000..476df0f334e --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/transport/ecn_marking.rs @@ -0,0 +1,35 @@ +/* + * Copyright 2024 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. + */ + +// TODO: bugs.webrtc.org/42225697 - L4S support is slowly being developed. +// Help is appreciated. + +// L4S Explicit Congestion Notification (ECN) -> +// https://www.rfc-editor.org/rfc/rfc9331.html ECT stands for ECN-Capable +// Transport and CE stands for Congestion Experienced. + +// RFC-3168, Section 5 +// +-----+-----+ +// | ECN FIELD | +// +-----+-----+ +// ECT CE [Obsolete] RFC 2481 names for the ECN bits. +// 0 0 Not-ECT +// 0 1 ECT(1) +// 1 0 ECT(0) +// 1 1 CE + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum EcnMarking { + #[default] + NotEct = 0, // Not ECN-Capable Transport + Ect1 = 1, // ECN-Capable Transport + Ect0 = 2, // Not used by L4s (or webrtc.) + Ce = 3, // Congestion experienced +} 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..7f402515935 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/transport/mod.rs @@ -0,0 +1,7 @@ +mod bandwidth_usage; +mod ecn_marking; +mod network_types; + +pub use bandwidth_usage::*; +pub use ecn_marking::*; +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..5582007c076 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/transport/network_types.rs @@ -0,0 +1,540 @@ +use std::cmp::Ordering; + +use crate::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; + +use super::EcnMarking; + +/// Represents constraints and rates related to the currently enabled streams. +/// This is used as input to the congestion controller via the StreamsConfig +/// struct. +#[derive(Default, Clone, Copy, Debug)] +pub struct BitrateAllocationLimits { + // The total minimum send bitrate required by all sending streams. + pub min_allocatable_rate: DataRate, + // The total maximum allocatable bitrate for all currently available streams. + pub max_allocatable_rate: DataRate, + // The max bitrate to use for padding. The sum of the per-stream max padding + // rate. + pub max_padding_rate: DataRate, +} + +/// 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)] +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: Option, + pub pacing_factor: Option, + + // TODO(srte): Use BitrateAllocationLimits here. + pub min_total_allocated_bitrate: Option, + pub max_padding_rate: Option, + pub max_total_allocated_bitrate: Option, +} + +impl Default for StreamsConfig { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + requests_alr_probing: None, + enable_repeated_initial_probing: None, + pacing_factor: None, + min_total_allocated_bitrate: None, + max_padding_rate: None, + max_total_allocated_bitrate: None, + } + } +} + +#[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)] +pub struct NetworkRouteChange { + pub at_time: Timestamp, + /// The TargetRateConstraints are set here so they can be changed synchronously + /// when network route changes. + pub constraints: TargetRateConstraints, +} + +impl Default for NetworkRouteChange { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + constraints: TargetRateConstraints::default(), + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct PacedPacketInfo { + pub send_bitrate: DataRate, + pub probe_cluster_id: i32, + pub probe_cluster_min_probes: i64, + pub probe_cluster_min_bytes: i64, + pub probe_cluster_bytes_sent: 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 { + send_bitrate: DataRate::from_bits_per_sec(0), + probe_cluster_id, + probe_cluster_min_probes, + probe_cluster_min_bytes, + probe_cluster_bytes_sent: 0, + } + } +} + +impl Default for PacedPacketInfo { + fn default() -> Self { + Self { + send_bitrate: DataRate::from_bits_per_sec(0), + probe_cluster_id: Self::NOT_APROBE, + probe_cluster_min_probes: -1, + probe_cluster_min_bytes: -1, + probe_cluster_bytes_sent: 0, + } + } +} + +impl PartialEq for PacedPacketInfo { + fn eq(&self, other: &Self) -> bool { + self.send_bitrate == other.send_bitrate + && self.probe_cluster_id == other.probe_cluster_id + && self.probe_cluster_min_probes == other.probe_cluster_min_probes + && self.probe_cluster_min_bytes == other.probe_cluster_min_bytes + } +} + +#[derive(Debug, Clone, Copy)] +pub struct SentPacket { + pub send_time: Timestamp, + /// Size of packet with overhead up to IP layer. + pub size: DataSize, + /// Size of preceeding packets that are not part of feedback. + pub prior_unacked_data: DataSize, + /// Probe cluster id and parameters including bitrate, number of packets and + /// number of bytes. + pub pacing_info: PacedPacketInfo, + /// True if the packet is an audio packet, false for video, padding, RTX etc. + pub audio: bool, + /// Transport independent sequence number, any tracked packet should have a + /// sequence number that is unique over the whole call and increasing by 1 for + /// each packet. + pub sequence_number: i64, + /// 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(), + prior_unacked_data: DataSize::zero(), + pacing_info: PacedPacketInfo::default(), + audio: false, + sequence_number: 0, + data_in_flight: DataSize::zero(), + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct ReceivedPacket { + pub send_time: Timestamp, + pub receive_time: Timestamp, + pub size: DataSize, +} + +impl Default for ReceivedPacket { + fn default() -> Self { + Self { + send_time: Timestamp::minus_infinity(), + receive_time: Timestamp::plus_infinity(), + size: DataSize::zero(), + } + } +} + +// Transport level feedback + +#[derive(Debug, Clone, Copy)] +pub struct RemoteBitrateReport { + pub receive_time: Timestamp, + pub bandwidth: DataRate, +} + +impl Default for RemoteBitrateReport { + fn default() -> Self { + Self { + receive_time: Timestamp::plus_infinity(), + bandwidth: DataRate::infinity(), + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct RoundTripTimeUpdate { + pub receive_time: Timestamp, + pub round_trip_time: TimeDelta, + pub smoothed: bool, +} + +impl Default for RoundTripTimeUpdate { + fn default() -> Self { + Self { + receive_time: Timestamp::plus_infinity(), + round_trip_time: TimeDelta::plus_infinity(), + smoothed: false, + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct TransportLossReport { + pub receive_time: Timestamp, + pub start_time: Timestamp, + pub end_time: Timestamp, + pub packets_lost_delta: u64, + pub packets_received_delta: u64, +} + +impl Default for TransportLossReport { + fn default() -> Self { + Self { + receive_time: Timestamp::plus_infinity(), + start_time: Timestamp::plus_infinity(), + end_time: Timestamp::plus_infinity(), + packets_lost_delta: 0, + packets_received_delta: 0, + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct PacketResult { + pub sent_packet: SentPacket, + pub receive_time: Timestamp, + pub ecn: EcnMarking, +} + +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(), + ecn: EcnMarking::NotEct, + } + } +} + +impl PartialOrd for PacketResult { + fn partial_cmp(&self, other: &Self) -> Option { + if self.receive_time != other.receive_time { + return Some(self.receive_time.cmp(&other.receive_time)); + } + + if self.sent_packet.send_time != other.sent_packet.send_time { + return Some(self.sent_packet.send_time.cmp(&other.sent_packet.send_time)); + } + + Some( + self.sent_packet + .sequence_number + .cmp(&other.sent_packet.sequence_number), + ) + } +} + +impl PartialEq for PacketResult { + fn eq(&self, other: &Self) -> bool { + self.receive_time == other.receive_time + && self.sent_packet.send_time == other.sent_packet.send_time + && self.sent_packet.sequence_number == other.sent_packet.sequence_number + } +} + +#[derive(Debug, Clone)] +pub struct TransportPacketsFeedback { + pub feedback_time: Timestamp, + pub data_in_flight: DataSize, + pub packet_feedbacks: Vec, + + /// Arrival times for messages without send time information. + pub sendless_arrival_times: Vec, +} + +impl Default for TransportPacketsFeedback { + fn default() -> Self { + Self { + feedback_time: Timestamp::plus_infinity(), + data_in_flight: DataSize::zero(), + packet_feedbacks: Vec::new(), + sendless_arrival_times: Vec::new(), + } + } +} + +impl TransportPacketsFeedback { + /// NOTE: These returned Vec copies in the original C++ code. Use collect() if you want that behavior. + pub fn received_with_send_info(&self) -> impl Iterator { + self.packet_feedbacks.iter().filter(|fb| fb.is_received()) + } + + pub fn lost_with_send_info(&self) -> impl Iterator { + self.packet_feedbacks.iter().filter(|fb| !fb.is_received()) + } + + pub fn packets_with_feedback(&self) -> &Vec { + &self.packet_feedbacks + } + + pub fn sorted_by_receive_time(&self) -> Vec { + let mut res: Vec = self.received_with_send_info().cloned().collect(); + res.sort_by(|a, b| a.receive_time.cmp(&b.receive_time)); + res + } +} + +#[derive(Debug, Clone, Copy)] +pub struct NetworkEstimate { + pub at_time: Timestamp, + /// Deprecated, use TargetTransferRate::target_rate instead. + pub bandwidth: DataRate, + pub round_trip_time: TimeDelta, + pub bwe_period: TimeDelta, + + pub loss_rate_ratio: f32, +} + +impl Default for NetworkEstimate { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + bandwidth: DataRate::infinity(), + round_trip_time: TimeDelta::plus_infinity(), + bwe_period: TimeDelta::plus_infinity(), + loss_rate_ratio: 0.0, + } + } +} + +#[derive(Debug, Clone, Copy)] +pub struct PacerConfig { + pub at_time: Timestamp, + /// Pacer should send at most data_window data over time_window duration. + pub data_window: DataSize, + pub time_window: TimeDelta, + /// Pacer should send at least pad_window data over time_window duration. + pub pad_window: DataSize, +} + +impl Default for PacerConfig { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + data_window: DataSize::infinity(), + time_window: TimeDelta::plus_infinity(), + pad_window: DataSize::zero(), + } + } +} + +impl PacerConfig { + pub fn data_rate(&self) -> DataRate { + self.data_window / self.time_window + } + pub fn pad_rate(&self) -> DataRate { + self.pad_window / self.time_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 at_time: Timestamp, + /// The estimate on which the target rate is based on. + pub network_estimate: NetworkEstimate, + pub target_rate: DataRate, + pub stable_target_rate: DataRate, + pub cwnd_reduce_ratio: f64, +} + +impl Default for TargetTransferRate { + fn default() -> Self { + Self { + at_time: Timestamp::plus_infinity(), + network_estimate: NetworkEstimate::default(), + target_rate: DataRate::zero(), + stable_target_rate: DataRate::zero(), + cwnd_reduce_ratio: 0.0, + } + } +} + +/// 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, +} + +impl NetworkControlUpdate { + pub fn has_updates(&self) -> bool { + self.congestion_window.is_some() + || self.pacer_config.is_some() + || !self.probe_cluster_configs.is_empty() + || self.target_rate.is_some() + } +} + +// 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, + } + } +} + +/// Under development, subject to change without notice. +#[derive(Debug, Clone, Copy)] +pub struct NetworkStateEstimate { + pub confidence: f64, + + // The time the estimate was received/calculated. + pub update_time: Timestamp, + pub last_receive_time: Timestamp, + pub last_send_time: Timestamp, + + // Total estimated link capacity. + pub link_capacity: DataRate, + // Used as a safe measure of available capacity. + pub link_capacity_lower: DataRate, + // Used as limit for increasing bitrate. + pub link_capacity_upper: DataRate, + + pub pre_link_buffer_delay: TimeDelta, + pub post_link_buffer_delay: TimeDelta, + pub propagation_delay: TimeDelta, + + // Only for debugging + pub time_delta: TimeDelta, + pub last_feed_time: Timestamp, + pub cross_delay_rate: f64, + pub spike_delay_rate: f64, + pub link_capacity_std_dev: DataRate, + pub link_capacity_min: DataRate, + pub cross_traffic_ratio: f64, +} + +impl Default for NetworkStateEstimate { + fn default() -> Self { + Self { + confidence: f64::NAN, + update_time: Timestamp::minus_infinity(), + last_receive_time: Timestamp::minus_infinity(), + last_send_time: Timestamp::minus_infinity(), + link_capacity: DataRate::minus_infinity(), + link_capacity_lower: DataRate::minus_infinity(), + link_capacity_upper: DataRate::minus_infinity(), + pre_link_buffer_delay: TimeDelta::minus_infinity(), + post_link_buffer_delay: TimeDelta::minus_infinity(), + propagation_delay: TimeDelta::minus_infinity(), + time_delta: TimeDelta::minus_infinity(), + last_feed_time: Timestamp::minus_infinity(), + cross_delay_rate: f64::NAN, + spike_delay_rate: f64::NAN, + link_capacity_std_dev: DataRate::minus_infinity(), + link_capacity_min: DataRate::minus_infinity(), + cross_traffic_ratio: f64::NAN, + } + } +} 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..9337aff6a09 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/data_rate.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. + */ + +//! 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 approx::assert_relative_eq; + + 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/is_newer.rs b/rs/moq-congestion/src/gcc/api/units/is_newer.rs new file mode 100644 index 00000000000..5c4cea52067 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/is_newer.rs @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2013 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. + */ + +const fn is_newer_16(value: u16, prev_value: u16) -> bool { + // kBreakpoint is the half-way mark for the type U. For instance, for a + // u16 it will be 0x8000, and for a u32, it will be 0x8000000. + const BREAKPOINT: u16 = (u16::MAX >> 1) + 1; + // Distinguish between elements that are exactly kBreakpoint apart. + // If t1>t2 and |t1-t2| = kBreakpoint: IsNewer(t1,t2)=true, + // IsNewer(t2,t1)=false + // rather than having IsNewer(t1,t2) = IsNewer(t2,t1) = false. + match value.wrapping_sub(prev_value) { + 1..BREAKPOINT => true, + BREAKPOINT => value > prev_value, + _ => false, + } +} + +const fn is_newer_32(value: u32, prev_value: u32) -> bool { + // kBreakpoint is the half-way mark for the type U. For instance, for a + // u16 it will be 0x8000, and for a u32, it will be 0x8000000. + const BREAKPOINT: u32 = (u32::MAX >> 1) + 1; + // Distinguish between elements that are exactly kBreakpoint apart. + // If t1>t2 and |t1-t2| = kBreakpoint: IsNewer(t1,t2)=true, + // IsNewer(t2,t1)=false + // rather than having IsNewer(t1,t2) = IsNewer(t2,t1) = false. + match value.wrapping_sub(prev_value) { + 1..BREAKPOINT => true, + BREAKPOINT => value > prev_value, + _ => false, + } +} + +// NB: Doesn't fulfill strict weak ordering requirements. +// Mustn't be used as std::map Compare function. +pub const fn is_newer_sequence_number(sequence_number: u16, prev_sequence_number: u16) -> bool { + is_newer_16(sequence_number, prev_sequence_number) +} + +// NB: Doesn't fulfill strict weak ordering requirements. +// Mustn't be used as std::map Compare function. +pub const fn is_newer_timestamp(timestamp: u32, prev_timestamp: u32) -> bool { + is_newer_32(timestamp, prev_timestamp) +} + +pub const fn latest_sequence_number(sequence_number1: u16, sequence_number2: u16) -> u16 { + if is_newer_sequence_number(sequence_number1, sequence_number2) { + sequence_number1 + } else { + sequence_number2 + } +} + +pub const fn latest_timestamp(timestamp1: u32, timestamp2: u32) -> u32 { + if is_newer_timestamp(timestamp1, timestamp2) { + timestamp1 + } else { + timestamp2 + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn is_newer_sequence_number_equal() { + assert!(!is_newer_sequence_number(0x0001, 0x0001)); + } + + #[test] + fn is_newer_sequence_number_no_wrap() { + assert!(is_newer_sequence_number(0xFFFF, 0xFFFE)); + assert!(is_newer_sequence_number(0x0001, 0x0000)); + assert!(is_newer_sequence_number(0x0100, 0x00FF)); + } + + #[test] + fn is_newer_sequence_number_forward_wrap() { + assert!(is_newer_sequence_number(0x0000, 0xFFFF)); + assert!(is_newer_sequence_number(0x0000, 0xFF00)); + assert!(is_newer_sequence_number(0x00FF, 0xFFFF)); + assert!(is_newer_sequence_number(0x00FF, 0xFF00)); + } + + #[test] + fn is_newer_sequence_number_backward_wrap() { + assert!(!is_newer_sequence_number(0xFFFF, 0x0000)); + assert!(!is_newer_sequence_number(0xFF00, 0x0000)); + assert!(!is_newer_sequence_number(0xFFFF, 0x00FF)); + assert!(!is_newer_sequence_number(0xFF00, 0x00FF)); + } + + #[test] + fn is_newer_sequence_number_half_way_apart() { + assert!(is_newer_sequence_number(0x8000, 0x0000)); + assert!(!is_newer_sequence_number(0x0000, 0x8000)); + } + + #[test] + fn is_newer_timestamp_equal() { + assert!(!is_newer_timestamp(0x00000001, 0x000000001)); + } + + #[test] + fn is_newer_timestamp_no_wrap() { + assert!(is_newer_timestamp(0xFFFFFFFF, 0xFFFFFFFE)); + assert!(is_newer_timestamp(0x00000001, 0x00000000)); + assert!(is_newer_timestamp(0x00010000, 0x0000FFFF)); + } + + #[test] + fn is_newer_timestamp_forward_wrap() { + assert!(is_newer_timestamp(0x00000000, 0xFFFFFFFF)); + assert!(is_newer_timestamp(0x00000000, 0xFFFF0000)); + assert!(is_newer_timestamp(0x0000FFFF, 0xFFFFFFFF)); + assert!(is_newer_timestamp(0x0000FFFF, 0xFFFF0000)); + } + + #[test] + fn is_newer_timestamp_backward_wrap() { + assert!(!is_newer_timestamp(0xFFFFFFFF, 0x00000000)); + assert!(!is_newer_timestamp(0xFFFF0000, 0x00000000)); + assert!(!is_newer_timestamp(0xFFFFFFFF, 0x0000FFFF)); + assert!(!is_newer_timestamp(0xFFFF0000, 0x0000FFFF)); + } + + #[test] + fn is_newer_timestamp_half_way_apart() { + assert!(is_newer_timestamp(0x80000000, 0x00000000)); + assert!(!is_newer_timestamp(0x00000000, 0x80000000)); + } + + #[test] + fn latest_sequence_number_no_wrap() { + assert_eq!(0xFFFF, latest_sequence_number(0xFFFF, 0xFFFE)); + assert_eq!(0x0001, latest_sequence_number(0x0001, 0x0000)); + assert_eq!(0x0100, latest_sequence_number(0x0100, 0x00FF)); + + assert_eq!(0xFFFF, latest_sequence_number(0xFFFE, 0xFFFF)); + assert_eq!(0x0001, latest_sequence_number(0x0000, 0x0001)); + assert_eq!(0x0100, latest_sequence_number(0x00FF, 0x0100)); + } + + #[test] + fn latest_sequence_number_wrap() { + assert_eq!(0x0000, latest_sequence_number(0x0000, 0xFFFF)); + assert_eq!(0x0000, latest_sequence_number(0x0000, 0xFF00)); + assert_eq!(0x00FF, latest_sequence_number(0x00FF, 0xFFFF)); + assert_eq!(0x00FF, latest_sequence_number(0x00FF, 0xFF00)); + + assert_eq!(0x0000, latest_sequence_number(0xFFFF, 0x0000)); + assert_eq!(0x0000, latest_sequence_number(0xFF00, 0x0000)); + assert_eq!(0x00FF, latest_sequence_number(0xFFFF, 0x00FF)); + assert_eq!(0x00FF, latest_sequence_number(0xFF00, 0x00FF)); + } + + #[test] + fn latest_timestamp_no_wrap() { + assert_eq!(0xFFFFFFFF, latest_timestamp(0xFFFFFFFF, 0xFFFFFFFE)); + assert_eq!(0x00000001, latest_timestamp(0x00000001, 0x00000000)); + assert_eq!(0x00010000, latest_timestamp(0x00010000, 0x0000FFFF)); + } + + #[test] + fn latest_timestamp_wrap() { + assert_eq!(0x00000000, latest_timestamp(0x00000000, 0xFFFFFFFF)); + assert_eq!(0x00000000, latest_timestamp(0x00000000, 0xFFFF0000)); + assert_eq!(0x0000FFFF, latest_timestamp(0x0000FFFF, 0xFFFFFFFF)); + assert_eq!(0x0000FFFF, latest_timestamp(0x0000FFFF, 0xFFFF0000)); + + assert_eq!(0x00000000, latest_timestamp(0xFFFFFFFF, 0x00000000)); + assert_eq!(0x00000000, latest_timestamp(0xFFFF0000, 0x00000000)); + assert_eq!(0x0000FFFF, latest_timestamp(0xFFFFFFFF, 0x0000FFFF)); + assert_eq!(0x0000FFFF, latest_timestamp(0xFFFF0000, 0x0000FFFF)); + } +} 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..f212751758b --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/mod.rs @@ -0,0 +1,16 @@ +mod data_rate; +mod data_size; +mod frequency; +// mod is_newer; +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 is_newer::*; +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..a944453aef3 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/time_delta.rs @@ -0,0 +1,394 @@ +/* + * 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 approx::assert_relative_eq; + + 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..648752e5573 --- /dev/null +++ b/rs/moq-congestion/src/gcc/api/units/timestamp.rs @@ -0,0 +1,366 @@ +/* + * 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, sync::LazyLock, time::{Duration, Instant}}; +use std::ops::*; + +use super::TimeDelta; + +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()) + } + } +} + +// Custom +impl From for Timestamp { + fn from(instant: Instant) -> Self { + let r = *REF; + if let Some(after) = instant.checked_duration_since(r) { + Timestamp::from_micros(after.as_micros() as i64) + } else if let Some(before) = r.checked_duration_since(instant) { + Timestamp::from_micros(-(before.as_micros() as i64)) + } else { + Timestamp::zero() + } + } +} + +impl From for Instant { + fn from(timestamp: Timestamp) -> Self { + let r = *REF; + let duration = Duration::from_micros(timestamp.us().unsigned_abs()); + + if timestamp.0 > 0 { + r.checked_add(duration).expect("Timestamp is too far in the future") + } else { + r.checked_sub(duration).expect("Timestamp is from before the program started") + } + } +} + +static REF: LazyLock = LazyLock::new(|| { + std::time::Instant::now() +}); + + +#[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..5274bd4d415 --- /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); + + 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)] + const fn from_fraction(denominator: i64, value: i64) -> Self { + assert!(denominator >= 0); + Self::from_value(value * denominator) + } + + #[allow(dead_code)] + fn from_fraction_float(denominator: f64, value: f64) -> Self { + Self::from_value_float(value * denominator) + } + + #[allow(dead_code)] + 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)] + fn to_fraction_float(self, denominator: f64) -> f64 { + assert!(denominator >= 0.0); + self.to_value_float() / denominator + } + + #[allow(dead_code)] + 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)] + 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::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; + + use approx::assert_relative_eq; + + use super::*; + + 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)] + 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/bitrate_estimator.rs b/rs/moq-congestion/src/gcc/bitrate_estimator.rs new file mode 100644 index 00000000000..7321d002e5a --- /dev/null +++ b/rs/moq-congestion/src/gcc/bitrate_estimator.rs @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2017 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::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; + +// WebRTC-BweThroughputWindowConfig +#[derive(Debug, Clone)] +pub struct BitrateEstimatorConfig { + pub initial_window_ms: i64, + pub window_ms: i64, + pub scale: f64, + pub scale_alr: f64, + pub scale_small: f64, + pub small_thresh: DataSize, + pub symmetry_cap: DataRate, + pub floor: DataRate, +} + +impl BitrateEstimatorConfig { + const INITIAL_RATE_WINDOW_MS: i64 = 500; + const RATE_WINDOW_MS: i64 = 150; + const MIN_RATE_WINDOW_MS: i64 = 150; + const MAX_RATE_WINDOW_MS: i64 = 1000; +} + +impl Default for BitrateEstimatorConfig { + fn default() -> Self { + Self { + initial_window_ms: Self::INITIAL_RATE_WINDOW_MS, + window_ms: Self::RATE_WINDOW_MS, + scale: 10.0, + scale_alr: 10.0, // scale + scale_small: 10.0, // scale + small_thresh: DataSize::zero(), + symmetry_cap: DataRate::zero(), + floor: DataRate::zero(), + } + } +} + +impl BitrateEstimatorConfig { + pub fn validate(&mut self) { + self.initial_window_ms = self + .initial_window_ms + .clamp(Self::MAX_RATE_WINDOW_MS, Self::MAX_RATE_WINDOW_MS); + self.window_ms = self + .window_ms + .clamp(Self::MIN_RATE_WINDOW_MS, Self::MAX_RATE_WINDOW_MS); + } +} + +// Computes a bayesian estimate of the throughput given acks containing +// the arrival time and payload size. Samples which are far from the current +// estimate or are based on few packets are given a smaller weight, as they +// are considered to be more likely to have been caused by, e.g., delay spikes +// unrelated to congestion. +pub struct BitrateEstimator { + sum: i64, + initial_window_ms: i64, + noninitial_window_ms: i64, + uncertainty_scale: f64, + uncertainty_scale_in_alr: f64, + small_sample_uncertainty_scale: f64, + small_sample_threshold: DataSize, + uncertainty_symmetry_cap: DataRate, + estimate_floor: DataRate, + current_window_ms: i64, + prev_time_ms: i64, + bitrate_estimate_kbps: f64, + bitrate_estimate_var: f64, +} + +impl Default for BitrateEstimator { + fn default() -> Self { + Self::new(BitrateEstimatorConfig::default()) + } +} + +impl BitrateEstimator { + pub fn new(mut config: BitrateEstimatorConfig) -> Self { + config.validate(); + Self { + sum: 0, + initial_window_ms: config.initial_window_ms, + noninitial_window_ms: config.window_ms, + uncertainty_scale: config.scale, + uncertainty_scale_in_alr: config.scale_alr, + small_sample_uncertainty_scale: config.scale_small, + small_sample_threshold: config.small_thresh, + uncertainty_symmetry_cap: config.symmetry_cap, + estimate_floor: config.floor, + current_window_ms: 0, + prev_time_ms: -1, + bitrate_estimate_kbps: -1.0, + bitrate_estimate_var: 50.0, + } + } + + pub fn update(&mut self, at_time: Timestamp, amount: DataSize, in_alr: bool) { + let mut rate_window_ms: i64 = self.noninitial_window_ms; + // We use a larger window at the beginning to get a more stable sample that + // we can use to initialize the estimate. + if self.bitrate_estimate_kbps < 0.0 { + rate_window_ms = self.initial_window_ms; + } + let mut is_small_sample: bool = false; + let bitrate_sample_kbps: f64 = self.update_window( + at_time.ms(), + amount.bytes(), + rate_window_ms, + &mut is_small_sample, + ); + if bitrate_sample_kbps < 0.0 { + return; + } + if self.bitrate_estimate_kbps < 0.0 { + // This is the very first sample we get. Use it to initialize the estimate. + self.bitrate_estimate_kbps = bitrate_sample_kbps; + return; + } + // Optionally use higher uncertainty for very small samples to avoid dropping + // estimate and for samples obtained in ALR. + let mut scale: f64 = self.uncertainty_scale; + if is_small_sample && bitrate_sample_kbps < self.bitrate_estimate_kbps { + scale = self.small_sample_uncertainty_scale; + } else if in_alr && bitrate_sample_kbps < self.bitrate_estimate_kbps { + // Optionally use higher uncertainty for samples obtained during ALR. + scale = self.uncertainty_scale_in_alr; + } + // Define the sample uncertainty as a function of how far away it is from the + // current estimate. With low values of self.uncertainty_symmetry_cap we add more + // uncertainty to increases than to decreases. For higher values we approach + // symmetry. + let sample_uncertainty: f64 = scale + * (self.bitrate_estimate_kbps - bitrate_sample_kbps).abs() + / (self.bitrate_estimate_kbps + + bitrate_sample_kbps.max(self.uncertainty_symmetry_cap.kbps_float())); + + let sample_var: f64 = sample_uncertainty * sample_uncertainty; + // Update a bayesian estimate of the rate, weighting it lower if the sample + // uncertainty is large. + // The bitrate estimate uncertainty is increased with each update to model + // that the bitrate changes over time. + let pred_bitrate_estimate_var: f64 = self.bitrate_estimate_var + 5.0; + self.bitrate_estimate_kbps = (sample_var * self.bitrate_estimate_kbps + + pred_bitrate_estimate_var * bitrate_sample_kbps) + / (sample_var + pred_bitrate_estimate_var); + self.bitrate_estimate_kbps = self + .bitrate_estimate_kbps + .max(self.estimate_floor.kbps_float()); + self.bitrate_estimate_var = + sample_var * pred_bitrate_estimate_var / (sample_var + pred_bitrate_estimate_var); + } + + pub fn bitrate(&self) -> Option { + if self.bitrate_estimate_kbps < 0.0 { + return None; + } + Some(DataRate::from_kilobits_per_sec_float( + self.bitrate_estimate_kbps, + )) + } + + pub fn peek_rate(&self) -> Option { + if self.current_window_ms > 0 { + return Some( + DataSize::from_bytes(self.sum) / TimeDelta::from_millis(self.current_window_ms), + ); + } + None + } + + pub fn expect_fast_rate_change(&mut self) { + // By setting the bitrate-estimate variance to a higher value we allow the + // bitrate to change fast for the next few samples. + self.bitrate_estimate_var += 200.0; + } + + fn update_window( + &mut self, + now_ms: i64, + bytes: i64, + rate_window_ms: i64, + is_small_sample: &mut bool, + ) -> f64 { + // Reset if time moves backwards. + if now_ms < self.prev_time_ms { + self.prev_time_ms = -1; + self.sum = 0; + self.current_window_ms = 0; + } + if self.prev_time_ms >= 0 { + self.current_window_ms += now_ms - self.prev_time_ms; + // Reset if nothing has been received for more than a full window. + if now_ms - self.prev_time_ms > rate_window_ms { + self.sum = 0; + self.current_window_ms %= rate_window_ms; + } + } + self.prev_time_ms = now_ms; + let mut bitrate_sample: f64 = -1.0; + if self.current_window_ms >= rate_window_ms { + *is_small_sample = self.sum < self.small_sample_threshold.bytes(); + bitrate_sample = 8.0 * self.sum as f64 / (rate_window_ms) as f64; + self.current_window_ms -= rate_window_ms; + self.sum = 0; + } + self.sum += bytes; + bitrate_sample + } +} 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..17e4a0b28d0 --- /dev/null +++ b/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs @@ -0,0 +1,186 @@ +/* + * 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::{ + 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. +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..2369fb41d76 --- /dev/null +++ b/rs/moq-congestion/src/gcc/delay_based_bwe.rs @@ -0,0 +1,1367 @@ +/* + * 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::{ + api::{ + transport::{BandwidthUsage, PacketResult, TransportPacketsFeedback}, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + experiments::FieldTrials, + DelayIncreaseDetectorInterface, InterArrivalDelta, TrendlineEstimator, + remote_bitrate_estimator::{AimdRateControl, RateControlInput}, +}; + +// WebRTC-Bwe-SeparateAudioPackets +#[derive(Clone, Debug)] +pub struct BweSeparateAudioPacketsSettings { + pub enabled: bool, + pub packet_threshold: i64, + pub time_threshold: TimeDelta, +} + +impl Default for BweSeparateAudioPacketsSettings { + fn default() -> Self { + Self { + enabled: false, + packet_threshold: 10, + time_threshold: TimeDelta::from_seconds(1), + } + } +} + +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, + } + } +} + +enum DelayDetector { + Video, + Audio, +} + +pub struct DelayBasedBwe { + // Alternatively, run two separate overuse detectors for audio and video, + // and fall back to the audio one if we haven't seen a video packet in a + // while. + separate_audio: BweSeparateAudioPacketsSettings, + audio_packets_since_last_video: i64, + last_video_packet_recv_time: Timestamp, + + // Unused? + //video_inter_arrival: InterArrival, + video_inter_arrival_delta: InterArrivalDelta, + video_delay_detector: TrendlineEstimator, + // Unused? + //audio_inter_arrival: InterArrival, + audio_inter_arrival_delta: InterArrivalDelta, + audio_delay_detector: TrendlineEstimator, + active_delay_detector_type: DelayDetector, + + last_seen_packet: Timestamp, + uma_recorded: bool, + 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 { + tracing::debug!( + "Initialized DelayBasedBwe with separate audio overuse detection: {:?}", + field_trials.separate_audio_packets + ); + Self { + separate_audio: field_trials.separate_audio_packets.clone(), + audio_packets_since_last_video: 0, + last_video_packet_recv_time: Timestamp::minus_infinity(), + video_delay_detector: TrendlineEstimator::new( + field_trials.trendline_estimator_settings.clone(), + ), + audio_delay_detector: TrendlineEstimator::new( + field_trials.trendline_estimator_settings.clone(), + ), + active_delay_detector_type: DelayDetector::Video, + last_seen_packet: Timestamp::minus_infinity(), + uma_recorded: false, + 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 + audio_inter_arrival_delta: InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH), + video_inter_arrival_delta: InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH), + } + } + + fn active_delay_detector_state(&self) -> BandwidthUsage { + match self.active_delay_detector_type { + DelayDetector::Audio => self.audio_delay_detector.state(), + DelayDetector::Video => self.video_delay_detector.state(), + } + } + + pub fn incoming_packet_feedback_vector( + &mut self, + msg: &TransportPacketsFeedback, + acked_bitrate: Option, + probe_bitrate: Option, + //network_estimate: Option, + in_alr: bool, + ) -> DelayBasedBweResult { + let packet_feedback_vector = msg.sorted_by_receive_time(); + // 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::warn!("Very late feedback received."); + return DelayBasedBweResult::default(); + } + + if !self.uma_recorded { + self.uma_recorded = true; + } + 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.rate_control.SetNetworkStateEstimate(network_estimate); + self.maybe_update_estimate( + acked_bitrate, + probe_bitrate, + //network_estimate, + 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); + } + + pub fn get_expected_bwe_period(&self) -> TimeDelta { + self.rate_control.get_expected_bandwidth_period() + } + + /* 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.audio_inter_arrival_delta = InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH); + + self.video_delay_detector = TrendlineEstimator::default(); + self.audio_delay_detector = TrendlineEstimator::default(); + self.active_delay_detector_type = DelayDetector::Video; + } + self.last_seen_packet = at_time; + + // As an alternative to ignoring small packets, we can separate audio and + // video packets for overuse detection. + let mut delay_detector_for_packet: DelayDetector = DelayDetector::Video; + if self.separate_audio.enabled { + if packet_feedback.sent_packet.audio { + delay_detector_for_packet = DelayDetector::Audio; + self.audio_packets_since_last_video += 1; + if self.audio_packets_since_last_video > self.separate_audio.packet_threshold + && packet_feedback.receive_time - self.last_video_packet_recv_time + > self.separate_audio.time_threshold + { + self.active_delay_detector_type = DelayDetector::Audio; + } + } else { + self.audio_packets_since_last_video = 0; + self.last_video_packet_recv_time = std::cmp::max( + self.last_video_packet_recv_time, + packet_feedback.receive_time, + ); + self.active_delay_detector_type = DelayDetector::Video; + } + } + 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 inter_arrival_for_packet: &mut InterArrivalDelta = + if self.separate_audio.enabled && packet_feedback.sent_packet.audio { + &mut self.audio_inter_arrival_delta + } else { + &mut self.video_inter_arrival_delta + }; + let calculated_deltas: bool = inter_arrival_for_packet.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, + ); + + match delay_detector_for_packet { + DelayDetector::Audio => self.audio_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, + ), + DelayDetector::Video => 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, + //_state_estimate: 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::api::transport::{PacedPacketInfo, SentPacket}; + use crate::{ + AcknowledgedBitrateEstimator, AcknowledgedBitrateEstimatorInterface, ProbeBitrateEstimator, + RobustThroughputEstimatorSettings, + }; + use approx::assert_relative_eq; + + use test_trace::test; + + 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, + next_sequence_number: &mut 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 _); + packet.sent_packet.sequence_number = *next_sequence_number; + *next_sequence_number += 1; + 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, + next_sequence_number: &mut 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, next_sequence_number, 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: Box, + probe_bitrate_estimator: ProbeBitrateEstimator, + bitrate_estimator: DelayBasedBwe, + stream_generator: StreamGenerator, + arrival_time_offset_ms: i64, + next_sequence_number: i64, + first_update: bool, + } + + impl DelayBasedBweTest { + pub fn new() -> Self { + let field_trials = FieldTrials { + robust_throughput_estimator_settings: RobustThroughputEstimatorSettings { + enabled: true, + ..Default::default() + }, + ..Default::default() + }; + let clock = Timestamp::from_micros(100000000); + + Self { + acknowledged_bitrate_estimator: AcknowledgedBitrateEstimator::create(&field_trials), + probe_bitrate_estimator: ProbeBitrateEstimator::default(), + bitrate_estimator: DelayBasedBwe::new(&field_trials), + stream_generator: StreamGenerator::new( + 1000000, // Capacity. + clock.us(), + ), + arrival_time_offset_ms: 0, + next_sequence_number: 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, + sent_packet: SentPacket { + send_time, + size: DataSize::from_bytes(payload_size as _), + pacing_info, + sequence_number: self.next_sequence_number, + ..Default::default() + }, + ..Default::default() + }; + + self.next_sequence_number += 1; + 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() + }; + self.acknowledged_bitrate_estimator + .incoming_packet_feedback(&msg.sorted_by_receive_time()); + let result = self.bitrate_estimator.incoming_packet_feedback_vector( + &msg, + 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 self.next_sequence_number, + &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 result: DelayBasedBweResult = + self.bitrate_estimator.incoming_packet_feedback_vector( + &msg, + 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 get_expected_bwe_period_ms() { + let mut bwe = DelayBasedBweTest::new(); + let default_interval = bwe.bitrate_estimator.get_expected_bwe_period(); + assert!(default_interval.ms() > 0); + bwe.capacity_drop_test_helper(1, true, 533, 0); + let interval = bwe.bitrate_estimator.get_expected_bwe_period(); + assert!(interval.ms() > 0); + assert_ne!(interval.ms(), default_interval.ms()); + } + + #[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..3b1ecc4842b --- /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::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/alr_experiment.rs b/rs/moq-congestion/src/gcc/experiments/alr_experiment.rs new file mode 100644 index 00000000000..d9cf6087143 --- /dev/null +++ b/rs/moq-congestion/src/gcc/experiments/alr_experiment.rs @@ -0,0 +1,27 @@ +/// WebRTC-ProbingScreenshareBwe +// Apparently these are now unconfigurable constants? +#[derive(Clone, Debug)] +pub struct AlrExperimentSettings { + pub pacing_factor: f64, + pub max_paced_queue_time: i64, + pub alr_bandwidth_usage_percent: i64, + pub alr_start_budget_level_percent: i64, + pub alr_stop_budget_level_percent: i64, + /// Will be sent to the receive side for stats slicing. + /// Can be 0..6, because it's sent as a 3 bits value and there's also + /// reserved value to indicate absence of experiment. + pub group_id: i64, +} + +impl Default for AlrExperimentSettings { + fn default() -> Self { + Self { + pacing_factor: 1.0, + max_paced_queue_time: 2875, + alr_bandwidth_usage_percent: 80, + alr_start_budget_level_percent: 40, + alr_stop_budget_level_percent: -60, + group_id: 3, + } + } +} 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..6df425f3a2a --- /dev/null +++ b/rs/moq-congestion/src/gcc/experiments/field_trials.rs @@ -0,0 +1,130 @@ +use super::{AlrExperimentSettings, CongestionWindowConfig, VideoRateControlConfig}; + +use crate::{ + AlrDetectorConfig, BitrateEstimatorConfig, BweLossExperiment, + BweSeparateAudioPacketsSettings, LossBasedBweV2Config, LossBasedControlConfig, + ProbeControllerConfig, RobustThroughputEstimatorSettings, RttBasedBackoffConfig, + SafeResetOnRouteChange, TrendlineEstimatorSettings, + remote_bitrate_estimator::{BweBackOffFactor, EstimateBoundedIncrease}, +}; + +/// Current field trials for WebRTC that impact GoogCC. +/// +/// These have been painstakingly extracted from the WebRTC source code. +/// Normally these field trials are enabled via bespoke string parsing, but that is very difficult to port correctly. +/// I decided to make everything type safe which doubles as documentation; ur welcome. +#[derive(Clone, Debug)] +pub struct FieldTrials { + /// WebRTC-Bwe-RobustThroughputEstimatorSettings + pub robust_throughput_estimator_settings: RobustThroughputEstimatorSettings, + + // Probing settings. + /// WebRTC-Bwe-ProbingConfiguration + pub probing_configuration: ProbeControllerConfig, + + // Use probing to recover faster after large bitrate estimate drops. + /// WebRTC-BweRapidRecoveryExperiment + pub rapid_recovery_experiment: bool, + + /// WebRTC-AlrDetectorParameters + pub alr_detector_parameters: AlrDetectorConfig, + + /// WebRTC-ProbingScreenshareBwe or WebRTC-StrictPacingAndProbing (legacy?) + pub alr_experiment_settings: AlrExperimentSettings, + + /// WebRTC-AddPacingToCongestionWindowPushback + pub add_pacing_to_congestion_window_pushback: bool, + + /// WebRTC-CongestionWindow + pub congestion_window: CongestionWindowConfig, + + /// WebRTC-VideoRateControl + pub video_rate_control: VideoRateControlConfig, + + /// WebRTC-UseBaseHeavyVP8TL3RateAllocation + pub vp8_base_heavy_tl3_alloc: bool, + + /// WebRTC-Bwe-SeparateAudioPackets + pub separate_audio_packets: BweSeparateAudioPacketsSettings, + + /// WebRTC-Bwe-LossBasedControl + pub loss_based_control: LossBasedControlConfig, + + /// LossBasedBweV2Config + 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-EstimateBoundedIncrease + pub estimate_bounded_increase: EstimateBoundedIncrease, + + /// WebRTC-Bwe-MinAllocAsLowerBound + pub min_alloc_as_lower_bound: bool, + + /// WebRTC-Bwe-IgnoreProbesLowerThanNetworkStateEstimate + pub ignore_probes_lower_than_network_state_estimate: bool, + + /// WebRTC-Bwe-LimitProbesLowerThanThroughputEstimate + pub limit_probes_lower_than_throughput_estimate: bool, + + /// WebRTC-Bwe-PaceAtMaxOfBweAndLowerLinkCapacity + pub pace_at_max_of_bwe_and_lower_link_capacity: bool, + + /// WebRTC-Bwe-LimitPacingFactorByUpperLinkCapacityEstimate + pub limit_pacing_factor_by_upper_link_capacity_estimate: bool, + + /// WebRTC-Bwe-SafeResetOnRouteChange + pub safe_reset_on_route_change: SafeResetOnRouteChange, + + /// WebRTC-BweThroughputWindowConfig + pub bwe_throughput_window_config: BitrateEstimatorConfig, + + /// WebRTC-BweLossExperiment + pub loss_experiment: BweLossExperiment, + + /// WebRTC-Bwe-ReceiverLimitCapsOnly + pub receiver_limit_caps_only: bool, +} + +impl Default for FieldTrials { + fn default() -> Self { + Self { + robust_throughput_estimator_settings: RobustThroughputEstimatorSettings::default(), + probing_configuration: ProbeControllerConfig::default(), + rapid_recovery_experiment: false, + alr_detector_parameters: AlrDetectorConfig::default(), + alr_experiment_settings: AlrExperimentSettings::default(), + add_pacing_to_congestion_window_pushback: false, + congestion_window: CongestionWindowConfig::default(), + video_rate_control: VideoRateControlConfig::default(), + vp8_base_heavy_tl3_alloc: false, + separate_audio_packets: BweSeparateAudioPacketsSettings::default(), + loss_based_control: LossBasedControlConfig::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, + estimate_bounded_increase: EstimateBoundedIncrease::default(), + min_alloc_as_lower_bound: true, + ignore_probes_lower_than_network_state_estimate: true, + limit_probes_lower_than_throughput_estimate: true, + pace_at_max_of_bwe_and_lower_link_capacity: false, + limit_pacing_factor_by_upper_link_capacity_estimate: false, + safe_reset_on_route_change: SafeResetOnRouteChange::default(), + bwe_throughput_window_config: BitrateEstimatorConfig::default(), + loss_experiment: BweLossExperiment::default(), + receiver_limit_caps_only: false, + } + } +} 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..95e8e7d189a --- /dev/null +++ b/rs/moq-congestion/src/gcc/experiments/mod.rs @@ -0,0 +1,7 @@ +mod alr_experiment; +mod field_trials; +mod rate_control; + +pub use alr_experiment::*; +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..162eea29ed1 --- /dev/null +++ b/rs/moq-congestion/src/gcc/experiments/rate_control.rs @@ -0,0 +1,168 @@ +/* + * 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 crate::{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, + } + } +} + +/// WebRTC-VideoRateControl +#[derive(Clone, Debug)] +pub struct VideoRateControlConfig { + pub pacing_factor: Option, + pub alr_probing: bool, + pub vp8_qp_max: Option, + pub vp8_min_pixels: Option, + pub trust_vp8: bool, // = true + pub trust_vp9: bool, // = true; + pub bitrate_adjuster: bool, // = true; + pub adjuster_use_headroom: bool, // = true; + pub vp8_s0_boost: bool, + pub vp8_base_heavy_tl3_alloc: bool, +} + +impl Default for VideoRateControlConfig { + fn default() -> Self { + Self { + pacing_factor: None, + alr_probing: false, + vp8_qp_max: None, + vp8_min_pixels: None, + trust_vp8: true, + trust_vp9: true, + bitrate_adjuster: true, + adjuster_use_headroom: true, + vp8_s0_boost: false, + vp8_base_heavy_tl3_alloc: false, + } + } +} + +#[derive(Default)] +pub struct RateControlSettings { + congestion_window_config: CongestionWindowConfig, + video_config: VideoRateControlConfig, +} + +impl RateControlSettings { + // const char kCongestionWindowDefaultFieldTrialString[] = + //"QueueSize:350,MinBitrate:30000,DropFrame:true"; + //const char kUseBaseHeavyVp8Tl3RateAllocationFieldTrialName[] = + // "WebRTC-UseBaseHeavyVP8TL3RateAllocation"; + + pub fn new(field_trials: &FieldTrials) -> Self { + let congestion_window_config = field_trials.congestion_window.clone(); + let mut video_config = field_trials.video_rate_control.clone(); + video_config.vp8_base_heavy_tl3_alloc = + video_config.vp8_base_heavy_tl3_alloc || field_trials.vp8_base_heavy_tl3_alloc; + Self { + congestion_window_config, + video_config, + } + } + + /// 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 + } + + pub fn get_pacing_factor(&self) -> Option { + self.video_config.pacing_factor + } + pub fn use_alr_probing(&self) -> bool { + self.video_config.alr_probing + } + + pub fn libvpx_vp8_qp_max(&self) -> Option { + if let Some(vp8_qp_max) = self.video_config.vp8_qp_max { + if !(0..=63).contains(&vp8_qp_max) { + tracing::warn!("Unsupported vp8_qp_max_ value, ignored."); + return None; + } + } + self.video_config.vp8_qp_max + } + pub fn libvpx_vp8_min_pixels(&self) -> Option { + if let Some(vp8_min_pixels) = self.video_config.vp8_min_pixels { + if vp8_min_pixels < 1 { + tracing::warn!("Unsupported vp8_min_pixels_ value, ignored."); + return None; + } + } + self.video_config.vp8_min_pixels + } + pub fn libvpx_vp8_trusted_rate_controller(&self) -> bool { + self.video_config.trust_vp8 + } + pub fn vp8_boost_base_layer_quality(&self) -> bool { + self.video_config.vp8_s0_boost + } + pub fn vp8_dynamic_rate_settings(&self) -> bool { + todo!(); + } + pub fn libvpx_vp9_trusted_rate_controller(&self) -> bool { + self.video_config.trust_vp9 + } + pub fn vp9_dynamic_rate_settings(&self) -> bool { + todo!(); + } + + pub fn vp8_base_heavy_tl3_rate_allocation(&self) -> bool { + self.video_config.vp8_base_heavy_tl3_alloc + } + + pub fn use_encoder_bitrate_adjuster(&self) -> bool { + self.video_config.bitrate_adjuster + } + pub fn bitrate_adjuster_can_use_network_headroom(&self) -> bool { + self.video_config.adjuster_use_headroom + } +} 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..b76d3b930ef --- /dev/null +++ b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs @@ -0,0 +1,2174 @@ +/* + * 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 std::collections::VecDeque; + +use crate::{ + api::{ + network_control::{NetworkControllerConfig, NetworkControllerInterface}, + transport::{ + BandwidthUsage, NetworkAvailability, NetworkControlUpdate, NetworkEstimate, + NetworkRouteChange, NetworkStateEstimate, PacedPacketInfo, PacerConfig, + ProbeClusterConfig, ProcessInterval, ReceivedPacket, RemoteBitrateReport, + RoundTripTimeUpdate, SentPacket, StreamsConfig, TargetRateConstraints, + TargetTransferRate, TransportLossReport, TransportPacketsFeedback, + }, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + experiments::{FieldTrials, RateControlSettings}, + AcknowledgedBitrateEstimator, + remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE, +}; + +use super::{ + AcknowledgedBitrateEstimatorInterface, AlrDetector, BandwidthLimitedCause, + CongestionWindowPushbackController, DelayBasedBwe, DelayBasedBweResult, LossBasedState, + ProbeBitrateEstimator, ProbeController, SendSideBandwidthEstimation, +}; + +#[derive(Clone, Debug)] +pub struct SafeResetOnRouteChange { + pub enabled: bool, // Enabled + pub ack: bool, // ack +} + +impl Default for SafeResetOnRouteChange { + fn default() -> Self { + Self { + enabled: true, + ack: true, + } + } +} + +/// Google Congestion Control specific configuration. +/// +/// NOTE: NetworkStateEstimator and NetworkStatePredictor have not been ported. +/// These are optional traits and do not seem to be implemented, except perhaps by Google internally. +#[derive(Clone, Debug, Default)] +pub struct GoogCcConfig { + // network_state_estimator: Box, + // network_state_predictor: Box, + pub feedback_only: bool, +} + +/// Google Congestion Control. +/// +/// This is the congestion controller used within WebRTC. +/// It has been extracted from the libwebrtc source code and ported to Rust to avoid linking headaches. +/// Use the [NetworkControllerInterface] to interact with this controller. +pub struct GoogCcNetworkController { + field_trials: FieldTrials, + + packet_feedback_only: bool, + safe_reset_on_route_change: bool, + safe_reset_acknowledged_rate: bool, + use_min_allocatable_as_lower_bound: bool, + ignore_probes_lower_than_network_estimate: bool, + limit_probes_lower_than_throughput_estimate: bool, + rate_control_settings: RateControlSettings, + pace_at_max_of_bwe_and_lower_link_capacity: bool, + limit_pacingfactor_by_upper_link_capacity_estimate: bool, + + 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: Box, + + initial_config: Option, + + min_target_rate: DataRate, + min_data_rate: DataRate, + max_data_rate: DataRate, // = DataRate::plus_infinity(); + starting_rate: Option, + + first_packet_sent: bool, + + estimate: Option, + + next_loss_update: Timestamp, // = Timestamp::minus_infinity(); + lost_packets_since_last_loss_update: i64, + expected_packets_since_last_loss_update: i64, + + feedback_max_rtts: VecDeque, + + last_loss_based_target_rate: DataRate, + last_pushback_target_rate: DataRate, + last_stable_target_rate: DataRate, + last_loss_base_state: LossBasedState, + + last_estimated_fraction_loss: u8, + last_estimated_round_trip_time: TimeDelta, //= TimeDelta::plus_infinity(); + + pacing_factor: f64, + min_total_allocated_bitrate: DataRate, + max_padding_rate: DataRate, + + previously_in_alr: bool, + + current_data_window: Option, +} + +impl GoogCcNetworkController { + // From RTCPSender video report interval. + const LOSS_UPDATE_INTERVAL: TimeDelta = TimeDelta::from_millis(1000); + + // 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 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, goog_cc_config: GoogCcConfig) -> Self { + let field_trials = config.field_trials.clone(); + let rate_control_settings = RateControlSettings::new(&field_trials); + + assert!(config.constraints.at_time.is_finite()); + let mut delay_based_bwe = DelayBasedBwe::new(&field_trials); + delay_based_bwe.set_min_bitrate(CONGESTION_CONTROLLER_MIN_BITRATE); + + Self { + packet_feedback_only: goog_cc_config.feedback_only, + safe_reset_on_route_change: field_trials.safe_reset_on_route_change.enabled, + safe_reset_acknowledged_rate: field_trials.safe_reset_on_route_change.ack, + use_min_allocatable_as_lower_bound: field_trials.min_alloc_as_lower_bound, + ignore_probes_lower_than_network_estimate: field_trials + .ignore_probes_lower_than_network_state_estimate, + limit_probes_lower_than_throughput_estimate: field_trials + .limit_probes_lower_than_throughput_estimate, + pace_at_max_of_bwe_and_lower_link_capacity: field_trials + .pace_at_max_of_bwe_and_lower_link_capacity, + limit_pacingfactor_by_upper_link_capacity_estimate: field_trials + .limit_pacing_factor_by_upper_link_capacity_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.clone()), + alr_detector: AlrDetector::new(field_trials.alr_detector_parameters.clone()), + probe_bitrate_estimator: ProbeBitrateEstimator::default(), + delay_based_bwe, + acknowledged_bitrate_estimator: AcknowledgedBitrateEstimator::create(&field_trials), + + 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, + estimate: None, + next_loss_update: Timestamp::minus_infinity(), + lost_packets_since_last_loss_update: 0, + expected_packets_since_last_loss_update: 0, + feedback_max_rtts: VecDeque::new(), + last_loss_based_target_rate: config.constraints.starting_rate.unwrap(), + last_pushback_target_rate: config.constraints.starting_rate.unwrap(), + last_stable_target_rate: config.constraints.starting_rate.unwrap(), + last_loss_base_state: LossBasedState::DelayBasedEstimate, + last_estimated_fraction_loss: 0, + last_estimated_round_trip_time: TimeDelta::plus_infinity(), + pacing_factor: config + .stream_based_config + .pacing_factor + .unwrap_or(Self::DEFAULT_PACE_MULTIPLIER), + min_total_allocated_bitrate: config + .stream_based_config + .min_total_allocated_bitrate + .unwrap_or(DataRate::zero()), + + max_padding_rate: config + .stream_based_config + .max_padding_rate + .unwrap_or(DataRate::zero()), + previously_in_alr: false, + current_data_window: None, + + initial_config: Some(config), + field_trials, + } + } + + pub fn get_network_state(&self, at_time: Timestamp) -> NetworkControlUpdate { + NetworkControlUpdate { + target_rate: Some(TargetTransferRate { + network_estimate: NetworkEstimate { + at_time, + loss_rate_ratio: self.last_estimated_fraction_loss as f32 / 255.0, + round_trip_time: self.last_estimated_round_trip_time, + bwe_period: self.delay_based_bwe.get_expected_bwe_period(), + ..Default::default() + }, + at_time, + target_rate: self.last_pushback_target_rate, + stable_target_rate: self.bandwidth_estimation.get_estimated_link_capacity(), + ..Default::default() + }), + pacer_config: Some(self.get_pacing_rates(at_time)), + congestion_window: self.current_data_window, + ..Default::default() + } + } + + 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 bitrate smaller than min bitrate"); + self.max_data_rate = self.min_data_rate; + } + match self.starting_rate { + Some(starting_rate) if starting_rate < self.min_data_rate => { + tracing::warn!("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 fraction_loss: u8 = self.bandwidth_estimation.fraction_loss(); + 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; + + let mut cwnd_reduce_ratio: f64 = 0.0; + 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); + if self + .rate_control_settings + .use_congestion_window_drop_frame_only() + { + cwnd_reduce_ratio = (loss_based_target_rate.bps_float() + - pushback_target_rate.bps_float()) + / loss_based_target_rate.bps_float(); + } + } + let mut stable_target_rate: DataRate = + self.bandwidth_estimation.get_estimated_link_capacity(); + stable_target_rate = std::cmp::min(stable_target_rate, pushback_target_rate); + + if (loss_based_target_rate != self.last_loss_based_target_rate) + || (loss_based_state != self.last_loss_base_state) + || (fraction_loss != self.last_estimated_fraction_loss) + || (round_trip_time != self.last_estimated_round_trip_time) + || (pushback_target_rate != self.last_pushback_target_rate) + || (stable_target_rate != self.last_stable_target_rate) + { + self.last_loss_based_target_rate = loss_based_target_rate; + self.last_pushback_target_rate = pushback_target_rate; + self.last_estimated_fraction_loss = fraction_loss; + self.last_estimated_round_trip_time = round_trip_time; + self.last_stable_target_rate = stable_target_rate; + self.last_loss_base_state = loss_based_state; + + self.alr_detector + .set_estimated_bitrate(loss_based_target_rate.bps()); + + let bwe_period: TimeDelta = self.delay_based_bwe.get_expected_bwe_period(); + + let mut target_rate_msg = TargetTransferRate { + at_time, + stable_target_rate, + network_estimate: NetworkEstimate { + at_time, + round_trip_time, + loss_rate_ratio: fraction_loss as f32 / 255.0, + bwe_period, + ..Default::default() + }, + ..Default::default() + }; + + if self + .rate_control_settings + .use_congestion_window_drop_frame_only() + { + target_rate_msg.target_rate = loss_based_target_rate; + target_rate_msg.cwnd_reduce_ratio = cwnd_reduce_ratio; + } else { + target_rate_msg.target_rate = pushback_target_rate; + } + + update.target_rate = Some(target_rate_msg); + + 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(at_time)); + tracing::trace!( + "bwe {} pushback_target_bps={} estimate_bps={}", + at_time.ms(), + self.last_pushback_target_rate.bps(), + loss_based_target_rate.bps() + ); + } + } + + fn get_pacing_rates(&self, at_time: Timestamp) -> 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 mut pacing_rate = match self.estimate { + Some(estimate) + if self.pace_at_max_of_bwe_and_lower_link_capacity + && !self.bandwidth_estimation.pace_at_loss_based_estimate() => + { + *[ + self.min_total_allocated_bitrate, + estimate.link_capacity_lower, + self.last_loss_based_target_rate, + ] + .iter() + .max() + .unwrap() + * self.pacing_factor + } + _ => { + self.min_total_allocated_bitrate + .max(self.last_loss_based_target_rate) + * self.pacing_factor + } + }; + + match self.estimate { + Some(estimate) + if self.limit_pacingfactor_by_upper_link_capacity_estimate + && estimate.link_capacity_upper.is_finite() + && pacing_rate > estimate.link_capacity_upper => + { + pacing_rate = *[ + estimate.link_capacity_upper, + self.min_total_allocated_bitrate, + self.last_loss_based_target_rate, + ] + .iter() + .max() + .unwrap() + } + _ => {} + }; + + let mut padding_rate: DataRate = + if self.last_loss_base_state == LossBasedState::IncreaseUsingPadding { + self.max_padding_rate.max(self.last_loss_based_target_rate) + } else { + self.max_padding_rate + }; + padding_rate = std::cmp::min(padding_rate, self.last_pushback_target_rate); + let mut msg = PacerConfig::default(); + msg.at_time = at_time; + msg.time_window = TimeDelta::from_seconds(1); + msg.data_window = pacing_rate * msg.time_window; + msg.pad_window = padding_rate * msg.time_window; + msg + } + + fn set_network_state_estimate(&mut self, estimate: Option) { + let prev_estimate = self.estimate; + self.estimate = estimate; + + match (self.estimate, prev_estimate) { + (Some(estimate), Some(prev_estimate)) + if estimate.update_time == prev_estimate.update_time => {} + (Some(estimate), _) => self.probe_controller.set_network_state_estimate(estimate), + _ => {} + }; + } + + fn update_congestion_window_size(&mut self) { + let min_feedback_max_rtt: TimeDelta = + TimeDelta::from_millis(*self.feedback_max_rtts.iter().min().unwrap()); + + 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); + } +} + +impl NetworkControllerInterface for GoogCcNetworkController { + fn on_network_availability(&mut self, msg: NetworkAvailability) -> NetworkControlUpdate { + NetworkControlUpdate { + probe_cluster_configs: self.probe_controller.on_network_availability(msg), + ..Default::default() + } + } + + fn on_network_route_change(&mut self, mut msg: NetworkRouteChange) -> NetworkControlUpdate { + if self.safe_reset_on_route_change { + let mut estimated_bitrate: Option; + if self.safe_reset_acknowledged_rate { + estimated_bitrate = self.acknowledged_bitrate_estimator.bitrate(); + if estimated_bitrate.is_none() { + estimated_bitrate = self.acknowledged_bitrate_estimator.peek_rate(); + } + } else { + estimated_bitrate = Some(self.bandwidth_estimation.target_rate()); + } + if let Some(estimated_bitrate) = estimated_bitrate { + msg.constraints.starting_rate = Some(match msg.constraints.starting_rate { + Some(starting_rate) => starting_rate.min(estimated_bitrate), + None => estimated_bitrate, + }); + } + } + + self.acknowledged_bitrate_estimator = + AcknowledgedBitrateEstimator::create(&self.field_trials); + self.probe_bitrate_estimator = ProbeBitrateEstimator::default(); + self.delay_based_bwe = DelayBasedBwe::new(&self.field_trials); + self.bandwidth_estimation.on_route_change(); + self.probe_controller.reset(msg.at_time); + let mut update = NetworkControlUpdate { + probe_cluster_configs: self.reset_constraints(msg.constraints), + ..Default::default() + }; + self.maybe_trigger_on_network_changed(&mut update, msg.at_time); + update + } + + 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(msg.at_time)); + + if let Some(requests_alr_probing) = + initial_config.stream_based_config.requests_alr_probing + { + self.probe_controller + .enable_periodic_alr_probing(requests_alr_probing); + } + if let Some(enable_repeated_initial_probing) = initial_config + .stream_based_config + .enable_repeated_initial_probing + { + self.probe_controller + .enable_repeated_initial_probing(enable_repeated_initial_probing); + } + let total_bitrate: Option = initial_config + .stream_based_config + .max_total_allocated_bitrate; + if let Some(total_bitrate) = total_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); + let start_time_ms: Option = self + .alr_detector + .get_application_limited_region_start_time(); + self.probe_controller.set_alr_start_time_ms(start_time_ms); + + 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 + .update_outstanding_data(current_data_window.bytes()) + } + _ => update.congestion_window = self.current_data_window, + }; + + self.maybe_trigger_on_network_changed(&mut update, msg.at_time); + update + } + + fn on_remote_bitrate_report(&mut self, msg: RemoteBitrateReport) -> NetworkControlUpdate { + if self.packet_feedback_only { + tracing::error!("Received REMB for packet feedback only GoogCC"); + return NetworkControlUpdate::default(); + } + self.bandwidth_estimation + .update_receiver_estimate(msg.receive_time, msg.bandwidth); + NetworkControlUpdate::default() + } + + fn on_round_trip_time_update(&mut self, msg: RoundTripTimeUpdate) -> NetworkControlUpdate { + if self.packet_feedback_only || msg.smoothed { + return NetworkControlUpdate::default(); + } + 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, msg.receive_time); + NetworkControlUpdate::default() + } + + fn on_sent_packet(&mut self, sent_packet: SentPacket) -> NetworkControlUpdate { + self.alr_detector + .on_bytes_sent(sent_packet.size.bytes() as _, sent_packet.send_time.ms()); + self.acknowledged_bitrate_estimator.set_alr( + self.alr_detector + .get_application_limited_region_start_time() + .is_some(), + ); + + 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() + } + } + + fn on_received_packet(&mut self, _: ReceivedPacket) -> NetworkControlUpdate { + NetworkControlUpdate::default() + } + + 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: bool = false; + match msg.pacing_factor { + Some(pacing_factor) if pacing_factor != self.pacing_factor => { + self.pacing_factor = pacing_factor; + pacing_changed = true; + } + _ => {} + }; + match msg.min_total_allocated_bitrate { + Some(min_total_allocated_bitrate) + if 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); + } + } + _ => {} + }; + + match msg.max_padding_rate { + Some(max_padding_rate) if max_padding_rate != self.max_padding_rate => { + self.max_padding_rate = max_padding_rate; + pacing_changed = true; + } + _ => {} + }; + + if pacing_changed { + update.pacer_config = Some(self.get_pacing_rates(msg.at_time)); + } + update + } + + fn on_target_rate_constraints( + &mut self, + constraints: TargetRateConstraints, + ) -> NetworkControlUpdate { + let mut update = NetworkControlUpdate { + probe_cluster_configs: self.reset_constraints(constraints), + ..Default::default() + }; + self.maybe_trigger_on_network_changed(&mut update, constraints.at_time); + update + } + + fn on_transport_loss_report(&mut self, msg: TransportLossReport) -> NetworkControlUpdate { + if self.packet_feedback_only { + return NetworkControlUpdate::default(); + } + let total_packets_delta: i64 = + msg.packets_received_delta as i64 + msg.packets_lost_delta as i64; + self.bandwidth_estimation.update_packets_lost( + msg.packets_lost_delta as _, + total_packets_delta, + msg.receive_time, + ); + NetworkControlUpdate::default() + } + + 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(); + } + + 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(); + + 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); + } + if self.packet_feedback_only { + if !self.feedback_max_rtts.is_empty() { + let sum_rtt_ms: i64 = self.feedback_max_rtts.iter().sum(); + let mean_rtt_ms: i64 = sum_rtt_ms / self.feedback_max_rtts.len() as i64; + self.delay_based_bwe + .on_rtt_update(TimeDelta::from_millis(mean_rtt_ms)); + } + + let mut feedback_min_rtt: TimeDelta = TimeDelta::plus_infinity(); + for packet_feedback in report.received_with_send_info() { + let pending_time: TimeDelta = max_recv_time - packet_feedback.receive_time; + let rtt: TimeDelta = + report.feedback_time - packet_feedback.sent_packet.send_time - pending_time; + // Value used for predicting NACK round trip time in FEC controller. + feedback_min_rtt = std::cmp::min(rtt, feedback_min_rtt); + } + if feedback_min_rtt.is_finite() { + self.bandwidth_estimation + .update_rtt(feedback_min_rtt, report.feedback_time); + } + + self.expected_packets_since_last_loss_update += + report.packets_with_feedback().len() as i64; + for packet_feedback in report.packets_with_feedback() { + if !packet_feedback.is_received() { + self.lost_packets_since_last_loss_update += 1; + } + } + if report.feedback_time > self.next_loss_update { + self.next_loss_update = report.feedback_time + Self::LOSS_UPDATE_INTERVAL; + self.bandwidth_estimation.update_packets_lost( + self.lost_packets_since_last_loss_update, + self.expected_packets_since_last_loss_update, + report.feedback_time, + ); + self.expected_packets_since_last_loss_update = 0; + self.lost_packets_since_last_loss_update = 0; + } + } + let alr_start_time: Option = self + .alr_detector + .get_application_limited_region_start_time(); + + if self.previously_in_alr && alr_start_time.is_none() { + let now_ms: i64 = report.feedback_time.ms(); + self.acknowledged_bitrate_estimator + .set_alr_ended_time(report.feedback_time); + self.probe_controller.set_alr_ended_time_ms(now_ms); + } + self.previously_in_alr = alr_start_time.is_some(); + self.acknowledged_bitrate_estimator + .incoming_packet_feedback(&report.sorted_by_receive_time()); + let acknowledged_bitrate = self.acknowledged_bitrate_estimator.bitrate(); + self.bandwidth_estimation + .set_acknowledged_rate(acknowledged_bitrate, report.feedback_time); + for feedback in report.sorted_by_receive_time() { + 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(); + match (probe_bitrate, self.estimate) { + (Some(current), Some(estimate)) + if self.ignore_probes_lower_than_network_estimate + && current < self.delay_based_bwe.last_estimate() + && current < estimate.link_capacity_lower => + { + probe_bitrate = None; + } + _ => {} + }; + + match (probe_bitrate, acknowledged_bitrate) { + (Some(probe), Some(acknowledged)) + if 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, + acknowledged_bitrate, + probe_bitrate, /*self.estimate,*/ + 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); + } + + let recovered_from_overuse = result.recovered_from_overuse; + + if recovered_from_overuse { + self.probe_controller.set_alr_start_time_ms(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(); + } + if let (Some(congestion_window_pushback_controller), Some(current_data_window)) = ( + &mut self.congestion_window_pushback_controller, + self.current_data_window, + ) { + congestion_window_pushback_controller.set_data_window(current_data_window); + } else { + update.congestion_window = self.current_data_window; + } + + update + } + + fn on_network_state_estimate( + &mut self, + estimate: NetworkStateEstimate, + ) -> NetworkControlUpdate { + self.set_network_state_estimate(Some(estimate)); + NetworkControlUpdate::default() + } + + fn get_process_interval() -> TimeDelta { + TimeDelta::from_millis(25) + } +} + +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, + LossBasedState::IncreaseUsingPadding => + // Probes may not be sent in this state. + { + BandwidthLimitedCause::LossLimitedBwe + } + LossBasedState::Increasing => + // Probes may be sent in this state. + { + BandwidthLimitedCause::LossLimitedBweIncreasing + } + LossBasedState::DelayBasedEstimate => BandwidthLimitedCause::DelayBasedLimited, + } +} + +#[cfg(test)] +mod test { + use approx::assert_relative_eq; + use test_trace::test; + + use crate::api::transport::PacketResult; + + use super::*; + + // Count dips from a constant high bandwidth level within a short window. + /* + fn count_bandwidth_dips(mut bandwidth_history: VecDeque, threshold: DataRate) -> i64 { + if bandwidth_history.is_empty() { + return 1; + } + let first = bandwidth_history.pop_front().unwrap(); + + let mut dips: i64 = 0; + let mut state_high: bool = true; + while let Some(front) = bandwidth_history.pop_front() { + if front + threshold < first && state_high { + dips += 1; + state_high = false; + } else if front == first { + state_high = true; + } else if front > first { + // If this is toggling we will catch it later when front becomes first. + state_high = false; + } + } + dips + } + */ + + 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; + + /* + fn CreateVideoSendingClient( + s: &Scenerio, + config: CallClientConfig, + send_link: Vec, + return_link: Vec) -> CallClient { + let client = s.CreateClient("send", config); + let route = s.CreateRoutes(client, send_link, + s.CreateClient("return", CallClientConfig()), + return_link); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + return client; + } + */ + + fn create_route_change( + time: Timestamp, + start_rate: Option, + min_rate: Option, + max_rate: Option, + ) -> NetworkRouteChange { + NetworkRouteChange { + at_time: time, + constraints: TargetRateConstraints { + at_time: time, + starting_rate: start_rate, + min_data_rate: min_rate, + max_data_rate: max_rate, + }, + } + } + + fn create_packet_result( + arrival_time: Timestamp, + send_time: Timestamp, + payload_size: usize, + pacing_info: PacedPacketInfo, + ) -> PacketResult { + PacketResult { + sent_packet: SentPacket { + send_time, + size: DataSize::from_bytes(payload_size as _), + pacing_info, + ..Default::default() + }, + receive_time: arrival_time, + ..Default::default() + } + } + + // Simulate sending packets and receiving transport feedback during + // `runtime_ms`, then return the final target birate. + fn packet_transmission_and_feedback_block( + controller: &mut T, + runtime_ms: i64, + delay: i64, + current_time: &mut Timestamp, + ) -> Option { + let mut update: NetworkControlUpdate; + 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: usize = 1000; + let packet: PacketResult = create_packet_result( + *current_time + TimeDelta::from_millis(delay_buildup), + *current_time, + PAYLOAD_SIZE, + PacedPacketInfo::default(), + ); + delay_buildup += delay; + 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() + }; + 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); + 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: usize = 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 + } + + // Scenarios: + + /* + fn UpdatesTargetRateBasedOnLinkCapacity(test_name: &str) { + let factory = CreateFeedbackOnlyFactory(); + let s = Scenario::new("googcc_unit/target_capacity" + std::string(test_name), false); + let mut config: CallClientConfig::default(); + config.transport.cc_factory = &factory; + config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); + config.transport.rates.max_rate = DataRate::KilobitsPerSec(1500); + config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); + let send_net = s.CreateMutableSimulationNode(|c: &mut NetworkSimulationConfig| { + c.bandwidth = DataRate::KilobitsPerSec(500); + c.delay = TimeDelta::Millis(100); + c.loss_rate = 0.0; + }); + let ret_net = s.CreateMutableSimulationNode( + |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); + let truth: &StatesPrinter = s.CreatePrinter( + "send.truth.txt", TimeDelta::plus_infinity(), {send_net.ConfigPrinter()}); + + let client = CreateVideoSendingClient(&s, config, {send_net.node()}, + {ret_net.node()}); + + truth.PrintRow(); + s.RunFor(TimeDelta::Seconds(25)); + truth.PrintRow(); + assert_relative_eq!(client.target_rate().kbps(), 450, 100); + + send_net.UpdateConfig(|c: &mut NetworkSimulationConfig| { + c.bandwidth = DataRate::KilobitsPerSec(800); + c.delay = TimeDelta::Millis(100); + }); + + truth.PrintRow(); + s.RunFor(TimeDelta::Seconds(20)); + truth.PrintRow(); + assert_relative_eq!(client.target_rate().kbps(), 750, 150); + + send_net.UpdateConfig(|c: &mut NetworkSimulationConfig| { + c.bandwidth = DataRate::KilobitsPerSec(100); + c.delay = TimeDelta::Millis(200); + }); + ret_net.UpdateConfig( + |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(200); }); + + truth.PrintRow(); + s.RunFor(TimeDelta::Seconds(50)); + truth.PrintRow(); + assert_relative_eq!(client.target_rate().kbps(), 90, 25); + } + + fn RunRembDipScenario(test_name: &str) -> DataRate { + let s = Scenario::new(test_name); + let net_conf = NetworkSimulationConfig::new(); + net_conf.bandwidth = DataRate::KilobitsPerSec(2000); + net_conf.delay = TimeDelta::Millis(50); + let client = s.CreateClient("send", |c: &mut CallClientConfig| { + c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); + }); + let send_net = {s.CreateSimulationNode(net_conf)}; + let ret_net = {s.CreateSimulationNode(net_conf)}; + let route = s.CreateRoutes( + client, send_net, s.CreateClient("return", CallClientConfig()), ret_net); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + + s.RunFor(TimeDelta::Seconds(10)); + assert!(client.send_bandwidth().kbps() > 1500); + + let RembLimit: DataRate = DataRate::KilobitsPerSec(250); + client.SetRemoteBitrate(RembLimit); + s.RunFor(TimeDelta::Seconds(1)); + assert_eq!(client.send_bandwidth(), RembLimit); + + let RembLimitLifted: DataRate = DataRate::KilobitsPerSec(10000); + client.SetRemoteBitrate(RembLimitLifted); + s.RunFor(TimeDelta::Seconds(10)); + + return client.send_bandwidth(); + } + */ + + fn create_controller( + field_trials: FieldTrials, + feedback_only: bool, + ) -> GoogCcNetworkController { + let config = NetworkControllerConfig { + field_trials, + 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)), + }, + ..Default::default() + }; + + let goog_cc_config = GoogCcConfig { feedback_only }; + + GoogCcNetworkController::new(config, goog_cc_config) + } + + #[test] + fn initialize_target_rate_on_first_process_interval_after_network_available() { + let mut controller = create_controller(FieldTrials::default(), false); + + let _ = controller.on_network_availability(NetworkAvailability { + at_time: Timestamp::from_millis(123456), + network_available: true, + }); + 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 reacts_to_changed_network_conditions() { + let mut controller = create_controller(FieldTrials::default(), false); + let mut current_time: Timestamp = Timestamp::from_millis(123); + controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: true, + }); + controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + controller.on_remote_bitrate_report(RemoteBitrateReport { + receive_time: current_time, + bandwidth: INITIAL_BITRATE * 2, + }); + + current_time += TimeDelta::from_millis(25); + let update = controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE * 2); + assert_eq!( + update.pacer_config.unwrap().data_rate(), + INITIAL_BITRATE * 2 * DEFAULT_PACING_RATE + ); + + controller.on_remote_bitrate_report(RemoteBitrateReport { + receive_time: current_time, + bandwidth: INITIAL_BITRATE, + }); + current_time += TimeDelta::from_millis(25); + let update = controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..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 + ); + } + + #[test] + fn on_network_route_changed() { + let mut controller = create_controller(FieldTrials::default(), false); + let current_time: Timestamp = Timestamp::from_millis(123); + controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: true, + }); + let new_bitrate: DataRate = DataRate::from_bits_per_sec(200000); + + let update = controller.on_network_route_change(create_route_change( + current_time, + Some(new_bitrate), + None, + None, + )); + assert_eq!(update.target_rate.unwrap().target_rate, new_bitrate); + assert_eq!( + update.pacer_config.unwrap().data_rate(), + new_bitrate * DEFAULT_PACING_RATE + ); + assert_eq!(update.probe_cluster_configs.len(), 2); + + // If the bitrate is reset to -1, the new starting bitrate will be + // the minimum default bitrate. + const DEFAULT_MIN_BITRATE: DataRate = DataRate::from_kilobits_per_sec(5); + let update = + controller.on_network_route_change(create_route_change(current_time, None, None, None)); + assert_eq!(update.target_rate.unwrap().target_rate, DEFAULT_MIN_BITRATE); + assert_relative_eq!( + update.pacer_config.unwrap().data_rate().bps_float(), + DEFAULT_MIN_BITRATE.bps_float() * DEFAULT_PACING_RATE, + epsilon = 10.0 + ); + assert_eq!(update.probe_cluster_configs.len(), 2); + } + + #[test] + fn probe_on_route_change() { + let mut controller = create_controller(FieldTrials::default(), false); + let mut current_time: Timestamp = Timestamp::from_millis(123); + controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: true, + }); + current_time += TimeDelta::from_seconds(3); + + let update = controller.on_network_route_change(create_route_change( + current_time, + Some(2 * INITIAL_BITRATE), + Some(DataRate::zero()), + Some(20 * INITIAL_BITRATE), + )); + + assert!(update.pacer_config.is_some()); + assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE * 2); + assert_eq!(update.probe_cluster_configs.len(), 2); + assert_eq!( + update.probe_cluster_configs[0].target_data_rate, + INITIAL_BITRATE * 6 + ); + assert_eq!( + update.probe_cluster_configs[1].target_data_rate, + INITIAL_BITRATE * 12 + ); + + controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + } + + #[test] + fn probe_after_route_change_when_transport_writable() { + let mut controller = create_controller(FieldTrials::default(), false); + let current_time: Timestamp = Timestamp::from_millis(123); + + let mut update: NetworkControlUpdate = + controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: false, + }); + assert!(update.probe_cluster_configs.is_empty()); + + update = controller.on_network_route_change(create_route_change( + current_time, + Some(2 * INITIAL_BITRATE), + Some(DataRate::zero()), + Some(20 * INITIAL_BITRATE), + )); + // Transport is not writable. So not point in sending a probe. + assert!(update.probe_cluster_configs.is_empty()); + + // Probe is sent when transport becomes writable. + update = controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: true, + }); + assert!(!update.probe_cluster_configs.is_empty()); + } + + // Bandwidth estimation is updated when feedbacks are received. + // Feedbacks which show an increasing delay cause the estimation to be reduced. + #[test] + fn updates_delay_based_estimate() { + let mut controller = create_controller(FieldTrials::default(), false); + const RUN_TIME_MS: i64 = 6000; + let mut current_time: Timestamp = Timestamp::from_millis(123); + controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: true, + }); + + // 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: Option = packet_transmission_and_feedback_block( + &mut controller, + RUN_TIME_MS, + 0, + &mut current_time, + ); + assert!(target_bitrate_before_delay.is_some()); + + // Repeat, but this time with a building delay, and make sure that the + // estimation is adjusted downwards. + let target_bitrate_after_delay: Option = packet_transmission_and_feedback_block( + &mut controller, + RUN_TIME_MS, + 50, + &mut current_time, + ); + assert!(target_bitrate_after_delay.unwrap() < target_bitrate_before_delay.unwrap()); + } + + #[test] + fn pace_at_max_of_lower_link_capacity_and_bwe() { + let field_trials = FieldTrials { + pace_at_max_of_bwe_and_lower_link_capacity: true, + ..Default::default() + }; + let mut controller = create_controller(field_trials, false); + let mut current_time: Timestamp = Timestamp::from_millis(123); + controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: true, + }); + controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + current_time += TimeDelta::from_millis(100); + let mut network_estimate: NetworkStateEstimate = NetworkStateEstimate { + link_capacity_lower: 10 * INITIAL_BITRATE, + ..Default::default() + }; + controller.set_network_state_estimate(Some(network_estimate)); + // OnNetworkStateEstimate does not trigger processing a new estimate. So add a + // dummy loss report to trigger a BWE update in the next process interval. + let loss_report = TransportLossReport { + start_time: current_time, + end_time: current_time, + receive_time: current_time, + packets_received_delta: 50, + packets_lost_delta: 1, + }; + controller.on_transport_loss_report(loss_report); + let update = controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + assert!(update.pacer_config.is_some()); + assert!(update.target_rate.is_some()); + assert!(update.target_rate.unwrap().target_rate < network_estimate.link_capacity_lower); + assert_eq!( + update.pacer_config.unwrap().data_rate().kbps_float(), + network_estimate.link_capacity_lower.kbps_float() * DEFAULT_PACING_RATE + ); + + current_time += TimeDelta::from_millis(100); + // Set a low link capacity estimate and verify that pacing rate is set + // relative to loss based/delay based estimate. + network_estimate = NetworkStateEstimate { + link_capacity_lower: 0.5 * INITIAL_BITRATE, + ..Default::default() + }; + controller.set_network_state_estimate(Some(network_estimate)); + // Again, we need to inject a dummy loss report to trigger an update of the + // BWE in the next process interval. + let loss_report = TransportLossReport { + start_time: current_time, + end_time: current_time, + receive_time: current_time, + packets_received_delta: 50, + packets_lost_delta: 0, + }; + controller.on_transport_loss_report(loss_report); + let update = controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + assert!(update.target_rate.is_some()); + assert!( + update.target_rate.as_ref().unwrap().target_rate > network_estimate.link_capacity_lower + ); + // NOTE: These are off by 4 bps + assert_eq!( + update.pacer_config.unwrap().data_rate().kbps(), + (update.target_rate.unwrap().target_rate * DEFAULT_PACING_RATE).kbps(), + ); + } + + #[test] + fn limit_pacing_factor_to_upper_link_capacity() { + let field_trials = FieldTrials { + limit_pacing_factor_by_upper_link_capacity_estimate: true, + ..Default::default() + }; + let mut controller = create_controller(field_trials, false); + let mut current_time: Timestamp = Timestamp::from_millis(123); + controller.on_network_availability(NetworkAvailability { + at_time: current_time, + network_available: true, + }); + controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + current_time += TimeDelta::from_millis(100); + let network_estimate: NetworkStateEstimate = NetworkStateEstimate { + link_capacity_upper: (INITIAL_BITRATE * DEFAULT_PACING_RATE) / 2, + ..Default::default() + }; + controller.set_network_state_estimate(Some(network_estimate)); + // OnNetworkStateEstimate does not trigger processing a new estimate. So add a + // dummy loss report to trigger a BWE update in the next process interval. + let loss_report = TransportLossReport { + start_time: current_time, + end_time: current_time, + receive_time: current_time, + packets_received_delta: 50, + packets_lost_delta: 1, + }; + controller.on_transport_loss_report(loss_report); + let update = controller.on_process_interval(ProcessInterval { + at_time: current_time, + ..Default::default() + }); + assert!(update.pacer_config.is_some()); + assert!(update.target_rate.is_some()); + assert!(update.target_rate.unwrap().target_rate >= INITIAL_BITRATE); + assert_eq!( + update.pacer_config.unwrap().data_rate(), + network_estimate.link_capacity_upper + ); + } + + // Test congestion window pushback on network delay happens. + /* + #[test] + fn CongestionWindowPushbackOnNetworkDelay() { + let factory = CreateFeedbackOnlyFactory(); + let field_trials = FieldTrials { + congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 800, min_bitrate_bps: 30000, ..Default::default() } + ..Default::default() + }; + let s = Scenario::new("googcc_unit/cwnd_on_delay", false); + let send_net = + s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| { + c.bandwidth = DataRate::KilobitsPerSec(1000); + c.delay = TimeDelta::Millis(100); + }); + let ret_net = s.CreateSimulationNode( + |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); + let mut config = CallClientConfig::default(); + config.transport.cc_factory = &factory; + // Start high so bandwidth drop has max effect. + config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); + config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000); + config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); + + let client = CreateVideoSendingClient(&s, config, + {send_net.node()}, {ret_net}); + + s.RunFor(TimeDelta::Seconds(10)); + send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(10)); + s.RunFor(TimeDelta::Seconds(3)); + + // After 3 seconds without feedback from any sent packets, we expect that the + // target rate is reduced to the minimum pushback threshold + // DefaultMinPushbackTargetBitrateBps, which is defined as 30 kbps in + // congestion_window_pushback_controller. + EXPECT_LT(client.target_rate().kbps(), 40); + } + */ + + /* + // Test congestion window pushback on network delay happens. + #[test] + fn CongestionWindowPushbackDropFrameOnNetworkDelay() { + let factory = CreateFeedbackOnlyFactory(); + let field_trials = FieldTrials { + congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 800, min_bitrate_bps: 30000, drop_frame: true, ..Default::default() } + ..Default::default() + }; + let s = Scenario::new("googcc_unit/cwnd_on_delay", false); + let send_net = + s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| { + c.bandwidth = DataRate::KilobitsPerSec(1000); + c.delay = TimeDelta::Millis(100); + }); + let ret_net = s.CreateSimulationNode( + |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); + let mut config = CallClientConfig::default(); + config.transport.cc_factory = &factory; + // Start high so bandwidth drop has max effect. + config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); + config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000); + config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); + + let client = CreateVideoSendingClient(&s, config, + {send_net.node()}, {ret_net}); + + s.RunFor(TimeDelta::Seconds(10)); + send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(10)); + s.RunFor(TimeDelta::Seconds(3)); + + // As the dropframe is set, after 3 seconds without feedback from any sent + // packets, we expect that the target rate is not reduced by congestion + // window. + assert!(client.target_rate().kbps() > 300); + } + */ + + /* + #[test] + fn PaddingRateLimitedByCongestionWindowInTrial() { + let field_trials = FieldTrials { + congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 200, min_bitrate_bps: 30000, ..Default::default() } + ..Default::default() + }; + + let s = Scenario::new("googcc_unit/padding_limited", false); + let send_net = + s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| { + c.bandwidth = DataRate::KilobitsPerSec(1000); + c.delay = TimeDelta::Millis(100); + }); + let ret_net = s.CreateSimulationNode( + |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); + let mut config = CallClientConfig::default(); + // Start high so bandwidth drop has max effect. + config.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); + config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000); + let client = s.CreateClient("send", config); + let route = + s.CreateRoutes(client, {send_net.node()}, + s.CreateClient("return", CallClientConfig()), {ret_net}); + let mut video = VideoStreamConfig::default(); + video.stream.pad_to_rate = config.transport.rates.max_rate; + s.CreateVideoStream(route.forward(), video); + + // Run for a few seconds to allow the controller to stabilize. + s.RunFor(TimeDelta::Seconds(10)); + + // Check that padding rate matches target rate. + assert_relative_eq!(client.padding_rate().kbps(), client.target_rate().kbps(), 1); + + // Check this is also the case when congestion window pushback kicks in. + send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(1)); + assert_relative_eq!(client.padding_rate().kbps(), client.target_rate().kbps(), 1); + } + */ + + /* + #[test] + fn LimitsToFloorIfRttIsHighInTrial() { + // The field trial limits maximum RTT to 2 seconds, higher RTT means that the + // controller backs off until it reaches the minimum configured bitrate. This + // allows the RTT to recover faster than the regular control mechanism would + // achieve. + const BandwidthFloor: DataRate = DataRate::KilobitsPerSec(50); + let field_trials = FieldTrials { + max_rtt_limit: experiments::MaxRttLimitConfig { max_rtt: TimeDelta::Seconds(2), floor: BandwidthFloor, ..Default::default() }, + ..Default::default() + }; + // In the test case, we limit the capacity and add a cross traffic packet + // burst that blocks media from being sent. This causes the RTT to quickly + // increase above the threshold in the trial. + const LinkCapacity: DataRate = DataRate::KilobitsPerSec(100); + const BufferBloatDuration: TimeDelta = TimeDelta::Seconds(10); + let s = Scenario::new("googcc_unit/limit_trial", false); + let send_net = s.CreateSimulationNode(|c: &mut NetworkSimulcationConfig| { + c.bandwidth = LinkCapacity; + c.delay = TimeDelta::Millis(100); + }); + let ret_net = s.CreateSimulationNode( + |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); + let mut config = CallClientConfig::default(); + config.transport.rates.start_rate = LinkCapacity; + + let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net}); + // Run for a few seconds to allow the controller to stabilize. + s.RunFor(TimeDelta::Seconds(10)); + const BloatPacketSize: DataSize = DataSize::Bytes(1000); + const BloatPacketCount: i64 = + (BufferBloatDuration * LinkCapacity / BloatPacketSize) as i64; + // This will cause the RTT to be large for a while. + s.TriggerPacketBurst({send_net}, BloatPacketCount, BloatPacketSize.bytes()); + // Wait to allow the high RTT to be detected and acted upon. + s.RunFor(TimeDelta::Seconds(6)); + // By now the target rate should have dropped to the minimum configured rate. + assert_relative_eq!(client.target_rate().kbps(), BandwidthFloor.kbps(), 5); + } + */ + + /* + #[test] + fn UpdatesTargetRateBasedOnLinkCapacity() { + UpdatesTargetRateBasedOnLinkCapacity(); + } + */ + + /* + #[test] + fn StableEstimateDoesNotVaryInSteadyState() { + let factory = CreateFeedbackOnlyFactory(); + let s = Scenario::new("googcc_unit/stable_target", false); + let mut config = CallClientConfig::default(); + config.transport.cc_factory = &factory; + let mut net_conf = NetworkSimulationConfig::default(); + net_conf.bandwidth = DataRate::KilobitsPerSec(500); + net_conf.delay = TimeDelta::Millis(100); + let send_net = s.CreateSimulationNode(net_conf); + let ret_net = s.CreateSimulationNode(net_conf); + + let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net}); + // Run for a while to allow the estimate to stabilize. + s.RunFor(TimeDelta::Seconds(30)); + let min_stable_target: DataRate = DataRate::plus_infinity(); + let max_stable_target: DataRate = DataRate::minus_infinity(); + let min_target: DataRate = DataRate::plus_infinity(); + let max_target: DataRate = DataRate::minus_infinity(); + + // Measure variation in steady state. + for i in 0..20 { + let stable_target_rate = client.stable_target_rate(); + let target_rate = client.target_rate(); + assert!(stable_target_rate <= target_rate); + + min_stable_target = std::cmp::min(min_stable_target, stable_target_rate); + max_stable_target = std::cmp::max(max_stable_target, stable_target_rate); + min_target = std::cmp::min(min_target, target_rate); + max_target = std::cmp::max(max_target, target_rate); + s.RunFor(TimeDelta::Seconds(1)); + } + // We should expect drops by at least 15% (default backoff.) + assert!(min_target / max_target < 0.85); + // We should expect the stable target to be more stable than the immediate one + assert!(min_stable_target / max_stable_target >= min_target / max_target); + } + */ + + /* + #[test] + fn LossBasedControlUpdatesTargetRateBasedOnLinkCapacity() { + ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/"); + // TODO(srte): Should the behavior be unaffected at low loss rates? + UpdatesTargetRateBasedOnLinkCapacity("_loss_based"); + } + */ + + /* + #[test] + fn LossBasedControlDoesModestBackoffToHighLoss() { + ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/"); + let s = Scenario::new("googcc_unit/high_loss_channel", false); + let mut config = CallClientConfig::default(); + config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); + config.transport.rates.max_rate = DataRate::KilobitsPerSec(1500); + config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); + let send_net = s.CreateSimulationNode(|c: &mut NetworkSimulationConfig| { + c.bandwidth = DataRate::KilobitsPerSec(2000); + c.delay = TimeDelta::Millis(200); + c.loss_rate = 0.1; + }); + let ret_net = s.CreateSimulationNode( + |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(200); }); + + let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net}); + + s.RunFor(TimeDelta::Seconds(120)); + // Without LossBasedControl trial, bandwidth drops to ~10 kbps. + EXPECT_GT(client.target_rate().kbps(), 100); + } + */ + + /* + fn AverageBitrateAfterCrossInducedLoss(absl::string_view name) -> DataRate { + Scenario s(name, false); + let mut net_conf = NetworkSimulationConfig::default(); + net_conf.bandwidth = DataRate::KilobitsPerSec(1000); + net_conf.delay = TimeDelta::Millis(100); + // Short queue length means that we'll induce loss when sudden TCP traffic + // spikes are induced. This corresponds to ca 200 ms for a packet size of 1000 + // bytes. Such limited buffers are common on for instance wifi routers. + net_conf.packet_queue_length_limit = 25; + + let send_net = {s.CreateSimulationNode(net_conf)}; + let ret_net = {s.CreateSimulationNode(net_conf)}; + + let client = s.CreateClient("send", CallClientConfig()); + let callee = s.CreateClient("return", CallClientConfig()); + let route = s.CreateRoutes(client, send_net, callee, ret_net); + // TODO(srte): Make this work with RTX enabled or remove it. + let video = s.CreateVideoStream(route.forward(), [](VideoStreamConfig* c) { + c.stream.use_rtx = false; + }); + s.RunFor(TimeDelta::Seconds(10)); + for i in 0..4 { + // Sends TCP cross traffic inducing loss. + let tcp_traffic = s.net().StartCrossTraffic(CreateFakeTcpCrossTraffic( + s.net().CreateRoute(send_net), s.net().CreateRoute(ret_net), + FakeTcpConfig())); + s.RunFor(TimeDelta::Seconds(2)); + // Allow the ccongestion controller to recover. + s.net().StopCrossTraffic(tcp_traffic); + s.RunFor(TimeDelta::Seconds(20)); + } + + // Querying the video stats from within the expected runtime environment + // (i.e. the TQ that belongs to the CallClient, not the Scenario TQ that + // we're currently on). + VideoReceiveStreamInterface::Stats video_receive_stats; + let video_stream = video.receive(); + callee.SendTask([&video_stream, &video_receive_stats]() { + video_receive_stats = video_stream.GetStats(); + }); + return DataSize::Bytes( + video_receive_stats.rtp_stats.packet_counter.TotalBytes()) / + s.TimeSinceStart(); + } + */ + + /* + #[test] + fn MaintainsLowRateInSafeResetTrial() { + const LinkCapacity: DataRate = DataRate::KilobitsPerSec(200); + const StartRate: DataRate = DataRate::KilobitsPerSec(300); + + ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled/"); + Scenario s("googcc_unit/safe_reset_low"); + let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = LinkCapacity; + c.delay = TimeDelta::Millis(10); + }); + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = StartRate; + }); + let route = s.CreateRoutes( + client, {send_net}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + // Allow the controller to stabilize. + s.RunFor(TimeDelta::Millis(500)); + assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 50); + s.ChangeRoute(route.forward(), {send_net}); + // Allow new settings to propagate. + s.RunFor(TimeDelta::Millis(100)); + // Under the trial, the target should be unchanged for low rates. + assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 50); + } + */ + + /* + #[test] + fn DoNotResetBweUnlessNetworkAdapterChangeOnRoutChange() { + ScopedFieldTrials trial("WebRTC-Bwe-ResetOnAdapterIdChange/Enabled/"); + Scenario s("googcc_unit/do_not_reset_bwe_unless_adapter_change"); + + const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); + const StartRate: DataRate = DataRate::KilobitsPerSec(300); + + let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = LinkCapacity; + c.delay = TimeDelta::Millis(50); + }); + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = StartRate; + }); + client.UpdateNetworkAdapterId(0); + let route = s.CreateRoutes( + client, {send_net}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + // Allow the controller to stabilize. + s.RunFor(TimeDelta::Millis(500)); + assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300); + s.ChangeRoute(route.forward(), {send_net}); + // Allow new settings to propagate. + s.RunFor(TimeDelta::Millis(50)); + // Under the trial, the target should not drop. + assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300); + + s.RunFor(TimeDelta::Millis(500)); + // But if adapter id change, BWE should reset and start from the beginning if + // the network route changes. + client.UpdateNetworkAdapterId(1); + s.ChangeRoute(route.forward(), {send_net}); + // Allow new settings to propagate. + s.RunFor(TimeDelta::Millis(50)); + assert_relative_eq!(client.send_bandwidth().kbps(), StartRate.kbps(), 30); + } + */ + + /* + #[test] + fn CutsHighRateInSafeResetTrial() { + const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); + const StartRate: DataRate = DataRate::KilobitsPerSec(300); + + ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled/"); + Scenario s("googcc_unit/safe_reset_high_cut"); + let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = LinkCapacity; + c.delay = TimeDelta::Millis(50); + }); + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = StartRate; + }); + let route = s.CreateRoutes( + client, {send_net}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + // Allow the controller to stabilize. + s.RunFor(TimeDelta::Millis(500)); + assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300); + client.UpdateNetworkAdapterId(1); + s.ChangeRoute(route.forward(), {send_net}); + // Allow new settings to propagate. + s.RunFor(TimeDelta::Millis(50)); + // Under the trial, the target should be reset from high values. + assert_relative_eq!(client.send_bandwidth().kbps(), StartRate.kbps(), 30); + } + */ + + /* + #[test] + fn DetectsHighRateInSafeResetTrial() { + ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled,ack/"); + const InitialLinkCapacity: DataRate = DataRate::KilobitsPerSec(200); + const NewLinkCapacity: DataRate = DataRate::KilobitsPerSec(800); + const StartRate: DataRate = DataRate::KilobitsPerSec(300); + + Scenario s("googcc_unit/safe_reset_high_detect"); + let initial_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = InitialLinkCapacity; + c.delay = TimeDelta::Millis(50); + }); + let new_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = NewLinkCapacity; + c.delay = TimeDelta::Millis(50); + }); + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = StartRate; + }); + let route = s.CreateRoutes( + client, {initial_net}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + // Allow the controller to stabilize. + s.RunFor(TimeDelta::Millis(2000)); + assert_relative_eq!(client.send_bandwidth().kbps(), InitialLinkCapacity.kbps(), 50); + client.UpdateNetworkAdapterId(1); + s.ChangeRoute(route.forward(), {new_net}); + // Allow new settings to propagate, but not probes to be received. + s.RunFor(TimeDelta::Millis(50)); + // Under the field trial, the target rate should be unchanged since it's lower + // than the starting rate. + assert_relative_eq!(client.send_bandwidth().kbps(), InitialLinkCapacity.kbps(), 50); + // However, probing should have made us detect the higher rate. + // NOTE: This test causes high loss rate, and the loss-based estimator reduces + // the bitrate, making the test fail if we wait longer than one second here. + s.RunFor(TimeDelta::Millis(1000)); + EXPECT_GT(client.send_bandwidth().kbps(), NewLinkCapacity.kbps() - 300); + } + */ + + /* + #[test] + fn TargetRateReducedOnPacingBufferBuildupInTrial() { + // Configure strict pacing to ensure build-up. + ScopedFieldTrials trial( + "WebRTC-CongestionWindow/QueueSize:100,MinBitrate:30000/" + "WebRTC-Video-Pacing/factor:1.0/" + "WebRTC-AddPacingToCongestionWindowPushback/Enabled/"); + + const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); + const StartRate: DataRate = DataRate::KilobitsPerSec(1000); + + Scenario s("googcc_unit/pacing_buffer_buildup"); + let net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = LinkCapacity; + c.delay = TimeDelta::Millis(50); + }); + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = StartRate; + }); + let route = s.CreateRoutes( + client, {net}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + // Allow some time for the buffer to build up. + s.RunFor(TimeDelta::Seconds(5)); + + // Without trial, pacer delay reaches ~250 ms. + EXPECT_LT(client.GetStats().pacer_delay_ms, 150); + } + */ + + /* + #[test] + fn NoBandwidthTogglingInLossControlTrial() { + ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/"); + Scenario s("googcc_unit/no_toggling"); + let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = DataRate::KilobitsPerSec(2000); + c.loss_rate = 0.2; + c.delay = TimeDelta::Millis(10); + }); + + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = DataRate::KilobitsPerSec(300); + }); + let route = s.CreateRoutes( + client, {send_net}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + // Allow the controller to initialize. + s.RunFor(TimeDelta::Millis(250)); + + std::queue bandwidth_history; + const step: TimeDelta = TimeDelta::Millis(50); + for (TimeDelta time = TimeDelta::zero(); time < TimeDelta::Millis(2000); + time += step) { + s.RunFor(step); + const window: TimeDelta = TimeDelta::Millis(500); + if (bandwidth_history.len() >= window / step) + bandwidth_history.pop(); + bandwidth_history.push(client.send_bandwidth()); + EXPECT_LT( + CountBandwidthDips(bandwidth_history, DataRate::KilobitsPerSec(100)), + 2); + } + } + + #[test] + fn NoRttBackoffCollapseWhenVideoStops() { + ScopedFieldTrials trial("WebRTC-Bwe-MaxRttLimit/limit:2s/"); + Scenario s("googcc_unit/rttbackoff_video_stop"); + let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = DataRate::KilobitsPerSec(2000); + c.delay = TimeDelta::Millis(100); + }); + + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); + }); + let route = s.CreateRoutes( + client, {send_net}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + let video = s.CreateVideoStream(route.forward(), VideoStreamConfig()); + // Allow the controller to initialize, then stop video. + s.RunFor(TimeDelta::Seconds(1)); + video.send().Stop(); + s.RunFor(TimeDelta::Seconds(4)); + EXPECT_GT(client.send_bandwidth().kbps(), 1000); + } + + #[test] + fn NoCrashOnVeryLateFeedback() { + Scenario s; + let ret_net = s.CreateMutableSimulationNode(NetworkSimulationConfig()); + let route = s.CreateRoutes( + s.CreateClient("send", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}, + s.CreateClient("return", CallClientConfig()), {ret_net.node()}); + let video = s.CreateVideoStream(route.forward(), VideoStreamConfig()); + s.RunFor(TimeDelta::Seconds(5)); + // Delay feedback by several minutes. This will cause removal of the send time + // history for the packets as long as SendTimeHistoryWindow is configured for + // a shorter time span. + ret_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(300)); + // Stopping video stream while waiting to save test execution time. + video.send().Stop(); + s.RunFor(TimeDelta::Seconds(299)); + // Starting to cause addition of new packet to history, which cause old + // packets to be removed. + video.send().Start(); + // Runs until the lost packets are received. We expect that this will run + // without causing any runtime failures. + s.RunFor(TimeDelta::Seconds(2)); + } + + #[test] + fn IsFairToTCP() { + Scenario s("googcc_unit/tcp_fairness"); + let mut net_conf = NetworkSimulationConfig::default(); + net_conf.bandwidth = DataRate::KilobitsPerSec(1000); + net_conf.delay = TimeDelta::Millis(50); + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); + }); + let send_net = {s.CreateSimulationNode(net_conf)}; + let ret_net = {s.CreateSimulationNode(net_conf)}; + let route = s.CreateRoutes( + client, send_net, s.CreateClient("return", CallClientConfig()), ret_net); + s.CreateVideoStream(route.forward(), VideoStreamConfig()); + s.net().StartCrossTraffic(CreateFakeTcpCrossTraffic( + s.net().CreateRoute(send_net), s.net().CreateRoute(ret_net), + FakeTcpConfig())); + s.RunFor(TimeDelta::Seconds(10)); + + // Currently only testing for the upper limit as we in practice back out + // quite a lot in this scenario. If this behavior is fixed, we should add a + // lower bound to ensure it stays fixed. + EXPECT_LT(client.send_bandwidth().kbps(), 750); + } + + #[test] + fn FastRampupOnRembCapLifted() { + let final_estimate: DataRate = + RunRembDipScenario("googcc_unit/default_fast_rampup_on_remb_cap_lifted"); + EXPECT_GT(final_estimate.kbps(), 1500); + } + + #[test] + fn FallbackToLossBasedBweWithoutPacketFeedback() { + const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); + const StartRate: DataRate = DataRate::KilobitsPerSec(1000); + + let s = Scenario::new("googcc_unit/high_loss_channel", false); + let net = s.CreateMutableSimulationNode([&](NetworkSimulationConfig* c) { + c.bandwidth = LinkCapacity; + c.delay = TimeDelta::Millis(100); + }); + let client = s.CreateClient("send", [&](CallClientConfig* c) { + c.transport.rates.start_rate = StartRate; + }); + let route = s.CreateRoutes( + client, {net.node()}, s.CreateClient("return", CallClientConfig()), + {s.CreateSimulationNode(NetworkSimulationConfig())}); + + // Create a config without packet feedback. + VideoStreamConfig video_config; + video_config.stream.packet_feedback = false; + s.CreateVideoStream(route.forward(), video_config); + + s.RunFor(TimeDelta::Seconds(20)); + // Bandwith does not backoff because network is normal. + EXPECT_GE(client.target_rate().kbps(), 500); + + // Update the network to create high loss ratio + net.UpdateConfig(|c: &mut NetworkSimulationConfig| { c.loss_rate = 0.15; }); + s.RunFor(TimeDelta::Seconds(20)); + + // Bandwidth decreases thanks to loss based bwe v0. + EXPECT_LE(client.target_rate().kbps(), 300); + } + */ + + #[test] + fn calculates_rtt_from_transport_feedback() { + let mut controller = create_controller(FieldTrials::default(), true); + let mut current_time: Timestamp = Timestamp::from_millis(123); + let one_way_delay: TimeDelta = TimeDelta::from_millis(10); + let mut rtt: Option = None; + + 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); + 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 { + rtt = Some(target_rate.network_estimate.round_trip_time); + } + assert!(rtt.is_some()); + assert_eq!(rtt.unwrap().ms(), 2 * one_way_delay.ms()); + } +} 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..9d364ca51c4 --- /dev/null +++ b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs @@ -0,0 +1,183 @@ +/* + * 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::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. +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)] + 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..ce05df73091 --- /dev/null +++ b/rs/moq-congestion/src/gcc/link_capacity_estimator.rs @@ -0,0 +1,99 @@ +/* + * 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::api::units::DataRate; + +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_bandwidth_estimation.rs b/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs new file mode 100644 index 00000000000..0dd432ae70d --- /dev/null +++ b/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs @@ -0,0 +1,291 @@ +/* + * 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::api::{ + transport::PacketResult, + units::{DataRate, TimeDelta, Timestamp}, +}; + +#[derive(Clone, Debug)] +pub struct LossBasedControlConfig { + pub enabled: bool, // Enabled + pub min_increase_factor: f64, // min_incr + pub max_increase_factor: f64, // max_incr + pub increase_low_rtt: TimeDelta, // incr_low_rtt + pub increase_high_rtt: TimeDelta, // incr_high_rtt + pub decrease_factor: f64, // decr + pub loss_window: TimeDelta, // loss_win + pub loss_max_window: TimeDelta, // loss_max_win + pub acknowledged_rate_max_window: TimeDelta, // ackrate_max_win + pub increase_offset: DataRate, // incr_offset + pub loss_bandwidth_balance_increase: DataRate, // balance_incr + pub loss_bandwidth_balance_decrease: DataRate, // balance_decr + pub loss_bandwidth_balance_reset: DataRate, // balance_reset + pub loss_bandwidth_balance_exponent: f64, // exponent + pub allow_resets: bool, // resets + pub decrease_interval: TimeDelta, // decr_intvl + pub loss_report_timeout: TimeDelta, // timeout +} + +impl Default for LossBasedControlConfig { + fn default() -> Self { + Self { + enabled: false, + min_increase_factor: 1.02, + max_increase_factor: 1.08, + increase_low_rtt: TimeDelta::from_millis(200), + increase_high_rtt: TimeDelta::from_millis(800), + decrease_factor: 0.99, + loss_window: TimeDelta::from_millis(800), + loss_max_window: TimeDelta::from_millis(800), + acknowledged_rate_max_window: TimeDelta::from_millis(800), + increase_offset: DataRate::from_bits_per_sec(1000), + loss_bandwidth_balance_increase: DataRate::from_kilobits_per_sec_float(0.5), + loss_bandwidth_balance_decrease: DataRate::from_kilobits_per_sec(4), + loss_bandwidth_balance_reset: DataRate::from_kilobits_per_sec_float(0.1), + loss_bandwidth_balance_exponent: 0.5, + allow_resets: false, + decrease_interval: TimeDelta::from_millis(300), + loss_report_timeout: TimeDelta::from_millis(6000), + } + } +} + +// Estimates an upper BWE limit based on loss. +// It requires knowledge about lost packets and acknowledged bitrate. +// Ie, this class require transport feedback. +pub struct LossBasedBandwidthEstimation { + config: LossBasedControlConfig, + average_loss: f64, + average_loss_max: f64, + loss_based_bitrate: DataRate, + acknowledged_bitrate_max: DataRate, + acknowledged_bitrate_last_update: Timestamp, + time_last_decrease: Timestamp, + has_decreased_since_last_loss_report: bool, + last_loss_packet_report: Timestamp, + last_loss_ratio: f64, +} + +impl LossBasedBandwidthEstimation { + // Expecting RTCP feedback to be sent with roughly 1s intervals, a 5s gap + // indicates a channel outage. + const MAX_RTCP_FEEDBACK_INTERVAL: TimeDelta = TimeDelta::from_millis(5000); + + pub fn new(config: LossBasedControlConfig) -> Self { + Self { + config, + average_loss: 0.0, + average_loss_max: 0.0, + loss_based_bitrate: DataRate::zero(), + acknowledged_bitrate_max: DataRate::zero(), + acknowledged_bitrate_last_update: Timestamp::minus_infinity(), + time_last_decrease: Timestamp::minus_infinity(), + has_decreased_since_last_loss_report: false, + last_loss_packet_report: Timestamp::minus_infinity(), + last_loss_ratio: 0.0, + } + } + + // Returns the new estimate. + pub fn update( + &mut self, + at_time: Timestamp, + min_bitrate: DataRate, + wanted_bitrate: DataRate, + last_round_trip_time: TimeDelta, + ) -> DataRate { + if self.loss_based_bitrate.is_zero() { + self.loss_based_bitrate = wanted_bitrate; + } + + // Only increase if loss has been low for some time. + let loss_estimate_for_increase: f64 = self.average_loss_max; + // Avoid multiple decreases from averaging over one loss spike. + let loss_estimate_for_decrease: f64 = self.average_loss.min(self.last_loss_ratio); + let allow_decrease: bool = !self.has_decreased_since_last_loss_report + && (at_time - self.time_last_decrease + >= last_round_trip_time + self.config.decrease_interval); + // If packet lost reports are too old, dont increase bitrate. + let loss_report_valid: bool = + at_time - self.last_loss_packet_report < 1.2 * Self::MAX_RTCP_FEEDBACK_INTERVAL; + + if loss_report_valid + && self.config.allow_resets + && loss_estimate_for_increase < self.loss_reset_threshold() + { + self.loss_based_bitrate = wanted_bitrate; + } else if loss_report_valid && loss_estimate_for_increase < self.loss_increase_threshold() { + // Increase bitrate by RTT-adaptive ratio. + let new_increased_bitrate: DataRate = min_bitrate + * get_increase_factor(&self.config, last_round_trip_time) + + self.config.increase_offset; + // The bitrate that would make the loss "just high enough". + let new_increased_bitrate_cap: DataRate = bitrate_from_loss( + loss_estimate_for_increase, + self.config.loss_bandwidth_balance_increase, + self.config.loss_bandwidth_balance_exponent, + ); + let new_increased_bitrate = + std::cmp::min(new_increased_bitrate, new_increased_bitrate_cap); + self.loss_based_bitrate = std::cmp::max(new_increased_bitrate, self.loss_based_bitrate); + } else if loss_estimate_for_decrease > self.loss_decrease_threshold() && allow_decrease { + // The bitrate that would make the loss "just acceptable". + let new_decreased_bitrate_floor: DataRate = bitrate_from_loss( + loss_estimate_for_decrease, + self.config.loss_bandwidth_balance_decrease, + self.config.loss_bandwidth_balance_exponent, + ); + let new_decreased_bitrate: DataRate = + std::cmp::max(self.decreased_bitrate(), new_decreased_bitrate_floor); + if new_decreased_bitrate < self.loss_based_bitrate { + self.time_last_decrease = at_time; + self.has_decreased_since_last_loss_report = true; + self.loss_based_bitrate = new_decreased_bitrate; + } + } + self.loss_based_bitrate + } + pub fn update_acknowledged_bitrate( + &mut self, + acknowledged_bitrate: DataRate, + at_time: Timestamp, + ) { + let time_passed: TimeDelta = if self.acknowledged_bitrate_last_update.is_finite() { + at_time - self.acknowledged_bitrate_last_update + } else { + TimeDelta::from_seconds(1) + }; + self.acknowledged_bitrate_last_update = at_time; + if acknowledged_bitrate > self.acknowledged_bitrate_max { + self.acknowledged_bitrate_max = acknowledged_bitrate; + } else { + self.acknowledged_bitrate_max -= + exponential_update(self.config.acknowledged_rate_max_window, time_passed) + * (self.acknowledged_bitrate_max - acknowledged_bitrate); + } + } + pub fn initialize(&mut self, bitrate: DataRate) { + self.loss_based_bitrate = bitrate; + self.average_loss = 0.0; + self.average_loss_max = 0.0; + } + pub fn enabled(&self) -> bool { + self.config.enabled + } + // Returns true if LossBasedBandwidthEstimation is enabled and have + // received loss statistics. Ie, this class require transport feedback. + pub fn in_use(&self) -> bool { + self.enabled() && self.last_loss_packet_report.is_finite() + } + pub fn update_loss_statistics(&mut self, packet_results: &[PacketResult], at_time: Timestamp) { + if packet_results.is_empty() { + unreachable!(); + } + let mut loss_count: i64 = 0; + for pkt in packet_results { + loss_count += if !pkt.is_received() { 1 } else { 0 }; + } + self.last_loss_ratio = (loss_count) as f64 / packet_results.len() as f64; + let time_passed: TimeDelta = if self.last_loss_packet_report.is_finite() { + at_time - self.last_loss_packet_report + } else { + TimeDelta::from_seconds(1) + }; + self.last_loss_packet_report = at_time; + self.has_decreased_since_last_loss_report = false; + + self.average_loss += exponential_update(self.config.loss_window, time_passed) + * (self.last_loss_ratio - self.average_loss); + if self.average_loss > self.average_loss_max { + self.average_loss_max = self.average_loss; + } else { + self.average_loss_max += exponential_update(self.config.loss_max_window, time_passed) + * (self.average_loss - self.average_loss_max); + } + } + + /* unused + pub fn get_estimate(&self) -> DataRate { + self.loss_based_bitrate + } + */ + + fn loss_increase_threshold(&self) -> f64 { + loss_from_bitrate( + self.loss_based_bitrate, + self.config.loss_bandwidth_balance_increase, + self.config.loss_bandwidth_balance_exponent, + ) + } + fn loss_decrease_threshold(&self) -> f64 { + loss_from_bitrate( + self.loss_based_bitrate, + self.config.loss_bandwidth_balance_decrease, + self.config.loss_bandwidth_balance_exponent, + ) + } + fn loss_reset_threshold(&self) -> f64 { + loss_from_bitrate( + self.loss_based_bitrate, + self.config.loss_bandwidth_balance_reset, + self.config.loss_bandwidth_balance_exponent, + ) + } + + fn decreased_bitrate(&self) -> DataRate { + self.config.decrease_factor * self.acknowledged_bitrate_max + } +} + +// Increase slower when RTT is high. +fn get_increase_factor(config: &LossBasedControlConfig, mut rtt: TimeDelta) -> f64 { + // Clamp the RTT + if rtt < config.increase_low_rtt { + rtt = config.increase_low_rtt; + } else if rtt > config.increase_high_rtt { + rtt = config.increase_high_rtt; + } + let rtt_range = config.increase_high_rtt - config.increase_low_rtt; + if rtt_range <= TimeDelta::zero() { + unreachable!(); // Only on misconfiguration. + } + let rtt_offset = rtt - config.increase_low_rtt; + let relative_offset = (rtt_offset / rtt_range).clamp(0.0, 1.0); + let factor_range = config.max_increase_factor - config.min_increase_factor; + config.min_increase_factor + (1.0 - relative_offset) * factor_range +} + +fn loss_from_bitrate(bitrate: DataRate, loss_bandwidth_balance: DataRate, exponent: f64) -> f64 { + if loss_bandwidth_balance >= bitrate { + return 1.0; + } + (loss_bandwidth_balance / bitrate).powf(exponent) +} + +fn bitrate_from_loss(loss: f64, loss_bandwidth_balance: DataRate, exponent: f64) -> DataRate { + if exponent <= 0.0 { + unreachable!(); + } + if loss < 1e-5 { + return DataRate::infinity(); + } + loss_bandwidth_balance * loss.powf(-1.0 / exponent) +} + +fn exponential_update(window: TimeDelta, interval: TimeDelta) -> f64 { + // Use the convention that exponential window length (which is really + // infinite) is the time it takes to dampen to 1/e. + if window <= TimeDelta::zero() { + unreachable!(); + } + + 1.0 - (interval / window * -1.0).exp() +} 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..c0b0bbbcc5f --- /dev/null +++ b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs @@ -0,0 +1,3943 @@ +/* + * 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 std::collections::HashMap; + +use crate::{ + 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 pace_at_loss_based_estimate: 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: 1000000.0, // BwRampupUpperBoundFactor + bw_rampup_upper_bound_in_hold_factor: 1000000.0, // 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.15, // LossThresholdOfHighBandwidthPreference + bandwidth_preference_smoothing_factor: 0.002, // BandwidthPreferenceSmoothingFactor + inherent_loss_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(75), // 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: 20, // 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(75), // 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: false, // UseInStartPhase + min_num_observations: 3, // MinNumObservations + lower_bound_by_acked_rate_factor: 0.0, // LowerBoundByAckedRateFactor + hold_duration_factor: 0.0, // HoldDurationFactor + use_byte_loss_rate: false, // UseByteLossRate + padding_duration: TimeDelta::zero(), // PaddingDuration + bound_best_candidate: false, // BoundBest + pace_at_loss_based_estimate: false, // PaceAtLossBasedEstimate + 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 + } +} + +struct PartialObservation { + pub num_packets: usize, + pub lost_packets: HashMap, + pub size: DataSize, +} + +impl Default for PartialObservation { + fn default() -> Self { + Self { + num_packets: 0, + lost_packets: HashMap::new(), + size: DataSize::zero(), + } + } +} + +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(), + } + } +} + +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(), + } + } +} + +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 true if loss based BWE can be used in the start phase. + pub fn use_in_start_phase(&self) -> bool { + 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); + if candidate_objective > objective_max { + 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 { + if 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 + { + tracing::debug!( + "Resetting loss based BWE to {:?} due to loss. Avg loss rate: {:?}", + bounded_bandwidth_estimate, + self.average_reported_loss_ratio + ); + 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; + } + } + pub fn pace_at_loss_based_estimate(&self) -> bool { + self.config.pace_at_loss_based_estimate + && self.loss_based_result.state != LossBasedState::DelayBasedEstimate + } + + // 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; + } + (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 { + if 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 { + if 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 + .lost_packets + .remove(&packet.sent_packet.sequence_number); + } else { + self.partial_observation + .lost_packets + .insert(packet.sent_packet.sequence_number, 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.lost_packets.len(); + observation.num_received_packets = observation.num_packets - observation.num_lost_packets; + observation.sending_rate = + self.get_sending_rate(self.partial_observation.size / observation_duration); + for packet_size in self.partial_observation.lost_packets.values() { + observation.lost_size += *packet_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() % 2 == 0 { + 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 approx::assert_relative_eq; + use test_trace::test; + + use crate::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 { + transport_sequence_number: i64, + } + + impl LossBasedBweV2Test { + fn create_packet_results_with_received_packets( + &mut self, + first_packet_timestamp: Timestamp, + ) -> Vec { + let mut enough_feedback = vec![PacketResult::default(); 2]; + self.transport_sequence_number += 1; + enough_feedback[0].sent_packet.sequence_number = self.transport_sequence_number; + self.transport_sequence_number += 1; + enough_feedback[1].sent_packet.sequence_number = self.transport_sequence_number; + 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() { + self.transport_sequence_number += 1; + packet.sent_packet.sequence_number = self.transport_sequence_number; + 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]; + self.transport_sequence_number += 1; + enough_feedback[0].sent_packet.sequence_number = self.transport_sequence_number; + self.transport_sequence_number += 1; + enough_feedback[1].sent_packet.sequence_number = self.transport_sequence_number; + 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) { + self.transport_sequence_number += 1; + + enough_feedback.push(PacketResult { + sent_packet: SentPacket { + sequence_number: self.transport_sequence_number, + 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(), + ..Default::default() + }); + } + self.transport_sequence_number += 1; + + enough_feedback.push(PacketResult { + sent_packet: SentPacket { + sequence_number: self.transport_sequence_number, + size: DataSize::from_bytes(PACKET_SIZE), + send_time: first_packet_timestamp + OBSERVATION_DURATION_LOWER_BOUND, + ..Default::default() + }, + receive_time: Timestamp::plus_infinity(), + ..Default::default() + }); + + 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_max_increase_factor_after_loss_based_bwe_backs_off() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + candidate_factors: vec![1.2, 1.0, 0.5], + instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10000), + max_increase_factor: 1.5, + 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); + 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, + ); + let result_at_loss: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + + // 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, + ); + + let result_after_recovery: LossBasedBweV2Result = + loss_based_bandwidth_estimator.get_loss_based_result(); + assert_eq!( + result_after_recovery.bandwidth_estimate, + result_at_loss.bandwidth_estimate * 1.5 + ); + } + + #[test] + fn loss_based_state_is_delay_based_estimate_after_network_recovering() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + candidate_factors: vec![1.2, 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 feedback_count < 5 && result.state != LossBasedState::Increasing { + 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::Increasing); + + // 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 feedback_count < 5 && result.state != LossBasedState::Increasing { + 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::Increasing); + // 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(500)); + + 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, + ); + 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 Increasing. + 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::Increasing + ); + } + + #[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 feedback_count < 5 && result.state != LossBasedState::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 + ); + + 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=*/ false, + ); + 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 increase_estimate_if_not_hold() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + hold_duration_factor: 0.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 + ); + 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=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Increasing + ); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + > estimate + ); + } + + #[test] + fn increase_estimate_after_hold_duration() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + hold_duration_factor: 10.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 + ); + let estimate: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + // During the hold duration, e.g. first 300ms, the estimate cannot increase. + 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=*/ 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, + estimate + ); + + // After the hold duration, the estimate can increase. + 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, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Increasing + ); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + >= estimate + ); + + // Get another 50p packet loss. + 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 * 3, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + let estimate_at_hold: DataRate = loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate; + + // In the hold duration, e.g. next 3s, the estimate cannot increase above the + // hold rate. Get some lost packets to get lower estimate than the HOLD rate. + for i in 4..=6 { + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_100p_loss_rate( + /*first_packet_timestamp=*/ + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * i, + 2, + ), + /*delay_based_estimate=*/ DataRate::plus_infinity(), + /*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 + < estimate_at_hold + ); + } + + let mut feedback_id: i64 = 7; + while loss_based_bandwidth_estimator.get_loss_based_result().state + != LossBasedState::Increasing + { + 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, + ); + if loss_based_bandwidth_estimator.get_loss_based_result().state + == LossBasedState::Decreasing + { + // In the hold duration, the estimate can not go higher than estimate at + // hold. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + <= estimate_at_hold + ); + } else if loss_based_bandwidth_estimator.get_loss_based_result().state + == LossBasedState::Increasing + { + // After the hold duration, the estimate can increase again. + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + > estimate_at_hold + ); + } + feedback_id += 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 = LossBasedBweV2Config { + hold_duration_factor: 3.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 + ); + 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, + ); + 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(150) + ); + } + + #[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, + ..short_observation_config() + }; + 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, + ..short_observation_config() + }; + 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 pace_at_loss_based_estimate() { + let mut test = LossBasedBweV2Test::default(); + let config = LossBasedBweV2Config { + pace_at_loss_based_estimate: true, + 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(1000)); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &test.create_packet_results_with_received_packets( + /*first_packet_timestamp=*/ Timestamp::zero(), + ), + /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(1000), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::DelayBasedEstimate + ); + assert!(!loss_based_bandwidth_estimator.pace_at_loss_based_estimate()); + + 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::from_kilobits_per_sec(1000), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::Decreasing + ); + assert!(loss_based_bandwidth_estimator.pace_at_loss_based_estimate()); + + 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::from_kilobits_per_sec(1000), + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator.get_loss_based_result().state, + LossBasedState::IncreaseUsingPadding + ); + assert!(loss_based_bandwidth_estimator.pace_at_loss_based_estimate()); + } + + #[test] + fn estimate_does_not_back_off_due_to_packet_reordering_between_feedback() { + let config = short_observation_config(); + let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); + const START_BITRATE: DataRate = DataRate::from_kilobits_per_sec(2500); + loss_based_bandwidth_estimator.set_bandwidth_estimate(START_BITRATE); + + let mut feedback_1 = vec![PacketResult::default(); 3]; + feedback_1[0].sent_packet.sequence_number = 1; + feedback_1[0].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + feedback_1[0].sent_packet.send_time = Timestamp::zero(); + feedback_1[0].receive_time = + feedback_1[0].sent_packet.send_time + TimeDelta::from_millis(10); + feedback_1[1].sent_packet.sequence_number = 2; + feedback_1[1].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + feedback_1[1].sent_packet.send_time = Timestamp::zero(); + // Lost or reordered + feedback_1[1].receive_time = Timestamp::plus_infinity(); + + feedback_1[2].sent_packet.sequence_number = 3; + feedback_1[2].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + feedback_1[2].sent_packet.send_time = Timestamp::zero(); + feedback_1[2].receive_time = + feedback_1[2].sent_packet.send_time + TimeDelta::from_millis(10); + + let mut feedback_2 = vec![PacketResult::default(); 3]; + feedback_2[0].sent_packet.sequence_number = 2; + feedback_2[0].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + feedback_2[0].sent_packet.send_time = Timestamp::zero(); + feedback_2[0].receive_time = + feedback_1[0].sent_packet.send_time + TimeDelta::from_millis(10); + feedback_2[1].sent_packet.sequence_number = 4; + feedback_2[1].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + feedback_2[1].sent_packet.send_time = Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND; + feedback_2[1].receive_time = + feedback_2[1].sent_packet.send_time + TimeDelta::from_millis(10); + feedback_2[2].sent_packet.sequence_number = 5; + feedback_2[2].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); + feedback_2[2].sent_packet.send_time = Timestamp::zero(); + feedback_2[2].receive_time = + feedback_2[2].sent_packet.send_time + TimeDelta::from_millis(10); + + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &feedback_1, + /*delay_based_estimate=*/ START_BITRATE, + /*in_alr=*/ false, + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &feedback_2, + /*delay_based_estimate=*/ START_BITRATE, + /*in_alr=*/ false, + ); + assert_eq!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate, + START_BITRATE + ); + } +} // 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..0fd3b27030d --- /dev/null +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -0,0 +1,85 @@ +//! # Google Congestion Control +//! +//! This crate is a Rust port of the [Google Congestion Control](https://webrtc.googlesource.com/src/+/refs/heads/main/modules/congestion_controller/goog_cc/) (GCC) implementation from the WebRTC project. +//! No more painful linking against libwebrtc. +//! +//! Google Congestion Control is the congestion control algorithm used in WebRTC. +//! It is a hybrid algorithm that combines the strengths of delay-based and loss-based congestion control. +//! The end goal is minimum latency and to avoid buffer-bloat as much as possible. +//! +//! All licensing from the original source code is preserved under a BSD-style license. +//! Thanks to the WebRTC project authors for their hard work on this implementation. +//! All I did was port it to Rust. +//! +//! ## Notable Changes +//! **NetworkStateEstimator and NetworkStatePredictor have been removed.** +//! They are not implemented in the WebRTC source code and are flagged with a "subject to change" comment. +//! I think this is a back-door for Google to tweak the congestion control algorithm? +//! +//! **FieldTrials is a type-safe struct instead of a string.** +//! The original implementation uses a string to represent field trials. +//! This was quite error-prone as many trials do not match the cooresponding field name. +//! I decided to create a [FieldTrials](experiments::FieldTrials) struct to represent field trials in a type-safe way. +//! +//! **RtcEventLog has been removed.** +//! The structured logs are cool, but have been removed for now to simplify the code. +//! +//! **RTC_LOG has been replaced with [tracing].** +//! Use [tracing_subscriber](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/index.html) to enable and configure the logger. + +mod api; + +/// [FieldTrials](experiments::FieldTrials) and dynamic configuration. +pub mod experiments; + +// We inline goog_cc modules to avoid goog_cc::goog_cc logging prefix. +mod pacing; +mod remote_bitrate_estimator; +mod acknowledged_bitrate_estimator; +mod acknowledged_bitrate_estimator_interface; +mod alr_detector; +mod bitrate_estimator; +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_bandwidth_estimation; +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 acknowledged_bitrate_estimator::*; +use acknowledged_bitrate_estimator_interface::*; +use alr_detector::*; +use bitrate_estimator::*; +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_bandwidth_estimation::*; +use loss_based_bwe_v2::*; +use probe_bitrate_estimator::*; +use probe_controller::*; +use robust_throughput_estimator::*; +use send_side_bandwidth_estimation::*; +use trendline_estimator::*; + +// Selectively export the public API. +pub use api::{network_control, transport, units}; +pub use goog_cc_network_control::{GoogCcConfig, GoogCcNetworkController}; +pub use { + alr_detector::AlrDetectorConfig, bitrate_estimator::BitrateEstimatorConfig, + send_side_bandwidth_estimation::BweLossExperiment, + delay_based_bwe::BweSeparateAudioPacketsSettings, + loss_based_bwe_v2::LossBasedBweV2Config, + loss_based_bandwidth_estimation::LossBasedControlConfig, + probe_controller::ProbeControllerConfig, + robust_throughput_estimator::RobustThroughputEstimatorSettings, + send_side_bandwidth_estimation::RttBasedBackoffConfig, goog_cc_network_control::SafeResetOnRouteChange, trendline_estimator::TrendlineEstimatorSettings, +}; 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..8b841b9a1dc --- /dev/null +++ b/rs/moq-congestion/src/gcc/pacing/interval_budget.rs @@ -0,0 +1,68 @@ +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..1e02c11a213 --- /dev/null +++ b/rs/moq-congestion/src/gcc/pacing/mod.rs @@ -0,0 +1,2 @@ +mod interval_budget; +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..91a4bcb39ba --- /dev/null +++ b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs @@ -0,0 +1,499 @@ +/* + * 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::api::{ + transport::{PacedPacketInfo, PacketResult}, + units::{DataRate, DataSize, TimeDelta, Timestamp}, +}; + +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)] +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::*; + use approx::assert_relative_eq; + + 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; + + let mut receive_time: i64 = 5; + for send_time in 0..25 { + test.add_packet_feedback(0, 12500, send_time, receive_time, MIN_PROBES, MIN_BYTES); + receive_time += 1; + } + 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..eb1b46ab15d --- /dev/null +++ b/rs/moq-congestion/src/gcc/probe_controller.rs @@ -0,0 +1,2579 @@ +/* + * 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::api::{ + transport::{NetworkAvailability, NetworkStateEstimate, 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 how often we send probes if NetworkStateEstimate is available. + pub network_state_interval: TimeDelta, + // Periodically probe as long as the ratio between current estimate and + // NetworkStateEstimate is lower then this. + pub est_lower_than_network_ratio: f64, + pub est_lower_than_network_interval: TimeDelta, + pub network_state_scale: f64, + // Overrides min_probe_duration if network_state_estimate_probing_interval + // is set and a network state estimate is known and equal or higher than the + // probe target. + pub network_state_probe_duration: TimeDelta, + // Overrides min_probe_delta if network_state_estimate_probing_interval + // is set and a network state estimate is known and equal or higher than the + // probe target. + pub network_state_min_probe_delta: TimeDelta, + + // Configures the probes emitted by changed to the allocated bitrate. + pub probe_max_allocation: 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, + network_state_interval: TimeDelta::plus_infinity(), + est_lower_than_network_ratio: 0.0, + est_lower_than_network_interval: TimeDelta::from_seconds(3), + network_state_scale: 1.0, + network_state_probe_duration: TimeDelta::from_millis(15), + network_state_min_probe_delta: TimeDelta::from_millis(20), + probe_max_allocation: 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, +} + +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. +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, + network_estimate: Option, + 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(), + network_estimate: None, + 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; + 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(); + } + let network_state_estimate_probe_further_limit: DataRate = match self.network_estimate { + Some(network_estimate) + if self.config.network_state_interval.is_finite() + && network_estimate.link_capacity_upper.is_finite() => + { + network_estimate.link_capacity_upper * self.config.further_probe_threshold + } + _ => DataRate::plus_infinity(), + }; + tracing::debug!( + "Measured bitrate: {:?} Minimum to probe further: {:?} upper limit: {:?}", + bitrate, + self.min_bitrate_to_probe_further, + network_state_estimate_probe_further_limit + ); + + if bitrate > self.min_bitrate_to_probe_further + && bitrate <= network_state_estimate_probe_further_limit + { + 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_ms(&mut self, alr_start_time: Option) { + self.alr_start_time = alr_start_time.map(Timestamp::from_millis); + } + pub fn set_alr_ended_time_ms(&mut self, alr_end_time: i64) { + self.alr_end_time = Some(Timestamp::from_millis(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![] + } + + pub fn set_network_state_estimate(&mut self, estimate: NetworkStateEstimate) { + self.network_estimate = Some(estimate); + } + + // 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.network_estimate = None; + 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) || self.time_for_network_state_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 network_estimate: DataRate = self + .network_estimate + .map(|x| x.link_capacity_upper) + .unwrap_or(DataRate::plus_infinity()); + 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 std::cmp::min(network_estimate, 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, + ); + } + _ => (), + }; + + match self.network_estimate { + Some(network_estimate) if network_estimate.link_capacity_upper.is_finite() => { + if self.config.network_state_interval.is_finite() { + if network_estimate.link_capacity_upper.is_zero() { + tracing::debug!("Not sending probe, Network state estimate is zero"); + return vec![]; + } + max_probe_bitrate = std::cmp::min( + max_probe_bitrate, + std::cmp::max( + self.estimated_bitrate, + network_estimate.link_capacity_upper * self.config.network_state_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 { + if 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_network_state_probe(&self, at_time: Timestamp) -> bool { + let network_estimate = match self.network_estimate.as_ref() { + Some(network_estimate) if !network_estimate.link_capacity_upper.is_infinite() => { + network_estimate + } + _ => return false, + }; + + let probe_due_to_low_estimate: bool = self.bandwidth_limited_cause + == BandwidthLimitedCause::DelayBasedLimited + && self.estimated_bitrate + < self.config.est_lower_than_network_ratio * network_estimate.link_capacity_upper; + if probe_due_to_low_estimate && self.config.est_lower_than_network_interval.is_finite() { + let next_probe_time: Timestamp = + self.time_last_probing_initiated + self.config.est_lower_than_network_interval; + return at_time >= next_probe_time; + } + + let periodic_probe: bool = self.estimated_bitrate < network_estimate.link_capacity_upper; + if periodic_probe && self.config.network_state_interval.is_finite() { + let next_probe_time: Timestamp = + self.time_last_probing_initiated + self.config.network_state_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; + + match self.network_estimate { + Some(network_estimate) + if network_estimate.link_capacity_upper.is_finite() + && network_estimate.link_capacity_upper >= bitrate => + { + config.target_duration = self.config.network_state_probe_duration; + config.min_probe_delta = self.config.network_state_min_probe_delta; + } + _ => { + 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 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_on_max_allocated_bitrate_increase_only_when_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()); + 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_ms(Some(clock.ms())); + 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); + + // Do not probe when not in alr. + probe_controller.set_alr_start_time_ms(None); + let probes = probe_controller + .on_max_total_allocated_bitrate(MAX_BITRATE + DataRate::from_bits_per_sec(2), 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_ms(Some(clock.ms())); + 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_ms(Some(clock.ms())); + + // Do no probe, since probe_max_allocation:false. + probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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_ms(Some(clock.ms())); + 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_ms(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_ms(clock.ms()); + 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_ms(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_ms(clock.ms()); + 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_ms(Some(clock.ms())); + 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_ms(Some(start_time.ms())); + 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_ms(Some(start_time.ms())); + 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_ms(Some(start_time.ms())); + 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_ms(Some(alr_start_time.ms())); + 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()); + const MBPS_MULTIPLIER: DataRate = DataRate::from_kilobits_per_sec(1000); + 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()); + const MBPS_MULTIPLIER: DataRate = DataRate::from_kilobits_per_sec(1000); + const MAX_BITRATE: DataRate = DataRate::from_kilobits_per_sec(100 * 1000); + probe_controller.set_bitrates(MIN_BITRATE, 10 * MBPS_MULTIPLIER, MAX_BITRATE, 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_ms(Some(alr_start_time.ms())); + + let estimated_bitrate: DataRate = MAX_BITRATE / 10; + 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_ms(Some(clock.ms())); + 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_ms(Some(clock.ms())); + 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 periodic_probe_at_upper_network_state_estimate() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + ..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( + DataRate::from_bits_per_sec(5000), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + // Expect the controller to send a new probe after 5s has passed. + let state_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_kilobits_per_sec(6), + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!( + probes[0].target_data_rate, + state_estimate.link_capacity_upper + ); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!( + probes[0].target_data_rate, + state_estimate.link_capacity_upper + ); + } + + #[test] + fn limit_probe_at_upper_network_state_estimate_if_loss_based_limited() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + ..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( + DataRate::from_bits_per_sec(500), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + // Expect the controller to send a new probe after 5s has passed. + let state_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_bits_per_sec(700), + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + 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, + ); + // Expect the controller to send a new probe after 5s has passed. + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + assert_eq!(probes[0].target_data_rate, DataRate::from_bits_per_sec(700)); + } + + #[test] + fn alr_probes_limited_by_network_state_estimate() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + ..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_kilobits_per_sec(6), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + probe_controller.set_alr_start_time_ms(Some(clock.ms())); + + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_data_rate, MAX_BITRATE); + + let state_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_bits_per_sec(8000), + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!( + probes[0].target_data_rate, + state_estimate.link_capacity_upper + ); + } + + #[test] + fn can_set_longer_probe_duration_after_network_state_estimate() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + network_state_probe_duration: TimeDelta::from_millis(100), + ..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); + let probes = probe_controller.set_estimated_bitrate( + DataRate::from_kilobits_per_sec(5), + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(!probes.is_empty()); + assert!(probes[0].target_duration < TimeDelta::from_millis(100)); + + let state_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_kilobits_per_sec(6), + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert_eq!(probes[0].target_duration, TimeDelta::from_millis(100)); + } + + #[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_ms(Some(clock.ms())); + 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_ms(Some(clock.ms())); + 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_ms(None); + clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } + + #[test] + fn probe_further_when_loss_based_is_same_as_delay_based_estimate() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + ..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 state_estimate = NetworkStateEstimate { + link_capacity_upper: 5 * START_BITRATE, + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + + let probe_target_rate: DataRate = probes[0].target_data_rate; + assert!(probe_target_rate < state_estimate.link_capacity_upper); + // Expect that more probes are sent if BWE is the same as delay based + // estimate. + let probes = probe_controller.set_estimated_bitrate( + probe_target_rate, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(!probes.is_empty()); + assert_eq!(probes[0].target_data_rate, 2 * probe_target_rate); + } + + #[test] + fn probe_if_estimate_lower_than_network_state_estimate() { + // Periodic probe every 1 second if estimate is lower than 50% of the + // NetworkStateEstimate. + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + est_lower_than_network_interval: TimeDelta::from_seconds(1), + est_lower_than_network_ratio: 0.5, + // limit_probe_target_rate_to_loss_bwe: true, + ..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, + ); + // 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 mut state_estimate = NetworkStateEstimate { + link_capacity_upper: START_BITRATE, + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + + state_estimate.link_capacity_upper = START_BITRATE * 3; + probe_controller.set_network_state_estimate(state_estimate); + let probes = probe_controller.process(clock); + assert_eq!(probes.len(), 1); + assert!(probes[0].target_data_rate > START_BITRATE); + + // If network state not increased, send another probe. + clock += TimeDelta::from_millis(1100); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + + // Stop probing if estimate increase. We might probe further here though. + probe_controller.set_estimated_bitrate( + 2 * START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + // No more periodic probes. + clock += TimeDelta::from_millis(1100); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } + + #[test] + fn dont_probe_further_when_loss_limited() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + ..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 state_estimate = NetworkStateEstimate { + link_capacity_upper: 3 * START_BITRATE, + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + assert!(probes[0].target_data_rate < state_estimate.link_capacity_upper); + // Expect that no more probes are sent immediately if BWE is loss limited. + let probes = probe_controller.set_estimated_bitrate( + probes[0].target_data_rate, + BandwidthLimitedCause::LossLimitedBwe, + clock, + ); + assert!(probes.is_empty()); + } + + #[test] + fn probe_further_when_delay_based_limited() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + ..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 state_estimate = NetworkStateEstimate { + link_capacity_upper: 3 * START_BITRATE, + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + assert!(probes[0].target_data_rate < state_estimate.link_capacity_upper); + // Since the probe was successfull, expect to continue probing. + let probes = probe_controller.set_estimated_bitrate( + probes[0].target_data_rate, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(!probes.is_empty()); + assert_eq!( + probes[0].target_data_rate, + state_estimate.link_capacity_upper + ); + } + + #[test] + fn probe_after_timeout_if_network_state_estimate_increase_after_probe_sent() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + est_lower_than_network_interval: TimeDelta::from_seconds(3), + est_lower_than_network_ratio: 0.7, + ..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()); + let mut state_estimate = NetworkStateEstimate { + link_capacity_upper: 1.2 * probes[0].target_data_rate / 2.0, + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + // No immediate further probing since probe result is low. + let probes = probe_controller.set_estimated_bitrate( + probes[0].target_data_rate / 2, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + clock += TimeDelta::from_seconds(5); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + assert!(probes[0].target_data_rate <= state_estimate.link_capacity_upper); + // If the network state estimate increase, even before the probe result, + // expect a new probe after `est_lower_than_network_interval` timeout. + state_estimate.link_capacity_upper = 3 * START_BITRATE; + probe_controller.set_network_state_estimate(state_estimate); + let probes = probe_controller.set_estimated_bitrate( + probes[0].target_data_rate, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + clock += TimeDelta::from_seconds(3); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + + // But no more probes if estimate is close to the link capacity. + let probes = probe_controller.set_estimated_bitrate( + state_estimate.link_capacity_upper * 0.9, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + } + + #[test] + fn skip_probe_further_if_already_probed_to_max_rate() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(2), + 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()); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + probe_controller.set_network_state_estimate(NetworkStateEstimate { + link_capacity_upper: 2 * MAX_BITRATE, + ..Default::default() + }); + + // Attempt to probe up to max rate. + let probes = probe_controller.set_estimated_bitrate( + MAX_BITRATE * 0.8, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(!probes.is_empty()); + assert_eq!(probes[0].target_data_rate, MAX_BITRATE); + + // If the probe result arrives, dont expect a new probe immediately since we + // already tried to probe at the max rate. + let probes = probe_controller.set_estimated_bitrate( + MAX_BITRATE * 0.8, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + clock += TimeDelta::from_millis(1000); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + // But when enough time has passed, expect a new probe. + clock += TimeDelta::from_millis(1000); + 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_ms(Some(clock.ms())); + 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_ms(Some(clock.ms())); + 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 skip_network_state_probe_if_estimate_larger_than_max_probe() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(2), + 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()); + let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); + assert!(!probes.is_empty()); + + probe_controller.set_network_state_estimate(NetworkStateEstimate { + link_capacity_upper: 2 * MAX_BITRATE, + ..Default::default() + }); + let probes = probe_controller.set_estimated_bitrate( + MAX_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + clock += TimeDelta::from_seconds(10); + let probes = probe_controller.process(clock); + assert!(probes.is_empty()); + } + + #[test] + fn sends_probe_if_network_state_estimate_lower_than_max_probe() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(2), + skip_if_est_larger_than_fraction_of_max: 0.9, + network_state_probe_duration: TimeDelta::from_millis(100), + network_state_min_probe_delta: TimeDelta::from_millis(20), + ..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.set_network_state_estimate(NetworkStateEstimate { + link_capacity_upper: 2 * MAX_BITRATE, + ..Default::default() + }); + let probes = probe_controller.set_estimated_bitrate( + MAX_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + + // Need to wait at least two seconds before process can trigger a new probe. + clock += TimeDelta::from_millis(2100); + + let probes = probe_controller.set_estimated_bitrate( + START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + assert!(probes.is_empty()); + probe_controller.set_network_state_estimate(NetworkStateEstimate { + link_capacity_upper: 2 * START_BITRATE, + ..Default::default() + }); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + assert!(probes[0].target_data_rate <= 2 * START_BITRATE); + // Expect probe durations to be picked from field trial probe target is lower + // or equal to the network state estimate. + assert_eq!(probes[0].min_probe_delta, TimeDelta::from_millis(20)); + assert_eq!(probes[0].target_duration, TimeDelta::from_millis(100)); + } + + #[test] + fn probe_not_limited_by_network_state_esimate_if_lower_thant_current() { + let mut clock = Timestamp::zero(); + let mut probe_controller = ProbeController::new(ProbeControllerConfig { + network_state_interval: TimeDelta::from_seconds(5), + network_state_probe_duration: TimeDelta::from_millis(100), + network_state_min_probe_delta: TimeDelta::from_millis(20), + ..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( + START_BITRATE, + BandwidthLimitedCause::DelayBasedLimited, + clock, + ); + probe_controller.set_network_state_estimate(NetworkStateEstimate { + link_capacity_upper: START_BITRATE, + ..Default::default() + }); + // 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()); + + probe_controller.set_alr_start_time_ms(Some(clock.ms())); + probe_controller.set_network_state_estimate(NetworkStateEstimate { + link_capacity_upper: START_BITRATE / 2, + ..Default::default() + }); + clock += TimeDelta::from_seconds(6); + let probes = probe_controller.process(clock); + assert!(!probes.is_empty()); + assert_eq!(probes[0].target_data_rate, START_BITRATE); + // Expect probe durations to be default since network state estimate is lower + // than the probe rate. + assert_eq!(probes[0].min_probe_delta, TimeDelta::from_millis(2)); + assert_eq!(probes[0].target_duration, TimeDelta::from_millis(15)); + } + + #[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 state_estimate = NetworkStateEstimate { + link_capacity_upper: 3 * START_BITRATE, + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + 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 state_estimate = NetworkStateEstimate { + link_capacity_upper: 3 * START_BITRATE, + ..Default::default() + }; + probe_controller.set_network_state_estimate(state_estimate); + 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..98335c3b6e0 --- /dev/null +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/aimd_rate_control.rs @@ -0,0 +1,919 @@ +/* + * 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::{ + api::{ + transport::{BandwidthUsage, NetworkStateEstimate}, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + experiments::FieldTrials, + LinkCapacityEstimator, + remote_bitrate_estimator::BITRATE_WINDOW, +}; + +use super::{RateControlInput, CONGESTION_CONTROLLER_MIN_BITRATE}; + +// 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 + } +} + +// WebRTC-Bwe-EstimateBoundedIncrease +#[derive(Debug, Clone)] +pub struct EstimateBoundedIncrease { + pub disable_estimate_bounded_increase: bool, + pub use_current_estimate_as_min_upper_bound: bool, +} + +impl Default for EstimateBoundedIncrease { + fn default() -> Self { + Self { + disable_estimate_bounded_increase: false, + use_current_estimate_as_min_upper_bound: true, + } + } +} + +#[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. +pub struct AimdRateControl { + min_configured_bitrate: DataRate, + current_bitrate: DataRate, + latest_estimated_throughput: DataRate, + link_capacity: LinkCapacityEstimator, + network_estimate: Option, + 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, + + // Field trials + disable_estimate_bounded_increase: bool, + use_current_estimate_as_min_upper_bound: bool, +} + +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, + network_estimate: None, + last_decrease: None, + disable_estimate_bounded_increase: false, + use_current_estimate_as_min_upper_bound: true, + } + } +} + +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 { + tracing::debug!( + "Using aimd rate control with back off factor {}", + field_trials.bwe_back_off_factor.backoff_factor + ); + + Self { + send_side, + beta: field_trials.bwe_back_off_factor.backoff_factor, + no_bitrate_increase_in_alr: field_trials.no_bitrate_increase_in_alr, + use_current_estimate_as_min_upper_bound: field_trials + .estimate_bounded_increase + .use_current_estimate_as_min_upper_bound, + disable_estimate_bounded_increase: field_trials + .estimate_bounded_increase + .disable_estimate_bounded_increase, + ..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; + } + } + + #[cfg(test)] + pub fn set_network_state_estimate(&mut self, estimate: Option) { + self.network_estimate = estimate; + } + + // 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) + } + // Returns the expected time between overuse signals (assuming steady state). + pub fn get_expected_bandwidth_period(&self) -> TimeDelta { + const MIN_PERIOD: TimeDelta = TimeDelta::from_seconds(2); + const DEFAULT_PERIOD: TimeDelta = TimeDelta::from_seconds(3); + const MAX_PERIOD: TimeDelta = TimeDelta::from_seconds(50); + + let increase_rate_bps_per_second: f64 = self.get_near_max_increase_rate_bps_per_second(); + if let Some(last_decrease) = self.last_decrease { + let time_to_recover_decrease_seconds: f64 = + last_decrease.bps_float() / increase_rate_bps_per_second; + let period: TimeDelta = TimeDelta::from_seconds_float(time_to_recover_decrease_seconds); + period.clamp(MIN_PERIOD, MAX_PERIOD) + } else { + DEFAULT_PERIOD + } + } + + // 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 { + if let Some(network_estimate) = self.network_estimate.as_ref() { + if !self.disable_estimate_bounded_increase + && network_estimate.link_capacity_upper.is_finite() + { + let upper_bound: DataRate = if self.use_current_estimate_as_min_upper_bound { + network_estimate + .link_capacity_upper + .max(self.current_bitrate) + } else { + network_estimate.link_capacity_upper + }; + new_bitrate = upper_bound.min(new_bitrate); + } + if network_estimate.link_capacity_lower.is_finite() + && new_bitrate < self.current_bitrate + { + new_bitrate = self + .current_bitrate + .min(new_bitrate.max(network_estimate.link_capacity_lower * self.beta)); + } + } + 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); + + const MIN_BWE_PERIOD: TimeDelta = TimeDelta::from_seconds(2); + const DEFAULT_PERIOD: TimeDelta = TimeDelta::from_seconds(3); + const MAX_BWE_PERIOD: TimeDelta = TimeDelta::from_seconds(50); + + // 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_and_bandwidth_period() { + 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, + ); + assert_eq!( + aimd_rate_control.get_expected_bandwidth_period(), + DEFAULT_PERIOD + ); + } + + #[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 default_period_until_first_overuse() { + let mut aimd_rate_control = AimdRateControl::default(); + aimd_rate_control.set_start_bitrate(DataRate::from_kilobits_per_sec(300)); + assert_eq!( + aimd_rate_control.get_expected_bandwidth_period(), + DEFAULT_PERIOD + ); + aimd_rate_control.update( + RateControlInput::new( + BandwidthUsage::Overusing, + Some(DataRate::from_kilobits_per_sec(280)), + ), + INITIAL_TIME, + ); + assert_ne!( + aimd_rate_control.get_expected_bandwidth_period(), + DEFAULT_PERIOD + ); + } + + #[test] + fn expected_period_after_typical_drop() { + let mut aimd_rate_control = AimdRateControl::default(); + // The rate increase at 216 kbps should be 12 kbps. If we drop from + // 216 + 4*12 = 264 kbps, it should take 4 seconds to recover. Since we + // back off to 0.85*acked_rate-5kbps, the acked bitrate needs to be 260 + // kbps to end up at 216 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 + ); + assert_eq!( + aimd_rate_control.get_expected_bandwidth_period(), + TimeDelta::from_seconds(4) + ); + } + + #[test] + fn bandwidth_period_is_not_below_min() { + let mut aimd_rate_control = AimdRateControl::default(); + const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(10_000); + let mut now: Timestamp = INITIAL_TIME; + aimd_rate_control.set_estimate(INITIAL_BITRATE, now); + now += TimeDelta::from_millis(100); + // Make a small (1.5 kbps) bitrate drop to 8.5 kbps. + aimd_rate_control.update( + RateControlInput::new( + BandwidthUsage::Overusing, + Some(INITIAL_BITRATE - DataRate::from_bits_per_sec(1)), + ), + now, + ); + assert_eq!( + aimd_rate_control.get_expected_bandwidth_period(), + MIN_BWE_PERIOD + ); + } + + #[test] + fn bandwidth_period_is_not_above_max_no_smoothing_exp() { + let mut aimd_rate_control = AimdRateControl::default(); + const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(10_010_000); + let mut now: Timestamp = INITIAL_TIME; + aimd_rate_control.set_estimate(INITIAL_BITRATE, now); + now += TimeDelta::from_millis(100); + // Make a large (10 Mbps) bitrate drop to 10 kbps. + let acked_bitrate: DataRate = DataRate::from_bits_per_sec(10_000) / FRACTION_AFTER_OVERUSE; + aimd_rate_control.update( + RateControlInput::new(BandwidthUsage::Overusing, Some(acked_bitrate)), + now, + ); + assert_eq!( + aimd_rate_control.get_expected_bandwidth_period(), + MAX_BWE_PERIOD + ); + } + + #[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 set_estimate_upper_limited_by_network_estimate() { + let field_trials = Default::default(); + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(300_000), INITIAL_TIME); + let network_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_bits_per_sec(400_000), + ..Default::default() + }; + aimd_rate_control.set_network_state_estimate(Some(network_estimate)); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(500_000), INITIAL_TIME); + assert_eq!( + aimd_rate_control.latest_estimate(), + network_estimate.link_capacity_upper + ); + } + + #[test] + fn set_estimate_default_upper_limited_by_current_bitrate_if_network_estimate_is_low() { + let field_trials = Default::default(); + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(500_000), INITIAL_TIME); + assert_eq!( + aimd_rate_control.latest_estimate(), + DataRate::from_bits_per_sec(500_000) + ); + + let network_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_bits_per_sec(300_000), + ..Default::default() + }; + + aimd_rate_control.set_network_state_estimate(Some(network_estimate)); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(700_000), INITIAL_TIME); + assert_eq!( + aimd_rate_control.latest_estimate(), + DataRate::from_bits_per_sec(500_000) + ); + } + + #[test] + fn set_estimate_not_upper_limited_by_current_bitrate_if_network_estimate_is_low_if() { + // WebRTC-Bwe-EstimateBoundedIncrease/c_upper:false/ + let field_trials = FieldTrials { + estimate_bounded_increase: EstimateBoundedIncrease { + use_current_estimate_as_min_upper_bound: false, + ..Default::default() + }, + ..Default::default() + }; + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(500_000), INITIAL_TIME); + assert_eq!( + aimd_rate_control.latest_estimate(), + DataRate::from_bits_per_sec(500_000) + ); + + let network_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_bits_per_sec(300_000), + ..Default::default() + }; + aimd_rate_control.set_network_state_estimate(Some(network_estimate)); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(700_000), INITIAL_TIME); + assert_eq!( + aimd_rate_control.latest_estimate(), + DataRate::from_bits_per_sec(300_000) + ); + } + + #[test] + fn set_estimate_lower_limited_by_network_estimate() { + let field_trials = Default::default(); + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + let network_estimate = NetworkStateEstimate { + link_capacity_lower: DataRate::from_bits_per_sec(400_000), + ..Default::default() + }; + aimd_rate_control.set_network_state_estimate(Some(network_estimate)); + aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(100_000), INITIAL_TIME); + // 0.85 is default backoff factor. (`beta_`) + assert_eq!( + aimd_rate_control.latest_estimate(), + network_estimate.link_capacity_lower * 0.85 + ); + } + + #[test] + fn set_estimate_ignored_if_lower_than_network_estimate_and_current() { + let field_trials = Default::default(); + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + aimd_rate_control.set_estimate(DataRate::from_kilobits_per_sec(200), INITIAL_TIME); + assert_eq!(aimd_rate_control.latest_estimate().kbps(), 200); + let network_estimate = NetworkStateEstimate { + link_capacity_lower: DataRate::from_kilobits_per_sec(400), + ..Default::default() + }; + aimd_rate_control.set_network_state_estimate(Some(network_estimate)); + // Ignore the next SetEstimate, since the estimate is lower than 85% of + // the network estimate. + aimd_rate_control.set_estimate(DataRate::from_kilobits_per_sec(100), INITIAL_TIME); + assert_eq!(aimd_rate_control.latest_estimate().kbps(), 200); + } + + #[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); + } + + #[test] + fn estimate_not_limited_by_network_estimate_if_disabled() { + // WebRTC-Bwe-EstimateBoundedIncrease/Disabled/ + let field_trials = FieldTrials { + estimate_bounded_increase: EstimateBoundedIncrease { + disable_estimate_bounded_increase: true, + ..Default::default() + }, + ..Default::default() + }; + let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); + aimd_rate_control.disable_estimate_bounded_increase = 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); + let network_estimate = NetworkStateEstimate { + link_capacity_upper: DataRate::from_bits_per_sec(150_000), + ..Default::default() + }; + aimd_rate_control.set_network_state_estimate(Some(network_estimate)); + + 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() > network_estimate.link_capacity_upper); + } +} 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..09f26221059 --- /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::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/inter_arrival.rs b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/inter_arrival.rs new file mode 100644 index 00000000000..6307fdfbefe --- /dev/null +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/inter_arrival.rs @@ -0,0 +1,953 @@ +/* + * Copyright (c) 2013 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::api::units::latest_timestamp; + +#[derive(Clone, Copy, Debug)] +struct TimestampGroup { + pub size: usize, + pub first_timestamp: u32, + pub timestamp: u32, + pub first_arrival_ms: i64, + pub complete_time_ms: i64, + pub last_system_time_ms: i64, +} +impl TimestampGroup { + pub fn is_first_packet(&self) -> bool { + self.complete_time_ms == -1 + } +} + +impl Default for TimestampGroup { + fn default() -> Self { + Self { + size: 0, + first_timestamp: 0, + timestamp: 0, + first_arrival_ms: -1, + complete_time_ms: -1, + last_system_time_ms: -1, + } + } +} + +// Helper class to compute the inter-arrival time delta and the size delta +// between two timestamp groups. A timestamp is a 32 bit unsigned number with +// a client defined rate. +pub struct InterArrival { + timestamp_group_length_ticks: u32, + current_timestamp_group: TimestampGroup, + prev_timestamp_group: TimestampGroup, + timestamp_to_ms_coeff: f64, + num_consecutive_reordered_packets: usize, +} + +impl InterArrival { + // After this many packet groups received out of order InterArrival will + // reset, assuming that clocks have made a jump. + pub const REORDERED_RESET_THRESHOLD: usize = 3; + pub const ARRIVAL_TIME_OFFSET_THRESHOLD_MS: i64 = 3000; + + const BURST_DELTA_THRESHOLD_MS: i64 = 5; + const MAX_BURST_DURATION_MS: i64 = 100; + + // A timestamp group is defined as all packets with a timestamp which are at + // most timestamp_group_length_ticks older than the first timestamp in that + // group. + pub fn new(timestamp_group_length_ticks: u32, timestamp_to_ms_coeff: f64) -> Self { + Self { + timestamp_group_length_ticks, + current_timestamp_group: TimestampGroup::default(), + prev_timestamp_group: TimestampGroup::default(), + timestamp_to_ms_coeff, + 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. + // `timestamp` is the timestamp. + // `arrival_time_ms` is the local time at which the packet arrived. + // `packet_size` is the size of the packet. + // `timestamp_delta` (output) is the computed timestamp delta. + // `arrival_time_delta_ms` (output) is the computed arrival-time delta. + // `packet_size_delta` (output) is the computed size delta. + #[allow(clippy::too_many_arguments)] + pub fn compute_deltas( + &mut self, + timestamp: u32, + arrival_time_ms: i64, + system_time_ms: i64, + packet_size: usize, + timestamp_delta: &mut u32, + arrival_time_delta_ms: &mut i64, + packet_size_delta: &mut i64, + ) -> bool { + let mut calculated_deltas = 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.timestamp = timestamp; + self.current_timestamp_group.first_timestamp = timestamp; + self.current_timestamp_group.first_arrival_ms = arrival_time_ms; + } else if !self.packet_in_order(timestamp) { + return false; + } else if self.new_timestamp_group(arrival_time_ms, timestamp) { + // First packet of a later frame, the previous frame sample is ready. + if self.prev_timestamp_group.complete_time_ms >= 0 { + *timestamp_delta = self + .current_timestamp_group + .timestamp + .wrapping_sub(self.prev_timestamp_group.timestamp); + *arrival_time_delta_ms = self.current_timestamp_group.complete_time_ms + - self.prev_timestamp_group.complete_time_ms; + // Check system time differences to see if we have an unproportional jump + // in arrival time. In that case reset the inter-arrival computations. + let system_time_delta_ms: i64 = self.current_timestamp_group.last_system_time_ms + - self.prev_timestamp_group.last_system_time_ms; + if *arrival_time_delta_ms - system_time_delta_ms + >= Self::ARRIVAL_TIME_OFFSET_THRESHOLD_MS + { + 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_ms < 0 { + // 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 are being reordered on the path from the + socket to the bandwidth estimator. Ignoring this + packet for bandwidth estimation, resetting." + ); + self.reset(); + } + return false; + } else { + self.num_consecutive_reordered_packets = 0; + } + assert!(*arrival_time_delta_ms >= 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_timestamp = timestamp; + self.current_timestamp_group.timestamp = timestamp; + self.current_timestamp_group.first_arrival_ms = arrival_time_ms; + self.current_timestamp_group.size = 0; + } else { + self.current_timestamp_group.timestamp = + latest_timestamp(self.current_timestamp_group.timestamp, timestamp); + } + // Accumulate the frame size. + self.current_timestamp_group.size += packet_size; + self.current_timestamp_group.complete_time_ms = arrival_time_ms; + self.current_timestamp_group.last_system_time_ms = system_time_ms; + + calculated_deltas + } + + // Returns true if the packet with timestamp `timestamp` arrived in order. + fn packet_in_order(&self, timestamp: u32) -> bool { + if self.current_timestamp_group.is_first_packet() { + true + } else { + // Assume that a diff which is bigger than half the timestamp interval + // (32 bits) must be due to reordering. This code is almost identical to + // that in IsNewerTimestamp() in module_common_types.h. + let timestamp_diff: u32 = + timestamp.wrapping_sub(self.current_timestamp_group.first_timestamp); + timestamp_diff < 0x80000000 + } + } + + // Returns true if the last packet was the end of the current batch and the + // packet with `timestamp` is the first of a new batch. + fn new_timestamp_group(&self, arrival_time_ms: i64, timestamp: u32) -> bool { + if self.current_timestamp_group.is_first_packet() { + false + } else if self.belongs_to_burst(arrival_time_ms, timestamp) { + return false; + } else { + let timestamp_diff: u32 = + timestamp.wrapping_sub(self.current_timestamp_group.first_timestamp); + return timestamp_diff > self.timestamp_group_length_ticks; + } + } + + fn belongs_to_burst(&self, arrival_time_ms: i64, timestamp: u32) -> bool { + assert!(self.current_timestamp_group.complete_time_ms >= 0); + let arrival_time_delta_ms: i64 = + arrival_time_ms - self.current_timestamp_group.complete_time_ms; + let timestamp_diff: u32 = timestamp.wrapping_sub(self.current_timestamp_group.timestamp); + let ts_delta_ms: i64 = (self.timestamp_to_ms_coeff * (timestamp_diff as f64) + 0.5) as i64; + if ts_delta_ms == 0 { + return true; + } + let propagation_delta_ms: i64 = arrival_time_delta_ms - ts_delta_ms; + if propagation_delta_ms < 0 + && arrival_time_delta_ms <= Self::BURST_DELTA_THRESHOLD_MS + && arrival_time_ms - self.current_timestamp_group.first_arrival_ms + < Self::MAX_BURST_DURATION_MS + { + return true; + } + false + } + + fn reset(&mut self) { + self.num_consecutive_reordered_packets = 0; + self.current_timestamp_group = TimestampGroup::default(); + self.prev_timestamp_group = TimestampGroup::default(); + } +} + +#[cfg(test)] +mod test { + use approx::assert_relative_eq; + + use super::*; + + const TIMESTAMP_GROUP_LENGTH_US: i64 = 5000; + const MIN_STEP: i64 = 20; + const TRIGGER_NEW_GROUP_US: i64 = TIMESTAMP_GROUP_LENGTH_US + MIN_STEP; + const BURST_THRESHOLD_MS: i64 = 5; + const ABS_SEND_TIME_FRACTION: i64 = 18; + const ABS_SEND_TIME_INTER_ARRIVAL_UPSHIFT: i64 = 8; + const INTER_ARRIVAL_SHIFT: i64 = ABS_SEND_TIME_FRACTION + ABS_SEND_TIME_INTER_ARRIVAL_UPSHIFT; + + const RTP_TIMESTAMP_TO_MS: f64 = 1.0 / 90.0; + const AST_TO_MS: f64 = 1000.0 / (1 << INTER_ARRIVAL_SHIFT) as f64; + + struct InterArrivalTest { + pub inter_arrival: InterArrival, + inter_arrival_rtp: InterArrival, + inter_arrival_ast: InterArrival, + } + + impl InterArrivalTest { + pub fn new() -> Self { + Self { + inter_arrival: InterArrival::new((TIMESTAMP_GROUP_LENGTH_US / 1000) as u32, 1.0), + inter_arrival_rtp: InterArrival::new( + Self::make_rtp_timestamp(TIMESTAMP_GROUP_LENGTH_US), + RTP_TIMESTAMP_TO_MS, + ), + inter_arrival_ast: InterArrival::new( + Self::make_abs_send_time(TIMESTAMP_GROUP_LENGTH_US), + AST_TO_MS, + ), + } + } + // Test that neither inter_arrival instance complete the timestamp group from + // the given data. + pub fn expect_false( + &mut self, + timestamp_us: i64, + arrival_time_ms: i64, + packet_size: usize, + ) { + Self::internal_expect_false( + &mut self.inter_arrival_rtp, + Self::make_rtp_timestamp(timestamp_us), + arrival_time_ms, + packet_size, + ); + Self::internal_expect_false( + &mut self.inter_arrival_ast, + Self::make_abs_send_time(timestamp_us), + arrival_time_ms, + packet_size, + ); + } + + // Test that both inter_arrival instances complete the timestamp group from + // the given data and that all returned deltas are as expected (except + // delta: Timestamp, which is rounded from us to different ranges and must + // match within an interval, given in |timestamp_near]. + #[allow(clippy::too_many_arguments)] + pub fn expect_true( + &mut self, + timestamp_us: i64, + arrival_time_ms: i64, + packet_size: usize, + expected_timestamp_delta_us: i64, + expected_arrival_time_delta_ms: i64, + expected_packet_size_delta: i64, + timestamp_near: u32, + ) { + Self::internal_expect_true( + &mut self.inter_arrival_rtp, + Self::make_rtp_timestamp(timestamp_us), + arrival_time_ms, + packet_size, + Self::make_rtp_timestamp(expected_timestamp_delta_us), + expected_arrival_time_delta_ms, + expected_packet_size_delta, + timestamp_near, + ); + Self::internal_expect_true( + &mut self.inter_arrival_ast, + Self::make_abs_send_time(timestamp_us), + arrival_time_ms, + packet_size, + Self::make_abs_send_time(expected_timestamp_delta_us), + expected_arrival_time_delta_ms, + expected_packet_size_delta, + timestamp_near << 8, + ); + } + + pub fn wrap_test_helper( + &mut self, + wrap_start_us: i64, + timestamp_near: u32, + unorderly_within_group: bool, + ) { + // Step through the range of a 32 bit int, 1/4 at a time to not cause + // packets close to wraparound to be judged as out of order. + + // G1 + let mut arrival_time: i64 = 17; + self.expect_false(0, arrival_time, 1); + + // G2 + arrival_time += BURST_THRESHOLD_MS + 1; + self.expect_false(wrap_start_us / 4, arrival_time, 1); + + // G3 + arrival_time += BURST_THRESHOLD_MS + 1; + self.expect_true( + wrap_start_us / 2, + arrival_time, + 1, + wrap_start_us / 4, + 6, + 0, // Delta G2-G1 + 0, + ); + + // G4 + arrival_time += BURST_THRESHOLD_MS + 1; + let g4_arrival_time: i64 = arrival_time; + self.expect_true( + wrap_start_us / 2 + wrap_start_us / 4, + arrival_time, + 1, + wrap_start_us / 4, + 6, + 0, // Delta G3-G2 + timestamp_near, + ); + + // G5 + arrival_time += BURST_THRESHOLD_MS + 1; + self.expect_true( + wrap_start_us, + arrival_time, + 2, + wrap_start_us / 4, + 6, + 0, // Delta G4-G3 + timestamp_near, + ); + for i in 0..10 { + // Slowly step across the wrap point. + arrival_time += BURST_THRESHOLD_MS + 1; + if unorderly_within_group { + // These packets arrive with timestamps in decreasing order but are + // nevertheless accumulated to group because their timestamps are higher + // than the initial timestamp of the group. + self.expect_false(wrap_start_us + MIN_STEP * (9 - i), arrival_time, 1); + } else { + self.expect_false(wrap_start_us + MIN_STEP * i, arrival_time, 1); + } + } + let g5_arrival_time: i64 = arrival_time; + + // This packet is out of order and should be dropped. + arrival_time += BURST_THRESHOLD_MS + 1; + self.expect_false(wrap_start_us - 100, arrival_time, 100); + + // G6 + arrival_time += BURST_THRESHOLD_MS + 1; + let g6_arrival_time: i64 = arrival_time; + + self.expect_true( + wrap_start_us + TRIGGER_NEW_GROUP_US, + arrival_time, + 10, + wrap_start_us / 4 + 9 * MIN_STEP, + g5_arrival_time - g4_arrival_time, + (2 + 10) - 1, // Delta G5-G4 + timestamp_near, + ); + + // This packet is out of order and should be dropped. + arrival_time += BURST_THRESHOLD_MS + 1; + self.expect_false(wrap_start_us + TIMESTAMP_GROUP_LENGTH_US, arrival_time, 100); + + // G7 + arrival_time += BURST_THRESHOLD_MS + 1; + self.expect_true( + wrap_start_us + 2 * TRIGGER_NEW_GROUP_US, + arrival_time, + 100, + // Delta G6-G5 + TRIGGER_NEW_GROUP_US - 9 * MIN_STEP, + g6_arrival_time - g5_arrival_time, + 10 - (2 + 10), + timestamp_near, + ); + } + + fn make_rtp_timestamp(us: i64) -> u32 { + ((us * 90 + 500) as u64 / 1000) as u32 + } + + fn make_abs_send_time(us: i64) -> u32 { + let absolute_send_time: u32 = + ((((us as u64) << 18) + 500000) / 1000000) as u32 & 0x00FFFFFF; + absolute_send_time << 8 + } + + fn internal_expect_false( + inter_arrival: &mut InterArrival, + timestamp: u32, + arrival_time_ms: i64, + packet_size: usize, + ) { + let mut dummy_timestamp: u32 = 101; + let mut dummy_arrival_time_ms: i64 = 303; + let mut dummy_packet_size: i64 = 909; + let computed: bool = inter_arrival.compute_deltas( + timestamp, + arrival_time_ms, + arrival_time_ms, + packet_size, + &mut dummy_timestamp, + &mut dummy_arrival_time_ms, + &mut dummy_packet_size, + ); + assert!(!computed); + assert_eq!(101, dummy_timestamp); + assert_eq!(303, dummy_arrival_time_ms); + assert_eq!(909, dummy_packet_size); + } + + #[allow(clippy::too_many_arguments)] + fn internal_expect_true( + inter_arrival: &mut InterArrival, + timestamp: u32, + arrival_time_ms: i64, + packet_size: usize, + expected_timestamp_delta: u32, + expected_arrival_time_delta_ms: i64, + expected_packet_size_delta: i64, + timestamp_near: u32, + ) { + let mut delta_timestamp: u32 = 101; + let mut delta_arrival_time_ms: i64 = 303; + let mut delta_packet_size: i64 = 909; + let computed: bool = inter_arrival.compute_deltas( + timestamp, + arrival_time_ms, + arrival_time_ms, + packet_size, + &mut delta_timestamp, + &mut delta_arrival_time_ms, + &mut delta_packet_size, + ); + assert!(computed); + assert_relative_eq!( + expected_timestamp_delta as f64, + delta_timestamp as f64, + epsilon = timestamp_near as f64 + ); + assert_eq!(expected_arrival_time_delta_ms, delta_arrival_time_ms); + assert_eq!(expected_packet_size_delta, delta_packet_size); + } + } + + #[test] + fn first_packet() { + let mut test = InterArrivalTest::new(); + test.expect_false(0, 17, 1); + } + + #[test] + fn first_group() { + let mut test = InterArrivalTest::new(); + // G1 + let mut arrival_time: i64 = 17; + let g1_arrival_time: i64 = arrival_time; + test.expect_false(0, arrival_time, 1); + + // G2 + arrival_time += BURST_THRESHOLD_MS + 1; + let g2_arrival_time: i64 = arrival_time; + test.expect_false(TRIGGER_NEW_GROUP_US, arrival_time, 2); + + // G3 + // Only once the first packet of the third group arrives, do we see the deltas + // between the first two. + arrival_time += BURST_THRESHOLD_MS + 1; + test.expect_true( + 2 * TRIGGER_NEW_GROUP_US, + arrival_time, + 1, + // Delta G2-G1 + TRIGGER_NEW_GROUP_US, + g2_arrival_time - g1_arrival_time, + 1, + 0, + ); + } + + #[test] + fn second_group() { + let mut test = InterArrivalTest::new(); + // G1 + let mut arrival_time: i64 = 17; + let g1_arrival_time: i64 = arrival_time; + test.expect_false(0, arrival_time, 1); + + // G2 + arrival_time += BURST_THRESHOLD_MS + 1; + let g2_arrival_time: i64 = arrival_time; + test.expect_false(TRIGGER_NEW_GROUP_US, arrival_time, 2); + + // G3 + arrival_time += BURST_THRESHOLD_MS + 1; + let g3_arrival_time: i64 = arrival_time; + test.expect_true( + 2 * TRIGGER_NEW_GROUP_US, + arrival_time, + 1, + // Delta G2-G1 + TRIGGER_NEW_GROUP_US, + g2_arrival_time - g1_arrival_time, + 1, + 0, + ); + + // G4 + // First packet of 4th group yields deltas between group 2 and 3. + arrival_time += BURST_THRESHOLD_MS + 1; + test.expect_true( + 3 * TRIGGER_NEW_GROUP_US, + arrival_time, + 2, + // Delta G3-G2 + TRIGGER_NEW_GROUP_US, + g3_arrival_time - g2_arrival_time, + -1, + 0, + ); + } + + #[test] + fn accumulated_group() { + let mut test = InterArrivalTest::new(); + // G1 + let mut arrival_time: i64 = 17; + let g1_arrival_time: i64 = arrival_time; + test.expect_false(0, arrival_time, 1); + + // G2 + arrival_time += BURST_THRESHOLD_MS + 1; + test.expect_false(TRIGGER_NEW_GROUP_US, 28, 2); + let mut timestamp: i64 = TRIGGER_NEW_GROUP_US; + for _ in 0..10 { + // A bunch of packets arriving within the same group. + arrival_time += BURST_THRESHOLD_MS + 1; + timestamp += MIN_STEP; + test.expect_false(timestamp, arrival_time, 1); + } + let g2_arrival_time: i64 = arrival_time; + let g2_timestamp: i64 = timestamp; + + // G3 + arrival_time = 500; + test.expect_true( + 2 * TRIGGER_NEW_GROUP_US, + arrival_time, + 100, + g2_timestamp, + g2_arrival_time - g1_arrival_time, + (2 + 10) - 1, // Delta G2-G1 + 0, + ); + } + + #[test] + fn out_of_order_packet() { + let mut test = InterArrivalTest::new(); + // G1 + let mut arrival_time: i64 = 17; + let mut timestamp: i64 = 0; + test.expect_false(timestamp, arrival_time, 1); + let g1_timestamp: i64 = timestamp; + let g1_arrival_time: i64 = arrival_time; + + // G2 + arrival_time += 11; + timestamp += TRIGGER_NEW_GROUP_US; + test.expect_false(timestamp, 28, 2); + for _ in 0..10 { + arrival_time += BURST_THRESHOLD_MS + 1; + timestamp += MIN_STEP; + test.expect_false(timestamp, arrival_time, 1); + } + let g2_timestamp: i64 = timestamp; + let g2_arrival_time: i64 = arrival_time; + + // This packet is out of order and should be dropped. + arrival_time = 281; + test.expect_false(g1_timestamp, arrival_time, 100); + + // G3 + arrival_time = 500; + timestamp = 2 * TRIGGER_NEW_GROUP_US; + test.expect_true( + timestamp, + arrival_time, + 100, + // Delta G2-G1 + g2_timestamp - g1_timestamp, + g2_arrival_time - g1_arrival_time, + (2 + 10) - 1, + 0, + ); + } + + #[test] + fn out_of_order_within_group() { + let mut test = InterArrivalTest::new(); + // G1 + let mut arrival_time: i64 = 17; + let mut timestamp: i64 = 0; + test.expect_false(timestamp, arrival_time, 1); + let g1_timestamp: i64 = timestamp; + let g1_arrival_time: i64 = arrival_time; + + // G2 + timestamp += TRIGGER_NEW_GROUP_US; + arrival_time += 11; + test.expect_false(TRIGGER_NEW_GROUP_US, 28, 2); + timestamp += 10 * MIN_STEP; + let g2_timestamp: i64 = timestamp; + for _ in 0..10 { + // These packets arrive with timestamps in decreasing order but are + // nevertheless accumulated to group because their timestamps are higher + // than the initial timestamp of the group. + arrival_time += BURST_THRESHOLD_MS + 1; + test.expect_false(timestamp, arrival_time, 1); + timestamp -= MIN_STEP; + } + let g2_arrival_time: i64 = arrival_time; + + // However, this packet is deemed out of order and should be dropped. + arrival_time = 281; + timestamp = g1_timestamp; + test.expect_false(timestamp, arrival_time, 100); + + // G3 + timestamp = 2 * TRIGGER_NEW_GROUP_US; + arrival_time = 500; + test.expect_true( + timestamp, + arrival_time, + 100, + g2_timestamp - g1_timestamp, + g2_arrival_time - g1_arrival_time, + (2 + 10) - 1, + 0, + ); + } + + #[test] + fn two_bursts() { + let mut test = InterArrivalTest::new(); + // G1 + let g1_arrival_time: i64 = 17; + test.expect_false(0, g1_arrival_time, 1); + + // G2 + let mut timestamp: i64 = TRIGGER_NEW_GROUP_US; + let mut arrival_time: i64 = 100; // Simulate no packets arriving for 100 ms. + for _ in 0..10 { + // A bunch of packets arriving in one burst (within 5 ms apart). + timestamp += 30000; + arrival_time += BURST_THRESHOLD_MS; + test.expect_false(timestamp, arrival_time, 1); + } + let g2_arrival_time: i64 = arrival_time; + let g2_timestamp: i64 = timestamp; + + // G3 + timestamp += 30000; + arrival_time += BURST_THRESHOLD_MS + 1; + test.expect_true( + timestamp, + arrival_time, + 100, + g2_timestamp, + g2_arrival_time - g1_arrival_time, + 10 - 1, // Delta G2-G1 + 0, + ); + } + + #[test] + fn no_bursts() { + let mut test = InterArrivalTest::new(); + // G1 + test.expect_false(0, 17, 1); + + // G2 + let timestamp: i64 = TRIGGER_NEW_GROUP_US; + let arrival_time: i64 = 28; + test.expect_false(timestamp, arrival_time, 2); + + // G3 + test.expect_true( + TRIGGER_NEW_GROUP_US + 30000, + arrival_time + BURST_THRESHOLD_MS + 1, + 100, + timestamp, + arrival_time - 17, + 2 - 1, // Delta G2-G1 + 0, + ); + } + + // Yields 0xfffffffe when converted to internal representation in + // self.inter_arrival_rtp and self.inter_arrival_ast respectively. + const START_RTP_TIMESTAMP_WRAP_US: i64 = 47721858827; + const START_ABS_SEND_TIME_WRAP_US: i64 = 63999995; + + #[test] + fn rtp_timestamp_wrap() { + let mut test = InterArrivalTest::new(); + test.wrap_test_helper(START_RTP_TIMESTAMP_WRAP_US, 1, false); + } + + #[test] + fn abs_send_time_wrap() { + let mut test = InterArrivalTest::new(); + test.wrap_test_helper(START_ABS_SEND_TIME_WRAP_US, 1, false); + } + + #[test] + fn rtp_timestamp_wrap_out_of_order_within_group() { + let mut test = InterArrivalTest::new(); + test.wrap_test_helper(START_RTP_TIMESTAMP_WRAP_US, 1, true); + } + + #[test] + fn abs_send_time_wrap_out_of_order_within_group() { + let mut test = InterArrivalTest::new(); + test.wrap_test_helper(START_ABS_SEND_TIME_WRAP_US, 1, true); + } + + #[test] + fn positive_arrival_time_jump() { + let mut test = InterArrivalTest::new(); + const PACKET_SIZE: usize = 1000; + let mut send_time_ms: u32 = 10000; + let mut arrival_time_ms: i64 = 20000; + let mut system_time_ms: i64 = 30000; + + let mut send_delta: u32 = 0; + let mut arrival_delta: i64 = 0; + let mut size_delta: i64 = 0; + assert!(!test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + + const TIME_DELTA_MS: i64 = 30; + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + assert!(!test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS + InterArrival::ARRIVAL_TIME_OFFSET_THRESHOLD_MS; + system_time_ms += TIME_DELTA_MS; + assert!(test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + assert_eq!(TIME_DELTA_MS, send_delta as i64); + assert_eq!(TIME_DELTA_MS, arrival_delta); + assert_eq!(size_delta, 0); + + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + // The previous arrival time jump should now be detected and cause a reset. + assert!(!test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + + // The two next packets will not give a valid delta since we're in the initial + // state. + for _ in 0..2 { + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + assert!(!test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + } + + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + assert!(test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + assert_eq!(TIME_DELTA_MS, send_delta as _); + assert_eq!(TIME_DELTA_MS, arrival_delta); + assert_eq!(size_delta, 0); + } + + #[test] + fn negative_arrival_time_jump() { + let mut test = InterArrivalTest::new(); + const PACKET_SIZE: usize = 1000; + let mut send_time_ms: u32 = 10000; + let mut arrival_time_ms: i64 = 20000; + let mut system_time_ms: i64 = 30000; + + let mut send_delta: u32 = 0; + let mut arrival_delta: i64 = 0; + let mut size_delta: i64 = 0; + assert!(!test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + + const TIME_DELTA_MS: i64 = 30; + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + assert!(!test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + assert!(test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + assert_eq!(TIME_DELTA_MS, send_delta as _); + assert_eq!(TIME_DELTA_MS, arrival_delta); + assert_eq!(size_delta, 0); + + // Three out of order will fail, after that we will be reset and two more will + // fail before we get our first valid delta after the reset. + arrival_time_ms -= 1000; + for _ in 0..InterArrival::REORDERED_RESET_THRESHOLD + 3 { + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + // The previous arrival time jump should now be detected and cause a reset. + assert!(!test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + } + + send_time_ms += TIME_DELTA_MS as u32; + arrival_time_ms += TIME_DELTA_MS; + system_time_ms += TIME_DELTA_MS; + assert!(test.inter_arrival.compute_deltas( + send_time_ms, + arrival_time_ms, + system_time_ms, + PACKET_SIZE, + &mut send_delta, + &mut arrival_delta, + &mut size_delta + )); + assert_eq!(TIME_DELTA_MS, send_delta as _); + assert_eq!(TIME_DELTA_MS, arrival_delta); + assert_eq!(size_delta, 0); + } +} 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..9abc712e64e --- /dev/null +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs @@ -0,0 +1,7 @@ +mod aimd_rate_control; +mod bwe_defines; +// unused? +//mod inter_arrival; + +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..6ee9531eb4d --- /dev/null +++ b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs @@ -0,0 +1,849 @@ +/* + * 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::collections::VecDeque; + +use crate::{ + api::{ + transport::PacketResult, + units::{DataRate, DataSize, TimeDelta, Timestamp}, + }, + AcknowledgedBitrateEstimatorInterface, +}; + +// WebRTC-Bwe-RobustThroughputEstimatorSettings +#[derive(Debug, Clone)] +pub struct RobustThroughputEstimatorSettings { + // Set `enabled` to true to use the RobustThroughputEstimator, false to use + // the AcknowledgedBitrateEstimator. + pub enabled: bool, + + // 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, + + // If audio packets aren't included in allocation (i.e. the + // estimated available bandwidth is divided only among the video + // streams), then `unacked_weight` should be set to 0. + // If audio packets are included in allocation, but not in bandwidth + // estimation (i.e. they don't have transport-wide sequence numbers, + // but we nevertheless divide the estimated available bandwidth among + // both audio and video streams), then `unacked_weight` should be set to 1. + // If all packets have transport-wide sequence numbers, then the value + // of `unacked_weight` doesn't matter. + pub unacked_weight: f64, +} + +impl Default for RobustThroughputEstimatorSettings { + fn default() -> Self { + Self { + enabled: true, + window_packets: 20, + max_window_packets: 500, + window_duration: TimeDelta::from_seconds(1), + max_window_duration: TimeDelta::from_seconds(5), + required_packets: 10, + unacked_weight: 1.0, + } + } +} + +impl RobustThroughputEstimatorSettings { + pub fn validate(&mut self) { + assert!(self.enabled); + + 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); + + if self.unacked_weight < 0.0 || 1.0 < self.unacked_weight { + tracing::warn!("Weight for prior unacked size must be between 0 and 1."); + self.unacked_weight = 1.0; + } + } +} + +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 + } +} + +impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { + 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); + self.window + .back_mut() + .unwrap() + .sent_packet + .prior_unacked_data *= self.settings.unacked_weight; + // 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. + for i in (1..self.window.len()).rev() { + if self.window[i].receive_time < self.window[i - 1].receive_time { + self.window.swap(i, 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(); + } + } + + 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 + packet.sent_packet.prior_unacked_data; + } + last_recv_time = std::cmp::max(last_recv_time, packet.receive_time); + recv_size += packet.sent_packet.size; + recv_size += packet.sent_packet.prior_unacked_data; + + 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 + packet.sent_packet.prior_unacked_data; + } + first_send_time = std::cmp::min(first_send_time, packet.sent_packet.send_time); + + send_size += packet.sent_packet.size; + send_size += packet.sent_packet.prior_unacked_data; + 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, + )) + } + + fn peek_rate(&self) -> Option { + self.bitrate() + } + + fn set_alr(&mut self, _in_alr: bool) { + // noop + } + + fn set_alr_ended_time(&mut self, _alr_ended_time: crate::api::units::Timestamp) { + // noop + } +} + +#[cfg(test)] +mod test { + use crate::api::transport::SentPacket; + use approx::assert_relative_eq; + + use super::*; + + struct FeedbackGenerator { + send_clock: Timestamp, + recv_clock: Timestamp, + sequence_number: i64, + } + + impl Default for FeedbackGenerator { + fn default() -> Self { + Self { + send_clock: Timestamp::from_millis(100000), + recv_clock: Timestamp::from_millis(10000), + sequence_number: 100, + } + } + } + + 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, + sequence_number: self.sequence_number, + size: packet_size, + ..Default::default() + }, + receive_time: self.recv_clock, + ..Default::default() + }); + self.send_clock += packet_size / send_rate; + self.sequence_number += 1; + } + + 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 { + enabled: true, + 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 { + enabled: true, + 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..05cd09600ce --- /dev/null +++ b/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs @@ -0,0 +1,1000 @@ +/* + * 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 + */ + +use std::collections::VecDeque; + +use crate::remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE; +use crate::{ + api::{ + transport::{BandwidthUsage, SentPacket, TransportPacketsFeedback}, + units::{DataRate, TimeDelta, Timestamp}, + }, + experiments::FieldTrials, + LossBasedBandwidthEstimation, LossBasedBweV2, LossBasedState, +}; + +#[derive(Clone, Debug)] +pub struct BweLossExperiment { + pub enabled: bool, + pub low_loss_threshold: f32, + pub high_loss_threshold: f32, + pub bitrate_threshold_kbps: f64, +} + +impl Default for BweLossExperiment { + fn default() -> Self { + Self { + enabled: false, + low_loss_threshold: 0.02, + high_loss_threshold: 0.1, + bitrate_threshold_kbps: 0.0, + } + } +} + +pub struct LinkCapacityTracker { + capacity_estimate_bps: f64, + last_link_capacity_update: Timestamp, // = Timestamp::minus_infinity(); + last_delay_based_estimate: DataRate, // = DataRate::plus_infinity(); +} + +impl Default for LinkCapacityTracker { + fn default() -> Self { + Self { + capacity_estimate_bps: 0.0, + last_link_capacity_update: Timestamp::minus_infinity(), + last_delay_based_estimate: DataRate::plus_infinity(), + } + } +} + +impl LinkCapacityTracker { + // Call when a new delay-based estimate is available. + pub fn update_delay_based_estimate( + &mut self, + at_time: Timestamp, + delay_based_bitrate: DataRate, + ) { + if delay_based_bitrate < self.last_delay_based_estimate { + self.capacity_estimate_bps = self + .capacity_estimate_bps + .min(delay_based_bitrate.bps_float()); + self.last_link_capacity_update = at_time; + } + self.last_delay_based_estimate = delay_based_bitrate; + } + pub fn on_starting_rate(&mut self, start_rate: DataRate) { + if self.last_link_capacity_update.is_infinite() { + self.capacity_estimate_bps = start_rate.bps_float(); + } + } + pub fn on_rate_update( + &mut self, + acknowledged: Option, + target: DataRate, + at_time: Timestamp, + ) { + let acknowledged = match acknowledged { + Some(ack) => ack, + None => return, + }; + let acknowledged_target: DataRate = std::cmp::min(acknowledged, target); + if acknowledged_target.bps_float() > self.capacity_estimate_bps { + let delta: TimeDelta = at_time - self.last_link_capacity_update; + let alpha: f64 = if delta.is_finite() { + (-(delta / TimeDelta::from_seconds(10))).exp() + } else { + 0.0 + }; + self.capacity_estimate_bps = alpha * self.capacity_estimate_bps + + (1.0 - alpha) * acknowledged_target.bps_float(); + } + self.last_link_capacity_update = at_time; + } + pub fn on_rtt_backoff(&mut self, backoff_rate: DataRate, at_time: Timestamp) { + self.capacity_estimate_bps = self.capacity_estimate_bps.min(backoff_rate.bps_float()); + self.last_link_capacity_update = at_time; + } + pub fn estimate(&self) -> DataRate { + DataRate::from_bits_per_sec_float(self.capacity_estimate_bps) + } +} + +// 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), + } + } +} + +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 + } +} + +pub struct SendSideBandwidthEstimation { + field_trials: FieldTrials, + rtt_backoff: RttBasedBackoff, + link_capacity: LinkCapacityTracker, + + min_bitrate_history: VecDeque<(Timestamp, DataRate)>, + + // incoming filters + lost_packets_since_last_loss_update: i64, + expected_packets_since_last_loss_update: i64, + + acknowledged_rate: Option, + current_target: DataRate, + min_bitrate_configured: DataRate, + max_bitrate_configured: DataRate, + last_low_bitrate_log: Timestamp, + + has_decreased_since_last_fraction_loss: bool, + last_loss_feedback: Timestamp, + last_loss_packet_report: Timestamp, + last_fraction_loss: u8, + last_logged_fraction_loss: u8, + last_round_trip_time: TimeDelta, + + // The max bitrate as set by the receiver in the call. This is typically + // signalled using the REMB RTCP message and is used when we don't have any + // send side delay based estimate. + receiver_limit: DataRate, + delay_based_limit: DataRate, + time_last_decrease: Timestamp, + first_report_time: Timestamp, + initially_lost_packets: i64, + bitrate_at_2_seconds: DataRate, + last_rtc_event_log: Timestamp, + low_loss_threshold: f32, + high_loss_threshold: f32, + bitrate_threshold: DataRate, + loss_based_bandwidth_estimator_v1: LossBasedBandwidthEstimation, + loss_based_bandwidth_estimator_v2: LossBasedBweV2, + loss_based_state: LossBasedState, + disable_receiver_limit_caps_only: bool, +} + +impl Default for SendSideBandwidthEstimation { + fn default() -> Self { + Self::new(FieldTrials::default()) + } +} + +impl SendSideBandwidthEstimation { + const BWE_INCREASE_INTERVAL: TimeDelta = TimeDelta::from_millis(1000); + const BWE_DECREASE_INTERVAL: TimeDelta = TimeDelta::from_millis(300); + const START_PHASE: TimeDelta = TimeDelta::from_millis(2000); + const LIMIT_NUM_PACKETS: i64 = 20; + const DEFAULT_MAX_BITRATE: DataRate = DataRate::from_bits_per_sec(1000000000); + const LOW_BITRATE_LOG_PERIOD: TimeDelta = TimeDelta::from_millis(10000); + // Expecting that RTCP feedback is sent uniformly within [0.5, 1.5]s intervals. + const MAX_RTCP_FEEDBACK_INTERVAL: TimeDelta = TimeDelta::from_millis(5000); + + pub fn new(field_trials: FieldTrials) -> Self { + let mut low_loss_threshold = 0.02; + let mut high_loss_threshold = 0.1; + let mut bitrate_threshold_kbps = 0.0; + + if field_trials.loss_experiment.enabled { + low_loss_threshold = field_trials.loss_experiment.low_loss_threshold; + high_loss_threshold = field_trials.loss_experiment.high_loss_threshold; + bitrate_threshold_kbps = field_trials.loss_experiment.bitrate_threshold_kbps; + assert!(low_loss_threshold > 0.0); + assert!(low_loss_threshold <= 1.0); + assert!(high_loss_threshold > 0.0); + assert!(high_loss_threshold <= 1.0); + assert!(low_loss_threshold <= high_loss_threshold); + assert!(bitrate_threshold_kbps >= 0.0); + tracing::debug!( + "Enabled BweLossExperiment with parameters {:?}, {:?}, {:?}", + low_loss_threshold, + high_loss_threshold, + bitrate_threshold_kbps + ); + } + let bitrate_threshold = DataRate::from_kilobits_per_sec_float(bitrate_threshold_kbps); + + let mut loss_based_bandwidth_estimator_v2 = + LossBasedBweV2::new(field_trials.loss_based_bwe_v2.clone()); + if field_trials.loss_based_bwe_v2.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), + link_capacity: LinkCapacityTracker::default(), + min_bitrate_history: VecDeque::new(), + lost_packets_since_last_loss_update: 0, + expected_packets_since_last_loss_update: 0, + 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(), + has_decreased_since_last_fraction_loss: false, + last_loss_feedback: Timestamp::minus_infinity(), + last_loss_packet_report: Timestamp::minus_infinity(), + last_fraction_loss: 0, + last_logged_fraction_loss: 0, + last_round_trip_time: TimeDelta::zero(), + receiver_limit: DataRate::plus_infinity(), + delay_based_limit: DataRate::plus_infinity(), + time_last_decrease: Timestamp::minus_infinity(), + first_report_time: Timestamp::minus_infinity(), + initially_lost_packets: 0, + bitrate_at_2_seconds: DataRate::zero(), + last_rtc_event_log: Timestamp::minus_infinity(), + low_loss_threshold, + high_loss_threshold, + bitrate_threshold, + loss_based_bandwidth_estimator_v1: LossBasedBandwidthEstimation::new( + field_trials.loss_based_control.clone(), + ), + loss_based_bandwidth_estimator_v2, + loss_based_state: LossBasedState::DelayBasedEstimate, + disable_receiver_limit_caps_only: field_trials.receiver_limit_caps_only, + acknowledged_rate: None, + field_trials, + } + } + + pub fn on_route_change(&mut self) { + self.lost_packets_since_last_loss_update = 0; + self.expected_packets_since_last_loss_update = 0; + self.current_target = DataRate::zero(); + self.min_bitrate_configured = CONGESTION_CONTROLLER_MIN_BITRATE; + self.max_bitrate_configured = Self::DEFAULT_MAX_BITRATE; + self.last_low_bitrate_log = Timestamp::minus_infinity(); + self.has_decreased_since_last_fraction_loss = false; + self.last_loss_feedback = Timestamp::minus_infinity(); + self.last_loss_packet_report = Timestamp::minus_infinity(); + self.last_fraction_loss = 0; + self.last_logged_fraction_loss = 0; + self.last_round_trip_time = TimeDelta::zero(); + self.receiver_limit = DataRate::plus_infinity(); + self.delay_based_limit = DataRate::plus_infinity(); + self.time_last_decrease = Timestamp::minus_infinity(); + self.first_report_time = Timestamp::minus_infinity(); + self.initially_lost_packets = 0; + self.bitrate_at_2_seconds = DataRate::zero(); + self.last_rtc_event_log = Timestamp::minus_infinity(); + if self.loss_based_bandwidth_estimator_v2_enabled() + && self.loss_based_bandwidth_estimator_v2.use_in_start_phase() + { + self.loss_based_bandwidth_estimator_v2 = + LossBasedBweV2::new(self.field_trials.loss_based_bwe_v2.clone()); + } + } + + pub fn target_rate(&self) -> DataRate { + let mut target: DataRate = self.current_target; + if !self.disable_receiver_limit_caps_only { + target = std::cmp::min(target, self.receiver_limit); + } + std::cmp::max(self.min_bitrate_configured, 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 fraction_loss(&self) -> u8 { + self.last_fraction_loss + } + + pub fn round_trip_time(&self) -> TimeDelta { + self.last_round_trip_time + } + + pub fn get_estimated_link_capacity(&self) -> DataRate { + self.link_capacity.estimate() + } + // 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.link_capacity.on_rtt_backoff(new_bitrate, at_time); + 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 REMB and/or delay-based estimate during the first 2 seconds if + // we haven't had any packet loss reported, to allow startup bitrate probing. + if self.last_fraction_loss == 0 + && self.is_in_start_phase(at_time) + && !self + .loss_based_bandwidth_estimator_v2 + .ready_to_use_in_start_phase() + { + let mut new_bitrate: DataRate = self.current_target; + // TODO(srte): We should not allow the new_bitrate to be larger than the + // receiver limit here. + if self.receiver_limit.is_finite() { + new_bitrate = std::cmp::max(self.receiver_limit, new_bitrate); + } + if self.delay_based_limit.is_finite() { + new_bitrate = std::cmp::max(self.delay_based_limit, new_bitrate); + } + if self.loss_based_bandwidth_estimator_v1_enabled() { + self.loss_based_bandwidth_estimator_v1 + .initialize(new_bitrate); + } + + if new_bitrate != self.current_target { + self.min_bitrate_history.clear(); + if self.loss_based_bandwidth_estimator_v1_enabled() { + self.min_bitrate_history.push_back((at_time, new_bitrate)); + } else { + self.min_bitrate_history + .push_back((at_time, self.current_target)); + } + self.update_target_bitrate(new_bitrate, at_time); + return; + } + } + self.update_min_history(at_time); + if self.last_loss_packet_report.is_infinite() { + // No feedback received. + // TODO(srte): This is likely redundant in most cases. + self.apply_target_limits(at_time); + return; + } + + if self.loss_based_bandwidth_estimator_v1_ready_for_use() { + let new_bitrate: DataRate = self.loss_based_bandwidth_estimator_v1.update( + at_time, + self.min_bitrate_history.front().unwrap().1, + self.delay_based_limit, + self.last_round_trip_time, + ); + self.update_target_bitrate(new_bitrate, 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; + } + + let time_since_loss_packet_report: TimeDelta = at_time - self.last_loss_packet_report; + if time_since_loss_packet_report < 1.2 * Self::MAX_RTCP_FEEDBACK_INTERVAL { + // We only care about loss above a given bitrate threshold. + let loss: f32 = self.last_fraction_loss as f32 / 256.0; + // We only make decisions based on loss when the bitrate is above a + // threshold. This is a crude way of handling loss which is uncorrelated + // to congestion. + if self.current_target < self.bitrate_threshold || loss <= self.low_loss_threshold { + // Loss < 2%: Increase rate by 8% of the min bitrate in the last + // BweIncreaseInterval. + // Note that by remembering the bitrate over the last second one can + // rampup up one second faster than if only allowed to start ramping + // at 8% per second rate now. E.g.: + // If sending a constant 100kbps it can rampup immediately to 108kbps + // whenever a receiver report is received with lower packet loss. + // If instead one would do: self.current_bitrate *= 1.08^(delta time), + // it would take over one second since the lower packet loss to achieve + // 108kbps. + let mut new_bitrate: DataRate = DataRate::from_bits_per_sec_float( + self.min_bitrate_history.front().unwrap().1.bps_float() * 1.08 + 0.5, + ); + + // Add 1 kbps extra, just to make sure that we do not get stuck + // (gives a little extra increase at low rates, negligible at higher + // rates). + new_bitrate += DataRate::from_bits_per_sec(1000); + self.update_target_bitrate(new_bitrate, at_time); + return; + } else if self.current_target > self.bitrate_threshold { + if loss <= self.high_loss_threshold { + // Loss between 2% - 10%: Do nothing. + } else { + // Loss > 10%: Limit the rate decreases to once a BweDecreaseInterval + // + rtt. + if !self.has_decreased_since_last_fraction_loss + && (at_time - self.time_last_decrease) + >= (Self::BWE_DECREASE_INTERVAL + self.last_round_trip_time) + { + self.time_last_decrease = at_time; + + // Reduce rate: + // newRate = rate * (1 - 0.5*lossRate); + // where packetLoss = 256*lossRate; + let new_bitrate: DataRate = DataRate::from_bits_per_sec_float( + (self.current_target.bps_float() + * (512.0 - self.last_fraction_loss as f64)) + / 512.0, + ); + self.has_decreased_since_last_fraction_loss = true; + self.update_target_bitrate(new_bitrate, 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 we receive a RTCP message with TMMBR or REMB. + pub fn update_receiver_estimate(&mut self, at_time: Timestamp, bandwidth: DataRate) { + // TODO(srte): Ensure caller passes plus_infinity, not zero, to represent no + // limitation. + self.receiver_limit = if bandwidth.is_zero() { + DataRate::plus_infinity() + } else { + bandwidth + }; + self.apply_target_limits(at_time); + } + // Call when a new delay-based estimate is available. + pub fn update_delay_based_estimate(&mut self, at_time: Timestamp, bitrate: DataRate) { + self.link_capacity + .update_delay_based_estimate(at_time, bitrate); + // 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); + } + // Call when we receive a RTCP message with a ReceiveBlock. + pub fn update_packets_lost( + &mut self, + packets_lost: i64, + number_of_packets: i64, + at_time: Timestamp, + ) { + self.last_loss_feedback = at_time; + if self.first_report_time.is_infinite() { + self.first_report_time = at_time; + } + + // Check sequence number diff and weight loss report + if number_of_packets > 0 { + let expected: i64 = self.expected_packets_since_last_loss_update + number_of_packets; + + // Don't generate a loss rate until it can be based on enough packets. + if expected < Self::LIMIT_NUM_PACKETS { + // Accumulate reports. + self.expected_packets_since_last_loss_update = expected; + self.lost_packets_since_last_loss_update += packets_lost; + return; + } + + self.has_decreased_since_last_fraction_loss = false; + let lost_q8: i64 = + std::cmp::max(self.lost_packets_since_last_loss_update + packets_lost, 0) << 8; + self.last_fraction_loss = (lost_q8 / expected).min(255) as u8; + + // Reset accumulators. + self.lost_packets_since_last_loss_update = 0; + self.expected_packets_since_last_loss_update = 0; + self.last_loss_packet_report = at_time; + self.update_estimate(at_time); + } + } + // Call when we receive a RTCP message with a ReceiveBlock. + pub fn update_rtt(&mut self, rtt: TimeDelta, _at_time: Timestamp) { + // Update RTT if we were able to compute an RTT based on this RTCP. + // FlexFEC doesn't send RTCP SR, which means we won't be able to compute RTT. + 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.link_capacity.on_starting_rate(send_bitrate); + self.set_send_bitrate(send_bitrate, at_time); + } + } + pub fn set_send_bitrate(&mut self, bitrate: DataRate, at_time: Timestamp) { + 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); + // Clear last sent bitrate history so the new value can be used directly + // and not capped. + self.min_bitrate_history.clear(); + } + 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, + at_time: Timestamp, + ) { + self.acknowledged_rate = acknowledged_rate; + let acknowledged_rate = match acknowledged_rate { + Some(rate) => rate, + None => return, + }; + if self.loss_based_bandwidth_estimator_v1_enabled() { + self.loss_based_bandwidth_estimator_v1 + .update_acknowledged_bitrate(acknowledged_rate, at_time); + } + if self.loss_based_bandwidth_estimator_v2_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_v1_enabled() { + self.loss_based_bandwidth_estimator_v1 + .update_loss_statistics(&report.packet_feedbacks, report.feedback_time); + } + if self.loss_based_bandwidth_estimator_v2_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); + } + } + pub fn pace_at_loss_based_estimate(&self) -> bool { + self.loss_based_bandwidth_estimator_v2_ready_for_use() + && self + .loss_based_bandwidth_estimator_v2 + .pace_at_loss_based_estimate() + } + + fn is_in_start_phase(&self, at_time: Timestamp) -> bool { + self.first_report_time.is_infinite() || at_time - self.first_report_time < Self::START_PHASE + } + + // Updates history of min bitrates. + // After this method returns self.min_bitrate_history.front().second contains the + // min bitrate used during last BweIncreaseIntervalMs. + fn update_min_history(&mut self, at_time: Timestamp) { + // Remove old data points from history. + // Since history precision is in ms, add one so it is able to increase + // bitrate if it is off by as little as 0.5ms. + while !self.min_bitrate_history.is_empty() + && at_time - self.min_bitrate_history.front().unwrap().0 + TimeDelta::from_millis(1) + > Self::BWE_INCREASE_INTERVAL + { + self.min_bitrate_history.pop_front(); + } + + // Typical minimum sliding-window algorithm: Pop values higher than current + // bitrate before pushing it. + while !self.min_bitrate_history.is_empty() + && self.current_target <= self.min_bitrate_history.back().unwrap().1 + { + self.min_bitrate_history.pop_back(); + } + + self.min_bitrate_history + .push_back((at_time, self.current_target)); + } + + // Gets the upper limit for the target bitrate. This is the minimum of the + // delay based limit, the receiver limit and the loss based controller limit. + fn get_upper_limit(&self) -> DataRate { + let mut upper_limit: DataRate = self.delay_based_limit; + if self.disable_receiver_limit_caps_only { + upper_limit = std::cmp::min(upper_limit, self.receiver_limit); + } + std::cmp::min(upper_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::warn!( + "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 and updates the event log. + 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; + self.link_capacity + .on_rate_update(self.acknowledged_rate, self.current_target, at_time); + } + // 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_v1_enabled(&self) -> bool { + self.field_trials.loss_based_control.enabled && !self.field_trials.loss_based_bwe_v2.enabled + } + fn loss_based_bandwidth_estimator_v2_enabled(&self) -> bool { + self.field_trials.loss_based_bwe_v2.enabled + } + + fn loss_based_bandwidth_estimator_v1_ready_for_use(&self) -> bool { + self.loss_based_bandwidth_estimator_v1_enabled() + && self.loss_based_bandwidth_estimator_v1.in_use() + } + 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::*; + + fn test_probing(use_delay_based: bool) { + let mut bwe = SendSideBandwidthEstimation::new(Default::default()); + let mut now_ms: i64 = 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), + Timestamp::from_millis(now_ms), + ); + + const REMB_BPS: i64 = 1000000; + const SECOND_REMB_BPS: i64 = REMB_BPS + 500000; + + bwe.update_packets_lost( + /*packets_lost=*/ 0, + /*number_of_packets=*/ 1, + Timestamp::from_millis(now_ms), + ); + bwe.update_rtt(TimeDelta::from_millis(50), Timestamp::from_millis(now_ms)); + + // Initial REMB applies immediately. + if use_delay_based { + bwe.update_delay_based_estimate( + Timestamp::from_millis(now_ms), + DataRate::from_bits_per_sec(REMB_BPS), + ); + } else { + bwe.update_receiver_estimate( + Timestamp::from_millis(now_ms), + DataRate::from_bits_per_sec(REMB_BPS), + ); + } + bwe.update_estimate(Timestamp::from_millis(now_ms)); + assert_eq!(REMB_BPS, bwe.target_rate().bps()); + + // Second REMB doesn't apply immediately. + now_ms += 2001; + if use_delay_based { + bwe.update_delay_based_estimate( + Timestamp::from_millis(now_ms), + DataRate::from_bits_per_sec(SECOND_REMB_BPS), + ); + } else { + bwe.update_receiver_estimate( + Timestamp::from_millis(now_ms), + DataRate::from_bits_per_sec(SECOND_REMB_BPS), + ); + } + bwe.update_estimate(Timestamp::from_millis(now_ms)); + assert_eq!(REMB_BPS, bwe.target_rate().bps()); + } + + #[test] + fn initial_remb_with_probing() { + test_probing(false); + } + + #[test] + fn initial_delay_based_bwe_with_probing() { + test_probing(true); + } + + #[test] + fn doesnt_reapply_bitrate_decrease_without_following_remb() { + let mut bwe = SendSideBandwidthEstimation::new(Default::default()); + const MIN_BITRATE_BPS: i64 = 100000; + const INITIAL_BITRATE_BPS: i64 = 1000000; + let mut now_ms: i64 = 1000; + bwe.set_min_max_bitrate( + DataRate::from_bits_per_sec(MIN_BITRATE_BPS), + DataRate::from_bits_per_sec(1500000), + ); + bwe.set_send_bitrate( + DataRate::from_bits_per_sec(INITIAL_BITRATE_BPS), + Timestamp::from_millis(now_ms), + ); + + const FRACTION_LOSS: u8 = 128; + const RTT_MS: i64 = 50; + now_ms += 10000; + + assert_eq!(INITIAL_BITRATE_BPS, bwe.target_rate().bps()); + assert_eq!(0, bwe.fraction_loss()); + assert_eq!(0, bwe.round_trip_time().ms()); + + // Signal heavy loss to go down in bitrate. + bwe.update_packets_lost( + /*packets_lost=*/ 50, + /*number_of_packets=*/ 100, + Timestamp::from_millis(now_ms), + ); + bwe.update_rtt( + TimeDelta::from_millis(RTT_MS), + Timestamp::from_millis(now_ms), + ); + + // Trigger an update 2 seconds later to not be rate limited. + now_ms += 1000; + bwe.update_estimate(Timestamp::from_millis(now_ms)); + assert!(bwe.target_rate().bps() < INITIAL_BITRATE_BPS); + // Verify that the obtained bitrate isn't hitting the min bitrate, or this + // test doesn't make sense. If this ever happens, update the thresholds or + // loss rates so that it doesn't hit min bitrate after one bitrate update. + assert!(bwe.target_rate().bps() > MIN_BITRATE_BPS); + assert_eq!(FRACTION_LOSS, bwe.fraction_loss()); + assert_eq!(RTT_MS, bwe.round_trip_time().ms()); + + // Triggering an update shouldn't apply further downgrade nor upgrade since + // there's no intermediate receiver block received indicating whether this is + // currently good or not. + let last_bitrate_bps: i64 = bwe.target_rate().bps(); + // Trigger an update 2 seconds later to not be rate limited (but it still + // shouldn't update). + now_ms += 1000; + bwe.update_estimate(Timestamp::from_millis(now_ms)); + + assert_eq!(last_bitrate_bps, bwe.target_rate().bps()); + // The old loss rate should still be applied though. + assert_eq!(FRACTION_LOSS, bwe.fraction_loss()); + assert_eq!(RTT_MS, bwe.round_trip_time().ms()); + } + + #[test] + fn setting_send_bitrate_overrides_delay_based_estimate() { + let mut bwe = SendSideBandwidthEstimation::new(Default::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 fraction_loss_is_not_overflowed() { + let mut bwe = SendSideBandwidthEstimation::new(Default::default()); + const MIN_BITRATE_BPS: i64 = 100000; + const INITIAL_BITRATE_BPS: i64 = 1000000; + let mut now_ms: i64 = 1000; + bwe.set_min_max_bitrate( + DataRate::from_bits_per_sec(MIN_BITRATE_BPS), + DataRate::from_bits_per_sec(1500000), + ); + bwe.set_send_bitrate( + DataRate::from_bits_per_sec(INITIAL_BITRATE_BPS), + Timestamp::from_millis(now_ms), + ); + + now_ms += 10000; + + assert_eq!(INITIAL_BITRATE_BPS, bwe.target_rate().bps()); + assert_eq!(0, bwe.fraction_loss()); + + // Signal negative loss. + bwe.update_packets_lost( + /*packets_lost=*/ -1, + /*number_of_packets=*/ 100, + Timestamp::from_millis(now_ms), + ); + assert_eq!(0, bwe.fraction_loss()); + } + + #[test] + fn rtt_is_above_limit_if_rtt_greater_than_limit() { + let mut bwe = SendSideBandwidthEstimation::new(Default::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::new(Default::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/trendline_estimator.rs b/rs/moq-congestion/src/gcc/trendline_estimator.rs new file mode 100644 index 00000000000..7afa18570d2 --- /dev/null +++ b/rs/moq-congestion/src/gcc/trendline_estimator.rs @@ -0,0 +1,525 @@ +/* + * 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::{api::transport::BandwidthUsage, DelayIncreaseDetectorInterface}; + +// 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, + } + } +} + +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, +} + +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 { + if 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 From b94535c342adcd8cc58fb74a5879ee0519efc076 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 03:24:17 -0800 Subject: [PATCH 03/15] build(congestion): build the vendored goog_cc as a private module Paths move under crate::gcc, approx gives way to a local macro, and the two unused files (inter_arrival, is_newer) go. Its 265 tests pass. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + rs/moq-congestion/Cargo.toml | 1 + .../src/gcc/acknowledged_bitrate_estimator.rs | 2 +- ...cknowledged_bitrate_estimator_interface.rs | 2 +- rs/moq-congestion/src/gcc/alr_detector.rs | 2 +- .../src/gcc/api/network_control.rs | 2 +- .../src/gcc/api/transport/network_types.rs | 2 +- .../src/gcc/api/units/data_rate.rs | 1 - .../src/gcc/api/units/is_newer.rs | 184 ---- rs/moq-congestion/src/gcc/api/units/mod.rs | 2 - .../src/gcc/api/units/time_delta.rs | 1 - .../src/gcc/api/units/unit_base.rs | 3 +- .../src/gcc/bitrate_estimator.rs | 2 +- .../congestion_window_pushback_controller.rs | 2 +- rs/moq-congestion/src/gcc/delay_based_bwe.rs | 8 +- .../gcc/delay_increase_detector_interface.rs | 2 +- .../src/gcc/experiments/field_trials.rs | 2 +- .../src/gcc/experiments/rate_control.rs | 2 +- .../src/gcc/goog_cc_network_control.rs | 6 +- .../src/gcc/inter_arrival_delta.rs | 2 +- .../src/gcc/link_capacity_estimator.rs | 2 +- .../gcc/loss_based_bandwidth_estimation.rs | 2 +- .../src/gcc/loss_based_bwe_v2.rs | 6 +- rs/moq-congestion/src/gcc/mod.rs | 45 +- .../src/gcc/probe_bitrate_estimator.rs | 3 +- rs/moq-congestion/src/gcc/probe_controller.rs | 2 +- .../aimd_rate_control.rs | 2 +- .../remote_bitrate_estimator/bwe_defines.rs | 2 +- .../remote_bitrate_estimator/inter_arrival.rs | 953 ------------------ .../src/gcc/remote_bitrate_estimator/mod.rs | 2 - .../src/gcc/robust_throughput_estimator.rs | 7 +- .../src/gcc/send_side_bandwidth_estimation.rs | 4 +- .../src/gcc/trendline_estimator.rs | 2 +- rs/moq-congestion/src/lib.rs | 1 + 34 files changed, 51 insertions(+), 1211 deletions(-) delete mode 100644 rs/moq-congestion/src/gcc/api/units/is_newer.rs delete mode 100644 rs/moq-congestion/src/gcc/remote_bitrate_estimator/inter_arrival.rs diff --git a/Cargo.lock b/Cargo.lock index df17b147ca5..955fb2e1d61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5572,6 +5572,7 @@ dependencies = [ "rand 0.10.3", "rand_pcg", "stats_alloc", + "tracing", ] [[package]] diff --git a/rs/moq-congestion/Cargo.toml b/rs/moq-congestion/Cargo.toml index aa9eedf09b7..579b4a37736 100644 --- a/rs/moq-congestion/Cargo.toml +++ b/rs/moq-congestion/Cargo.toml @@ -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/src/gcc/acknowledged_bitrate_estimator.rs b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs index b9f5e43acc3..7399729fae2 100644 --- a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs +++ b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::{ +use crate::gcc::{ api::{ transport::PacketResult, units::{DataRate, Timestamp}, diff --git a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs index eadcf74789b..e4d227c69cf 100644 --- a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs +++ b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::{ +use crate::gcc::api::{ transport::PacketResult, units::{DataRate, Timestamp}, }; diff --git a/rs/moq-congestion/src/gcc/alr_detector.rs b/rs/moq-congestion/src/gcc/alr_detector.rs index fa87054c921..92c7e921de3 100644 --- a/rs/moq-congestion/src/gcc/alr_detector.rs +++ b/rs/moq-congestion/src/gcc/alr_detector.rs @@ -10,7 +10,7 @@ use std::time::Instant; -use crate::{experiments::AlrExperimentSettings, pacing::IntervalBudget}; +use crate::gcc::{experiments::AlrExperimentSettings, pacing::IntervalBudget}; #[derive(Clone, Debug)] pub struct AlrDetectorConfig { diff --git a/rs/moq-congestion/src/gcc/api/network_control.rs b/rs/moq-congestion/src/gcc/api/network_control.rs index 00bf6858124..47ed789bb2a 100644 --- a/rs/moq-congestion/src/gcc/api/network_control.rs +++ b/rs/moq-congestion/src/gcc/api/network_control.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::experiments::FieldTrials; +use crate::gcc::experiments::FieldTrials; use super::{transport::*, units::TimeDelta}; diff --git a/rs/moq-congestion/src/gcc/api/transport/network_types.rs b/rs/moq-congestion/src/gcc/api/transport/network_types.rs index 5582007c076..828bc59532f 100644 --- a/rs/moq-congestion/src/gcc/api/transport/network_types.rs +++ b/rs/moq-congestion/src/gcc/api/transport/network_types.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use crate::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; +use crate::gcc::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; use super::EcnMarking; diff --git a/rs/moq-congestion/src/gcc/api/units/data_rate.rs b/rs/moq-congestion/src/gcc/api/units/data_rate.rs index 9337aff6a09..b534adb2ffc 100644 --- a/rs/moq-congestion/src/gcc/api/units/data_rate.rs +++ b/rs/moq-congestion/src/gcc/api/units/data_rate.rs @@ -178,7 +178,6 @@ impl fmt::Debug for DataRate { #[cfg(test)] mod test { - use approx::assert_relative_eq; use super::*; diff --git a/rs/moq-congestion/src/gcc/api/units/is_newer.rs b/rs/moq-congestion/src/gcc/api/units/is_newer.rs deleted file mode 100644 index 5c4cea52067..00000000000 --- a/rs/moq-congestion/src/gcc/api/units/is_newer.rs +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright (c) 2013 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. - */ - -const fn is_newer_16(value: u16, prev_value: u16) -> bool { - // kBreakpoint is the half-way mark for the type U. For instance, for a - // u16 it will be 0x8000, and for a u32, it will be 0x8000000. - const BREAKPOINT: u16 = (u16::MAX >> 1) + 1; - // Distinguish between elements that are exactly kBreakpoint apart. - // If t1>t2 and |t1-t2| = kBreakpoint: IsNewer(t1,t2)=true, - // IsNewer(t2,t1)=false - // rather than having IsNewer(t1,t2) = IsNewer(t2,t1) = false. - match value.wrapping_sub(prev_value) { - 1..BREAKPOINT => true, - BREAKPOINT => value > prev_value, - _ => false, - } -} - -const fn is_newer_32(value: u32, prev_value: u32) -> bool { - // kBreakpoint is the half-way mark for the type U. For instance, for a - // u16 it will be 0x8000, and for a u32, it will be 0x8000000. - const BREAKPOINT: u32 = (u32::MAX >> 1) + 1; - // Distinguish between elements that are exactly kBreakpoint apart. - // If t1>t2 and |t1-t2| = kBreakpoint: IsNewer(t1,t2)=true, - // IsNewer(t2,t1)=false - // rather than having IsNewer(t1,t2) = IsNewer(t2,t1) = false. - match value.wrapping_sub(prev_value) { - 1..BREAKPOINT => true, - BREAKPOINT => value > prev_value, - _ => false, - } -} - -// NB: Doesn't fulfill strict weak ordering requirements. -// Mustn't be used as std::map Compare function. -pub const fn is_newer_sequence_number(sequence_number: u16, prev_sequence_number: u16) -> bool { - is_newer_16(sequence_number, prev_sequence_number) -} - -// NB: Doesn't fulfill strict weak ordering requirements. -// Mustn't be used as std::map Compare function. -pub const fn is_newer_timestamp(timestamp: u32, prev_timestamp: u32) -> bool { - is_newer_32(timestamp, prev_timestamp) -} - -pub const fn latest_sequence_number(sequence_number1: u16, sequence_number2: u16) -> u16 { - if is_newer_sequence_number(sequence_number1, sequence_number2) { - sequence_number1 - } else { - sequence_number2 - } -} - -pub const fn latest_timestamp(timestamp1: u32, timestamp2: u32) -> u32 { - if is_newer_timestamp(timestamp1, timestamp2) { - timestamp1 - } else { - timestamp2 - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn is_newer_sequence_number_equal() { - assert!(!is_newer_sequence_number(0x0001, 0x0001)); - } - - #[test] - fn is_newer_sequence_number_no_wrap() { - assert!(is_newer_sequence_number(0xFFFF, 0xFFFE)); - assert!(is_newer_sequence_number(0x0001, 0x0000)); - assert!(is_newer_sequence_number(0x0100, 0x00FF)); - } - - #[test] - fn is_newer_sequence_number_forward_wrap() { - assert!(is_newer_sequence_number(0x0000, 0xFFFF)); - assert!(is_newer_sequence_number(0x0000, 0xFF00)); - assert!(is_newer_sequence_number(0x00FF, 0xFFFF)); - assert!(is_newer_sequence_number(0x00FF, 0xFF00)); - } - - #[test] - fn is_newer_sequence_number_backward_wrap() { - assert!(!is_newer_sequence_number(0xFFFF, 0x0000)); - assert!(!is_newer_sequence_number(0xFF00, 0x0000)); - assert!(!is_newer_sequence_number(0xFFFF, 0x00FF)); - assert!(!is_newer_sequence_number(0xFF00, 0x00FF)); - } - - #[test] - fn is_newer_sequence_number_half_way_apart() { - assert!(is_newer_sequence_number(0x8000, 0x0000)); - assert!(!is_newer_sequence_number(0x0000, 0x8000)); - } - - #[test] - fn is_newer_timestamp_equal() { - assert!(!is_newer_timestamp(0x00000001, 0x000000001)); - } - - #[test] - fn is_newer_timestamp_no_wrap() { - assert!(is_newer_timestamp(0xFFFFFFFF, 0xFFFFFFFE)); - assert!(is_newer_timestamp(0x00000001, 0x00000000)); - assert!(is_newer_timestamp(0x00010000, 0x0000FFFF)); - } - - #[test] - fn is_newer_timestamp_forward_wrap() { - assert!(is_newer_timestamp(0x00000000, 0xFFFFFFFF)); - assert!(is_newer_timestamp(0x00000000, 0xFFFF0000)); - assert!(is_newer_timestamp(0x0000FFFF, 0xFFFFFFFF)); - assert!(is_newer_timestamp(0x0000FFFF, 0xFFFF0000)); - } - - #[test] - fn is_newer_timestamp_backward_wrap() { - assert!(!is_newer_timestamp(0xFFFFFFFF, 0x00000000)); - assert!(!is_newer_timestamp(0xFFFF0000, 0x00000000)); - assert!(!is_newer_timestamp(0xFFFFFFFF, 0x0000FFFF)); - assert!(!is_newer_timestamp(0xFFFF0000, 0x0000FFFF)); - } - - #[test] - fn is_newer_timestamp_half_way_apart() { - assert!(is_newer_timestamp(0x80000000, 0x00000000)); - assert!(!is_newer_timestamp(0x00000000, 0x80000000)); - } - - #[test] - fn latest_sequence_number_no_wrap() { - assert_eq!(0xFFFF, latest_sequence_number(0xFFFF, 0xFFFE)); - assert_eq!(0x0001, latest_sequence_number(0x0001, 0x0000)); - assert_eq!(0x0100, latest_sequence_number(0x0100, 0x00FF)); - - assert_eq!(0xFFFF, latest_sequence_number(0xFFFE, 0xFFFF)); - assert_eq!(0x0001, latest_sequence_number(0x0000, 0x0001)); - assert_eq!(0x0100, latest_sequence_number(0x00FF, 0x0100)); - } - - #[test] - fn latest_sequence_number_wrap() { - assert_eq!(0x0000, latest_sequence_number(0x0000, 0xFFFF)); - assert_eq!(0x0000, latest_sequence_number(0x0000, 0xFF00)); - assert_eq!(0x00FF, latest_sequence_number(0x00FF, 0xFFFF)); - assert_eq!(0x00FF, latest_sequence_number(0x00FF, 0xFF00)); - - assert_eq!(0x0000, latest_sequence_number(0xFFFF, 0x0000)); - assert_eq!(0x0000, latest_sequence_number(0xFF00, 0x0000)); - assert_eq!(0x00FF, latest_sequence_number(0xFFFF, 0x00FF)); - assert_eq!(0x00FF, latest_sequence_number(0xFF00, 0x00FF)); - } - - #[test] - fn latest_timestamp_no_wrap() { - assert_eq!(0xFFFFFFFF, latest_timestamp(0xFFFFFFFF, 0xFFFFFFFE)); - assert_eq!(0x00000001, latest_timestamp(0x00000001, 0x00000000)); - assert_eq!(0x00010000, latest_timestamp(0x00010000, 0x0000FFFF)); - } - - #[test] - fn latest_timestamp_wrap() { - assert_eq!(0x00000000, latest_timestamp(0x00000000, 0xFFFFFFFF)); - assert_eq!(0x00000000, latest_timestamp(0x00000000, 0xFFFF0000)); - assert_eq!(0x0000FFFF, latest_timestamp(0x0000FFFF, 0xFFFFFFFF)); - assert_eq!(0x0000FFFF, latest_timestamp(0x0000FFFF, 0xFFFF0000)); - - assert_eq!(0x00000000, latest_timestamp(0xFFFFFFFF, 0x00000000)); - assert_eq!(0x00000000, latest_timestamp(0xFFFF0000, 0x00000000)); - assert_eq!(0x0000FFFF, latest_timestamp(0xFFFFFFFF, 0x0000FFFF)); - assert_eq!(0x0000FFFF, latest_timestamp(0xFFFF0000, 0x0000FFFF)); - } -} diff --git a/rs/moq-congestion/src/gcc/api/units/mod.rs b/rs/moq-congestion/src/gcc/api/units/mod.rs index f212751758b..4a04ce5e0b6 100644 --- a/rs/moq-congestion/src/gcc/api/units/mod.rs +++ b/rs/moq-congestion/src/gcc/api/units/mod.rs @@ -1,7 +1,6 @@ mod data_rate; mod data_size; mod frequency; -// mod is_newer; mod time_delta; mod timestamp; mod unit_base; @@ -12,5 +11,4 @@ pub use frequency::*; pub use time_delta::*; pub use timestamp::*; -//pub(crate) use is_newer::*; 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 index a944453aef3..3f9451abaad 100644 --- a/rs/moq-congestion/src/gcc/api/units/time_delta.rs +++ b/rs/moq-congestion/src/gcc/api/units/time_delta.rs @@ -140,7 +140,6 @@ impl TryFrom for Duration { #[cfg(test)] mod test { - use approx::assert_relative_eq; use super::*; diff --git a/rs/moq-congestion/src/gcc/api/units/unit_base.rs b/rs/moq-congestion/src/gcc/api/units/unit_base.rs index 5274bd4d415..74c13fd2dec 100644 --- a/rs/moq-congestion/src/gcc/api/units/unit_base.rs +++ b/rs/moq-congestion/src/gcc/api/units/unit_base.rs @@ -174,7 +174,7 @@ macro_rules! unit_base { macro_rules! relative_unit { ($ty:ident) => { - crate::api::units::unit_base!($ty); + crate::gcc::api::units::unit_base!($ty); impl $ty { pub fn clamp_mut(&mut self, min_value: Self, max_value: Self) { @@ -495,7 +495,6 @@ pub(crate) use unit_base; mod test { use std::fmt; - use approx::assert_relative_eq; use super::*; diff --git a/rs/moq-congestion/src/gcc/bitrate_estimator.rs b/rs/moq-congestion/src/gcc/bitrate_estimator.rs index 7321d002e5a..0721f1e2ac2 100644 --- a/rs/moq-congestion/src/gcc/bitrate_estimator.rs +++ b/rs/moq-congestion/src/gcc/bitrate_estimator.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; +use crate::gcc::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; // WebRTC-BweThroughputWindowConfig #[derive(Debug, Clone)] diff --git a/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs b/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs index 17e4a0b28d0..14e5186f586 100644 --- a/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs +++ b/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::{ +use crate::gcc::{ api::units::DataSize, experiments::{FieldTrials, RateControlSettings}, }; diff --git a/rs/moq-congestion/src/gcc/delay_based_bwe.rs b/rs/moq-congestion/src/gcc/delay_based_bwe.rs index 2369fb41d76..b50b7ee2038 100644 --- a/rs/moq-congestion/src/gcc/delay_based_bwe.rs +++ b/rs/moq-congestion/src/gcc/delay_based_bwe.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::{ +use crate::gcc::{ api::{ transport::{BandwidthUsage, PacketResult, TransportPacketsFeedback}, units::{DataRate, DataSize, TimeDelta, Timestamp}, @@ -392,14 +392,12 @@ impl DelayBasedBwe { #[cfg(test)] mod test { - use crate::api::transport::{PacedPacketInfo, SentPacket}; - use crate::{ + use crate::gcc::api::transport::{PacedPacketInfo, SentPacket}; + use crate::gcc::{ AcknowledgedBitrateEstimator, AcknowledgedBitrateEstimatorInterface, ProbeBitrateEstimator, RobustThroughputEstimatorSettings, }; - use approx::assert_relative_eq; - use test_trace::test; pub use super::*; diff --git a/rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs b/rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs index 3b1ecc4842b..cfb4e72e67e 100644 --- a/rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs +++ b/rs/moq-congestion/src/gcc/delay_increase_detector_interface.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::transport::BandwidthUsage; +use crate::gcc::api::transport::BandwidthUsage; pub trait DelayIncreaseDetectorInterface { // Update the detector with a new sample. The deltas should represent deltas diff --git a/rs/moq-congestion/src/gcc/experiments/field_trials.rs b/rs/moq-congestion/src/gcc/experiments/field_trials.rs index 6df425f3a2a..3c832ec5a66 100644 --- a/rs/moq-congestion/src/gcc/experiments/field_trials.rs +++ b/rs/moq-congestion/src/gcc/experiments/field_trials.rs @@ -1,6 +1,6 @@ use super::{AlrExperimentSettings, CongestionWindowConfig, VideoRateControlConfig}; -use crate::{ +use crate::gcc::{ AlrDetectorConfig, BitrateEstimatorConfig, BweLossExperiment, BweSeparateAudioPacketsSettings, LossBasedBweV2Config, LossBasedControlConfig, ProbeControllerConfig, RobustThroughputEstimatorSettings, RttBasedBackoffConfig, diff --git a/rs/moq-congestion/src/gcc/experiments/rate_control.rs b/rs/moq-congestion/src/gcc/experiments/rate_control.rs index 162eea29ed1..35e42e68834 100644 --- a/rs/moq-congestion/src/gcc/experiments/rate_control.rs +++ b/rs/moq-congestion/src/gcc/experiments/rate_control.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::{api::units::DataSize, experiments::FieldTrials}; +use crate::gcc::{api::units::DataSize, experiments::FieldTrials}; /// WebRTC-CongestionWindow #[derive(Clone, Debug)] diff --git a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs index b76d3b930ef..977d41edd41 100644 --- a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs +++ b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs @@ -10,7 +10,7 @@ use std::collections::VecDeque; -use crate::{ +use crate::gcc::{ api::{ network_control::{NetworkControllerConfig, NetworkControllerInterface}, transport::{ @@ -946,10 +946,8 @@ fn get_bandwidth_limited_cause( #[cfg(test)] mod test { - use approx::assert_relative_eq; - use test_trace::test; - use crate::api::transport::PacketResult; + use crate::gcc::api::transport::PacketResult; use super::*; diff --git a/rs/moq-congestion/src/gcc/inter_arrival_delta.rs b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs index 9d364ca51c4..01ce1c6d929 100644 --- a/rs/moq-congestion/src/gcc/inter_arrival_delta.rs +++ b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::units::{TimeDelta, Timestamp}; +use crate::gcc::api::units::{TimeDelta, Timestamp}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] struct SendTimeGroup { diff --git a/rs/moq-congestion/src/gcc/link_capacity_estimator.rs b/rs/moq-congestion/src/gcc/link_capacity_estimator.rs index ce05df73091..0b94aa59545 100644 --- a/rs/moq-congestion/src/gcc/link_capacity_estimator.rs +++ b/rs/moq-congestion/src/gcc/link_capacity_estimator.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::units::DataRate; +use crate::gcc::api::units::DataRate; pub struct LinkCapacityEstimator { estimate_kbps: Option, diff --git a/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs b/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs index 0dd432ae70d..02b99d90766 100644 --- a/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs +++ b/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::{ +use crate::gcc::api::{ transport::PacketResult, units::{DataRate, TimeDelta, Timestamp}, }; diff --git a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs index c0b0bbbcc5f..6f4d4426ae4 100644 --- a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs +++ b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; -use crate::{ +use crate::gcc::{ api::{ transport::PacketResult, units::{DataRate, DataSize, TimeDelta, Timestamp}, @@ -1389,10 +1389,8 @@ fn get_loss_probability( #[cfg(test)] mod test { - use approx::assert_relative_eq; - use test_trace::test; - use crate::api::transport::SentPacket; + use crate::gcc::api::transport::SentPacket; use super::*; diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 0fd3b27030d..2569c3b8827 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -1,31 +1,19 @@ -//! # Google Congestion Control -//! -//! This crate is a Rust port of the [Google Congestion Control](https://webrtc.googlesource.com/src/+/refs/heads/main/modules/congestion_controller/goog_cc/) (GCC) implementation from the WebRTC project. -//! No more painful linking against libwebrtc. -//! -//! Google Congestion Control is the congestion control algorithm used in WebRTC. -//! It is a hybrid algorithm that combines the strengths of delay-based and loss-based congestion control. -//! The end goal is minimum latency and to avoid buffer-bloat as much as possible. -//! -//! All licensing from the original source code is preserved under a BSD-style license. -//! Thanks to the WebRTC project authors for their hard work on this implementation. -//! All I did was port it to Rust. -//! -//! ## Notable Changes -//! **NetworkStateEstimator and NetworkStatePredictor have been removed.** -//! They are not implemented in the WebRTC source code and are flagged with a "subject to change" comment. -//! I think this is a back-door for Google to tweak the congestion control algorithm? -//! -//! **FieldTrials is a type-safe struct instead of a string.** -//! The original implementation uses a string to represent field trials. -//! This was quite error-prone as many trials do not match the cooresponding field name. -//! I decided to create a [FieldTrials](experiments::FieldTrials) struct to represent field trials in a type-safe way. -//! -//! **RtcEventLog has been removed.** -//! The structured logs are cool, but have been removed for now to simplify the code. -//! -//! **RTC_LOG has been replaced with [tracing].** -//! Use [tracing_subscriber](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/index.html) to enable and configure the logger. +//! libwebrtc's Google Congestion Control, vendored from kixelated/goog_cc. + +/// `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; @@ -34,6 +22,7 @@ pub mod experiments; // We inline goog_cc modules to avoid goog_cc::goog_cc logging prefix. mod pacing; + mod remote_bitrate_estimator; mod acknowledged_bitrate_estimator; mod acknowledged_bitrate_estimator_interface; diff --git a/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs index 91a4bcb39ba..3243ed2fe98 100644 --- a/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs +++ b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs @@ -10,7 +10,7 @@ use std::collections::HashMap; -use crate::api::{ +use crate::gcc::api::{ transport::{PacedPacketInfo, PacketResult}, units::{DataRate, DataSize, TimeDelta, Timestamp}, }; @@ -202,7 +202,6 @@ impl ProbeBitrateEstimator { #[cfg(test)] mod test { use super::*; - use approx::assert_relative_eq; const DEFAULT_MIN_PROBES: i64 = 5; const DEFAULT_MIN_BYTES: i64 = 5000; diff --git a/rs/moq-congestion/src/gcc/probe_controller.rs b/rs/moq-congestion/src/gcc/probe_controller.rs index eb1b46ab15d..48d7a697703 100644 --- a/rs/moq-congestion/src/gcc/probe_controller.rs +++ b/rs/moq-congestion/src/gcc/probe_controller.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::{ +use crate::gcc::api::{ transport::{NetworkAvailability, NetworkStateEstimate, ProbeClusterConfig}, units::{DataRate, TimeDelta, Timestamp}, }; 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 index 98335c3b6e0..725ffe234e0 100644 --- 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 @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::{ +use crate::gcc::{ api::{ transport::{BandwidthUsage, NetworkStateEstimate}, units::{DataRate, DataSize, TimeDelta, Timestamp}, 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 index 09f26221059..5c2a2a977ed 100644 --- a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/bwe_defines.rs +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/bwe_defines.rs @@ -8,7 +8,7 @@ * be found in the AUTHORS file in the root of the source tree. */ -use crate::api::{ +use crate::gcc::api::{ transport::BandwidthUsage, units::{DataRate, TimeDelta}, }; diff --git a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/inter_arrival.rs b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/inter_arrival.rs deleted file mode 100644 index 6307fdfbefe..00000000000 --- a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/inter_arrival.rs +++ /dev/null @@ -1,953 +0,0 @@ -/* - * Copyright (c) 2013 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::api::units::latest_timestamp; - -#[derive(Clone, Copy, Debug)] -struct TimestampGroup { - pub size: usize, - pub first_timestamp: u32, - pub timestamp: u32, - pub first_arrival_ms: i64, - pub complete_time_ms: i64, - pub last_system_time_ms: i64, -} -impl TimestampGroup { - pub fn is_first_packet(&self) -> bool { - self.complete_time_ms == -1 - } -} - -impl Default for TimestampGroup { - fn default() -> Self { - Self { - size: 0, - first_timestamp: 0, - timestamp: 0, - first_arrival_ms: -1, - complete_time_ms: -1, - last_system_time_ms: -1, - } - } -} - -// Helper class to compute the inter-arrival time delta and the size delta -// between two timestamp groups. A timestamp is a 32 bit unsigned number with -// a client defined rate. -pub struct InterArrival { - timestamp_group_length_ticks: u32, - current_timestamp_group: TimestampGroup, - prev_timestamp_group: TimestampGroup, - timestamp_to_ms_coeff: f64, - num_consecutive_reordered_packets: usize, -} - -impl InterArrival { - // After this many packet groups received out of order InterArrival will - // reset, assuming that clocks have made a jump. - pub const REORDERED_RESET_THRESHOLD: usize = 3; - pub const ARRIVAL_TIME_OFFSET_THRESHOLD_MS: i64 = 3000; - - const BURST_DELTA_THRESHOLD_MS: i64 = 5; - const MAX_BURST_DURATION_MS: i64 = 100; - - // A timestamp group is defined as all packets with a timestamp which are at - // most timestamp_group_length_ticks older than the first timestamp in that - // group. - pub fn new(timestamp_group_length_ticks: u32, timestamp_to_ms_coeff: f64) -> Self { - Self { - timestamp_group_length_ticks, - current_timestamp_group: TimestampGroup::default(), - prev_timestamp_group: TimestampGroup::default(), - timestamp_to_ms_coeff, - 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. - // `timestamp` is the timestamp. - // `arrival_time_ms` is the local time at which the packet arrived. - // `packet_size` is the size of the packet. - // `timestamp_delta` (output) is the computed timestamp delta. - // `arrival_time_delta_ms` (output) is the computed arrival-time delta. - // `packet_size_delta` (output) is the computed size delta. - #[allow(clippy::too_many_arguments)] - pub fn compute_deltas( - &mut self, - timestamp: u32, - arrival_time_ms: i64, - system_time_ms: i64, - packet_size: usize, - timestamp_delta: &mut u32, - arrival_time_delta_ms: &mut i64, - packet_size_delta: &mut i64, - ) -> bool { - let mut calculated_deltas = 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.timestamp = timestamp; - self.current_timestamp_group.first_timestamp = timestamp; - self.current_timestamp_group.first_arrival_ms = arrival_time_ms; - } else if !self.packet_in_order(timestamp) { - return false; - } else if self.new_timestamp_group(arrival_time_ms, timestamp) { - // First packet of a later frame, the previous frame sample is ready. - if self.prev_timestamp_group.complete_time_ms >= 0 { - *timestamp_delta = self - .current_timestamp_group - .timestamp - .wrapping_sub(self.prev_timestamp_group.timestamp); - *arrival_time_delta_ms = self.current_timestamp_group.complete_time_ms - - self.prev_timestamp_group.complete_time_ms; - // Check system time differences to see if we have an unproportional jump - // in arrival time. In that case reset the inter-arrival computations. - let system_time_delta_ms: i64 = self.current_timestamp_group.last_system_time_ms - - self.prev_timestamp_group.last_system_time_ms; - if *arrival_time_delta_ms - system_time_delta_ms - >= Self::ARRIVAL_TIME_OFFSET_THRESHOLD_MS - { - 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_ms < 0 { - // 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 are being reordered on the path from the - socket to the bandwidth estimator. Ignoring this - packet for bandwidth estimation, resetting." - ); - self.reset(); - } - return false; - } else { - self.num_consecutive_reordered_packets = 0; - } - assert!(*arrival_time_delta_ms >= 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_timestamp = timestamp; - self.current_timestamp_group.timestamp = timestamp; - self.current_timestamp_group.first_arrival_ms = arrival_time_ms; - self.current_timestamp_group.size = 0; - } else { - self.current_timestamp_group.timestamp = - latest_timestamp(self.current_timestamp_group.timestamp, timestamp); - } - // Accumulate the frame size. - self.current_timestamp_group.size += packet_size; - self.current_timestamp_group.complete_time_ms = arrival_time_ms; - self.current_timestamp_group.last_system_time_ms = system_time_ms; - - calculated_deltas - } - - // Returns true if the packet with timestamp `timestamp` arrived in order. - fn packet_in_order(&self, timestamp: u32) -> bool { - if self.current_timestamp_group.is_first_packet() { - true - } else { - // Assume that a diff which is bigger than half the timestamp interval - // (32 bits) must be due to reordering. This code is almost identical to - // that in IsNewerTimestamp() in module_common_types.h. - let timestamp_diff: u32 = - timestamp.wrapping_sub(self.current_timestamp_group.first_timestamp); - timestamp_diff < 0x80000000 - } - } - - // Returns true if the last packet was the end of the current batch and the - // packet with `timestamp` is the first of a new batch. - fn new_timestamp_group(&self, arrival_time_ms: i64, timestamp: u32) -> bool { - if self.current_timestamp_group.is_first_packet() { - false - } else if self.belongs_to_burst(arrival_time_ms, timestamp) { - return false; - } else { - let timestamp_diff: u32 = - timestamp.wrapping_sub(self.current_timestamp_group.first_timestamp); - return timestamp_diff > self.timestamp_group_length_ticks; - } - } - - fn belongs_to_burst(&self, arrival_time_ms: i64, timestamp: u32) -> bool { - assert!(self.current_timestamp_group.complete_time_ms >= 0); - let arrival_time_delta_ms: i64 = - arrival_time_ms - self.current_timestamp_group.complete_time_ms; - let timestamp_diff: u32 = timestamp.wrapping_sub(self.current_timestamp_group.timestamp); - let ts_delta_ms: i64 = (self.timestamp_to_ms_coeff * (timestamp_diff as f64) + 0.5) as i64; - if ts_delta_ms == 0 { - return true; - } - let propagation_delta_ms: i64 = arrival_time_delta_ms - ts_delta_ms; - if propagation_delta_ms < 0 - && arrival_time_delta_ms <= Self::BURST_DELTA_THRESHOLD_MS - && arrival_time_ms - self.current_timestamp_group.first_arrival_ms - < Self::MAX_BURST_DURATION_MS - { - return true; - } - false - } - - fn reset(&mut self) { - self.num_consecutive_reordered_packets = 0; - self.current_timestamp_group = TimestampGroup::default(); - self.prev_timestamp_group = TimestampGroup::default(); - } -} - -#[cfg(test)] -mod test { - use approx::assert_relative_eq; - - use super::*; - - const TIMESTAMP_GROUP_LENGTH_US: i64 = 5000; - const MIN_STEP: i64 = 20; - const TRIGGER_NEW_GROUP_US: i64 = TIMESTAMP_GROUP_LENGTH_US + MIN_STEP; - const BURST_THRESHOLD_MS: i64 = 5; - const ABS_SEND_TIME_FRACTION: i64 = 18; - const ABS_SEND_TIME_INTER_ARRIVAL_UPSHIFT: i64 = 8; - const INTER_ARRIVAL_SHIFT: i64 = ABS_SEND_TIME_FRACTION + ABS_SEND_TIME_INTER_ARRIVAL_UPSHIFT; - - const RTP_TIMESTAMP_TO_MS: f64 = 1.0 / 90.0; - const AST_TO_MS: f64 = 1000.0 / (1 << INTER_ARRIVAL_SHIFT) as f64; - - struct InterArrivalTest { - pub inter_arrival: InterArrival, - inter_arrival_rtp: InterArrival, - inter_arrival_ast: InterArrival, - } - - impl InterArrivalTest { - pub fn new() -> Self { - Self { - inter_arrival: InterArrival::new((TIMESTAMP_GROUP_LENGTH_US / 1000) as u32, 1.0), - inter_arrival_rtp: InterArrival::new( - Self::make_rtp_timestamp(TIMESTAMP_GROUP_LENGTH_US), - RTP_TIMESTAMP_TO_MS, - ), - inter_arrival_ast: InterArrival::new( - Self::make_abs_send_time(TIMESTAMP_GROUP_LENGTH_US), - AST_TO_MS, - ), - } - } - // Test that neither inter_arrival instance complete the timestamp group from - // the given data. - pub fn expect_false( - &mut self, - timestamp_us: i64, - arrival_time_ms: i64, - packet_size: usize, - ) { - Self::internal_expect_false( - &mut self.inter_arrival_rtp, - Self::make_rtp_timestamp(timestamp_us), - arrival_time_ms, - packet_size, - ); - Self::internal_expect_false( - &mut self.inter_arrival_ast, - Self::make_abs_send_time(timestamp_us), - arrival_time_ms, - packet_size, - ); - } - - // Test that both inter_arrival instances complete the timestamp group from - // the given data and that all returned deltas are as expected (except - // delta: Timestamp, which is rounded from us to different ranges and must - // match within an interval, given in |timestamp_near]. - #[allow(clippy::too_many_arguments)] - pub fn expect_true( - &mut self, - timestamp_us: i64, - arrival_time_ms: i64, - packet_size: usize, - expected_timestamp_delta_us: i64, - expected_arrival_time_delta_ms: i64, - expected_packet_size_delta: i64, - timestamp_near: u32, - ) { - Self::internal_expect_true( - &mut self.inter_arrival_rtp, - Self::make_rtp_timestamp(timestamp_us), - arrival_time_ms, - packet_size, - Self::make_rtp_timestamp(expected_timestamp_delta_us), - expected_arrival_time_delta_ms, - expected_packet_size_delta, - timestamp_near, - ); - Self::internal_expect_true( - &mut self.inter_arrival_ast, - Self::make_abs_send_time(timestamp_us), - arrival_time_ms, - packet_size, - Self::make_abs_send_time(expected_timestamp_delta_us), - expected_arrival_time_delta_ms, - expected_packet_size_delta, - timestamp_near << 8, - ); - } - - pub fn wrap_test_helper( - &mut self, - wrap_start_us: i64, - timestamp_near: u32, - unorderly_within_group: bool, - ) { - // Step through the range of a 32 bit int, 1/4 at a time to not cause - // packets close to wraparound to be judged as out of order. - - // G1 - let mut arrival_time: i64 = 17; - self.expect_false(0, arrival_time, 1); - - // G2 - arrival_time += BURST_THRESHOLD_MS + 1; - self.expect_false(wrap_start_us / 4, arrival_time, 1); - - // G3 - arrival_time += BURST_THRESHOLD_MS + 1; - self.expect_true( - wrap_start_us / 2, - arrival_time, - 1, - wrap_start_us / 4, - 6, - 0, // Delta G2-G1 - 0, - ); - - // G4 - arrival_time += BURST_THRESHOLD_MS + 1; - let g4_arrival_time: i64 = arrival_time; - self.expect_true( - wrap_start_us / 2 + wrap_start_us / 4, - arrival_time, - 1, - wrap_start_us / 4, - 6, - 0, // Delta G3-G2 - timestamp_near, - ); - - // G5 - arrival_time += BURST_THRESHOLD_MS + 1; - self.expect_true( - wrap_start_us, - arrival_time, - 2, - wrap_start_us / 4, - 6, - 0, // Delta G4-G3 - timestamp_near, - ); - for i in 0..10 { - // Slowly step across the wrap point. - arrival_time += BURST_THRESHOLD_MS + 1; - if unorderly_within_group { - // These packets arrive with timestamps in decreasing order but are - // nevertheless accumulated to group because their timestamps are higher - // than the initial timestamp of the group. - self.expect_false(wrap_start_us + MIN_STEP * (9 - i), arrival_time, 1); - } else { - self.expect_false(wrap_start_us + MIN_STEP * i, arrival_time, 1); - } - } - let g5_arrival_time: i64 = arrival_time; - - // This packet is out of order and should be dropped. - arrival_time += BURST_THRESHOLD_MS + 1; - self.expect_false(wrap_start_us - 100, arrival_time, 100); - - // G6 - arrival_time += BURST_THRESHOLD_MS + 1; - let g6_arrival_time: i64 = arrival_time; - - self.expect_true( - wrap_start_us + TRIGGER_NEW_GROUP_US, - arrival_time, - 10, - wrap_start_us / 4 + 9 * MIN_STEP, - g5_arrival_time - g4_arrival_time, - (2 + 10) - 1, // Delta G5-G4 - timestamp_near, - ); - - // This packet is out of order and should be dropped. - arrival_time += BURST_THRESHOLD_MS + 1; - self.expect_false(wrap_start_us + TIMESTAMP_GROUP_LENGTH_US, arrival_time, 100); - - // G7 - arrival_time += BURST_THRESHOLD_MS + 1; - self.expect_true( - wrap_start_us + 2 * TRIGGER_NEW_GROUP_US, - arrival_time, - 100, - // Delta G6-G5 - TRIGGER_NEW_GROUP_US - 9 * MIN_STEP, - g6_arrival_time - g5_arrival_time, - 10 - (2 + 10), - timestamp_near, - ); - } - - fn make_rtp_timestamp(us: i64) -> u32 { - ((us * 90 + 500) as u64 / 1000) as u32 - } - - fn make_abs_send_time(us: i64) -> u32 { - let absolute_send_time: u32 = - ((((us as u64) << 18) + 500000) / 1000000) as u32 & 0x00FFFFFF; - absolute_send_time << 8 - } - - fn internal_expect_false( - inter_arrival: &mut InterArrival, - timestamp: u32, - arrival_time_ms: i64, - packet_size: usize, - ) { - let mut dummy_timestamp: u32 = 101; - let mut dummy_arrival_time_ms: i64 = 303; - let mut dummy_packet_size: i64 = 909; - let computed: bool = inter_arrival.compute_deltas( - timestamp, - arrival_time_ms, - arrival_time_ms, - packet_size, - &mut dummy_timestamp, - &mut dummy_arrival_time_ms, - &mut dummy_packet_size, - ); - assert!(!computed); - assert_eq!(101, dummy_timestamp); - assert_eq!(303, dummy_arrival_time_ms); - assert_eq!(909, dummy_packet_size); - } - - #[allow(clippy::too_many_arguments)] - fn internal_expect_true( - inter_arrival: &mut InterArrival, - timestamp: u32, - arrival_time_ms: i64, - packet_size: usize, - expected_timestamp_delta: u32, - expected_arrival_time_delta_ms: i64, - expected_packet_size_delta: i64, - timestamp_near: u32, - ) { - let mut delta_timestamp: u32 = 101; - let mut delta_arrival_time_ms: i64 = 303; - let mut delta_packet_size: i64 = 909; - let computed: bool = inter_arrival.compute_deltas( - timestamp, - arrival_time_ms, - arrival_time_ms, - packet_size, - &mut delta_timestamp, - &mut delta_arrival_time_ms, - &mut delta_packet_size, - ); - assert!(computed); - assert_relative_eq!( - expected_timestamp_delta as f64, - delta_timestamp as f64, - epsilon = timestamp_near as f64 - ); - assert_eq!(expected_arrival_time_delta_ms, delta_arrival_time_ms); - assert_eq!(expected_packet_size_delta, delta_packet_size); - } - } - - #[test] - fn first_packet() { - let mut test = InterArrivalTest::new(); - test.expect_false(0, 17, 1); - } - - #[test] - fn first_group() { - let mut test = InterArrivalTest::new(); - // G1 - let mut arrival_time: i64 = 17; - let g1_arrival_time: i64 = arrival_time; - test.expect_false(0, arrival_time, 1); - - // G2 - arrival_time += BURST_THRESHOLD_MS + 1; - let g2_arrival_time: i64 = arrival_time; - test.expect_false(TRIGGER_NEW_GROUP_US, arrival_time, 2); - - // G3 - // Only once the first packet of the third group arrives, do we see the deltas - // between the first two. - arrival_time += BURST_THRESHOLD_MS + 1; - test.expect_true( - 2 * TRIGGER_NEW_GROUP_US, - arrival_time, - 1, - // Delta G2-G1 - TRIGGER_NEW_GROUP_US, - g2_arrival_time - g1_arrival_time, - 1, - 0, - ); - } - - #[test] - fn second_group() { - let mut test = InterArrivalTest::new(); - // G1 - let mut arrival_time: i64 = 17; - let g1_arrival_time: i64 = arrival_time; - test.expect_false(0, arrival_time, 1); - - // G2 - arrival_time += BURST_THRESHOLD_MS + 1; - let g2_arrival_time: i64 = arrival_time; - test.expect_false(TRIGGER_NEW_GROUP_US, arrival_time, 2); - - // G3 - arrival_time += BURST_THRESHOLD_MS + 1; - let g3_arrival_time: i64 = arrival_time; - test.expect_true( - 2 * TRIGGER_NEW_GROUP_US, - arrival_time, - 1, - // Delta G2-G1 - TRIGGER_NEW_GROUP_US, - g2_arrival_time - g1_arrival_time, - 1, - 0, - ); - - // G4 - // First packet of 4th group yields deltas between group 2 and 3. - arrival_time += BURST_THRESHOLD_MS + 1; - test.expect_true( - 3 * TRIGGER_NEW_GROUP_US, - arrival_time, - 2, - // Delta G3-G2 - TRIGGER_NEW_GROUP_US, - g3_arrival_time - g2_arrival_time, - -1, - 0, - ); - } - - #[test] - fn accumulated_group() { - let mut test = InterArrivalTest::new(); - // G1 - let mut arrival_time: i64 = 17; - let g1_arrival_time: i64 = arrival_time; - test.expect_false(0, arrival_time, 1); - - // G2 - arrival_time += BURST_THRESHOLD_MS + 1; - test.expect_false(TRIGGER_NEW_GROUP_US, 28, 2); - let mut timestamp: i64 = TRIGGER_NEW_GROUP_US; - for _ in 0..10 { - // A bunch of packets arriving within the same group. - arrival_time += BURST_THRESHOLD_MS + 1; - timestamp += MIN_STEP; - test.expect_false(timestamp, arrival_time, 1); - } - let g2_arrival_time: i64 = arrival_time; - let g2_timestamp: i64 = timestamp; - - // G3 - arrival_time = 500; - test.expect_true( - 2 * TRIGGER_NEW_GROUP_US, - arrival_time, - 100, - g2_timestamp, - g2_arrival_time - g1_arrival_time, - (2 + 10) - 1, // Delta G2-G1 - 0, - ); - } - - #[test] - fn out_of_order_packet() { - let mut test = InterArrivalTest::new(); - // G1 - let mut arrival_time: i64 = 17; - let mut timestamp: i64 = 0; - test.expect_false(timestamp, arrival_time, 1); - let g1_timestamp: i64 = timestamp; - let g1_arrival_time: i64 = arrival_time; - - // G2 - arrival_time += 11; - timestamp += TRIGGER_NEW_GROUP_US; - test.expect_false(timestamp, 28, 2); - for _ in 0..10 { - arrival_time += BURST_THRESHOLD_MS + 1; - timestamp += MIN_STEP; - test.expect_false(timestamp, arrival_time, 1); - } - let g2_timestamp: i64 = timestamp; - let g2_arrival_time: i64 = arrival_time; - - // This packet is out of order and should be dropped. - arrival_time = 281; - test.expect_false(g1_timestamp, arrival_time, 100); - - // G3 - arrival_time = 500; - timestamp = 2 * TRIGGER_NEW_GROUP_US; - test.expect_true( - timestamp, - arrival_time, - 100, - // Delta G2-G1 - g2_timestamp - g1_timestamp, - g2_arrival_time - g1_arrival_time, - (2 + 10) - 1, - 0, - ); - } - - #[test] - fn out_of_order_within_group() { - let mut test = InterArrivalTest::new(); - // G1 - let mut arrival_time: i64 = 17; - let mut timestamp: i64 = 0; - test.expect_false(timestamp, arrival_time, 1); - let g1_timestamp: i64 = timestamp; - let g1_arrival_time: i64 = arrival_time; - - // G2 - timestamp += TRIGGER_NEW_GROUP_US; - arrival_time += 11; - test.expect_false(TRIGGER_NEW_GROUP_US, 28, 2); - timestamp += 10 * MIN_STEP; - let g2_timestamp: i64 = timestamp; - for _ in 0..10 { - // These packets arrive with timestamps in decreasing order but are - // nevertheless accumulated to group because their timestamps are higher - // than the initial timestamp of the group. - arrival_time += BURST_THRESHOLD_MS + 1; - test.expect_false(timestamp, arrival_time, 1); - timestamp -= MIN_STEP; - } - let g2_arrival_time: i64 = arrival_time; - - // However, this packet is deemed out of order and should be dropped. - arrival_time = 281; - timestamp = g1_timestamp; - test.expect_false(timestamp, arrival_time, 100); - - // G3 - timestamp = 2 * TRIGGER_NEW_GROUP_US; - arrival_time = 500; - test.expect_true( - timestamp, - arrival_time, - 100, - g2_timestamp - g1_timestamp, - g2_arrival_time - g1_arrival_time, - (2 + 10) - 1, - 0, - ); - } - - #[test] - fn two_bursts() { - let mut test = InterArrivalTest::new(); - // G1 - let g1_arrival_time: i64 = 17; - test.expect_false(0, g1_arrival_time, 1); - - // G2 - let mut timestamp: i64 = TRIGGER_NEW_GROUP_US; - let mut arrival_time: i64 = 100; // Simulate no packets arriving for 100 ms. - for _ in 0..10 { - // A bunch of packets arriving in one burst (within 5 ms apart). - timestamp += 30000; - arrival_time += BURST_THRESHOLD_MS; - test.expect_false(timestamp, arrival_time, 1); - } - let g2_arrival_time: i64 = arrival_time; - let g2_timestamp: i64 = timestamp; - - // G3 - timestamp += 30000; - arrival_time += BURST_THRESHOLD_MS + 1; - test.expect_true( - timestamp, - arrival_time, - 100, - g2_timestamp, - g2_arrival_time - g1_arrival_time, - 10 - 1, // Delta G2-G1 - 0, - ); - } - - #[test] - fn no_bursts() { - let mut test = InterArrivalTest::new(); - // G1 - test.expect_false(0, 17, 1); - - // G2 - let timestamp: i64 = TRIGGER_NEW_GROUP_US; - let arrival_time: i64 = 28; - test.expect_false(timestamp, arrival_time, 2); - - // G3 - test.expect_true( - TRIGGER_NEW_GROUP_US + 30000, - arrival_time + BURST_THRESHOLD_MS + 1, - 100, - timestamp, - arrival_time - 17, - 2 - 1, // Delta G2-G1 - 0, - ); - } - - // Yields 0xfffffffe when converted to internal representation in - // self.inter_arrival_rtp and self.inter_arrival_ast respectively. - const START_RTP_TIMESTAMP_WRAP_US: i64 = 47721858827; - const START_ABS_SEND_TIME_WRAP_US: i64 = 63999995; - - #[test] - fn rtp_timestamp_wrap() { - let mut test = InterArrivalTest::new(); - test.wrap_test_helper(START_RTP_TIMESTAMP_WRAP_US, 1, false); - } - - #[test] - fn abs_send_time_wrap() { - let mut test = InterArrivalTest::new(); - test.wrap_test_helper(START_ABS_SEND_TIME_WRAP_US, 1, false); - } - - #[test] - fn rtp_timestamp_wrap_out_of_order_within_group() { - let mut test = InterArrivalTest::new(); - test.wrap_test_helper(START_RTP_TIMESTAMP_WRAP_US, 1, true); - } - - #[test] - fn abs_send_time_wrap_out_of_order_within_group() { - let mut test = InterArrivalTest::new(); - test.wrap_test_helper(START_ABS_SEND_TIME_WRAP_US, 1, true); - } - - #[test] - fn positive_arrival_time_jump() { - let mut test = InterArrivalTest::new(); - const PACKET_SIZE: usize = 1000; - let mut send_time_ms: u32 = 10000; - let mut arrival_time_ms: i64 = 20000; - let mut system_time_ms: i64 = 30000; - - let mut send_delta: u32 = 0; - let mut arrival_delta: i64 = 0; - let mut size_delta: i64 = 0; - assert!(!test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - - const TIME_DELTA_MS: i64 = 30; - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - assert!(!test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS + InterArrival::ARRIVAL_TIME_OFFSET_THRESHOLD_MS; - system_time_ms += TIME_DELTA_MS; - assert!(test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - assert_eq!(TIME_DELTA_MS, send_delta as i64); - assert_eq!(TIME_DELTA_MS, arrival_delta); - assert_eq!(size_delta, 0); - - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - // The previous arrival time jump should now be detected and cause a reset. - assert!(!test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - - // The two next packets will not give a valid delta since we're in the initial - // state. - for _ in 0..2 { - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - assert!(!test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - } - - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - assert!(test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - assert_eq!(TIME_DELTA_MS, send_delta as _); - assert_eq!(TIME_DELTA_MS, arrival_delta); - assert_eq!(size_delta, 0); - } - - #[test] - fn negative_arrival_time_jump() { - let mut test = InterArrivalTest::new(); - const PACKET_SIZE: usize = 1000; - let mut send_time_ms: u32 = 10000; - let mut arrival_time_ms: i64 = 20000; - let mut system_time_ms: i64 = 30000; - - let mut send_delta: u32 = 0; - let mut arrival_delta: i64 = 0; - let mut size_delta: i64 = 0; - assert!(!test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - - const TIME_DELTA_MS: i64 = 30; - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - assert!(!test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - assert!(test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - assert_eq!(TIME_DELTA_MS, send_delta as _); - assert_eq!(TIME_DELTA_MS, arrival_delta); - assert_eq!(size_delta, 0); - - // Three out of order will fail, after that we will be reset and two more will - // fail before we get our first valid delta after the reset. - arrival_time_ms -= 1000; - for _ in 0..InterArrival::REORDERED_RESET_THRESHOLD + 3 { - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - // The previous arrival time jump should now be detected and cause a reset. - assert!(!test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - } - - send_time_ms += TIME_DELTA_MS as u32; - arrival_time_ms += TIME_DELTA_MS; - system_time_ms += TIME_DELTA_MS; - assert!(test.inter_arrival.compute_deltas( - send_time_ms, - arrival_time_ms, - system_time_ms, - PACKET_SIZE, - &mut send_delta, - &mut arrival_delta, - &mut size_delta - )); - assert_eq!(TIME_DELTA_MS, send_delta as _); - assert_eq!(TIME_DELTA_MS, arrival_delta); - assert_eq!(size_delta, 0); - } -} diff --git a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs index 9abc712e64e..b49f9aa6183 100644 --- a/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs +++ b/rs/moq-congestion/src/gcc/remote_bitrate_estimator/mod.rs @@ -1,7 +1,5 @@ mod aimd_rate_control; mod bwe_defines; -// unused? -//mod inter_arrival; 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 index 6ee9531eb4d..d5793ac11c9 100644 --- a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs +++ b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs @@ -10,7 +10,7 @@ use std::collections::VecDeque; -use crate::{ +use crate::gcc::{ api::{ transport::PacketResult, units::{DataRate, DataSize, TimeDelta, Timestamp}, @@ -317,15 +317,14 @@ impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { // noop } - fn set_alr_ended_time(&mut self, _alr_ended_time: crate::api::units::Timestamp) { + fn set_alr_ended_time(&mut self, _alr_ended_time: crate::gcc::api::units::Timestamp) { // noop } } #[cfg(test)] mod test { - use crate::api::transport::SentPacket; - use approx::assert_relative_eq; + use crate::gcc::api::transport::SentPacket; use super::*; diff --git a/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs b/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs index 05cd09600ce..e337a1b0787 100644 --- a/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs +++ b/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs @@ -12,8 +12,8 @@ use std::collections::VecDeque; -use crate::remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE; -use crate::{ +use crate::gcc::remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE; +use crate::gcc::{ api::{ transport::{BandwidthUsage, SentPacket, TransportPacketsFeedback}, units::{DataRate, TimeDelta, Timestamp}, diff --git a/rs/moq-congestion/src/gcc/trendline_estimator.rs b/rs/moq-congestion/src/gcc/trendline_estimator.rs index 7afa18570d2..1c108d1ec2f 100644 --- a/rs/moq-congestion/src/gcc/trendline_estimator.rs +++ b/rs/moq-congestion/src/gcc/trendline_estimator.rs @@ -10,7 +10,7 @@ use std::collections::VecDeque; -use crate::{api::transport::BandwidthUsage, DelayIncreaseDetectorInterface}; +use crate::gcc::{api::transport::BandwidthUsage, DelayIncreaseDetectorInterface}; // WebRTC-Bwe-TrendlineEstimatorSettings #[derive(Debug, Clone)] diff --git a/rs/moq-congestion/src/lib.rs b/rs/moq-congestion/src/lib.rs index ccb566de3c2..806ef220381 100644 --- a/rs/moq-congestion/src/lib.rs +++ b/rs/moq-congestion/src/lib.rs @@ -16,6 +16,7 @@ mod cubic; mod pacer; mod rate; mod unlimited; +mod gcc; pub use bbr3::{Bbr3, Bbr3Config}; pub use cubic::{Cubic, CubicConfig}; From 503ff5aa249233a501795014c9cdbce992c80cca Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 03:46:38 -0800 Subject: [PATCH 04/15] feat(congestion): refresh GoogCC to WebRTC 556f7eec27, trim it to QUIC, and drive it as a Controller Refreshes the vendored port from WebRTC da2a3d6c4d to 556f7eec27: the LossBasedBweV2 defaults and candidate tie-break, the send-side pacing factor of 1.1, ALR probing gated on transport feedback, allocation probes outside ALR, and the removal of the link capacity tracker, LossBasedBandwidthEstimation and GetExpectedBwePeriod, with their tests. Trims what QUIC has no source for: RTCP loss reports and REMB, route changes, the network state estimate, separate audio detection, RTP sequence numbers, ECN feedback, padding, and the disabled-by-default AcknowledgedBitrateEstimator. The ALR detector stamps send times rather than the wall clock. Gcc adapts it to moq_congestion::Controller: ACKs and losses become transport feedback with the peer's receive times, the window is the congestion window, the pacing rate and target rate become the metrics, and a port of WebRTC's BitrateProber paces each probe cluster and reports what it still needs as probe_inflight. Co-Authored-By: Claude Opus 5.5 --- .../src/gcc/acknowledged_bitrate_estimator.rs | 82 - ...cknowledged_bitrate_estimator_interface.rs | 22 - rs/moq-congestion/src/gcc/alr_detector.rs | 123 +- rs/moq-congestion/src/gcc/api/mod.rs | 7 +- .../src/gcc/api/network_control.rs | 110 - .../src/gcc/api/transport/ecn_marking.rs | 35 - .../src/gcc/api/transport/mod.rs | 2 - .../src/gcc/api/transport/network_types.rs | 355 +-- .../src/gcc/api/units/timestamp.rs | 17 +- .../src/gcc/api/units/unit_base.rs | 3 - .../src/gcc/bitrate_estimator.rs | 218 -- .../congestion_window_pushback_controller.rs | 1 + rs/moq-congestion/src/gcc/delay_based_bwe.rs | 240 +- .../src/gcc/experiments/alr_experiment.rs | 27 - .../src/gcc/experiments/field_trials.rs | 77 +- rs/moq-congestion/src/gcc/experiments/mod.rs | 2 - .../src/gcc/experiments/rate_control.rs | 103 +- .../src/gcc/goog_cc_network_control.rs | 2114 ++++------------- .../src/gcc/inter_arrival_delta.rs | 7 +- .../src/gcc/link_capacity_estimator.rs | 1 + .../gcc/loss_based_bandwidth_estimation.rs | 291 --- .../src/gcc/loss_based_bwe_v2.rs | 580 ++--- rs/moq-congestion/src/gcc/mod.rs | 423 +++- .../src/gcc/pacing/bitrate_prober.rs | 368 +++ .../src/gcc/pacing/interval_budget.rs | 1 + rs/moq-congestion/src/gcc/pacing/mod.rs | 3 + .../src/gcc/probe_bitrate_estimator.rs | 55 +- rs/moq-congestion/src/gcc/probe_controller.rs | 1514 ++++-------- .../aimd_rate_control.rs | 306 +-- .../src/gcc/robust_throughput_estimator.rs | 82 +- .../src/gcc/send_side_bandwidth_estimation.rs | 643 +---- .../src/gcc/trendline_estimator.rs | 9 +- rs/moq-congestion/src/lib.rs | 3 +- rs/moq-congestion/src/rate.rs | 6 + 34 files changed, 2202 insertions(+), 5628 deletions(-) delete mode 100644 rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs delete mode 100644 rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs delete mode 100644 rs/moq-congestion/src/gcc/api/network_control.rs delete mode 100644 rs/moq-congestion/src/gcc/api/transport/ecn_marking.rs delete mode 100644 rs/moq-congestion/src/gcc/bitrate_estimator.rs delete mode 100644 rs/moq-congestion/src/gcc/experiments/alr_experiment.rs delete mode 100644 rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs create mode 100644 rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs diff --git a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs deleted file mode 100644 index 7399729fae2..00000000000 --- a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator.rs +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2017 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, Timestamp}, - }, - experiments::FieldTrials, - AcknowledgedBitrateEstimatorInterface, BitrateEstimator, RobustThroughputEstimator, -}; - -pub struct AcknowledgedBitrateEstimator { - alr_ended_time: Option, - in_alr: bool, - bitrate_estimator: BitrateEstimator, -} - -impl AcknowledgedBitrateEstimatorInterface for AcknowledgedBitrateEstimator { - 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.iter() { - if let Some(alr_ended_time) = self.alr_ended_time { - if packet.sent_packet.send_time > alr_ended_time { - self.bitrate_estimator.expect_fast_rate_change(); - self.alr_ended_time = None; - } - } - let mut acknowledged_estimate = packet.sent_packet.size; - acknowledged_estimate += packet.sent_packet.prior_unacked_data; - self.bitrate_estimator - .update(packet.receive_time, acknowledged_estimate, self.in_alr); - } - } - - fn bitrate(&self) -> Option { - self.bitrate_estimator.bitrate() - } - - fn peek_rate(&self) -> Option { - self.bitrate_estimator.peek_rate() - } - - fn set_alr_ended_time(&mut self, alr_ended_time: Timestamp) { - self.alr_ended_time.replace(alr_ended_time); - } - - fn set_alr(&mut self, in_alr: bool) { - self.in_alr = in_alr; - } -} - -impl AcknowledgedBitrateEstimator { - pub fn new(bitrate_estimator: BitrateEstimator) -> Self { - Self { - alr_ended_time: None, - in_alr: false, - bitrate_estimator, - } - } - - pub fn create(field_trials: &FieldTrials) -> Box { - let simplified_estimator_settings = &field_trials.robust_throughput_estimator_settings; - if simplified_estimator_settings.enabled { - Box::new(RobustThroughputEstimator::new( - simplified_estimator_settings.clone(), - )) - } else { - Box::new(AcknowledgedBitrateEstimator::new(BitrateEstimator::new( - field_trials.bwe_throughput_window_config.clone(), - ))) - } - } -} diff --git a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs b/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs deleted file mode 100644 index e4d227c69cf..00000000000 --- a/rs/moq-congestion/src/gcc/acknowledged_bitrate_estimator_interface.rs +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 crate::gcc::api::{ - transport::PacketResult, - units::{DataRate, Timestamp}, -}; - -pub trait AcknowledgedBitrateEstimatorInterface { - fn incoming_packet_feedback(&mut self, packet_feedback: &[PacketResult]); - fn bitrate(&self) -> Option; - fn peek_rate(&self) -> Option; - fn set_alr(&mut self, in_alr: bool); - fn set_alr_ended_time(&mut self, alr_ended_time: Timestamp); -} diff --git a/rs/moq-congestion/src/gcc/alr_detector.rs b/rs/moq-congestion/src/gcc/alr_detector.rs index 92c7e921de3..70fa35ed766 100644 --- a/rs/moq-congestion/src/gcc/alr_detector.rs +++ b/rs/moq-congestion/src/gcc/alr_detector.rs @@ -8,9 +8,10 @@ * be found in the AUTHORS file in the root of the source tree. */ -use std::time::Instant; - -use crate::gcc::{experiments::AlrExperimentSettings, pacing::IntervalBudget}; +use crate::gcc::{ + api::units::{DataRate, DataSize, Timestamp}, + pacing::IntervalBudget, +}; #[derive(Clone, Debug)] pub struct AlrDetectorConfig { @@ -26,13 +27,12 @@ pub struct AlrDetectorConfig { impl Default for AlrDetectorConfig { fn default() -> Self { - // The ALR experiment seems hard-coded on. - // TODO Technically, WebRTC-AlrDetectorParameters could override these values. - let settings = AlrExperimentSettings::default(); + // 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: settings.alr_bandwidth_usage_percent as f64 / 100.0, - start_budget_level_ratio: settings.alr_start_budget_level_percent as f64 / 100.0, - stop_budget_level_ratio: settings.alr_stop_budget_level_percent as f64 / 100.0, + bandwidth_usage_ratio: 0.80, + start_budget_level_ratio: 0.40, + stop_budget_level_ratio: -0.60, } } } @@ -44,16 +44,14 @@ impl Default for AlrDetectorConfig { // 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_ms: Option, + last_send_time: Option, alr_budget: IntervalBudget, - alr_started_time_ms: Option, - - // Replacement for rtc_time - start: Instant, + alr_started_time: Option, } impl Default for AlrDetector { @@ -66,53 +64,48 @@ impl AlrDetector { pub fn new(conf: AlrDetectorConfig) -> Self { Self { conf, - last_send_time_ms: None, + last_send_time: None, alr_budget: IntervalBudget::new(0, true), - alr_started_time_ms: None, - start: Instant::now(), + alr_started_time: None, } } - pub fn on_bytes_sent(&mut self, bytes_sent: usize, send_time_ms: i64) { - let last_send_time_ms = match self.last_send_time_ms { - Some(v) => v, - None => { - // Since the duration for sending the bytes is unknwon, return without - // updating alr state. - self.last_send_time_ms = Some(send_time_ms); - return; - } + // 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; }; + let delta_time = send_time - last_send_time; + self.last_send_time = Some(send_time); - let delta_time_ms = send_time_ms - last_send_time_ms; - self.last_send_time_ms = Some(send_time_ms); - - self.alr_budget.use_budget(bytes_sent); - self.alr_budget.increase_budget(delta_time_ms); + 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_ms.is_none() + && self.alr_started_time.is_none() { - self.alr_started_time_ms - .replace(self.start.elapsed().as_millis() as i64); + self.alr_started_time = Some(send_time); } else if self.alr_budget.budget_ratio() < self.conf.stop_budget_level_ratio - && self.alr_started_time_ms.is_some() + && self.alr_started_time.is_some() { - self.alr_started_time_ms.take(); + self.alr_started_time = None; } } // Set current estimated bandwidth. - pub fn set_estimated_bitrate(&mut self, bitrate_bps: i64) { - assert!(bitrate_bps > 0); - let target_rate_kbps: i64 = - (bitrate_bps as f64 * self.conf.bandwidth_usage_ratio / 1000.0) as i64; - self.alr_budget.set_target_rate_kbps(target_rate_kbps); + 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 in milliseconds 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_ms + // 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 } } @@ -158,9 +151,10 @@ mod test { while t < interval_ms { *self.timestamp_ms += TIME_STEP_MS; self.alr_detector.on_bytes_sent( - (ESTIMATED_BITRATE_BPS * usage_percentage * TIME_STEP_MS / (8 * 100 * 1000)) - as usize, - *self.timestamp_ms, + DataSize::from_bytes( + ESTIMATED_BITRATE_BPS * usage_percentage * TIME_STEP_MS / (8 * 100 * 1000), + ), + Timestamp::from_millis(*self.timestamp_ms), ); t += TIME_STEP_MS; } @@ -168,9 +162,10 @@ mod test { if remainder_ms > 0 { *self.timestamp_ms += TIME_STEP_MS; self.alr_detector.on_bytes_sent( - (ESTIMATED_BITRATE_BPS * usage_percentage * remainder_ms / (8 * 100 * 1000)) - as usize, - *self.timestamp_ms, + DataSize::from_bytes( + ESTIMATED_BITRATE_BPS * usage_percentage * remainder_ms / (8 * 100 * 1000), + ), + Timestamp::from_millis(*self.timestamp_ms), ); } } @@ -180,20 +175,24 @@ mod test { fn alr_detection() { let mut timestamp_ms: i64 = 1000; let mut alr_detector = AlrDetector::default(); - alr_detector.set_estimated_bitrate(ESTIMATED_BITRATE_BPS); + 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()); + 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()); + 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) @@ -220,7 +219,7 @@ mod test { fn short_spike() { let mut timestamp_ms: i64 = 1000; let mut alr_detector = AlrDetector::default(); - alr_detector.set_estimated_bitrate(ESTIMATED_BITRATE_BPS); + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS)); // Start in non-ALR state. assert!( (alr_detector @@ -263,7 +262,7 @@ mod test { fn bandwidth_estimate_changes() { let mut timestamp_ms: i64 = 1000; let mut alr_detector = AlrDetector::default(); - alr_detector.set_estimated_bitrate(ESTIMATED_BITRATE_BPS); + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS)); // Start in non-ALR state. assert!( @@ -286,7 +285,7 @@ mod test { // 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(ESTIMATED_BITRATE_BPS / 5); + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS / 5)); assert!( (alr_detector .get_application_limited_region_start_time() @@ -311,7 +310,7 @@ mod test { }; let mut alr_detector = AlrDetector::new(config); let mut timestamp_ms: i64 = 1000; - alr_detector.set_estimated_bitrate(ESTIMATED_BITRATE_BPS); + alr_detector.set_estimated_bitrate(DataRate::from_bits_per_sec(ESTIMATED_BITRATE_BPS)); // Start in non-ALR state. assert!( diff --git a/rs/moq-congestion/src/gcc/api/mod.rs b/rs/moq-congestion/src/gcc/api/mod.rs index 999a83d6b7d..1fc28f0ed8e 100644 --- a/rs/moq-congestion/src/gcc/api/mod.rs +++ b/rs/moq-congestion/src/gcc/api/mod.rs @@ -1,8 +1,5 @@ -/// A generic congestion control interface: [NetworkControllerInterface](network_control::NetworkControllerInterface). -pub mod network_control; - -/// Network structs and types. +/// The messages GoogCC exchanges with its transport. pub mod transport; -/// Some unit types, such as [DataSize](units::DataSize) and [DataRate](units::DataRate). +/// 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/network_control.rs b/rs/moq-congestion/src/gcc/api/network_control.rs deleted file mode 100644 index 47ed789bb2a..00000000000 --- a/rs/moq-congestion/src/gcc/api/network_control.rs +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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::experiments::FieldTrials; - -use super::{transport::*, units::TimeDelta}; - -/* -pub trait TargetTransferRateObserver { - /// Called to indicate target transfer rate as well as giving information about - /// the current estimate of network parameters. - fn on_target_transfer_rate(target_transfer_rate: TargetTransferRate); - /// Called to provide updates to the expected target rate in case it changes - /// before the first call to OnTargetTransferRate. - fn on_start_rate_update(data_rate: DataRate); -} -*/ - -/// 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, these can be changed at any later - /// time by calls to OnTargetRateConstraints. 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, these are changed at any later time - /// by calls to OnStreamsConfig. - pub stream_based_config: StreamsConfig, -} - -/// NetworkControllerInterface is implemented by network controllers. A network -/// controller is a class that uses information about network state and traffic -/// to estimate network parameters such as round trip time and bandwidth. Network -/// controllers does not guarantee thread safety, the interface must be used in a -/// non-concurrent fashion. -pub trait NetworkControllerInterface { - /// Called when network availabilty changes. - fn on_network_availability(&mut self, msg: NetworkAvailability) -> NetworkControlUpdate; - /// Called when the receiving or sending endpoint changes address. - fn on_network_route_change(&mut self, msg: NetworkRouteChange) -> NetworkControlUpdate; - /// Called periodically with a periodicy as specified by - /// NetworkControllerFactoryInterface::GetProcessInterval. - fn on_process_interval(&mut self, msg: ProcessInterval) -> NetworkControlUpdate; - /// Called when remotely calculated bitrate is received. - fn on_remote_bitrate_report(&mut self, msg: RemoteBitrateReport) -> NetworkControlUpdate; - /// Called round trip time has been calculated by protocol specific mechanisms. - fn on_round_trip_time_update(&mut self, msg: RoundTripTimeUpdate) -> NetworkControlUpdate; - /// Called when a packet is sent on the network. - fn on_sent_packet(&mut self, msg: SentPacket) -> NetworkControlUpdate; - /// Called when a packet is received from the remote client. - fn on_received_packet(&mut self, msg: ReceivedPacket) -> NetworkControlUpdate; - /// Called when the stream specific configuration has been updated. - fn on_streams_config(&mut self, msg: StreamsConfig) -> NetworkControlUpdate; - /// Called when target transfer rate constraints has been changed. - fn on_target_rate_constraints(&mut self, msg: TargetRateConstraints) -> NetworkControlUpdate; - /// Called when a protocol specific calculation of packet loss has been made. - fn on_transport_loss_report(&mut self, msg: TransportLossReport) -> NetworkControlUpdate; - /// Called with per packet feedback regarding receive time. - fn on_transport_packets_feedback( - &mut self, - msg: TransportPacketsFeedback, - ) -> NetworkControlUpdate; - /// Called with network state estimate updates. - fn on_network_state_estimate(&mut self, msg: NetworkStateEstimate) -> NetworkControlUpdate; - - /// Returns the interval by which the network controller expects - /// OnProcessInterval calls. - fn get_process_interval() -> TimeDelta; -} - -/* -// NetworkControllerFactoryInterface is an interface for creating a network -// controller. -pub trait NetworkControllerFactoryInterface { - // Used to create a new network controller, requires an observer to be - // provided to handle callbacks. - fn create(config: NetworkControllerConfig) -> impl NetworkControllerInterface; -} -*/ - -// Under development, subject to change without notice. -/* Purposely removed because nothing implements it (except for secret Google stuff?) -pub trait NetworkStateEstimator { - // Gets the current best estimate according to the estimator. - fn GetCurrentEstimate(&self) -> Option; - // Called with per packet feedback regarding receive time. - // Used when the NetworkStateEstimator runs in the sending endpoint. - fn OnTransportPacketsFeedback(&mut self, event: &TransportPacketsFeedback); - // Called with per packet feedback regarding receive time. - // Used when the NetworkStateEstimator runs in the receiving endpoint. - fn OnReceivedPacket(&mut self, event: &PacketResult); - // Called when the receiving or sending endpoint changes address. - fn OnRouteChange(&mut self, event: &NetworkRouteChange); -} - -pub trait NetworkStateEstimatorFactory { - fn Create() -> impl NetworkStateEstimator; -} -*/ diff --git a/rs/moq-congestion/src/gcc/api/transport/ecn_marking.rs b/rs/moq-congestion/src/gcc/api/transport/ecn_marking.rs deleted file mode 100644 index 476df0f334e..00000000000 --- a/rs/moq-congestion/src/gcc/api/transport/ecn_marking.rs +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2024 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. - */ - -// TODO: bugs.webrtc.org/42225697 - L4S support is slowly being developed. -// Help is appreciated. - -// L4S Explicit Congestion Notification (ECN) -> -// https://www.rfc-editor.org/rfc/rfc9331.html ECT stands for ECN-Capable -// Transport and CE stands for Congestion Experienced. - -// RFC-3168, Section 5 -// +-----+-----+ -// | ECN FIELD | -// +-----+-----+ -// ECT CE [Obsolete] RFC 2481 names for the ECN bits. -// 0 0 Not-ECT -// 0 1 ECT(1) -// 1 0 ECT(0) -// 1 1 CE - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub enum EcnMarking { - #[default] - NotEct = 0, // Not ECN-Capable Transport - Ect1 = 1, // ECN-Capable Transport - Ect0 = 2, // Not used by L4s (or webrtc.) - Ce = 3, // Congestion experienced -} diff --git a/rs/moq-congestion/src/gcc/api/transport/mod.rs b/rs/moq-congestion/src/gcc/api/transport/mod.rs index 7f402515935..a057b687a16 100644 --- a/rs/moq-congestion/src/gcc/api/transport/mod.rs +++ b/rs/moq-congestion/src/gcc/api/transport/mod.rs @@ -1,7 +1,5 @@ mod bandwidth_usage; -mod ecn_marking; mod network_types; pub use bandwidth_usage::*; -pub use ecn_marking::*; 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 index 828bc59532f..3e5ed63feea 100644 --- a/rs/moq-congestion/src/gcc/api/transport/network_types.rs +++ b/rs/moq-congestion/src/gcc/api/transport/network_types.rs @@ -1,56 +1,37 @@ -use std::cmp::Ordering; +/* + * 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 super::EcnMarking; - -/// Represents constraints and rates related to the currently enabled streams. -/// This is used as input to the congestion controller via the StreamsConfig -/// struct. -#[derive(Default, Clone, Copy, Debug)] -pub struct BitrateAllocationLimits { - // The total minimum send bitrate required by all sending streams. - pub min_allocatable_rate: DataRate, - // The total maximum allocatable bitrate for all currently available streams. - pub max_allocatable_rate: DataRate, - // The max bitrate to use for padding. The sum of the per-stream max padding - // rate. - pub max_padding_rate: DataRate, -} - /// 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)] +#[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: Option, + pub enable_repeated_initial_probing: bool, pub pacing_factor: Option, // TODO(srte): Use BitrateAllocationLimits here. pub min_total_allocated_bitrate: Option, - pub max_padding_rate: Option, pub max_total_allocated_bitrate: Option, } -impl Default for StreamsConfig { - fn default() -> Self { - Self { - at_time: Timestamp::plus_infinity(), - requests_alr_probing: None, - enable_repeated_initial_probing: None, - pacing_factor: None, - min_total_allocated_bitrate: None, - max_padding_rate: None, - max_total_allocated_bitrate: None, - } - } -} - #[derive(Debug, Clone, Copy)] pub struct TargetRateConstraints { pub at_time: Timestamp, @@ -87,30 +68,11 @@ impl Default for NetworkAvailability { } } -#[derive(Debug, Clone, Copy)] -pub struct NetworkRouteChange { - pub at_time: Timestamp, - /// The TargetRateConstraints are set here so they can be changed synchronously - /// when network route changes. - pub constraints: TargetRateConstraints, -} - -impl Default for NetworkRouteChange { - fn default() -> Self { - Self { - at_time: Timestamp::plus_infinity(), - constraints: TargetRateConstraints::default(), - } - } -} - -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct PacedPacketInfo { - pub send_bitrate: DataRate, pub probe_cluster_id: i32, pub probe_cluster_min_probes: i64, pub probe_cluster_min_bytes: i64, - pub probe_cluster_bytes_sent: i64, } impl PacedPacketInfo { @@ -122,33 +84,16 @@ impl PacedPacketInfo { probe_cluster_min_bytes: i64, ) -> Self { Self { - send_bitrate: DataRate::from_bits_per_sec(0), probe_cluster_id, probe_cluster_min_probes, probe_cluster_min_bytes, - probe_cluster_bytes_sent: 0, } } } impl Default for PacedPacketInfo { fn default() -> Self { - Self { - send_bitrate: DataRate::from_bits_per_sec(0), - probe_cluster_id: Self::NOT_APROBE, - probe_cluster_min_probes: -1, - probe_cluster_min_bytes: -1, - probe_cluster_bytes_sent: 0, - } - } -} - -impl PartialEq for PacedPacketInfo { - fn eq(&self, other: &Self) -> bool { - self.send_bitrate == other.send_bitrate - && self.probe_cluster_id == other.probe_cluster_id - && self.probe_cluster_min_probes == other.probe_cluster_min_probes - && self.probe_cluster_min_bytes == other.probe_cluster_min_bytes + Self::new(Self::NOT_APROBE, -1, -1) } } @@ -157,17 +102,9 @@ pub struct SentPacket { pub send_time: Timestamp, /// Size of packet with overhead up to IP layer. pub size: DataSize, - /// Size of preceeding packets that are not part of feedback. - pub prior_unacked_data: DataSize, /// Probe cluster id and parameters including bitrate, number of packets and /// number of bytes. pub pacing_info: PacedPacketInfo, - /// True if the packet is an audio packet, false for video, padding, RTX etc. - pub audio: bool, - /// Transport independent sequence number, any tracked packet should have a - /// sequence number that is unique over the whole call and increasing by 1 for - /// each packet. - pub sequence_number: i64, /// Tracked data in flight when the packet was sent, excluding unacked data. pub data_in_flight: DataSize, } @@ -177,92 +114,22 @@ impl Default for SentPacket { Self { send_time: Timestamp::plus_infinity(), size: DataSize::zero(), - prior_unacked_data: DataSize::zero(), pacing_info: PacedPacketInfo::default(), - audio: false, - sequence_number: 0, data_in_flight: DataSize::zero(), } } } -#[derive(Debug, Clone, Copy)] -pub struct ReceivedPacket { - pub send_time: Timestamp, - pub receive_time: Timestamp, - pub size: DataSize, -} - -impl Default for ReceivedPacket { - fn default() -> Self { - Self { - send_time: Timestamp::minus_infinity(), - receive_time: Timestamp::plus_infinity(), - size: DataSize::zero(), - } - } -} - -// Transport level feedback - -#[derive(Debug, Clone, Copy)] -pub struct RemoteBitrateReport { - pub receive_time: Timestamp, - pub bandwidth: DataRate, -} - -impl Default for RemoteBitrateReport { - fn default() -> Self { - Self { - receive_time: Timestamp::plus_infinity(), - bandwidth: DataRate::infinity(), - } - } -} - #[derive(Debug, Clone, Copy)] pub struct RoundTripTimeUpdate { - pub receive_time: Timestamp, pub round_trip_time: TimeDelta, - pub smoothed: bool, -} - -impl Default for RoundTripTimeUpdate { - fn default() -> Self { - Self { - receive_time: Timestamp::plus_infinity(), - round_trip_time: TimeDelta::plus_infinity(), - smoothed: false, - } - } -} - -#[derive(Debug, Clone, Copy)] -pub struct TransportLossReport { - pub receive_time: Timestamp, - pub start_time: Timestamp, - pub end_time: Timestamp, - pub packets_lost_delta: u64, - pub packets_received_delta: u64, -} - -impl Default for TransportLossReport { - fn default() -> Self { - Self { - receive_time: Timestamp::plus_infinity(), - start_time: Timestamp::plus_infinity(), - end_time: Timestamp::plus_infinity(), - packets_lost_delta: 0, - packets_received_delta: 0, - } - } } +/// Packet level feedback #[derive(Debug, Clone, Copy)] pub struct PacketResult { pub sent_packet: SentPacket, pub receive_time: Timestamp, - pub ecn: EcnMarking, } impl PacketResult { @@ -276,45 +143,15 @@ impl Default for PacketResult { Self { sent_packet: SentPacket::default(), receive_time: Timestamp::plus_infinity(), - ecn: EcnMarking::NotEct, } } } -impl PartialOrd for PacketResult { - fn partial_cmp(&self, other: &Self) -> Option { - if self.receive_time != other.receive_time { - return Some(self.receive_time.cmp(&other.receive_time)); - } - - if self.sent_packet.send_time != other.sent_packet.send_time { - return Some(self.sent_packet.send_time.cmp(&other.sent_packet.send_time)); - } - - Some( - self.sent_packet - .sequence_number - .cmp(&other.sent_packet.sequence_number), - ) - } -} - -impl PartialEq for PacketResult { - fn eq(&self, other: &Self) -> bool { - self.receive_time == other.receive_time - && self.sent_packet.send_time == other.sent_packet.send_time - && self.sent_packet.sequence_number == other.sent_packet.sequence_number - } -} - #[derive(Debug, Clone)] pub struct TransportPacketsFeedback { pub feedback_time: Timestamp, pub data_in_flight: DataSize, pub packet_feedbacks: Vec, - - /// Arrival times for messages without send time information. - pub sendless_arrival_times: Vec, } impl Default for TransportPacketsFeedback { @@ -323,82 +160,69 @@ impl Default for TransportPacketsFeedback { feedback_time: Timestamp::plus_infinity(), data_in_flight: DataSize::zero(), packet_feedbacks: Vec::new(), - sendless_arrival_times: Vec::new(), } } } impl TransportPacketsFeedback { - /// NOTE: These returned Vec copies in the original C++ code. Use collect() if you want that behavior. pub fn received_with_send_info(&self) -> impl Iterator { self.packet_feedbacks.iter().filter(|fb| fb.is_received()) } - pub fn lost_with_send_info(&self) -> impl Iterator { - self.packet_feedbacks.iter().filter(|fb| !fb.is_received()) - } - - pub fn packets_with_feedback(&self) -> &Vec { - &self.packet_feedbacks - } - pub fn sorted_by_receive_time(&self) -> Vec { let mut res: Vec = self.received_with_send_info().cloned().collect(); - res.sort_by(|a, b| a.receive_time.cmp(&b.receive_time)); + // 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 NetworkEstimate { - pub at_time: Timestamp, - /// Deprecated, use TargetTransferRate::target_rate instead. - pub bandwidth: DataRate, - pub round_trip_time: TimeDelta, - pub bwe_period: TimeDelta, - - pub loss_rate_ratio: f32, -} - -impl Default for NetworkEstimate { - fn default() -> Self { - Self { - at_time: Timestamp::plus_infinity(), - bandwidth: DataRate::infinity(), - round_trip_time: TimeDelta::plus_infinity(), - bwe_period: TimeDelta::plus_infinity(), - loss_rate_ratio: 0.0, - } - } -} - #[derive(Debug, Clone, Copy)] pub struct PacerConfig { - pub at_time: Timestamp, /// 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, - /// Pacer should send at least pad_window data over time_window duration. - pub pad_window: DataSize, } impl Default for PacerConfig { fn default() -> Self { Self { - at_time: Timestamp::plus_infinity(), data_window: DataSize::infinity(), time_window: TimeDelta::plus_infinity(), - pad_window: DataSize::zero(), } } } impl PacerConfig { - pub fn data_rate(&self) -> DataRate { - self.data_window / self.time_window + 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 pad_rate(&self) -> DataRate { - self.pad_window / self.time_window + + pub fn data_rate(&self) -> DataRate { + self.data_window / self.rate_window() } } @@ -429,24 +253,7 @@ impl Default for ProbeClusterConfig { #[derive(Debug, Clone, Copy)] pub struct TargetTransferRate { - pub at_time: Timestamp, - /// The estimate on which the target rate is based on. - pub network_estimate: NetworkEstimate, pub target_rate: DataRate, - pub stable_target_rate: DataRate, - pub cwnd_reduce_ratio: f64, -} - -impl Default for TargetTransferRate { - fn default() -> Self { - Self { - at_time: Timestamp::plus_infinity(), - network_estimate: NetworkEstimate::default(), - target_rate: DataRate::zero(), - stable_target_rate: DataRate::zero(), - cwnd_reduce_ratio: 0.0, - } - } } /// Contains updates of network controller comand state. Using optionals to @@ -460,15 +267,6 @@ pub struct NetworkControlUpdate { pub target_rate: Option, } -impl NetworkControlUpdate { - pub fn has_updates(&self) -> bool { - self.congestion_window.is_some() - || self.pacer_config.is_some() - || !self.probe_cluster_configs.is_empty() - || self.target_rate.is_some() - } -} - // Process control pub struct ProcessInterval { pub at_time: Timestamp, @@ -483,58 +281,3 @@ impl Default for ProcessInterval { } } } - -/// Under development, subject to change without notice. -#[derive(Debug, Clone, Copy)] -pub struct NetworkStateEstimate { - pub confidence: f64, - - // The time the estimate was received/calculated. - pub update_time: Timestamp, - pub last_receive_time: Timestamp, - pub last_send_time: Timestamp, - - // Total estimated link capacity. - pub link_capacity: DataRate, - // Used as a safe measure of available capacity. - pub link_capacity_lower: DataRate, - // Used as limit for increasing bitrate. - pub link_capacity_upper: DataRate, - - pub pre_link_buffer_delay: TimeDelta, - pub post_link_buffer_delay: TimeDelta, - pub propagation_delay: TimeDelta, - - // Only for debugging - pub time_delta: TimeDelta, - pub last_feed_time: Timestamp, - pub cross_delay_rate: f64, - pub spike_delay_rate: f64, - pub link_capacity_std_dev: DataRate, - pub link_capacity_min: DataRate, - pub cross_traffic_ratio: f64, -} - -impl Default for NetworkStateEstimate { - fn default() -> Self { - Self { - confidence: f64::NAN, - update_time: Timestamp::minus_infinity(), - last_receive_time: Timestamp::minus_infinity(), - last_send_time: Timestamp::minus_infinity(), - link_capacity: DataRate::minus_infinity(), - link_capacity_lower: DataRate::minus_infinity(), - link_capacity_upper: DataRate::minus_infinity(), - pre_link_buffer_delay: TimeDelta::minus_infinity(), - post_link_buffer_delay: TimeDelta::minus_infinity(), - propagation_delay: TimeDelta::minus_infinity(), - time_delta: TimeDelta::minus_infinity(), - last_feed_time: Timestamp::minus_infinity(), - cross_delay_rate: f64::NAN, - spike_delay_rate: f64::NAN, - link_capacity_std_dev: DataRate::minus_infinity(), - link_capacity_min: DataRate::minus_infinity(), - cross_traffic_ratio: f64::NAN, - } - } -} diff --git a/rs/moq-congestion/src/gcc/api/units/timestamp.rs b/rs/moq-congestion/src/gcc/api/units/timestamp.rs index 648752e5573..cf85a900f73 100644 --- a/rs/moq-congestion/src/gcc/api/units/timestamp.rs +++ b/rs/moq-congestion/src/gcc/api/units/timestamp.rs @@ -14,8 +14,12 @@ //! difference of two Timestamps results in a TimeDelta. super::unit_base!(Timestamp); -use std::{fmt, sync::LazyLock, time::{Duration, Instant}}; use std::ops::*; +use std::{ + fmt, + sync::LazyLock, + time::{Duration, Instant}, +}; use super::TimeDelta; @@ -182,17 +186,16 @@ impl From for Instant { let duration = Duration::from_micros(timestamp.us().unsigned_abs()); if timestamp.0 > 0 { - r.checked_add(duration).expect("Timestamp is too far in the future") + r.checked_add(duration) + .expect("Timestamp is too far in the future") } else { - r.checked_sub(duration).expect("Timestamp is from before the program started") + r.checked_sub(duration) + .expect("Timestamp is from before the program started") } } } -static REF: LazyLock = LazyLock::new(|| { - std::time::Instant::now() -}); - +static REF: LazyLock = LazyLock::new(|| std::time::Instant::now()); #[cfg(test)] mod 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 index 74c13fd2dec..384296ce272 100644 --- a/rs/moq-congestion/src/gcc/api/units/unit_base.rs +++ b/rs/moq-congestion/src/gcc/api/units/unit_base.rs @@ -495,9 +495,6 @@ pub(crate) use unit_base; mod test { use std::fmt; - - use super::*; - relative_unit!(TestUnit); impl TestUnit { diff --git a/rs/moq-congestion/src/gcc/bitrate_estimator.rs b/rs/moq-congestion/src/gcc/bitrate_estimator.rs deleted file mode 100644 index 0721f1e2ac2..00000000000 --- a/rs/moq-congestion/src/gcc/bitrate_estimator.rs +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright (c) 2017 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, TimeDelta, Timestamp}; - -// WebRTC-BweThroughputWindowConfig -#[derive(Debug, Clone)] -pub struct BitrateEstimatorConfig { - pub initial_window_ms: i64, - pub window_ms: i64, - pub scale: f64, - pub scale_alr: f64, - pub scale_small: f64, - pub small_thresh: DataSize, - pub symmetry_cap: DataRate, - pub floor: DataRate, -} - -impl BitrateEstimatorConfig { - const INITIAL_RATE_WINDOW_MS: i64 = 500; - const RATE_WINDOW_MS: i64 = 150; - const MIN_RATE_WINDOW_MS: i64 = 150; - const MAX_RATE_WINDOW_MS: i64 = 1000; -} - -impl Default for BitrateEstimatorConfig { - fn default() -> Self { - Self { - initial_window_ms: Self::INITIAL_RATE_WINDOW_MS, - window_ms: Self::RATE_WINDOW_MS, - scale: 10.0, - scale_alr: 10.0, // scale - scale_small: 10.0, // scale - small_thresh: DataSize::zero(), - symmetry_cap: DataRate::zero(), - floor: DataRate::zero(), - } - } -} - -impl BitrateEstimatorConfig { - pub fn validate(&mut self) { - self.initial_window_ms = self - .initial_window_ms - .clamp(Self::MAX_RATE_WINDOW_MS, Self::MAX_RATE_WINDOW_MS); - self.window_ms = self - .window_ms - .clamp(Self::MIN_RATE_WINDOW_MS, Self::MAX_RATE_WINDOW_MS); - } -} - -// Computes a bayesian estimate of the throughput given acks containing -// the arrival time and payload size. Samples which are far from the current -// estimate or are based on few packets are given a smaller weight, as they -// are considered to be more likely to have been caused by, e.g., delay spikes -// unrelated to congestion. -pub struct BitrateEstimator { - sum: i64, - initial_window_ms: i64, - noninitial_window_ms: i64, - uncertainty_scale: f64, - uncertainty_scale_in_alr: f64, - small_sample_uncertainty_scale: f64, - small_sample_threshold: DataSize, - uncertainty_symmetry_cap: DataRate, - estimate_floor: DataRate, - current_window_ms: i64, - prev_time_ms: i64, - bitrate_estimate_kbps: f64, - bitrate_estimate_var: f64, -} - -impl Default for BitrateEstimator { - fn default() -> Self { - Self::new(BitrateEstimatorConfig::default()) - } -} - -impl BitrateEstimator { - pub fn new(mut config: BitrateEstimatorConfig) -> Self { - config.validate(); - Self { - sum: 0, - initial_window_ms: config.initial_window_ms, - noninitial_window_ms: config.window_ms, - uncertainty_scale: config.scale, - uncertainty_scale_in_alr: config.scale_alr, - small_sample_uncertainty_scale: config.scale_small, - small_sample_threshold: config.small_thresh, - uncertainty_symmetry_cap: config.symmetry_cap, - estimate_floor: config.floor, - current_window_ms: 0, - prev_time_ms: -1, - bitrate_estimate_kbps: -1.0, - bitrate_estimate_var: 50.0, - } - } - - pub fn update(&mut self, at_time: Timestamp, amount: DataSize, in_alr: bool) { - let mut rate_window_ms: i64 = self.noninitial_window_ms; - // We use a larger window at the beginning to get a more stable sample that - // we can use to initialize the estimate. - if self.bitrate_estimate_kbps < 0.0 { - rate_window_ms = self.initial_window_ms; - } - let mut is_small_sample: bool = false; - let bitrate_sample_kbps: f64 = self.update_window( - at_time.ms(), - amount.bytes(), - rate_window_ms, - &mut is_small_sample, - ); - if bitrate_sample_kbps < 0.0 { - return; - } - if self.bitrate_estimate_kbps < 0.0 { - // This is the very first sample we get. Use it to initialize the estimate. - self.bitrate_estimate_kbps = bitrate_sample_kbps; - return; - } - // Optionally use higher uncertainty for very small samples to avoid dropping - // estimate and for samples obtained in ALR. - let mut scale: f64 = self.uncertainty_scale; - if is_small_sample && bitrate_sample_kbps < self.bitrate_estimate_kbps { - scale = self.small_sample_uncertainty_scale; - } else if in_alr && bitrate_sample_kbps < self.bitrate_estimate_kbps { - // Optionally use higher uncertainty for samples obtained during ALR. - scale = self.uncertainty_scale_in_alr; - } - // Define the sample uncertainty as a function of how far away it is from the - // current estimate. With low values of self.uncertainty_symmetry_cap we add more - // uncertainty to increases than to decreases. For higher values we approach - // symmetry. - let sample_uncertainty: f64 = scale - * (self.bitrate_estimate_kbps - bitrate_sample_kbps).abs() - / (self.bitrate_estimate_kbps - + bitrate_sample_kbps.max(self.uncertainty_symmetry_cap.kbps_float())); - - let sample_var: f64 = sample_uncertainty * sample_uncertainty; - // Update a bayesian estimate of the rate, weighting it lower if the sample - // uncertainty is large. - // The bitrate estimate uncertainty is increased with each update to model - // that the bitrate changes over time. - let pred_bitrate_estimate_var: f64 = self.bitrate_estimate_var + 5.0; - self.bitrate_estimate_kbps = (sample_var * self.bitrate_estimate_kbps - + pred_bitrate_estimate_var * bitrate_sample_kbps) - / (sample_var + pred_bitrate_estimate_var); - self.bitrate_estimate_kbps = self - .bitrate_estimate_kbps - .max(self.estimate_floor.kbps_float()); - self.bitrate_estimate_var = - sample_var * pred_bitrate_estimate_var / (sample_var + pred_bitrate_estimate_var); - } - - pub fn bitrate(&self) -> Option { - if self.bitrate_estimate_kbps < 0.0 { - return None; - } - Some(DataRate::from_kilobits_per_sec_float( - self.bitrate_estimate_kbps, - )) - } - - pub fn peek_rate(&self) -> Option { - if self.current_window_ms > 0 { - return Some( - DataSize::from_bytes(self.sum) / TimeDelta::from_millis(self.current_window_ms), - ); - } - None - } - - pub fn expect_fast_rate_change(&mut self) { - // By setting the bitrate-estimate variance to a higher value we allow the - // bitrate to change fast for the next few samples. - self.bitrate_estimate_var += 200.0; - } - - fn update_window( - &mut self, - now_ms: i64, - bytes: i64, - rate_window_ms: i64, - is_small_sample: &mut bool, - ) -> f64 { - // Reset if time moves backwards. - if now_ms < self.prev_time_ms { - self.prev_time_ms = -1; - self.sum = 0; - self.current_window_ms = 0; - } - if self.prev_time_ms >= 0 { - self.current_window_ms += now_ms - self.prev_time_ms; - // Reset if nothing has been received for more than a full window. - if now_ms - self.prev_time_ms > rate_window_ms { - self.sum = 0; - self.current_window_ms %= rate_window_ms; - } - } - self.prev_time_ms = now_ms; - let mut bitrate_sample: f64 = -1.0; - if self.current_window_ms >= rate_window_ms { - *is_small_sample = self.sum < self.small_sample_threshold.bytes(); - bitrate_sample = 8.0 * self.sum as f64 / (rate_window_ms) as f64; - self.current_window_ms -= rate_window_ms; - self.sum = 0; - } - self.sum += bytes; - bitrate_sample - } -} diff --git a/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs b/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs index 14e5186f586..a7975e67f06 100644 --- a/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs +++ b/rs/moq-congestion/src/gcc/congestion_window_pushback_controller.rs @@ -18,6 +18,7 @@ use crate::gcc::{ // 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, diff --git a/rs/moq-congestion/src/gcc/delay_based_bwe.rs b/rs/moq-congestion/src/gcc/delay_based_bwe.rs index b50b7ee2038..aa01d3d6e29 100644 --- a/rs/moq-congestion/src/gcc/delay_based_bwe.rs +++ b/rs/moq-congestion/src/gcc/delay_based_bwe.rs @@ -8,34 +8,20 @@ * 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, - DelayIncreaseDetectorInterface, InterArrivalDelta, TrendlineEstimator, remote_bitrate_estimator::{AimdRateControl, RateControlInput}, }; -// WebRTC-Bwe-SeparateAudioPackets -#[derive(Clone, Debug)] -pub struct BweSeparateAudioPacketsSettings { - pub enabled: bool, - pub packet_threshold: i64, - pub time_threshold: TimeDelta, -} - -impl Default for BweSeparateAudioPacketsSettings { - fn default() -> Self { - Self { - enabled: false, - packet_threshold: 10, - time_threshold: TimeDelta::from_seconds(1), - } - } -} - pub struct DelayBasedBweResult { pub updated: bool, pub probe: bool, @@ -56,31 +42,13 @@ impl Default for DelayBasedBweResult { } } -enum DelayDetector { - Video, - Audio, -} - +#[derive(Clone)] pub struct DelayBasedBwe { - // Alternatively, run two separate overuse detectors for audio and video, - // and fall back to the audio one if we haven't seen a video packet in a - // while. - separate_audio: BweSeparateAudioPacketsSettings, - audio_packets_since_last_video: i64, - last_video_packet_recv_time: Timestamp, - - // Unused? - //video_inter_arrival: InterArrival, + trendline_settings: TrendlineEstimatorSettings, video_inter_arrival_delta: InterArrivalDelta, video_delay_detector: TrendlineEstimator, - // Unused? - //audio_inter_arrival: InterArrival, - audio_inter_arrival_delta: InterArrivalDelta, - audio_delay_detector: TrendlineEstimator, - active_delay_detector_type: DelayDetector, last_seen_packet: Timestamp, - uma_recorded: bool, rate_control: AimdRateControl, prev_bitrate: DataRate, prev_state: BandwidthUsage, @@ -96,49 +64,34 @@ impl DelayBasedBwe { const FIXED_SSRC: u32 = 0; pub fn new(field_trials: &FieldTrials) -> Self { - tracing::debug!( - "Initialized DelayBasedBwe with separate audio overuse detection: {:?}", - field_trials.separate_audio_packets - ); Self { - separate_audio: field_trials.separate_audio_packets.clone(), - audio_packets_since_last_video: 0, - last_video_packet_recv_time: Timestamp::minus_infinity(), + trendline_settings: field_trials.trendline_estimator_settings.clone(), video_delay_detector: TrendlineEstimator::new( field_trials.trendline_estimator_settings.clone(), ), - audio_delay_detector: TrendlineEstimator::new( - field_trials.trendline_estimator_settings.clone(), - ), - active_delay_detector_type: DelayDetector::Video, last_seen_packet: Timestamp::minus_infinity(), - uma_recorded: false, 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 - audio_inter_arrival_delta: InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH), video_inter_arrival_delta: InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH), } } fn active_delay_detector_state(&self) -> BandwidthUsage { - match self.active_delay_detector_type { - DelayDetector::Audio => self.audio_delay_detector.state(), - DelayDetector::Video => self.video_delay_detector.state(), - } + 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, - //network_estimate: Option, in_alr: bool, ) -> DelayBasedBweResult { - let packet_feedback_vector = msg.sorted_by_receive_time(); // 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. @@ -147,13 +100,10 @@ impl DelayBasedBwe { return DelayBasedBweResult::default(); } - if !self.uma_recorded { - self.uma_recorded = true; - } 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 { + 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 @@ -170,11 +120,9 @@ impl DelayBasedBwe { return DelayBasedBweResult::default(); } self.rate_control.set_in_application_limited_region(in_alr); - //self.rate_control.SetNetworkStateEstimate(network_estimate); self.maybe_update_estimate( acked_bitrate, probe_bitrate, - //network_estimate, recovered_from_overuse, in_alr, msg.feedback_time, @@ -210,10 +158,6 @@ impl DelayBasedBwe { self.rate_control.set_min_bitrate(min_bitrate); } - pub fn get_expected_bwe_period(&self) -> TimeDelta { - self.rate_control.get_expected_bandwidth_period() - } - /* unused? pub fn trigger_overuse( &mut self, @@ -239,49 +183,17 @@ impl DelayBasedBwe { || at_time - self.last_seen_packet > Self::STREAM_TIME_OUT { self.video_inter_arrival_delta = InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH); - self.audio_inter_arrival_delta = InterArrivalDelta::new(Self::SEND_TIME_GROUP_LENGTH); - - self.video_delay_detector = TrendlineEstimator::default(); - self.audio_delay_detector = TrendlineEstimator::default(); - self.active_delay_detector_type = DelayDetector::Video; + self.video_delay_detector = TrendlineEstimator::new(self.trendline_settings.clone()); } self.last_seen_packet = at_time; - // As an alternative to ignoring small packets, we can separate audio and - // video packets for overuse detection. - let mut delay_detector_for_packet: DelayDetector = DelayDetector::Video; - if self.separate_audio.enabled { - if packet_feedback.sent_packet.audio { - delay_detector_for_packet = DelayDetector::Audio; - self.audio_packets_since_last_video += 1; - if self.audio_packets_since_last_video > self.separate_audio.packet_threshold - && packet_feedback.receive_time - self.last_video_packet_recv_time - > self.separate_audio.time_threshold - { - self.active_delay_detector_type = DelayDetector::Audio; - } - } else { - self.audio_packets_since_last_video = 0; - self.last_video_packet_recv_time = std::cmp::max( - self.last_video_packet_recv_time, - packet_feedback.receive_time, - ); - self.active_delay_detector_type = DelayDetector::Video; - } - } 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 inter_arrival_for_packet: &mut InterArrivalDelta = - if self.separate_audio.enabled && packet_feedback.sent_packet.audio { - &mut self.audio_inter_arrival_delta - } else { - &mut self.video_inter_arrival_delta - }; - let calculated_deltas: bool = inter_arrival_for_packet.compute_deltas( + let calculated_deltas: bool = self.video_inter_arrival_delta.compute_deltas( packet_feedback.sent_packet.send_time, packet_feedback.receive_time, at_time, @@ -291,31 +203,20 @@ impl DelayBasedBwe { &mut size_delta, ); - match delay_detector_for_packet { - DelayDetector::Audio => self.audio_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, - ), - DelayDetector::Video => 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, - ), - } + 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, - //_state_estimate: Option, recovered_from_overuse: bool, _in_alr: bool, at_time: Timestamp, @@ -393,11 +294,7 @@ impl DelayBasedBwe { #[cfg(test)] mod test { use crate::gcc::api::transport::{PacedPacketInfo, SentPacket}; - use crate::gcc::{ - AcknowledgedBitrateEstimator, AcknowledgedBitrateEstimatorInterface, ProbeBitrateEstimator, - RobustThroughputEstimatorSettings, - }; - + use crate::gcc::{ProbeBitrateEstimator, RobustThroughputEstimator}; pub use super::*; @@ -462,12 +359,7 @@ mod test { // 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, - next_sequence_number: &mut i64, - packets: &mut Vec, - ) -> i64 { + 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; } @@ -479,8 +371,6 @@ mod test { 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 _); - packet.sent_packet.sequence_number = *next_sequence_number; - *next_sequence_number += 1; packets.push(packet); } self.next_rtp_time = time_now_us + (1000000 + self.fps / 2) / self.fps; @@ -553,12 +443,7 @@ mod test { // 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, - next_sequence_number: &mut i64, - packets: &mut Vec, - ) -> i64 { + 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 @@ -566,7 +451,7 @@ mod test { .iter_mut() .min_by_key(|x| x.next_rtp_time()) .unwrap(); - it.generate_frame(time_now_us, next_sequence_number, packets); + 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() @@ -590,28 +475,21 @@ mod test { pub struct DelayBasedBweTest { clock: Timestamp, // Time at the receiver. bitrate_observer: TestBitrateObserver, - acknowledged_bitrate_estimator: Box, + acknowledged_bitrate_estimator: RobustThroughputEstimator, probe_bitrate_estimator: ProbeBitrateEstimator, bitrate_estimator: DelayBasedBwe, stream_generator: StreamGenerator, arrival_time_offset_ms: i64, - next_sequence_number: i64, first_update: bool, } impl DelayBasedBweTest { pub fn new() -> Self { - let field_trials = FieldTrials { - robust_throughput_estimator_settings: RobustThroughputEstimatorSettings { - enabled: true, - ..Default::default() - }, - ..Default::default() - }; + let field_trials = FieldTrials::default(); let clock = Timestamp::from_micros(100000000); Self { - acknowledged_bitrate_estimator: AcknowledgedBitrateEstimator::create(&field_trials), + acknowledged_bitrate_estimator: RobustThroughputEstimator::default(), probe_bitrate_estimator: ProbeBitrateEstimator::default(), bitrate_estimator: DelayBasedBwe::new(&field_trials), stream_generator: StreamGenerator::new( @@ -619,7 +497,6 @@ mod test { clock.us(), ), arrival_time_offset_ms: 0, - next_sequence_number: 0, first_update: true, clock, bitrate_observer: TestBitrateObserver::new(), @@ -646,13 +523,10 @@ mod test { send_time, size: DataSize::from_bytes(payload_size as _), pacing_info, - sequence_number: self.next_sequence_number, ..Default::default() }, - ..Default::default() }; - self.next_sequence_number += 1; if packet.sent_packet.pacing_info.probe_cluster_id != PacedPacketInfo::NOT_APROBE { self.probe_bitrate_estimator .handle_probe_and_estimate_bitrate(&packet); @@ -663,10 +537,12 @@ mod test { packet_feedbacks: vec![packet], ..Default::default() }; + let sorted = msg.sorted_by_receive_time(); self.acknowledged_bitrate_estimator - .incoming_packet_feedback(&msg.sorted_by_receive_time()); + .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(), @@ -688,11 +564,9 @@ mod test { 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 self.next_sequence_number, - &mut packets, - ); + let next_time_us: i64 = self + .stream_generator + .generate_frame(self.clock.us(), &mut packets); if packets.is_empty() { return false; } @@ -718,9 +592,11 @@ mod test { ..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(), @@ -866,14 +742,18 @@ mod test { 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!( + !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_estimator + .latest_estimate(&mut ssrcs, &mut bitrate) + ); assert!(!self.bitrate_observer.updated()); self.bitrate_observer.reset(); self.clock += TimeDelta::from_millis(1000); @@ -888,9 +768,11 @@ mod test { }; if i == NUM_INITIAL_PACKETS { - assert!(!self - .bitrate_estimator - .latest_estimate(&mut ssrcs, &mut bitrate)); + assert!( + !self + .bitrate_estimator + .latest_estimate(&mut ssrcs, &mut bitrate) + ); assert_eq!(0, ssrcs.len()); assert!(!self.bitrate_observer.updated()); self.bitrate_observer.reset(); @@ -904,9 +786,10 @@ mod test { self.clock += TimeDelta::from_millis(1000 / FRAMERATE); send_time_ms += FRAME_INTERVAL_MS; } - assert!(self - .bitrate_estimator - .latest_estimate(&mut ssrcs, &mut bitrate)); + 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!( @@ -1195,17 +1078,6 @@ mod test { ); } - #[test] - fn get_expected_bwe_period_ms() { - let mut bwe = DelayBasedBweTest::new(); - let default_interval = bwe.bitrate_estimator.get_expected_bwe_period(); - assert!(default_interval.ms() > 0); - bwe.capacity_drop_test_helper(1, true, 533, 0); - let interval = bwe.bitrate_estimator.get_expected_bwe_period(); - assert!(interval.ms() > 0); - assert_ne!(interval.ms(), default_interval.ms()); - } - #[test] fn initial_behavior() { let mut bwe = DelayBasedBweTest::new(); diff --git a/rs/moq-congestion/src/gcc/experiments/alr_experiment.rs b/rs/moq-congestion/src/gcc/experiments/alr_experiment.rs deleted file mode 100644 index d9cf6087143..00000000000 --- a/rs/moq-congestion/src/gcc/experiments/alr_experiment.rs +++ /dev/null @@ -1,27 +0,0 @@ -/// WebRTC-ProbingScreenshareBwe -// Apparently these are now unconfigurable constants? -#[derive(Clone, Debug)] -pub struct AlrExperimentSettings { - pub pacing_factor: f64, - pub max_paced_queue_time: i64, - pub alr_bandwidth_usage_percent: i64, - pub alr_start_budget_level_percent: i64, - pub alr_stop_budget_level_percent: i64, - /// Will be sent to the receive side for stats slicing. - /// Can be 0..6, because it's sent as a 3 bits value and there's also - /// reserved value to indicate absence of experiment. - pub group_id: i64, -} - -impl Default for AlrExperimentSettings { - fn default() -> Self { - Self { - pacing_factor: 1.0, - max_paced_queue_time: 2875, - alr_bandwidth_usage_percent: 80, - alr_start_budget_level_percent: 40, - alr_stop_budget_level_percent: -60, - group_id: 3, - } - } -} diff --git a/rs/moq-congestion/src/gcc/experiments/field_trials.rs b/rs/moq-congestion/src/gcc/experiments/field_trials.rs index 3c832ec5a66..c1ab33a1bd0 100644 --- a/rs/moq-congestion/src/gcc/experiments/field_trials.rs +++ b/rs/moq-congestion/src/gcc/experiments/field_trials.rs @@ -1,56 +1,33 @@ -use super::{AlrExperimentSettings, CongestionWindowConfig, VideoRateControlConfig}; +use super::CongestionWindowConfig; use crate::gcc::{ - AlrDetectorConfig, BitrateEstimatorConfig, BweLossExperiment, - BweSeparateAudioPacketsSettings, LossBasedBweV2Config, LossBasedControlConfig, - ProbeControllerConfig, RobustThroughputEstimatorSettings, RttBasedBackoffConfig, - SafeResetOnRouteChange, TrendlineEstimatorSettings, - remote_bitrate_estimator::{BweBackOffFactor, EstimateBoundedIncrease}, + AlrDetectorConfig, LossBasedBweV2Config, ProbeControllerConfig, + RobustThroughputEstimatorSettings, RttBasedBackoffConfig, TrendlineEstimatorSettings, + remote_bitrate_estimator::BweBackOffFactor, }; -/// Current field trials for WebRTC that impact GoogCC. +/// The WebRTC field trials that still reach a code path here, as typed settings. /// -/// These have been painstakingly extracted from the WebRTC source code. -/// Normally these field trials are enabled via bespoke string parsing, but that is very difficult to port correctly. -/// I decided to make everything type safe which doubles as documentation; ur welcome. +/// 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, - // Probing settings. /// WebRTC-Bwe-ProbingConfiguration pub probing_configuration: ProbeControllerConfig, - // Use probing to recover faster after large bitrate estimate drops. - /// WebRTC-BweRapidRecoveryExperiment - pub rapid_recovery_experiment: bool, - /// WebRTC-AlrDetectorParameters pub alr_detector_parameters: AlrDetectorConfig, - /// WebRTC-ProbingScreenshareBwe or WebRTC-StrictPacingAndProbing (legacy?) - pub alr_experiment_settings: AlrExperimentSettings, - /// WebRTC-AddPacingToCongestionWindowPushback pub add_pacing_to_congestion_window_pushback: bool, /// WebRTC-CongestionWindow pub congestion_window: CongestionWindowConfig, - /// WebRTC-VideoRateControl - pub video_rate_control: VideoRateControlConfig, - - /// WebRTC-UseBaseHeavyVP8TL3RateAllocation - pub vp8_base_heavy_tl3_alloc: bool, - - /// WebRTC-Bwe-SeparateAudioPackets - pub separate_audio_packets: BweSeparateAudioPacketsSettings, - - /// WebRTC-Bwe-LossBasedControl - pub loss_based_control: LossBasedControlConfig, - - /// LossBasedBweV2Config + /// WebRTC-Bwe-LossBasedBweV2 pub loss_based_bwe_v2: LossBasedBweV2Config, /// WebRTC-Bwe-MaxRttLimit @@ -65,35 +42,11 @@ pub struct FieldTrials { /// WebRTC-DontIncreaseDelayBasedBweInAlr pub no_bitrate_increase_in_alr: bool, - /// WebRTC-Bwe-EstimateBoundedIncrease - pub estimate_bounded_increase: EstimateBoundedIncrease, - /// WebRTC-Bwe-MinAllocAsLowerBound pub min_alloc_as_lower_bound: bool, - /// WebRTC-Bwe-IgnoreProbesLowerThanNetworkStateEstimate - pub ignore_probes_lower_than_network_state_estimate: bool, - /// WebRTC-Bwe-LimitProbesLowerThanThroughputEstimate pub limit_probes_lower_than_throughput_estimate: bool, - - /// WebRTC-Bwe-PaceAtMaxOfBweAndLowerLinkCapacity - pub pace_at_max_of_bwe_and_lower_link_capacity: bool, - - /// WebRTC-Bwe-LimitPacingFactorByUpperLinkCapacityEstimate - pub limit_pacing_factor_by_upper_link_capacity_estimate: bool, - - /// WebRTC-Bwe-SafeResetOnRouteChange - pub safe_reset_on_route_change: SafeResetOnRouteChange, - - /// WebRTC-BweThroughputWindowConfig - pub bwe_throughput_window_config: BitrateEstimatorConfig, - - /// WebRTC-BweLossExperiment - pub loss_experiment: BweLossExperiment, - - /// WebRTC-Bwe-ReceiverLimitCapsOnly - pub receiver_limit_caps_only: bool, } impl Default for FieldTrials { @@ -101,30 +54,16 @@ impl Default for FieldTrials { Self { robust_throughput_estimator_settings: RobustThroughputEstimatorSettings::default(), probing_configuration: ProbeControllerConfig::default(), - rapid_recovery_experiment: false, alr_detector_parameters: AlrDetectorConfig::default(), - alr_experiment_settings: AlrExperimentSettings::default(), add_pacing_to_congestion_window_pushback: false, congestion_window: CongestionWindowConfig::default(), - video_rate_control: VideoRateControlConfig::default(), - vp8_base_heavy_tl3_alloc: false, - separate_audio_packets: BweSeparateAudioPacketsSettings::default(), - loss_based_control: LossBasedControlConfig::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, - estimate_bounded_increase: EstimateBoundedIncrease::default(), min_alloc_as_lower_bound: true, - ignore_probes_lower_than_network_state_estimate: true, limit_probes_lower_than_throughput_estimate: true, - pace_at_max_of_bwe_and_lower_link_capacity: false, - limit_pacing_factor_by_upper_link_capacity_estimate: false, - safe_reset_on_route_change: SafeResetOnRouteChange::default(), - bwe_throughput_window_config: BitrateEstimatorConfig::default(), - loss_experiment: BweLossExperiment::default(), - receiver_limit_caps_only: false, } } } diff --git a/rs/moq-congestion/src/gcc/experiments/mod.rs b/rs/moq-congestion/src/gcc/experiments/mod.rs index 95e8e7d189a..04e51c1d649 100644 --- a/rs/moq-congestion/src/gcc/experiments/mod.rs +++ b/rs/moq-congestion/src/gcc/experiments/mod.rs @@ -1,7 +1,5 @@ -mod alr_experiment; mod field_trials; mod rate_control; -pub use alr_experiment::*; 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 index 35e42e68834..30ba4f80f8d 100644 --- a/rs/moq-congestion/src/gcc/experiments/rate_control.rs +++ b/rs/moq-congestion/src/gcc/experiments/rate_control.rs @@ -8,6 +8,10 @@ * 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 @@ -31,58 +35,15 @@ impl Default for CongestionWindowConfig { } } -/// WebRTC-VideoRateControl -#[derive(Clone, Debug)] -pub struct VideoRateControlConfig { - pub pacing_factor: Option, - pub alr_probing: bool, - pub vp8_qp_max: Option, - pub vp8_min_pixels: Option, - pub trust_vp8: bool, // = true - pub trust_vp9: bool, // = true; - pub bitrate_adjuster: bool, // = true; - pub adjuster_use_headroom: bool, // = true; - pub vp8_s0_boost: bool, - pub vp8_base_heavy_tl3_alloc: bool, -} - -impl Default for VideoRateControlConfig { - fn default() -> Self { - Self { - pacing_factor: None, - alr_probing: false, - vp8_qp_max: None, - vp8_min_pixels: None, - trust_vp8: true, - trust_vp9: true, - bitrate_adjuster: true, - adjuster_use_headroom: true, - vp8_s0_boost: false, - vp8_base_heavy_tl3_alloc: false, - } - } -} - -#[derive(Default)] +#[derive(Clone, Default)] pub struct RateControlSettings { congestion_window_config: CongestionWindowConfig, - video_config: VideoRateControlConfig, } impl RateControlSettings { - // const char kCongestionWindowDefaultFieldTrialString[] = - //"QueueSize:350,MinBitrate:30000,DropFrame:true"; - //const char kUseBaseHeavyVp8Tl3RateAllocationFieldTrialName[] = - // "WebRTC-UseBaseHeavyVP8TL3RateAllocation"; - pub fn new(field_trials: &FieldTrials) -> Self { - let congestion_window_config = field_trials.congestion_window.clone(); - let mut video_config = field_trials.video_rate_control.clone(); - video_config.vp8_base_heavy_tl3_alloc = - video_config.vp8_base_heavy_tl3_alloc || field_trials.vp8_base_heavy_tl3_alloc; Self { - congestion_window_config, - video_config, + congestion_window_config: field_trials.congestion_window.clone(), } } @@ -113,56 +74,4 @@ impl RateControlSettings { pub fn congestion_window_initial_data_window(&self) -> Option { self.congestion_window_config.initial_data_window } - - pub fn get_pacing_factor(&self) -> Option { - self.video_config.pacing_factor - } - pub fn use_alr_probing(&self) -> bool { - self.video_config.alr_probing - } - - pub fn libvpx_vp8_qp_max(&self) -> Option { - if let Some(vp8_qp_max) = self.video_config.vp8_qp_max { - if !(0..=63).contains(&vp8_qp_max) { - tracing::warn!("Unsupported vp8_qp_max_ value, ignored."); - return None; - } - } - self.video_config.vp8_qp_max - } - pub fn libvpx_vp8_min_pixels(&self) -> Option { - if let Some(vp8_min_pixels) = self.video_config.vp8_min_pixels { - if vp8_min_pixels < 1 { - tracing::warn!("Unsupported vp8_min_pixels_ value, ignored."); - return None; - } - } - self.video_config.vp8_min_pixels - } - pub fn libvpx_vp8_trusted_rate_controller(&self) -> bool { - self.video_config.trust_vp8 - } - pub fn vp8_boost_base_layer_quality(&self) -> bool { - self.video_config.vp8_s0_boost - } - pub fn vp8_dynamic_rate_settings(&self) -> bool { - todo!(); - } - pub fn libvpx_vp9_trusted_rate_controller(&self) -> bool { - self.video_config.trust_vp9 - } - pub fn vp9_dynamic_rate_settings(&self) -> bool { - todo!(); - } - - pub fn vp8_base_heavy_tl3_rate_allocation(&self) -> bool { - self.video_config.vp8_base_heavy_tl3_alloc - } - - pub fn use_encoder_bitrate_adjuster(&self) -> bool { - self.video_config.bitrate_adjuster - } - pub fn bitrate_adjuster_can_use_network_headroom(&self) -> bool { - self.video_config.adjuster_use_headroom - } } diff --git a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs index 977d41edd41..c0d736caad0 100644 --- a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs +++ b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs @@ -8,74 +8,51 @@ * 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 stream and constraint changes, and a network state estimate. A QUIC +//! sender has none of them: a path change rebuilds the controller, and the constraints and stream +//! settings come once, at construction. What remains is the network becoming available, the +//! process timer, the RTT, each sent packet, and each ACK's feedback. + use std::collections::VecDeque; use crate::gcc::{ + AlrDetector, BandwidthLimitedCause, CongestionWindowPushbackController, DelayBasedBwe, + DelayBasedBweResult, LossBasedState, ProbeBitrateEstimator, ProbeController, + RobustThroughputEstimator, SendSideBandwidthEstimation, api::{ - network_control::{NetworkControllerConfig, NetworkControllerInterface}, transport::{ - BandwidthUsage, NetworkAvailability, NetworkControlUpdate, NetworkEstimate, - NetworkRouteChange, NetworkStateEstimate, PacedPacketInfo, PacerConfig, - ProbeClusterConfig, ProcessInterval, ReceivedPacket, RemoteBitrateReport, - RoundTripTimeUpdate, SentPacket, StreamsConfig, TargetRateConstraints, - TargetTransferRate, TransportLossReport, TransportPacketsFeedback, + BandwidthUsage, NetworkAvailability, NetworkControlUpdate, PacedPacketInfo, + PacerConfig, ProbeClusterConfig, ProcessInterval, RoundTripTimeUpdate, SentPacket, + StreamsConfig, TargetRateConstraints, TargetTransferRate, TransportPacketsFeedback, }, units::{DataRate, DataSize, TimeDelta, Timestamp}, }, experiments::{FieldTrials, RateControlSettings}, - AcknowledgedBitrateEstimator, remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE, }; -use super::{ - AcknowledgedBitrateEstimatorInterface, AlrDetector, BandwidthLimitedCause, - CongestionWindowPushbackController, DelayBasedBwe, DelayBasedBweResult, LossBasedState, - ProbeBitrateEstimator, ProbeController, SendSideBandwidthEstimation, -}; - -#[derive(Clone, Debug)] -pub struct SafeResetOnRouteChange { - pub enabled: bool, // Enabled - pub ack: bool, // ack -} - -impl Default for SafeResetOnRouteChange { - fn default() -> Self { - Self { - enabled: true, - ack: true, - } - } -} - -/// Google Congestion Control specific configuration. -/// -/// NOTE: NetworkStateEstimator and NetworkStatePredictor have not been ported. -/// These are optional traits and do not seem to be implemented, except perhaps by Google internally. -#[derive(Clone, Debug, Default)] -pub struct GoogCcConfig { - // network_state_estimator: Box, - // network_state_predictor: Box, - pub feedback_only: bool, +/// 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. -/// -/// This is the congestion controller used within WebRTC. -/// It has been extracted from the libwebrtc source code and ported to Rust to avoid linking headaches. -/// Use the [NetworkControllerInterface] to interact with this controller. +/// Google Congestion Control, as WebRTC's `GoogCcNetworkController`. +#[derive(Clone)] pub struct GoogCcNetworkController { - field_trials: FieldTrials, - - packet_feedback_only: bool, - safe_reset_on_route_change: bool, - safe_reset_acknowledged_rate: bool, use_min_allocatable_as_lower_bound: bool, - ignore_probes_lower_than_network_estimate: bool, limit_probes_lower_than_throughput_estimate: bool, rate_control_settings: RateControlSettings, - pace_at_max_of_bwe_and_lower_link_capacity: bool, - limit_pacingfactor_by_upper_link_capacity_estimate: bool, probe_controller: ProbeController, congestion_window_pushback_controller: Option, @@ -84,52 +61,46 @@ pub struct GoogCcNetworkController { alr_detector: AlrDetector, probe_bitrate_estimator: ProbeBitrateEstimator, delay_based_bwe: DelayBasedBwe, - acknowledged_bitrate_estimator: Box, + acknowledged_bitrate_estimator: RobustThroughputEstimator, initial_config: Option, min_target_rate: DataRate, min_data_rate: DataRate, - max_data_rate: DataRate, // = DataRate::plus_infinity(); + max_data_rate: DataRate, starting_rate: Option, first_packet_sent: bool, - - estimate: Option, - - next_loss_update: Timestamp, // = Timestamp::minus_infinity(); - lost_packets_since_last_loss_update: i64, - expected_packets_since_last_loss_update: i64, + first_transport_feedback_received: bool, feedback_max_rtts: VecDeque, last_loss_based_target_rate: DataRate, last_pushback_target_rate: DataRate, - last_stable_target_rate: DataRate, last_loss_base_state: LossBasedState, - last_estimated_fraction_loss: u8, - last_estimated_round_trip_time: TimeDelta, //= TimeDelta::plus_infinity(); + last_estimated_round_trip_time: TimeDelta, - pacing_factor: f64, + pacing_factor: Option, + pacing_time_window: TimeDelta, min_total_allocated_bitrate: DataRate, - max_padding_rate: DataRate, previously_in_alr: bool, + last_is_bandwidth_limited: bool, current_data_window: Option, } impl GoogCcNetworkController { - // From RTCPSender video report interval. - const LOSS_UPDATE_INTERVAL: TimeDelta = TimeDelta::from_millis(1000); - // 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. @@ -137,386 +108,69 @@ impl GoogCcNetworkController { // below the current throughput estimate to drain the network queues. const PROBE_DROP_THROUGHPUT_FRACTION: f64 = 0.85; - pub fn new(config: NetworkControllerConfig, goog_cc_config: GoogCcConfig) -> Self { - let field_trials = config.field_trials.clone(); - let rate_control_settings = RateControlSettings::new(&field_trials); + 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"); - assert!(config.constraints.at_time.is_finite()); - let mut delay_based_bwe = DelayBasedBwe::new(&field_trials); + let mut delay_based_bwe = DelayBasedBwe::new(field_trials); delay_based_bwe.set_min_bitrate(CONGESTION_CONTROLLER_MIN_BITRATE); Self { - packet_feedback_only: goog_cc_config.feedback_only, - safe_reset_on_route_change: field_trials.safe_reset_on_route_change.enabled, - safe_reset_acknowledged_rate: field_trials.safe_reset_on_route_change.ack, use_min_allocatable_as_lower_bound: field_trials.min_alloc_as_lower_bound, - ignore_probes_lower_than_network_estimate: field_trials - .ignore_probes_lower_than_network_state_estimate, limit_probes_lower_than_throughput_estimate: field_trials .limit_probes_lower_than_throughput_estimate, - pace_at_max_of_bwe_and_lower_link_capacity: field_trials - .pace_at_max_of_bwe_and_lower_link_capacity, - limit_pacingfactor_by_upper_link_capacity_estimate: field_trials - .limit_pacing_factor_by_upper_link_capacity_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)), + .then(|| CongestionWindowPushbackController::new(field_trials)), rate_control_settings, - bandwidth_estimation: SendSideBandwidthEstimation::new(field_trials.clone()), + 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: AcknowledgedBitrateEstimator::create(&field_trials), + 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, - estimate: None, - next_loss_update: Timestamp::minus_infinity(), - lost_packets_since_last_loss_update: 0, - expected_packets_since_last_loss_update: 0, + first_transport_feedback_received: false, feedback_max_rtts: VecDeque::new(), - last_loss_based_target_rate: config.constraints.starting_rate.unwrap(), - last_pushback_target_rate: config.constraints.starting_rate.unwrap(), - last_stable_target_rate: config.constraints.starting_rate.unwrap(), + last_loss_based_target_rate: starting_rate, + last_pushback_target_rate: starting_rate, last_loss_base_state: LossBasedState::DelayBasedEstimate, - last_estimated_fraction_loss: 0, last_estimated_round_trip_time: TimeDelta::plus_infinity(), - pacing_factor: config - .stream_based_config - .pacing_factor - .unwrap_or(Self::DEFAULT_PACE_MULTIPLIER), + 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()), - - max_padding_rate: config - .stream_based_config - .max_padding_rate - .unwrap_or(DataRate::zero()), previously_in_alr: false, + last_is_bandwidth_limited: true, current_data_window: None, initial_config: Some(config), - field_trials, - } - } - - pub fn get_network_state(&self, at_time: Timestamp) -> NetworkControlUpdate { - NetworkControlUpdate { - target_rate: Some(TargetTransferRate { - network_estimate: NetworkEstimate { - at_time, - loss_rate_ratio: self.last_estimated_fraction_loss as f32 / 255.0, - round_trip_time: self.last_estimated_round_trip_time, - bwe_period: self.delay_based_bwe.get_expected_bwe_period(), - ..Default::default() - }, - at_time, - target_rate: self.last_pushback_target_rate, - stable_target_rate: self.bandwidth_estimation.get_estimated_link_capacity(), - ..Default::default() - }), - pacer_config: Some(self.get_pacing_rates(at_time)), - congestion_window: self.current_data_window, - ..Default::default() - } - } - - 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 bitrate smaller than min bitrate"); - self.max_data_rate = self.min_data_rate; - } - match self.starting_rate { - Some(starting_rate) if starting_rate < self.min_data_rate => { - tracing::warn!("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 fraction_loss: u8 = self.bandwidth_estimation.fraction_loss(); - 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; - - let mut cwnd_reduce_ratio: f64 = 0.0; - 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); - if self - .rate_control_settings - .use_congestion_window_drop_frame_only() - { - cwnd_reduce_ratio = (loss_based_target_rate.bps_float() - - pushback_target_rate.bps_float()) - / loss_based_target_rate.bps_float(); - } - } - let mut stable_target_rate: DataRate = - self.bandwidth_estimation.get_estimated_link_capacity(); - stable_target_rate = std::cmp::min(stable_target_rate, pushback_target_rate); - - if (loss_based_target_rate != self.last_loss_based_target_rate) - || (loss_based_state != self.last_loss_base_state) - || (fraction_loss != self.last_estimated_fraction_loss) - || (round_trip_time != self.last_estimated_round_trip_time) - || (pushback_target_rate != self.last_pushback_target_rate) - || (stable_target_rate != self.last_stable_target_rate) - { - self.last_loss_based_target_rate = loss_based_target_rate; - self.last_pushback_target_rate = pushback_target_rate; - self.last_estimated_fraction_loss = fraction_loss; - self.last_estimated_round_trip_time = round_trip_time; - self.last_stable_target_rate = stable_target_rate; - self.last_loss_base_state = loss_based_state; - - self.alr_detector - .set_estimated_bitrate(loss_based_target_rate.bps()); - - let bwe_period: TimeDelta = self.delay_based_bwe.get_expected_bwe_period(); - - let mut target_rate_msg = TargetTransferRate { - at_time, - stable_target_rate, - network_estimate: NetworkEstimate { - at_time, - round_trip_time, - loss_rate_ratio: fraction_loss as f32 / 255.0, - bwe_period, - ..Default::default() - }, - ..Default::default() - }; - - if self - .rate_control_settings - .use_congestion_window_drop_frame_only() - { - target_rate_msg.target_rate = loss_based_target_rate; - target_rate_msg.cwnd_reduce_ratio = cwnd_reduce_ratio; - } else { - target_rate_msg.target_rate = pushback_target_rate; - } - - update.target_rate = Some(target_rate_msg); - - 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(at_time)); - tracing::trace!( - "bwe {} pushback_target_bps={} estimate_bps={}", - at_time.ms(), - self.last_pushback_target_rate.bps(), - loss_based_target_rate.bps() - ); - } - } - - fn get_pacing_rates(&self, at_time: Timestamp) -> 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 mut pacing_rate = match self.estimate { - Some(estimate) - if self.pace_at_max_of_bwe_and_lower_link_capacity - && !self.bandwidth_estimation.pace_at_loss_based_estimate() => - { - *[ - self.min_total_allocated_bitrate, - estimate.link_capacity_lower, - self.last_loss_based_target_rate, - ] - .iter() - .max() - .unwrap() - * self.pacing_factor - } - _ => { - self.min_total_allocated_bitrate - .max(self.last_loss_based_target_rate) - * self.pacing_factor - } - }; - - match self.estimate { - Some(estimate) - if self.limit_pacingfactor_by_upper_link_capacity_estimate - && estimate.link_capacity_upper.is_finite() - && pacing_rate > estimate.link_capacity_upper => - { - pacing_rate = *[ - estimate.link_capacity_upper, - self.min_total_allocated_bitrate, - self.last_loss_based_target_rate, - ] - .iter() - .max() - .unwrap() - } - _ => {} - }; - - let mut padding_rate: DataRate = - if self.last_loss_base_state == LossBasedState::IncreaseUsingPadding { - self.max_padding_rate.max(self.last_loss_based_target_rate) - } else { - self.max_padding_rate - }; - padding_rate = std::cmp::min(padding_rate, self.last_pushback_target_rate); - let mut msg = PacerConfig::default(); - msg.at_time = at_time; - msg.time_window = TimeDelta::from_seconds(1); - msg.data_window = pacing_rate * msg.time_window; - msg.pad_window = padding_rate * msg.time_window; - msg - } - - fn set_network_state_estimate(&mut self, estimate: Option) { - let prev_estimate = self.estimate; - self.estimate = estimate; - - match (self.estimate, prev_estimate) { - (Some(estimate), Some(prev_estimate)) - if estimate.update_time == prev_estimate.update_time => {} - (Some(estimate), _) => self.probe_controller.set_network_state_estimate(estimate), - _ => {} - }; - } - - fn update_congestion_window_size(&mut self) { - let min_feedback_max_rtt: TimeDelta = - TimeDelta::from_millis(*self.feedback_max_rtts.iter().min().unwrap()); - - 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); } -} -impl NetworkControllerInterface for GoogCcNetworkController { - fn on_network_availability(&mut self, msg: NetworkAvailability) -> NetworkControlUpdate { + pub fn on_network_availability(&mut self, msg: NetworkAvailability) -> NetworkControlUpdate { NetworkControlUpdate { probe_cluster_configs: self.probe_controller.on_network_availability(msg), ..Default::default() } } - fn on_network_route_change(&mut self, mut msg: NetworkRouteChange) -> NetworkControlUpdate { - if self.safe_reset_on_route_change { - let mut estimated_bitrate: Option; - if self.safe_reset_acknowledged_rate { - estimated_bitrate = self.acknowledged_bitrate_estimator.bitrate(); - if estimated_bitrate.is_none() { - estimated_bitrate = self.acknowledged_bitrate_estimator.peek_rate(); - } - } else { - estimated_bitrate = Some(self.bandwidth_estimation.target_rate()); - } - if let Some(estimated_bitrate) = estimated_bitrate { - msg.constraints.starting_rate = Some(match msg.constraints.starting_rate { - Some(starting_rate) => starting_rate.min(estimated_bitrate), - None => estimated_bitrate, - }); - } - } - - self.acknowledged_bitrate_estimator = - AcknowledgedBitrateEstimator::create(&self.field_trials); - self.probe_bitrate_estimator = ProbeBitrateEstimator::default(); - self.delay_based_bwe = DelayBasedBwe::new(&self.field_trials); - self.bandwidth_estimation.on_route_change(); - self.probe_controller.reset(msg.at_time); - let mut update = NetworkControlUpdate { - probe_cluster_configs: self.reset_constraints(msg.constraints), - ..Default::default() - }; - self.maybe_trigger_on_network_changed(&mut update, msg.at_time); - update - } - - fn on_process_interval(&mut self, msg: ProcessInterval) -> NetworkControlUpdate { + 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(msg.at_time)); + update.pacer_config = Some(self.get_pacing_rates()); if let Some(requests_alr_probing) = initial_config.stream_based_config.requests_alr_probing @@ -524,17 +178,15 @@ impl NetworkControllerInterface for GoogCcNetworkController { self.probe_controller .enable_periodic_alr_probing(requests_alr_probing); } - if let Some(enable_repeated_initial_probing) = initial_config + 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 - .enable_repeated_initial_probing + .max_total_allocated_bitrate { - self.probe_controller - .enable_repeated_initial_probing(enable_repeated_initial_probing); - } - let total_bitrate: Option = initial_config - .stream_based_config - .max_total_allocated_bitrate; - if let Some(total_bitrate) = total_bitrate { let mut probes = self .probe_controller .on_max_total_allocated_bitrate(total_bitrate, msg.at_time); @@ -549,10 +201,15 @@ impl NetworkControllerInterface for GoogCcNetworkController { congestion_window_pushback_controller.update_pacing_queue(pacer_queue.bytes()) }; self.bandwidth_estimation.update_estimate(msg.at_time); - let start_time_ms: Option = self - .alr_detector - .get_application_limited_region_start_time(); - self.probe_controller.set_alr_start_time_ms(start_time_ms); + // 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); @@ -561,14 +218,12 @@ impl NetworkControllerInterface for GoogCcNetworkController { { 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 - .update_outstanding_data(current_data_window.bytes()) + congestion_window_pushback_controller.set_data_window(current_data_window) } _ => update.congestion_window = self.current_data_window, }; @@ -577,35 +232,15 @@ impl NetworkControllerInterface for GoogCcNetworkController { update } - fn on_remote_bitrate_report(&mut self, msg: RemoteBitrateReport) -> NetworkControlUpdate { - if self.packet_feedback_only { - tracing::error!("Received REMB for packet feedback only GoogCC"); - return NetworkControlUpdate::default(); - } - self.bandwidth_estimation - .update_receiver_estimate(msg.receive_time, msg.bandwidth); - NetworkControlUpdate::default() - } - - fn on_round_trip_time_update(&mut self, msg: RoundTripTimeUpdate) -> NetworkControlUpdate { - if self.packet_feedback_only || msg.smoothed { - return NetworkControlUpdate::default(); - } - assert!(!msg.round_trip_time.is_zero()); + 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, msg.receive_time); - NetworkControlUpdate::default() + self.bandwidth_estimation.update_rtt(msg.round_trip_time); } - fn on_sent_packet(&mut self, sent_packet: SentPacket) -> NetworkControlUpdate { + pub fn on_sent_packet(&mut self, sent_packet: SentPacket) -> NetworkControlUpdate { self.alr_detector - .on_bytes_sent(sent_packet.size.bytes() as _, sent_packet.send_time.ms()); - self.acknowledged_bitrate_estimator.set_alr( - self.alr_detector - .get_application_limited_region_start_time() - .is_some(), - ); + .on_bytes_sent(sent_packet.size, sent_packet.send_time); if !self.first_packet_sent { self.first_packet_sent = true; @@ -614,7 +249,7 @@ impl NetworkControllerInterface for GoogCcNetworkController { self.bandwidth_estimation .update_propagation_rtt(sent_packet.send_time, TimeDelta::zero()); } - self.bandwidth_estimation.on_sent_packet(sent_packet); + self.bandwidth_estimation.on_sent_packet(&sent_packet); if let Some(congestion_window_pushback_controller) = &mut self.congestion_window_pushback_controller @@ -629,96 +264,17 @@ impl NetworkControllerInterface for GoogCcNetworkController { } } - fn on_received_packet(&mut self, _: ReceivedPacket) -> NetworkControlUpdate { - NetworkControlUpdate::default() - } - - 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: bool = false; - match msg.pacing_factor { - Some(pacing_factor) if pacing_factor != self.pacing_factor => { - self.pacing_factor = pacing_factor; - pacing_changed = true; - } - _ => {} - }; - match msg.min_total_allocated_bitrate { - Some(min_total_allocated_bitrate) - if 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); - } - } - _ => {} - }; - - match msg.max_padding_rate { - Some(max_padding_rate) if max_padding_rate != self.max_padding_rate => { - self.max_padding_rate = max_padding_rate; - pacing_changed = true; - } - _ => {} - }; - - if pacing_changed { - update.pacer_config = Some(self.get_pacing_rates(msg.at_time)); - } - update - } - - fn on_target_rate_constraints( - &mut self, - constraints: TargetRateConstraints, - ) -> NetworkControlUpdate { - let mut update = NetworkControlUpdate { - probe_cluster_configs: self.reset_constraints(constraints), - ..Default::default() - }; - self.maybe_trigger_on_network_changed(&mut update, constraints.at_time); - update - } - - fn on_transport_loss_report(&mut self, msg: TransportLossReport) -> NetworkControlUpdate { - if self.packet_feedback_only { - return NetworkControlUpdate::default(); - } - let total_packets_delta: i64 = - msg.packets_received_delta as i64 + msg.packets_lost_delta as i64; - self.bandwidth_estimation.update_packets_lost( - msg.packets_lost_delta as _, - total_packets_delta, - msg.receive_time, - ); - NetworkControlUpdate::default() - } - - fn on_transport_packets_feedback( + pub fn on_transport_packets_feedback( &mut self, - report: TransportPacketsFeedback, + 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 @@ -732,7 +288,7 @@ impl NetworkControllerInterface for GoogCcNetworkController { .received_with_send_info() .map(|x| x.receive_time) .max() - .unwrap(); + .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; @@ -752,108 +308,56 @@ impl NetworkControllerInterface for GoogCcNetworkController { self.bandwidth_estimation .update_propagation_rtt(report.feedback_time, min_propagation_rtt); } - if self.packet_feedback_only { - if !self.feedback_max_rtts.is_empty() { - let sum_rtt_ms: i64 = self.feedback_max_rtts.iter().sum(); - let mean_rtt_ms: i64 = sum_rtt_ms / self.feedback_max_rtts.len() as i64; - self.delay_based_bwe - .on_rtt_update(TimeDelta::from_millis(mean_rtt_ms)); - } - - let mut feedback_min_rtt: TimeDelta = TimeDelta::plus_infinity(); - for packet_feedback in report.received_with_send_info() { - let pending_time: TimeDelta = max_recv_time - packet_feedback.receive_time; - let rtt: TimeDelta = - report.feedback_time - packet_feedback.sent_packet.send_time - pending_time; - // Value used for predicting NACK round trip time in FEC controller. - feedback_min_rtt = std::cmp::min(rtt, feedback_min_rtt); - } - if feedback_min_rtt.is_finite() { - self.bandwidth_estimation - .update_rtt(feedback_min_rtt, report.feedback_time); - } - self.expected_packets_since_last_loss_update += - report.packets_with_feedback().len() as i64; - for packet_feedback in report.packets_with_feedback() { - if !packet_feedback.is_received() { - self.lost_packets_since_last_loss_update += 1; - } - } - if report.feedback_time > self.next_loss_update { - self.next_loss_update = report.feedback_time + Self::LOSS_UPDATE_INTERVAL; - self.bandwidth_estimation.update_packets_lost( - self.lost_packets_since_last_loss_update, - self.expected_packets_since_last_loss_update, - report.feedback_time, - ); - self.expected_packets_since_last_loss_update = 0; - self.lost_packets_since_last_loss_update = 0; - } - } - let alr_start_time: Option = self + let alr_start_time: Option = self .alr_detector .get_application_limited_region_start_time(); - if self.previously_in_alr && alr_start_time.is_none() { - let now_ms: i64 = report.feedback_time.ms(); - self.acknowledged_bitrate_estimator + self.probe_controller .set_alr_ended_time(report.feedback_time); - self.probe_controller.set_alr_ended_time_ms(now_ms); } self.previously_in_alr = alr_start_time.is_some(); + let sorted = report.sorted_by_receive_time(); self.acknowledged_bitrate_estimator - .incoming_packet_feedback(&report.sorted_by_receive_time()); + .incoming_packet_feedback(&sorted); let acknowledged_bitrate = self.acknowledged_bitrate_estimator.bitrate(); self.bandwidth_estimation - .set_acknowledged_rate(acknowledged_bitrate, report.feedback_time); - for feedback in report.sorted_by_receive_time() { + .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); + .handle_probe_and_estimate_bitrate(feedback); } } let mut probe_bitrate: Option = self .probe_bitrate_estimator .fetch_and_reset_last_estimated_bitrate(); - match (probe_bitrate, self.estimate) { - (Some(current), Some(estimate)) - if self.ignore_probes_lower_than_network_estimate - && current < self.delay_based_bwe.last_estimate() - && current < estimate.link_capacity_lower => - { - probe_bitrate = None; - } - _ => {} - }; - match (probe_bitrate, acknowledged_bitrate) { - (Some(probe), Some(acknowledged)) - if 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)); - } - _ => {} - }; + 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, + report, + &sorted, acknowledged_bitrate, - probe_bitrate, /*self.estimate,*/ + probe_bitrate, alr_start_time.is_some(), ); @@ -868,7 +372,7 @@ impl NetworkControllerInterface for GoogCcNetworkController { .update_delay_based_estimate(report.feedback_time, result.target_bitrate); } self.bandwidth_estimation.update_loss_based_estimator( - &report, + report, result.delay_detector_state, probe_bitrate, alr_start_time.is_some(), @@ -878,10 +382,8 @@ impl NetworkControllerInterface for GoogCcNetworkController { self.maybe_trigger_on_network_changed(&mut update, report.feedback_time); } - let recovered_from_overuse = result.recovered_from_overuse; - - if recovered_from_overuse { - self.probe_controller.set_alr_start_time_ms(alr_start_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); } @@ -891,28 +393,194 @@ impl NetworkControllerInterface for GoogCcNetworkController { if self.rate_control_settings.use_congestion_window() && max_feedback_rtt.is_finite() { self.update_congestion_window_size(); } - if let (Some(congestion_window_pushback_controller), Some(current_data_window)) = ( + match ( &mut self.congestion_window_pushback_controller, self.current_data_window, ) { - congestion_window_pushback_controller.set_data_window(current_data_window); - } else { - update.congestion_window = 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 } - fn on_network_state_estimate( + /// 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, - estimate: NetworkStateEstimate, - ) -> NetworkControlUpdate { - self.set_network_state_estimate(Some(estimate)); - NetworkControlUpdate::default() + 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 get_process_interval() -> TimeDelta { - TimeDelta::from_millis(25) + 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); } } @@ -930,120 +598,55 @@ fn get_bandwidth_limited_cause( match loss_based_state { // Probes may not be sent in this state. LossBasedState::Decreasing => BandwidthLimitedCause::LossLimitedBwe, - LossBasedState::IncreaseUsingPadding => // Probes may not be sent in this state. - { - BandwidthLimitedCause::LossLimitedBwe - } - LossBasedState::Increasing => + LossBasedState::IncreaseUsingPadding => BandwidthLimitedCause::LossLimitedBwe, // Probes may be sent in this state. - { - BandwidthLimitedCause::LossLimitedBweIncreasing - } + LossBasedState::Increasing => BandwidthLimitedCause::LossLimitedBweIncreasing, LossBasedState::DelayBasedEstimate => BandwidthLimitedCause::DelayBasedLimited, } } #[cfg(test)] mod test { - use crate::gcc::api::transport::PacketResult; use super::*; - // Count dips from a constant high bandwidth level within a short window. - /* - fn count_bandwidth_dips(mut bandwidth_history: VecDeque, threshold: DataRate) -> i64 { - if bandwidth_history.is_empty() { - return 1; - } - let first = bandwidth_history.pop_front().unwrap(); - - let mut dips: i64 = 0; - let mut state_high: bool = true; - while let Some(front) = bandwidth_history.pop_front() { - if front + threshold < first && state_high { - dips += 1; - state_high = false; - } else if front == first { - state_high = true; - } else if front > first { - // If this is toggling we will catch it later when front becomes first. - state_high = false; - } - } - dips - } - */ - 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; - - /* - fn CreateVideoSendingClient( - s: &Scenerio, - config: CallClientConfig, - send_link: Vec, - return_link: Vec) -> CallClient { - let client = s.CreateClient("send", config); - let route = s.CreateRoutes(client, send_link, - s.CreateClient("return", CallClientConfig()), - return_link); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - return client; - } - */ - - fn create_route_change( - time: Timestamp, - start_rate: Option, - min_rate: Option, - max_rate: Option, - ) -> NetworkRouteChange { - NetworkRouteChange { - at_time: time, - constraints: TargetRateConstraints { - at_time: time, - starting_rate: start_rate, - min_data_rate: min_rate, - max_data_rate: max_rate, - }, - } - } + const DEFAULT_PACING_RATE_SEND_SIDE_BWE: f64 = 1.1; fn create_packet_result( arrival_time: Timestamp, send_time: Timestamp, - payload_size: usize, + payload_size: i64, pacing_info: PacedPacketInfo, ) -> PacketResult { PacketResult { sent_packet: SentPacket { send_time, - size: DataSize::from_bytes(payload_size as _), + size: DataSize::from_bytes(payload_size), pacing_info, ..Default::default() }, receive_time: arrival_time, - ..Default::default() } } // Simulate sending packets and receiving transport feedback during // `runtime_ms`, then return the final target birate. - fn packet_transmission_and_feedback_block( - controller: &mut T, + fn packet_transmission_and_feedback_block( + controller: &mut GoogCcNetworkController, runtime_ms: i64, delay: i64, current_time: &mut Timestamp, ) -> Option { - let mut update: NetworkControlUpdate; 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: usize = 1000; + const PAYLOAD_SIZE: i64 = 1000; let packet: PacketResult = create_packet_result( *current_time + TimeDelta::from_millis(delay_buildup), *current_time, @@ -1051,7 +654,7 @@ mod test { PacedPacketInfo::default(), ); delay_buildup += delay; - update = controller.on_sent_packet(packet.sent_packet); + let update = controller.on_sent_packet(packet.sent_packet); if let Some(target_rate) = update.target_rate { target_bitrate = Some(target_rate.target_rate); } @@ -1060,12 +663,12 @@ mod test { packet_feedbacks: vec![packet], ..Default::default() }; - update = controller.on_transport_packets_feedback(feedback); + 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); - update = controller.on_process_interval(ProcessInterval { + let update = controller.on_process_interval(ProcessInterval { at_time: *current_time, ..Default::default() }); @@ -1084,7 +687,7 @@ mod test { ) -> TransportPacketsFeedback { let mut delay_buildup: TimeDelta = one_way_delay; const FEEDBACK_SIZE: i64 = 3; - const PAYLOAD_SIZE: usize = 1000; + const PAYLOAD_SIZE: i64 = 1000; let mut feedback = TransportPacketsFeedback::default(); for _ in 0..FEEDBACK_SIZE { let packet: PacketResult = create_packet_result( @@ -1100,116 +703,31 @@ mod test { feedback } - // Scenarios: - - /* - fn UpdatesTargetRateBasedOnLinkCapacity(test_name: &str) { - let factory = CreateFeedbackOnlyFactory(); - let s = Scenario::new("googcc_unit/target_capacity" + std::string(test_name), false); - let mut config: CallClientConfig::default(); - config.transport.cc_factory = &factory; - config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); - config.transport.rates.max_rate = DataRate::KilobitsPerSec(1500); - config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); - let send_net = s.CreateMutableSimulationNode(|c: &mut NetworkSimulationConfig| { - c.bandwidth = DataRate::KilobitsPerSec(500); - c.delay = TimeDelta::Millis(100); - c.loss_rate = 0.0; - }); - let ret_net = s.CreateMutableSimulationNode( - |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); - let truth: &StatesPrinter = s.CreatePrinter( - "send.truth.txt", TimeDelta::plus_infinity(), {send_net.ConfigPrinter()}); - - let client = CreateVideoSendingClient(&s, config, {send_net.node()}, - {ret_net.node()}); - - truth.PrintRow(); - s.RunFor(TimeDelta::Seconds(25)); - truth.PrintRow(); - assert_relative_eq!(client.target_rate().kbps(), 450, 100); - - send_net.UpdateConfig(|c: &mut NetworkSimulationConfig| { - c.bandwidth = DataRate::KilobitsPerSec(800); - c.delay = TimeDelta::Millis(100); - }); - - truth.PrintRow(); - s.RunFor(TimeDelta::Seconds(20)); - truth.PrintRow(); - assert_relative_eq!(client.target_rate().kbps(), 750, 150); - - send_net.UpdateConfig(|c: &mut NetworkSimulationConfig| { - c.bandwidth = DataRate::KilobitsPerSec(100); - c.delay = TimeDelta::Millis(200); - }); - ret_net.UpdateConfig( - |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(200); }); - - truth.PrintRow(); - s.RunFor(TimeDelta::Seconds(50)); - truth.PrintRow(); - assert_relative_eq!(client.target_rate().kbps(), 90, 25); - } - - fn RunRembDipScenario(test_name: &str) -> DataRate { - let s = Scenario::new(test_name); - let net_conf = NetworkSimulationConfig::new(); - net_conf.bandwidth = DataRate::KilobitsPerSec(2000); - net_conf.delay = TimeDelta::Millis(50); - let client = s.CreateClient("send", |c: &mut CallClientConfig| { - c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); - }); - let send_net = {s.CreateSimulationNode(net_conf)}; - let ret_net = {s.CreateSimulationNode(net_conf)}; - let route = s.CreateRoutes( - client, send_net, s.CreateClient("return", CallClientConfig()), ret_net); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - - s.RunFor(TimeDelta::Seconds(10)); - assert!(client.send_bandwidth().kbps() > 1500); - - let RembLimit: DataRate = DataRate::KilobitsPerSec(250); - client.SetRemoteBitrate(RembLimit); - s.RunFor(TimeDelta::Seconds(1)); - assert_eq!(client.send_bandwidth(), RembLimit); - - let RembLimitLifted: DataRate = DataRate::KilobitsPerSec(10000); - client.SetRemoteBitrate(RembLimitLifted); - s.RunFor(TimeDelta::Seconds(10)); - - return client.send_bandwidth(); - } - */ - - fn create_controller( - field_trials: FieldTrials, - feedback_only: bool, - ) -> GoogCcNetworkController { - let config = NetworkControllerConfig { - field_trials, + 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() - }; - - let goog_cc_config = GoogCcConfig { feedback_only }; - - GoogCcNetworkController::new(config, goog_cc_config) + }) } - #[test] - fn initialize_target_rate_on_first_process_interval_after_network_available() { - let mut controller = create_controller(FieldTrials::default(), false); - + fn available(controller: &mut GoogCcNetworkController, at_time: Timestamp) { let _ = controller.on_network_availability(NetworkAvailability { - at_time: Timestamp::from_millis(123456), + 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() @@ -1231,942 +749,182 @@ mod test { } #[test] - fn reacts_to_changed_network_conditions() { - let mut controller = create_controller(FieldTrials::default(), false); - let mut current_time: Timestamp = Timestamp::from_millis(123); - controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: true, - }); - controller.on_process_interval(ProcessInterval { - at_time: current_time, - ..Default::default() - }); - controller.on_remote_bitrate_report(RemoteBitrateReport { - receive_time: current_time, - bandwidth: INITIAL_BITRATE * 2, - }); - - current_time += TimeDelta::from_millis(25); + 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: current_time, + at_time: Timestamp::from_millis(123456), ..Default::default() }); - assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE * 2); assert_eq!( update.pacer_config.unwrap().data_rate(), - INITIAL_BITRATE * 2 * DEFAULT_PACING_RATE + INITIAL_BITRATE * DEFAULT_PACING_RATE ); - controller.on_remote_bitrate_report(RemoteBitrateReport { - receive_time: current_time, - bandwidth: INITIAL_BITRATE, - }); - current_time += TimeDelta::from_millis(25); - let update = controller.on_process_interval(ProcessInterval { - at_time: current_time, + 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() - }); - assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE); - assert_eq!( - update.pacer_config.unwrap().data_rate(), - INITIAL_BITRATE * DEFAULT_PACING_RATE + }; + 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 on_network_route_changed() { - let mut controller = create_controller(FieldTrials::default(), false); - let current_time: Timestamp = Timestamp::from_millis(123); - controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: true, + fn respects_configured_pacing_factor() { + let mut controller = create_controller(StreamsConfig { + pacing_factor: Some(10.0), + ..Default::default() }); - let new_bitrate: DataRate = DataRate::from_bits_per_sec(200000); - - let update = controller.on_network_route_change(create_route_change( - current_time, - Some(new_bitrate), - None, - None, - )); - assert_eq!(update.target_rate.unwrap().target_rate, new_bitrate); - assert_eq!( - update.pacer_config.unwrap().data_rate(), - new_bitrate * DEFAULT_PACING_RATE - ); - assert_eq!(update.probe_cluster_configs.len(), 2); - - // If the bitrate is reset to -1, the new starting bitrate will be - // the minimum default bitrate. - const DEFAULT_MIN_BITRATE: DataRate = DataRate::from_kilobits_per_sec(5); - let update = - controller.on_network_route_change(create_route_change(current_time, None, None, None)); - assert_eq!(update.target_rate.unwrap().target_rate, DEFAULT_MIN_BITRATE); - assert_relative_eq!( - update.pacer_config.unwrap().data_rate().bps_float(), - DEFAULT_MIN_BITRATE.bps_float() * DEFAULT_PACING_RATE, - epsilon = 10.0 - ); - assert_eq!(update.probe_cluster_configs.len(), 2); - } - - #[test] - fn probe_on_route_change() { - let mut controller = create_controller(FieldTrials::default(), false); - let mut current_time: Timestamp = Timestamp::from_millis(123); - controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: true, + available(&mut controller, Timestamp::from_millis(123456)); + let update = controller.on_process_interval(ProcessInterval { + at_time: Timestamp::from_millis(123456), + ..Default::default() }); - current_time += TimeDelta::from_seconds(3); - - let update = controller.on_network_route_change(create_route_change( - current_time, - Some(2 * INITIAL_BITRATE), - Some(DataRate::zero()), - Some(20 * INITIAL_BITRATE), - )); - - assert!(update.pacer_config.is_some()); - assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE * 2); - assert_eq!(update.probe_cluster_configs.len(), 2); - assert_eq!( - update.probe_cluster_configs[0].target_data_rate, - INITIAL_BITRATE * 6 - ); assert_eq!( - update.probe_cluster_configs[1].target_data_rate, - INITIAL_BITRATE * 12 + update.pacer_config.unwrap().data_rate(), + INITIAL_BITRATE * 10.0 ); - controller.on_process_interval(ProcessInterval { - at_time: current_time, + 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() - }); - } - - #[test] - fn probe_after_route_change_when_transport_writable() { - let mut controller = create_controller(FieldTrials::default(), false); - let current_time: Timestamp = Timestamp::from_millis(123); - - let mut update: NetworkControlUpdate = - controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: false, - }); - assert!(update.probe_cluster_configs.is_empty()); - - update = controller.on_network_route_change(create_route_change( - current_time, - Some(2 * INITIAL_BITRATE), - Some(DataRate::zero()), - Some(20 * INITIAL_BITRATE), - )); - // Transport is not writable. So not point in sending a probe. - assert!(update.probe_cluster_configs.is_empty()); - - // Probe is sent when transport becomes writable. - update = controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: true, - }); - assert!(!update.probe_cluster_configs.is_empty()); + }; + 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 + ); } - // Bandwidth estimation is updated when feedbacks are received. - // Feedbacks which show an increasing delay cause the estimation to be reduced. #[test] fn updates_delay_based_estimate() { - let mut controller = create_controller(FieldTrials::default(), false); + let mut controller = create_controller(StreamsConfig::default()); const RUN_TIME_MS: i64 = 6000; let mut current_time: Timestamp = Timestamp::from_millis(123); - controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: true, - }); + 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: Option = packet_transmission_and_feedback_block( + let target_bitrate_before_delay = packet_transmission_and_feedback_block( &mut controller, RUN_TIME_MS, 0, &mut current_time, - ); - assert!(target_bitrate_before_delay.is_some()); + ) + .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: Option = packet_transmission_and_feedback_block( + let target_bitrate_after_delay = packet_transmission_and_feedback_block( &mut controller, RUN_TIME_MS, 50, &mut current_time, - ); - assert!(target_bitrate_after_delay.unwrap() < target_bitrate_before_delay.unwrap()); + ) + .expect("an estimate"); + assert!(target_bitrate_after_delay < target_bitrate_before_delay); } #[test] - fn pace_at_max_of_lower_link_capacity_and_bwe() { - let field_trials = FieldTrials { - pace_at_max_of_bwe_and_lower_link_capacity: true, - ..Default::default() - }; - let mut controller = create_controller(field_trials, false); - let mut current_time: Timestamp = Timestamp::from_millis(123); - controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: true, - }); - controller.on_process_interval(ProcessInterval { - at_time: current_time, - ..Default::default() - }); - current_time += TimeDelta::from_millis(100); - let mut network_estimate: NetworkStateEstimate = NetworkStateEstimate { - link_capacity_lower: 10 * INITIAL_BITRATE, - ..Default::default() - }; - controller.set_network_state_estimate(Some(network_estimate)); - // OnNetworkStateEstimate does not trigger processing a new estimate. So add a - // dummy loss report to trigger a BWE update in the next process interval. - let loss_report = TransportLossReport { - start_time: current_time, - end_time: current_time, - receive_time: current_time, - packets_received_delta: 50, - packets_lost_delta: 1, - }; - controller.on_transport_loss_report(loss_report); - let update = controller.on_process_interval(ProcessInterval { - at_time: current_time, - ..Default::default() - }); - assert!(update.pacer_config.is_some()); - assert!(update.target_rate.is_some()); - assert!(update.target_rate.unwrap().target_rate < network_estimate.link_capacity_lower); - assert_eq!( - update.pacer_config.unwrap().data_rate().kbps_float(), - network_estimate.link_capacity_lower.kbps_float() * DEFAULT_PACING_RATE - ); + 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); - current_time += TimeDelta::from_millis(100); - // Set a low link capacity estimate and verify that pacing rate is set - // relative to loss based/delay based estimate. - network_estimate = NetworkStateEstimate { - link_capacity_lower: 0.5 * INITIAL_BITRATE, - ..Default::default() - }; - controller.set_network_state_estimate(Some(network_estimate)); - // Again, we need to inject a dummy loss report to trigger an update of the - // BWE in the next process interval. - let loss_report = TransportLossReport { - start_time: current_time, - end_time: current_time, - receive_time: current_time, - packets_received_delta: 50, - packets_lost_delta: 0, - }; - controller.on_transport_loss_report(loss_report); - let update = controller.on_process_interval(ProcessInterval { - at_time: current_time, - ..Default::default() - }); - assert!(update.target_rate.is_some()); - assert!( - update.target_rate.as_ref().unwrap().target_rate > network_estimate.link_capacity_lower + let feedback: TransportPacketsFeedback = create_transport_packets_feedback( + /*per_packet_network_delay=*/ TimeDelta::from_millis(50), + one_way_delay, + /*send_time=*/ current_time, ); - // NOTE: These are off by 4 bps + 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!( - update.pacer_config.unwrap().data_rate().kbps(), - (update.target_rate.unwrap().target_rate * DEFAULT_PACING_RATE).kbps(), + controller.feedback_max_rtts.back(), + Some(&(2 * one_way_delay.ms() + 100)) ); } #[test] - fn limit_pacing_factor_to_upper_link_capacity() { - let field_trials = FieldTrials { - limit_pacing_factor_by_upper_link_capacity_estimate: true, - ..Default::default() - }; - let mut controller = create_controller(field_trials, false); - let mut current_time: Timestamp = Timestamp::from_millis(123); - controller.on_network_availability(NetworkAvailability { - at_time: current_time, - network_available: true, - }); - controller.on_process_interval(ProcessInterval { - at_time: current_time, - ..Default::default() - }); - current_time += TimeDelta::from_millis(100); - let network_estimate: NetworkStateEstimate = NetworkStateEstimate { - link_capacity_upper: (INITIAL_BITRATE * DEFAULT_PACING_RATE) / 2, - ..Default::default() - }; - controller.set_network_state_estimate(Some(network_estimate)); - // OnNetworkStateEstimate does not trigger processing a new estimate. So add a - // dummy loss report to trigger a BWE update in the next process interval. - let loss_report = TransportLossReport { - start_time: current_time, - end_time: current_time, - receive_time: current_time, - packets_received_delta: 50, - packets_lost_delta: 1, - }; - controller.on_transport_loss_report(loss_report); + 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.pacer_config.is_some()); assert!(update.target_rate.is_some()); - assert!(update.target_rate.unwrap().target_rate >= INITIAL_BITRATE); - assert_eq!( - update.pacer_config.unwrap().data_rate(), - network_estimate.link_capacity_upper - ); - } - - // Test congestion window pushback on network delay happens. - /* - #[test] - fn CongestionWindowPushbackOnNetworkDelay() { - let factory = CreateFeedbackOnlyFactory(); - let field_trials = FieldTrials { - congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 800, min_bitrate_bps: 30000, ..Default::default() } - ..Default::default() - }; - let s = Scenario::new("googcc_unit/cwnd_on_delay", false); - let send_net = - s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| { - c.bandwidth = DataRate::KilobitsPerSec(1000); - c.delay = TimeDelta::Millis(100); - }); - let ret_net = s.CreateSimulationNode( - |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); - let mut config = CallClientConfig::default(); - config.transport.cc_factory = &factory; - // Start high so bandwidth drop has max effect. - config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); - config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000); - config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); - - let client = CreateVideoSendingClient(&s, config, - {send_net.node()}, {ret_net}); - - s.RunFor(TimeDelta::Seconds(10)); - send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(10)); - s.RunFor(TimeDelta::Seconds(3)); - - // After 3 seconds without feedback from any sent packets, we expect that the - // target rate is reduced to the minimum pushback threshold - // DefaultMinPushbackTargetBitrateBps, which is defined as 30 kbps in - // congestion_window_pushback_controller. - EXPECT_LT(client.target_rate().kbps(), 40); - } - */ - - /* - // Test congestion window pushback on network delay happens. - #[test] - fn CongestionWindowPushbackDropFrameOnNetworkDelay() { - let factory = CreateFeedbackOnlyFactory(); - let field_trials = FieldTrials { - congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 800, min_bitrate_bps: 30000, drop_frame: true, ..Default::default() } - ..Default::default() - }; - let s = Scenario::new("googcc_unit/cwnd_on_delay", false); - let send_net = - s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| { - c.bandwidth = DataRate::KilobitsPerSec(1000); - c.delay = TimeDelta::Millis(100); - }); - let ret_net = s.CreateSimulationNode( - |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); - let mut config = CallClientConfig::default(); - config.transport.cc_factory = &factory; - // Start high so bandwidth drop has max effect. - config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); - config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000); - config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); - - let client = CreateVideoSendingClient(&s, config, - {send_net.node()}, {ret_net}); - - s.RunFor(TimeDelta::Seconds(10)); - send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(10)); - s.RunFor(TimeDelta::Seconds(3)); - - // As the dropframe is set, after 3 seconds without feedback from any sent - // packets, we expect that the target rate is not reduced by congestion - // window. - assert!(client.target_rate().kbps() > 300); - } - */ + assert!(controller.last_is_bandwidth_limited); - /* - #[test] - fn PaddingRateLimitedByCongestionWindowInTrial() { - let field_trials = FieldTrials { - congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 200, min_bitrate_bps: 30000, ..Default::default() } - ..Default::default() - }; - - let s = Scenario::new("googcc_unit/padding_limited", false); - let send_net = - s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| { - c.bandwidth = DataRate::KilobitsPerSec(1000); - c.delay = TimeDelta::Millis(100); - }); - let ret_net = s.CreateSimulationNode( - |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); - let mut config = CallClientConfig::default(); - // Start high so bandwidth drop has max effect. - config.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); - config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000); - let client = s.CreateClient("send", config); - let route = - s.CreateRoutes(client, {send_net.node()}, - s.CreateClient("return", CallClientConfig()), {ret_net}); - let mut video = VideoStreamConfig::default(); - video.stream.pad_to_rate = config.transport.rates.max_rate; - s.CreateVideoStream(route.forward(), video); - - // Run for a few seconds to allow the controller to stabilize. - s.RunFor(TimeDelta::Seconds(10)); - - // Check that padding rate matches target rate. - assert_relative_eq!(client.padding_rate().kbps(), client.target_rate().kbps(), 1); - - // Check this is also the case when congestion window pushback kicks in. - send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(1)); - assert_relative_eq!(client.padding_rate().kbps(), client.target_rate().kbps(), 1); - } - */ - - /* - #[test] - fn LimitsToFloorIfRttIsHighInTrial() { - // The field trial limits maximum RTT to 2 seconds, higher RTT means that the - // controller backs off until it reaches the minimum configured bitrate. This - // allows the RTT to recover faster than the regular control mechanism would - // achieve. - const BandwidthFloor: DataRate = DataRate::KilobitsPerSec(50); - let field_trials = FieldTrials { - max_rtt_limit: experiments::MaxRttLimitConfig { max_rtt: TimeDelta::Seconds(2), floor: BandwidthFloor, ..Default::default() }, - ..Default::default() - }; - // In the test case, we limit the capacity and add a cross traffic packet - // burst that blocks media from being sent. This causes the RTT to quickly - // increase above the threshold in the trial. - const LinkCapacity: DataRate = DataRate::KilobitsPerSec(100); - const BufferBloatDuration: TimeDelta = TimeDelta::Seconds(10); - let s = Scenario::new("googcc_unit/limit_trial", false); - let send_net = s.CreateSimulationNode(|c: &mut NetworkSimulcationConfig| { - c.bandwidth = LinkCapacity; - c.delay = TimeDelta::Millis(100); - }); - let ret_net = s.CreateSimulationNode( - |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); }); - let mut config = CallClientConfig::default(); - config.transport.rates.start_rate = LinkCapacity; - - let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net}); - // Run for a few seconds to allow the controller to stabilize. - s.RunFor(TimeDelta::Seconds(10)); - const BloatPacketSize: DataSize = DataSize::Bytes(1000); - const BloatPacketCount: i64 = - (BufferBloatDuration * LinkCapacity / BloatPacketSize) as i64; - // This will cause the RTT to be large for a while. - s.TriggerPacketBurst({send_net}, BloatPacketCount, BloatPacketSize.bytes()); - // Wait to allow the high RTT to be detected and acted upon. - s.RunFor(TimeDelta::Seconds(6)); - // By now the target rate should have dropped to the minimum configured rate. - assert_relative_eq!(client.target_rate().kbps(), BandwidthFloor.kbps(), 5); - } - */ - - /* - #[test] - fn UpdatesTargetRateBasedOnLinkCapacity() { - UpdatesTargetRateBasedOnLinkCapacity(); - } - */ - - /* - #[test] - fn StableEstimateDoesNotVaryInSteadyState() { - let factory = CreateFeedbackOnlyFactory(); - let s = Scenario::new("googcc_unit/stable_target", false); - let mut config = CallClientConfig::default(); - config.transport.cc_factory = &factory; - let mut net_conf = NetworkSimulationConfig::default(); - net_conf.bandwidth = DataRate::KilobitsPerSec(500); - net_conf.delay = TimeDelta::Millis(100); - let send_net = s.CreateSimulationNode(net_conf); - let ret_net = s.CreateSimulationNode(net_conf); - - let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net}); - // Run for a while to allow the estimate to stabilize. - s.RunFor(TimeDelta::Seconds(30)); - let min_stable_target: DataRate = DataRate::plus_infinity(); - let max_stable_target: DataRate = DataRate::minus_infinity(); - let min_target: DataRate = DataRate::plus_infinity(); - let max_target: DataRate = DataRate::minus_infinity(); - - // Measure variation in steady state. - for i in 0..20 { - let stable_target_rate = client.stable_target_rate(); - let target_rate = client.target_rate(); - assert!(stable_target_rate <= target_rate); - - min_stable_target = std::cmp::min(min_stable_target, stable_target_rate); - max_stable_target = std::cmp::max(max_stable_target, stable_target_rate); - min_target = std::cmp::min(min_target, target_rate); - max_target = std::cmp::max(max_target, target_rate); - s.RunFor(TimeDelta::Seconds(1)); - } - // We should expect drops by at least 15% (default backoff.) - assert!(min_target / max_target < 0.85); - // We should expect the stable target to be more stable than the immediate one - assert!(min_stable_target / max_stable_target >= min_target / max_target); - } - */ - - /* - #[test] - fn LossBasedControlUpdatesTargetRateBasedOnLinkCapacity() { - ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/"); - // TODO(srte): Should the behavior be unaffected at low loss rates? - UpdatesTargetRateBasedOnLinkCapacity("_loss_based"); - } - */ - - /* - #[test] - fn LossBasedControlDoesModestBackoffToHighLoss() { - ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/"); - let s = Scenario::new("googcc_unit/high_loss_channel", false); - let mut config = CallClientConfig::default(); - config.transport.rates.min_rate = DataRate::KilobitsPerSec(10); - config.transport.rates.max_rate = DataRate::KilobitsPerSec(1500); - config.transport.rates.start_rate = DataRate::KilobitsPerSec(300); - let send_net = s.CreateSimulationNode(|c: &mut NetworkSimulationConfig| { - c.bandwidth = DataRate::KilobitsPerSec(2000); - c.delay = TimeDelta::Millis(200); - c.loss_rate = 0.1; - }); - let ret_net = s.CreateSimulationNode( - |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(200); }); - - let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net}); - - s.RunFor(TimeDelta::Seconds(120)); - // Without LossBasedControl trial, bandwidth drops to ~10 kbps. - EXPECT_GT(client.target_rate().kbps(), 100); - } - */ - - /* - fn AverageBitrateAfterCrossInducedLoss(absl::string_view name) -> DataRate { - Scenario s(name, false); - let mut net_conf = NetworkSimulationConfig::default(); - net_conf.bandwidth = DataRate::KilobitsPerSec(1000); - net_conf.delay = TimeDelta::Millis(100); - // Short queue length means that we'll induce loss when sudden TCP traffic - // spikes are induced. This corresponds to ca 200 ms for a packet size of 1000 - // bytes. Such limited buffers are common on for instance wifi routers. - net_conf.packet_queue_length_limit = 25; - - let send_net = {s.CreateSimulationNode(net_conf)}; - let ret_net = {s.CreateSimulationNode(net_conf)}; - - let client = s.CreateClient("send", CallClientConfig()); - let callee = s.CreateClient("return", CallClientConfig()); - let route = s.CreateRoutes(client, send_net, callee, ret_net); - // TODO(srte): Make this work with RTX enabled or remove it. - let video = s.CreateVideoStream(route.forward(), [](VideoStreamConfig* c) { - c.stream.use_rtx = false; - }); - s.RunFor(TimeDelta::Seconds(10)); - for i in 0..4 { - // Sends TCP cross traffic inducing loss. - let tcp_traffic = s.net().StartCrossTraffic(CreateFakeTcpCrossTraffic( - s.net().CreateRoute(send_net), s.net().CreateRoute(ret_net), - FakeTcpConfig())); - s.RunFor(TimeDelta::Seconds(2)); - // Allow the ccongestion controller to recover. - s.net().StopCrossTraffic(tcp_traffic); - s.RunFor(TimeDelta::Seconds(20)); - } - - // Querying the video stats from within the expected runtime environment - // (i.e. the TQ that belongs to the CallClient, not the Scenario TQ that - // we're currently on). - VideoReceiveStreamInterface::Stats video_receive_stats; - let video_stream = video.receive(); - callee.SendTask([&video_stream, &video_receive_stats]() { - video_receive_stats = video_stream.GetStats(); - }); - return DataSize::Bytes( - video_receive_stats.rtp_stats.packet_counter.TotalBytes()) / - s.TimeSinceStart(); - } - */ - - /* - #[test] - fn MaintainsLowRateInSafeResetTrial() { - const LinkCapacity: DataRate = DataRate::KilobitsPerSec(200); - const StartRate: DataRate = DataRate::KilobitsPerSec(300); - - ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled/"); - Scenario s("googcc_unit/safe_reset_low"); - let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = LinkCapacity; - c.delay = TimeDelta::Millis(10); - }); - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = StartRate; - }); - let route = s.CreateRoutes( - client, {send_net}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - // Allow the controller to stabilize. - s.RunFor(TimeDelta::Millis(500)); - assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 50); - s.ChangeRoute(route.forward(), {send_net}); - // Allow new settings to propagate. - s.RunFor(TimeDelta::Millis(100)); - // Under the trial, the target should be unchanged for low rates. - assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 50); - } - */ - - /* - #[test] - fn DoNotResetBweUnlessNetworkAdapterChangeOnRoutChange() { - ScopedFieldTrials trial("WebRTC-Bwe-ResetOnAdapterIdChange/Enabled/"); - Scenario s("googcc_unit/do_not_reset_bwe_unless_adapter_change"); - - const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); - const StartRate: DataRate = DataRate::KilobitsPerSec(300); - - let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = LinkCapacity; - c.delay = TimeDelta::Millis(50); - }); - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = StartRate; - }); - client.UpdateNetworkAdapterId(0); - let route = s.CreateRoutes( - client, {send_net}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - // Allow the controller to stabilize. - s.RunFor(TimeDelta::Millis(500)); - assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300); - s.ChangeRoute(route.forward(), {send_net}); - // Allow new settings to propagate. - s.RunFor(TimeDelta::Millis(50)); - // Under the trial, the target should not drop. - assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300); - - s.RunFor(TimeDelta::Millis(500)); - // But if adapter id change, BWE should reset and start from the beginning if - // the network route changes. - client.UpdateNetworkAdapterId(1); - s.ChangeRoute(route.forward(), {send_net}); - // Allow new settings to propagate. - s.RunFor(TimeDelta::Millis(50)); - assert_relative_eq!(client.send_bandwidth().kbps(), StartRate.kbps(), 30); - } - */ - - /* - #[test] - fn CutsHighRateInSafeResetTrial() { - const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); - const StartRate: DataRate = DataRate::KilobitsPerSec(300); - - ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled/"); - Scenario s("googcc_unit/safe_reset_high_cut"); - let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = LinkCapacity; - c.delay = TimeDelta::Millis(50); - }); - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = StartRate; - }); - let route = s.CreateRoutes( - client, {send_net}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - // Allow the controller to stabilize. - s.RunFor(TimeDelta::Millis(500)); - assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300); - client.UpdateNetworkAdapterId(1); - s.ChangeRoute(route.forward(), {send_net}); - // Allow new settings to propagate. - s.RunFor(TimeDelta::Millis(50)); - // Under the trial, the target should be reset from high values. - assert_relative_eq!(client.send_bandwidth().kbps(), StartRate.kbps(), 30); - } - */ - - /* - #[test] - fn DetectsHighRateInSafeResetTrial() { - ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled,ack/"); - const InitialLinkCapacity: DataRate = DataRate::KilobitsPerSec(200); - const NewLinkCapacity: DataRate = DataRate::KilobitsPerSec(800); - const StartRate: DataRate = DataRate::KilobitsPerSec(300); - - Scenario s("googcc_unit/safe_reset_high_detect"); - let initial_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = InitialLinkCapacity; - c.delay = TimeDelta::Millis(50); - }); - let new_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = NewLinkCapacity; - c.delay = TimeDelta::Millis(50); - }); - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = StartRate; - }); - let route = s.CreateRoutes( - client, {initial_net}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - // Allow the controller to stabilize. - s.RunFor(TimeDelta::Millis(2000)); - assert_relative_eq!(client.send_bandwidth().kbps(), InitialLinkCapacity.kbps(), 50); - client.UpdateNetworkAdapterId(1); - s.ChangeRoute(route.forward(), {new_net}); - // Allow new settings to propagate, but not probes to be received. - s.RunFor(TimeDelta::Millis(50)); - // Under the field trial, the target rate should be unchanged since it's lower - // than the starting rate. - assert_relative_eq!(client.send_bandwidth().kbps(), InitialLinkCapacity.kbps(), 50); - // However, probing should have made us detect the higher rate. - // NOTE: This test causes high loss rate, and the loss-based estimator reduces - // the bitrate, making the test fail if we wait longer than one second here. - s.RunFor(TimeDelta::Millis(1000)); - EXPECT_GT(client.send_bandwidth().kbps(), NewLinkCapacity.kbps() - 300); - } - */ - - /* - #[test] - fn TargetRateReducedOnPacingBufferBuildupInTrial() { - // Configure strict pacing to ensure build-up. - ScopedFieldTrials trial( - "WebRTC-CongestionWindow/QueueSize:100,MinBitrate:30000/" - "WebRTC-Video-Pacing/factor:1.0/" - "WebRTC-AddPacingToCongestionWindowPushback/Enabled/"); - - const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); - const StartRate: DataRate = DataRate::KilobitsPerSec(1000); - - Scenario s("googcc_unit/pacing_buffer_buildup"); - let net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = LinkCapacity; - c.delay = TimeDelta::Millis(50); - }); - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = StartRate; - }); - let route = s.CreateRoutes( - client, {net}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - // Allow some time for the buffer to build up. - s.RunFor(TimeDelta::Seconds(5)); - - // Without trial, pacer delay reaches ~250 ms. - EXPECT_LT(client.GetStats().pacer_delay_ms, 150); - } - */ - - /* - #[test] - fn NoBandwidthTogglingInLossControlTrial() { - ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/"); - Scenario s("googcc_unit/no_toggling"); - let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = DataRate::KilobitsPerSec(2000); - c.loss_rate = 0.2; - c.delay = TimeDelta::Millis(10); - }); - - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = DataRate::KilobitsPerSec(300); - }); - let route = s.CreateRoutes( - client, {send_net}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - // Allow the controller to initialize. - s.RunFor(TimeDelta::Millis(250)); - - std::queue bandwidth_history; - const step: TimeDelta = TimeDelta::Millis(50); - for (TimeDelta time = TimeDelta::zero(); time < TimeDelta::Millis(2000); - time += step) { - s.RunFor(step); - const window: TimeDelta = TimeDelta::Millis(500); - if (bandwidth_history.len() >= window / step) - bandwidth_history.pop(); - bandwidth_history.push(client.send_bandwidth()); - EXPECT_LT( - CountBandwidthDips(bandwidth_history, DataRate::KilobitsPerSec(100)), - 2); - } - } - - #[test] - fn NoRttBackoffCollapseWhenVideoStops() { - ScopedFieldTrials trial("WebRTC-Bwe-MaxRttLimit/limit:2s/"); - Scenario s("googcc_unit/rttbackoff_video_stop"); - let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = DataRate::KilobitsPerSec(2000); - c.delay = TimeDelta::Millis(100); - }); - - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); - }); - let route = s.CreateRoutes( - client, {send_net}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - let video = s.CreateVideoStream(route.forward(), VideoStreamConfig()); - // Allow the controller to initialize, then stop video. - s.RunFor(TimeDelta::Seconds(1)); - video.send().Stop(); - s.RunFor(TimeDelta::Seconds(4)); - EXPECT_GT(client.send_bandwidth().kbps(), 1000); - } - - #[test] - fn NoCrashOnVeryLateFeedback() { - Scenario s; - let ret_net = s.CreateMutableSimulationNode(NetworkSimulationConfig()); - let route = s.CreateRoutes( - s.CreateClient("send", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}, - s.CreateClient("return", CallClientConfig()), {ret_net.node()}); - let video = s.CreateVideoStream(route.forward(), VideoStreamConfig()); - s.RunFor(TimeDelta::Seconds(5)); - // Delay feedback by several minutes. This will cause removal of the send time - // history for the packets as long as SendTimeHistoryWindow is configured for - // a shorter time span. - ret_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(300)); - // Stopping video stream while waiting to save test execution time. - video.send().Stop(); - s.RunFor(TimeDelta::Seconds(299)); - // Starting to cause addition of new packet to history, which cause old - // packets to be removed. - video.send().Start(); - // Runs until the lost packets are received. We expect that this will run - // without causing any runtime failures. - s.RunFor(TimeDelta::Seconds(2)); - } - - #[test] - fn IsFairToTCP() { - Scenario s("googcc_unit/tcp_fairness"); - let mut net_conf = NetworkSimulationConfig::default(); - net_conf.bandwidth = DataRate::KilobitsPerSec(1000); - net_conf.delay = TimeDelta::Millis(50); - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000); - }); - let send_net = {s.CreateSimulationNode(net_conf)}; - let ret_net = {s.CreateSimulationNode(net_conf)}; - let route = s.CreateRoutes( - client, send_net, s.CreateClient("return", CallClientConfig()), ret_net); - s.CreateVideoStream(route.forward(), VideoStreamConfig()); - s.net().StartCrossTraffic(CreateFakeTcpCrossTraffic( - s.net().CreateRoute(send_net), s.net().CreateRoute(ret_net), - FakeTcpConfig())); - s.RunFor(TimeDelta::Seconds(10)); - - // Currently only testing for the upper limit as we in practice back out - // quite a lot in this scenario. If this behavior is fixed, we should add a - // lower bound to ensure it stays fixed. - EXPECT_LT(client.send_bandwidth().kbps(), 750); - } - - #[test] - fn FastRampupOnRembCapLifted() { - let final_estimate: DataRate = - RunRembDipScenario("googcc_unit/default_fast_rampup_on_remb_cap_lifted"); - EXPECT_GT(final_estimate.kbps(), 1500); - } - - #[test] - fn FallbackToLossBasedBweWithoutPacketFeedback() { - const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000); - const StartRate: DataRate = DataRate::KilobitsPerSec(1000); - - let s = Scenario::new("googcc_unit/high_loss_channel", false); - let net = s.CreateMutableSimulationNode([&](NetworkSimulationConfig* c) { - c.bandwidth = LinkCapacity; - c.delay = TimeDelta::Millis(100); - }); - let client = s.CreateClient("send", [&](CallClientConfig* c) { - c.transport.rates.start_rate = StartRate; - }); - let route = s.CreateRoutes( - client, {net.node()}, s.CreateClient("return", CallClientConfig()), - {s.CreateSimulationNode(NetworkSimulationConfig())}); - - // Create a config without packet feedback. - VideoStreamConfig video_config; - video_config.stream.packet_feedback = false; - s.CreateVideoStream(route.forward(), video_config); - - s.RunFor(TimeDelta::Seconds(20)); - // Bandwith does not backoff because network is normal. - EXPECT_GE(client.target_rate().kbps(), 500); - - // Update the network to create high loss ratio - net.UpdateConfig(|c: &mut NetworkSimulationConfig| { c.loss_rate = 0.15; }); - s.RunFor(TimeDelta::Seconds(20)); - - // Bandwidth decreases thanks to loss based bwe v0. - EXPECT_LE(client.target_rate().kbps(), 300); - } - */ - - #[test] - fn calculates_rtt_from_transport_feedback() { - let mut controller = create_controller(FieldTrials::default(), true); - let mut current_time: Timestamp = Timestamp::from_millis(123); - let one_way_delay: TimeDelta = TimeDelta::from_millis(10); - let mut rtt: Option = None; - - 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); - current_time += TimeDelta::from_millis(50); - let update = controller.on_process_interval(ProcessInterval { - at_time: current_time, + // Send packet to initialize ALR detector + let mut sent_packet = SentPacket { + send_time: current_time, + size: DataSize::from_bytes(1000), ..Default::default() - }); - if let Some(target_rate) = update.target_rate { - rtt = Some(target_rate.network_estimate.round_trip_time); + }; + 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!(rtt.is_some()); - assert_eq!(rtt.unwrap().ms(), 2 * one_way_delay.ms()); + 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 index 01ce1c6d929..8f05e23da92 100644 --- a/rs/moq-congestion/src/gcc/inter_arrival_delta.rs +++ b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs @@ -41,6 +41,7 @@ impl SendTimeGroup { // 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, @@ -116,7 +117,11 @@ impl InterArrivalDelta { // 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()); + 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; diff --git a/rs/moq-congestion/src/gcc/link_capacity_estimator.rs b/rs/moq-congestion/src/gcc/link_capacity_estimator.rs index 0b94aa59545..73547343c04 100644 --- a/rs/moq-congestion/src/gcc/link_capacity_estimator.rs +++ b/rs/moq-congestion/src/gcc/link_capacity_estimator.rs @@ -10,6 +10,7 @@ use crate::gcc::api::units::DataRate; +#[derive(Clone)] pub struct LinkCapacityEstimator { estimate_kbps: Option, deviation_kbps: f64, diff --git a/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs b/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs deleted file mode 100644 index 02b99d90766..00000000000 --- a/rs/moq-congestion/src/gcc/loss_based_bandwidth_estimation.rs +++ /dev/null @@ -1,291 +0,0 @@ -/* - * 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::PacketResult, - units::{DataRate, TimeDelta, Timestamp}, -}; - -#[derive(Clone, Debug)] -pub struct LossBasedControlConfig { - pub enabled: bool, // Enabled - pub min_increase_factor: f64, // min_incr - pub max_increase_factor: f64, // max_incr - pub increase_low_rtt: TimeDelta, // incr_low_rtt - pub increase_high_rtt: TimeDelta, // incr_high_rtt - pub decrease_factor: f64, // decr - pub loss_window: TimeDelta, // loss_win - pub loss_max_window: TimeDelta, // loss_max_win - pub acknowledged_rate_max_window: TimeDelta, // ackrate_max_win - pub increase_offset: DataRate, // incr_offset - pub loss_bandwidth_balance_increase: DataRate, // balance_incr - pub loss_bandwidth_balance_decrease: DataRate, // balance_decr - pub loss_bandwidth_balance_reset: DataRate, // balance_reset - pub loss_bandwidth_balance_exponent: f64, // exponent - pub allow_resets: bool, // resets - pub decrease_interval: TimeDelta, // decr_intvl - pub loss_report_timeout: TimeDelta, // timeout -} - -impl Default for LossBasedControlConfig { - fn default() -> Self { - Self { - enabled: false, - min_increase_factor: 1.02, - max_increase_factor: 1.08, - increase_low_rtt: TimeDelta::from_millis(200), - increase_high_rtt: TimeDelta::from_millis(800), - decrease_factor: 0.99, - loss_window: TimeDelta::from_millis(800), - loss_max_window: TimeDelta::from_millis(800), - acknowledged_rate_max_window: TimeDelta::from_millis(800), - increase_offset: DataRate::from_bits_per_sec(1000), - loss_bandwidth_balance_increase: DataRate::from_kilobits_per_sec_float(0.5), - loss_bandwidth_balance_decrease: DataRate::from_kilobits_per_sec(4), - loss_bandwidth_balance_reset: DataRate::from_kilobits_per_sec_float(0.1), - loss_bandwidth_balance_exponent: 0.5, - allow_resets: false, - decrease_interval: TimeDelta::from_millis(300), - loss_report_timeout: TimeDelta::from_millis(6000), - } - } -} - -// Estimates an upper BWE limit based on loss. -// It requires knowledge about lost packets and acknowledged bitrate. -// Ie, this class require transport feedback. -pub struct LossBasedBandwidthEstimation { - config: LossBasedControlConfig, - average_loss: f64, - average_loss_max: f64, - loss_based_bitrate: DataRate, - acknowledged_bitrate_max: DataRate, - acknowledged_bitrate_last_update: Timestamp, - time_last_decrease: Timestamp, - has_decreased_since_last_loss_report: bool, - last_loss_packet_report: Timestamp, - last_loss_ratio: f64, -} - -impl LossBasedBandwidthEstimation { - // Expecting RTCP feedback to be sent with roughly 1s intervals, a 5s gap - // indicates a channel outage. - const MAX_RTCP_FEEDBACK_INTERVAL: TimeDelta = TimeDelta::from_millis(5000); - - pub fn new(config: LossBasedControlConfig) -> Self { - Self { - config, - average_loss: 0.0, - average_loss_max: 0.0, - loss_based_bitrate: DataRate::zero(), - acknowledged_bitrate_max: DataRate::zero(), - acknowledged_bitrate_last_update: Timestamp::minus_infinity(), - time_last_decrease: Timestamp::minus_infinity(), - has_decreased_since_last_loss_report: false, - last_loss_packet_report: Timestamp::minus_infinity(), - last_loss_ratio: 0.0, - } - } - - // Returns the new estimate. - pub fn update( - &mut self, - at_time: Timestamp, - min_bitrate: DataRate, - wanted_bitrate: DataRate, - last_round_trip_time: TimeDelta, - ) -> DataRate { - if self.loss_based_bitrate.is_zero() { - self.loss_based_bitrate = wanted_bitrate; - } - - // Only increase if loss has been low for some time. - let loss_estimate_for_increase: f64 = self.average_loss_max; - // Avoid multiple decreases from averaging over one loss spike. - let loss_estimate_for_decrease: f64 = self.average_loss.min(self.last_loss_ratio); - let allow_decrease: bool = !self.has_decreased_since_last_loss_report - && (at_time - self.time_last_decrease - >= last_round_trip_time + self.config.decrease_interval); - // If packet lost reports are too old, dont increase bitrate. - let loss_report_valid: bool = - at_time - self.last_loss_packet_report < 1.2 * Self::MAX_RTCP_FEEDBACK_INTERVAL; - - if loss_report_valid - && self.config.allow_resets - && loss_estimate_for_increase < self.loss_reset_threshold() - { - self.loss_based_bitrate = wanted_bitrate; - } else if loss_report_valid && loss_estimate_for_increase < self.loss_increase_threshold() { - // Increase bitrate by RTT-adaptive ratio. - let new_increased_bitrate: DataRate = min_bitrate - * get_increase_factor(&self.config, last_round_trip_time) - + self.config.increase_offset; - // The bitrate that would make the loss "just high enough". - let new_increased_bitrate_cap: DataRate = bitrate_from_loss( - loss_estimate_for_increase, - self.config.loss_bandwidth_balance_increase, - self.config.loss_bandwidth_balance_exponent, - ); - let new_increased_bitrate = - std::cmp::min(new_increased_bitrate, new_increased_bitrate_cap); - self.loss_based_bitrate = std::cmp::max(new_increased_bitrate, self.loss_based_bitrate); - } else if loss_estimate_for_decrease > self.loss_decrease_threshold() && allow_decrease { - // The bitrate that would make the loss "just acceptable". - let new_decreased_bitrate_floor: DataRate = bitrate_from_loss( - loss_estimate_for_decrease, - self.config.loss_bandwidth_balance_decrease, - self.config.loss_bandwidth_balance_exponent, - ); - let new_decreased_bitrate: DataRate = - std::cmp::max(self.decreased_bitrate(), new_decreased_bitrate_floor); - if new_decreased_bitrate < self.loss_based_bitrate { - self.time_last_decrease = at_time; - self.has_decreased_since_last_loss_report = true; - self.loss_based_bitrate = new_decreased_bitrate; - } - } - self.loss_based_bitrate - } - pub fn update_acknowledged_bitrate( - &mut self, - acknowledged_bitrate: DataRate, - at_time: Timestamp, - ) { - let time_passed: TimeDelta = if self.acknowledged_bitrate_last_update.is_finite() { - at_time - self.acknowledged_bitrate_last_update - } else { - TimeDelta::from_seconds(1) - }; - self.acknowledged_bitrate_last_update = at_time; - if acknowledged_bitrate > self.acknowledged_bitrate_max { - self.acknowledged_bitrate_max = acknowledged_bitrate; - } else { - self.acknowledged_bitrate_max -= - exponential_update(self.config.acknowledged_rate_max_window, time_passed) - * (self.acknowledged_bitrate_max - acknowledged_bitrate); - } - } - pub fn initialize(&mut self, bitrate: DataRate) { - self.loss_based_bitrate = bitrate; - self.average_loss = 0.0; - self.average_loss_max = 0.0; - } - pub fn enabled(&self) -> bool { - self.config.enabled - } - // Returns true if LossBasedBandwidthEstimation is enabled and have - // received loss statistics. Ie, this class require transport feedback. - pub fn in_use(&self) -> bool { - self.enabled() && self.last_loss_packet_report.is_finite() - } - pub fn update_loss_statistics(&mut self, packet_results: &[PacketResult], at_time: Timestamp) { - if packet_results.is_empty() { - unreachable!(); - } - let mut loss_count: i64 = 0; - for pkt in packet_results { - loss_count += if !pkt.is_received() { 1 } else { 0 }; - } - self.last_loss_ratio = (loss_count) as f64 / packet_results.len() as f64; - let time_passed: TimeDelta = if self.last_loss_packet_report.is_finite() { - at_time - self.last_loss_packet_report - } else { - TimeDelta::from_seconds(1) - }; - self.last_loss_packet_report = at_time; - self.has_decreased_since_last_loss_report = false; - - self.average_loss += exponential_update(self.config.loss_window, time_passed) - * (self.last_loss_ratio - self.average_loss); - if self.average_loss > self.average_loss_max { - self.average_loss_max = self.average_loss; - } else { - self.average_loss_max += exponential_update(self.config.loss_max_window, time_passed) - * (self.average_loss - self.average_loss_max); - } - } - - /* unused - pub fn get_estimate(&self) -> DataRate { - self.loss_based_bitrate - } - */ - - fn loss_increase_threshold(&self) -> f64 { - loss_from_bitrate( - self.loss_based_bitrate, - self.config.loss_bandwidth_balance_increase, - self.config.loss_bandwidth_balance_exponent, - ) - } - fn loss_decrease_threshold(&self) -> f64 { - loss_from_bitrate( - self.loss_based_bitrate, - self.config.loss_bandwidth_balance_decrease, - self.config.loss_bandwidth_balance_exponent, - ) - } - fn loss_reset_threshold(&self) -> f64 { - loss_from_bitrate( - self.loss_based_bitrate, - self.config.loss_bandwidth_balance_reset, - self.config.loss_bandwidth_balance_exponent, - ) - } - - fn decreased_bitrate(&self) -> DataRate { - self.config.decrease_factor * self.acknowledged_bitrate_max - } -} - -// Increase slower when RTT is high. -fn get_increase_factor(config: &LossBasedControlConfig, mut rtt: TimeDelta) -> f64 { - // Clamp the RTT - if rtt < config.increase_low_rtt { - rtt = config.increase_low_rtt; - } else if rtt > config.increase_high_rtt { - rtt = config.increase_high_rtt; - } - let rtt_range = config.increase_high_rtt - config.increase_low_rtt; - if rtt_range <= TimeDelta::zero() { - unreachable!(); // Only on misconfiguration. - } - let rtt_offset = rtt - config.increase_low_rtt; - let relative_offset = (rtt_offset / rtt_range).clamp(0.0, 1.0); - let factor_range = config.max_increase_factor - config.min_increase_factor; - config.min_increase_factor + (1.0 - relative_offset) * factor_range -} - -fn loss_from_bitrate(bitrate: DataRate, loss_bandwidth_balance: DataRate, exponent: f64) -> f64 { - if loss_bandwidth_balance >= bitrate { - return 1.0; - } - (loss_bandwidth_balance / bitrate).powf(exponent) -} - -fn bitrate_from_loss(loss: f64, loss_bandwidth_balance: DataRate, exponent: f64) -> DataRate { - if exponent <= 0.0 { - unreachable!(); - } - if loss < 1e-5 { - return DataRate::infinity(); - } - loss_bandwidth_balance * loss.powf(-1.0 / exponent) -} - -fn exponential_update(window: TimeDelta, interval: TimeDelta) -> f64 { - // Use the convention that exponential window length (which is really - // infinite) is the time it takes to dampen to 1/e. - if window <= TimeDelta::zero() { - unreachable!(); - } - - 1.0 - (interval / window * -1.0).exp() -} diff --git a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs index 6f4d4426ae4..6b67bf4fb57 100644 --- a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs +++ b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs @@ -8,8 +8,6 @@ * be found in the AUTHORS file in the root of the source tree. */ -use std::collections::HashMap; - use crate::gcc::{ api::{ transport::PacketResult, @@ -103,7 +101,6 @@ pub struct LossBasedBweV2Config { pub use_byte_loss_rate: bool, pub padding_duration: TimeDelta, pub bound_best_candidate: bool, - pub pace_at_loss_based_estimate: bool, pub median_sending_rate_factor: f64, } @@ -162,7 +159,9 @@ impl LossBasedBweV2Config { && !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."); + 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; } @@ -208,7 +207,11 @@ impl LossBasedBweV2Config { 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); + 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 { @@ -326,19 +329,19 @@ impl LossBasedBweV2Config { impl Default for LossBasedBweV2Config { fn default() -> Self { Self { - enabled: true, // Enabled - bw_rampup_upper_bound_factor: 1000000.0, // BwRampupUpperBoundFactor - bw_rampup_upper_bound_in_hold_factor: 1000000.0, // BwRampupUpperBoundInHoldFactor + 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_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.15, // LossThresholdOfHighBandwidthPreference - bandwidth_preference_smoothing_factor: 0.002, // BandwidthPreferenceSmoothingFactor - inherent_loss_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(75), // InherentLossUpperBoundBwBalance + 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 @@ -347,10 +350,10 @@ impl Default for LossBasedBweV2Config { delay_based_candidate: true, // DelayBasedCandidate upper_bound_candidate_in_alr: false, // UpperBoundCandidateInAlr observation_duration_lower_bound: TimeDelta::from_millis(250), // ObservationDurationLowerBound - observation_window_size: 20, // ObservationWindowSize + 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(75), // InstantUpperBoundBwBalance + 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 @@ -358,14 +361,13 @@ impl Default for LossBasedBweV2Config { 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: false, // UseInStartPhase + use_in_start_phase: true, // UseInStartPhase min_num_observations: 3, // MinNumObservations - lower_bound_by_acked_rate_factor: 0.0, // LowerBoundByAckedRateFactor - hold_duration_factor: 0.0, // HoldDurationFactor - use_byte_loss_rate: false, // UseByteLossRate - padding_duration: TimeDelta::zero(), // PaddingDuration - bound_best_candidate: false, // BoundBest - pace_at_loss_based_estimate: false, // PaceAtLossBasedEstimate + 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 } } @@ -408,9 +410,13 @@ impl Observation { } } +// 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 lost_packets: HashMap, + pub num_lost_packets: usize, + pub lost_size: DataSize, pub size: DataSize, } @@ -418,12 +424,14 @@ impl Default for PartialObservation { fn default() -> Self { Self { num_packets: 0, - lost_packets: HashMap::new(), + num_lost_packets: 0, + lost_size: DataSize::zero(), size: DataSize::zero(), } } } +#[derive(Clone)] struct PaddingInfo { pub padding_rate: DataRate, pub padding_timestamp: Timestamp, @@ -438,6 +446,7 @@ impl Default for PaddingInfo { } } +#[derive(Clone)] struct HoldInfo { timestamp: Timestamp, duration: TimeDelta, @@ -454,6 +463,7 @@ impl Default for HoldInfo { } } +#[derive(Clone)] pub struct LossBasedBweV2 { acknowledged_bitrate: Option, config: LossBasedBweV2Config, @@ -508,7 +518,9 @@ impl LossBasedBweV2 { }; if !this.config.enabled { - tracing::debug!("The configuration does not specify that the estimator should be enabled, disabling it."); + tracing::debug!( + "The configuration does not specify that the estimator should be enabled, disabling it." + ); return this; } @@ -557,11 +569,6 @@ impl LossBasedBweV2 { self.is_ready() && self.config.use_in_start_phase } - // Returns true if loss based BWE can be used in the start phase. - pub fn use_in_start_phase(&self) -> bool { - 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() { @@ -664,7 +671,12 @@ impl LossBasedBweV2 { self.newtons_method_update(&mut candidate); let candidate_objective: f64 = self.get_objective(&candidate); - if candidate_objective > objective_max { + // 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; } @@ -761,11 +773,9 @@ impl LossBasedBweV2 { if self.config.bound_best_candidate && bounded_bandwidth_estimate < best_candidate.loss_limited_bandwidth { - tracing::debug!( - "Resetting loss based BWE to {:?} due to loss. Avg loss rate: {:?}", - bounded_bandwidth_estimate, - self.average_reported_loss_ratio - ); + // 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 { @@ -863,11 +873,6 @@ impl LossBasedBweV2 { self.recovering_after_loss_timestamp = self.last_send_time_most_recent_observation; } } - pub fn pace_at_loss_based_estimate(&self) -> bool { - self.config.pace_at_loss_based_estimate - && self.loss_based_result.state != LossBasedState::DelayBasedEstimate - } - // For unit testing only. #[cfg(test)] pub fn set_bandwidth_estimate(&mut self, bandwidth_estimate: DataRate) { @@ -970,6 +975,12 @@ impl LossBasedBweV2 { // 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) } @@ -1245,14 +1256,9 @@ impl LossBasedBweV2 { 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 - .lost_packets - .remove(&packet.sent_packet.sequence_number); - } else { - self.partial_observation - .lost_packets - .insert(packet.sent_packet.sequence_number, packet.sent_packet.size); + 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); @@ -1279,13 +1285,11 @@ impl LossBasedBweV2 { let mut observation = Observation::default(); observation.num_packets = self.partial_observation.num_packets; - observation.num_lost_packets = self.partial_observation.lost_packets.len(); + 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); - for packet_size in self.partial_observation.lost_packets.values() { - observation.lost_size += *packet_size; - } + 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] = @@ -1434,9 +1438,7 @@ mod test { } #[derive(Default)] - struct LossBasedBweV2Test { - transport_sequence_number: i64, - } + struct LossBasedBweV2Test {} impl LossBasedBweV2Test { fn create_packet_results_with_received_packets( @@ -1444,10 +1446,6 @@ mod test { first_packet_timestamp: Timestamp, ) -> Vec { let mut enough_feedback = vec![PacketResult::default(); 2]; - self.transport_sequence_number += 1; - enough_feedback[0].sent_packet.sequence_number = self.transport_sequence_number; - self.transport_sequence_number += 1; - enough_feedback[1].sent_packet.sequence_number = self.transport_sequence_number; 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; @@ -1467,8 +1465,6 @@ mod test { ) -> Vec { let mut enough_feedback = vec![PacketResult::default(); 10]; for (i, packet) in enough_feedback.iter_mut().enumerate() { - self.transport_sequence_number += 1; - packet.sent_packet.sequence_number = self.transport_sequence_number; packet.sent_packet.size = DataSize::from_bytes(PACKET_SIZE); packet.sent_packet.send_time = first_packet_timestamp + (i) as i64 * OBSERVATION_DURATION_LOWER_BOUND; @@ -1485,10 +1481,6 @@ mod test { first_packet_timestamp: Timestamp, ) -> Vec { let mut enough_feedback = vec![PacketResult::default(); 2]; - self.transport_sequence_number += 1; - enough_feedback[0].sent_packet.sequence_number = self.transport_sequence_number; - self.transport_sequence_number += 1; - enough_feedback[1].sent_packet.sequence_number = self.transport_sequence_number; 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; @@ -1508,11 +1500,8 @@ mod test { let mut enough_feedback = Vec::with_capacity(num_packets); for i in 0..(num_packets - 1) { - self.transport_sequence_number += 1; - enough_feedback.push(PacketResult { sent_packet: SentPacket { - sequence_number: self.transport_sequence_number, size: DataSize::from_bytes(PACKET_SIZE), send_time: first_packet_timestamp + TimeDelta::from_millis(i as i64 * 10), ..Default::default() @@ -1521,11 +1510,9 @@ mod test { ..Default::default() }); } - self.transport_sequence_number += 1; enough_feedback.push(PacketResult { sent_packet: SentPacket { - sequence_number: self.transport_sequence_number, size: DataSize::from_bytes(PACKET_SIZE), send_time: first_packet_timestamp + OBSERVATION_DURATION_LOWER_BOUND, ..Default::default() @@ -1643,10 +1630,12 @@ mod test { ); assert!(loss_based_bandwidth_estimator.is_ready()); - assert!(loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate - .is_finite()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_finite() + ); } #[test] @@ -1665,10 +1654,12 @@ mod test { ); assert!(!loss_based_bandwidth_estimator.is_ready()); - assert!(loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate - .is_plus_infinity()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_plus_infinity() + ); } #[test] @@ -1691,10 +1682,12 @@ mod test { 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()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_plus_infinity() + ); loss_based_bandwidth_estimator.update_bandwidth_estimate( ¬_enough_feedback, @@ -1703,10 +1696,12 @@ mod test { ); assert!(!loss_based_bandwidth_estimator.is_ready()); - assert!(loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate - .is_plus_infinity()); + assert!( + loss_based_bandwidth_estimator + .get_loss_based_result() + .bandwidth_estimate + .is_plus_infinity() + ); } #[test] @@ -2106,19 +2101,13 @@ mod test { } #[test] - fn increase_by_max_increase_factor_after_loss_based_bwe_backs_off() { + fn increase_by_hold_factor_after_loss_based_bwe_backs_off() { let mut test = LossBasedBweV2Test::default(); - let config = LossBasedBweV2Config { - candidate_factors: vec![1.2, 1.0, 0.5], - instant_upper_bound_bw_balance: DataRate::from_kilobits_per_sec(10000), - max_increase_factor: 1.5, - not_increase_if_inherent_loss_less_than_average_loss: false, - ..short_observation_config() - }; + 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(300); + 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); @@ -2136,24 +2125,28 @@ mod test { loss_based_bandwidth_estimator.get_loss_based_result(); // 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, + 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, ); - 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, - ); + 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.5 + result_at_loss.bandwidth_estimate * 1.2 ); } @@ -2161,7 +2154,7 @@ mod test { fn loss_based_state_is_delay_based_estimate_after_network_recovering() { let mut test = LossBasedBweV2Test::default(); let config = LossBasedBweV2Config { - candidate_factors: vec![1.2, 1.0, 0.5], + 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, @@ -2309,7 +2302,8 @@ mod test { loss_based_bandwidth_estimator.set_acknowledged_bitrate(estimate_1 * 0.9); let mut feedback_count: i64 = 1; - while feedback_count < 5 && result.state != LossBasedState::Increasing { + 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=*/ @@ -2321,7 +2315,7 @@ mod test { feedback_count += 1; result = loss_based_bandwidth_estimator.get_loss_based_result(); } - assert_eq!(result.state, LossBasedState::Increasing); + 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); @@ -2385,7 +2379,8 @@ mod test { loss_based_bandwidth_estimator.set_acknowledged_bitrate(estimate_1 / 2); let mut feedback_count: i64 = 1; - while feedback_count < 5 && result.state != LossBasedState::Increasing { + 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=*/ @@ -2398,7 +2393,7 @@ mod test { result = loss_based_bandwidth_estimator.get_loss_based_result(); } - assert_eq!(result.state, LossBasedState::Increasing); + assert_eq!(result.state, LossBasedState::IncreaseUsingPadding); // The estimate increases by 1kbps. assert_eq!( result.bandwidth_estimate, @@ -2889,7 +2884,7 @@ mod test { ); 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(500)); + .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( @@ -2954,7 +2949,7 @@ mod test { 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, + Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * 2, ); loss_based_bandwidth_estimator.update_bandwidth_estimate( &enough_feedback_50p_loss_2, @@ -2963,7 +2958,7 @@ mod test { ); // Verify that the instant lower bound increases the estimate, and state is - // updated to Increasing. + // updated to IncreaseUsingPadding. assert_eq!( loss_based_bandwidth_estimator .get_loss_based_result() @@ -2972,7 +2967,7 @@ mod test { ); assert_eq!( loss_based_bandwidth_estimator.get_loss_based_result().state, - LossBasedState::Increasing + LossBasedState::IncreaseUsingPadding ); } @@ -3063,7 +3058,8 @@ mod test { // Eventually the estimator recovers to delay based state. let mut feedback_count: i64 = 2; - while feedback_count < 5 && result.state != LossBasedState::DelayBasedEstimate { + 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( @@ -3107,14 +3103,21 @@ mod test { 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=*/ false, - ); + 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 @@ -3325,195 +3328,6 @@ mod test { ); } - #[test] - fn increase_estimate_if_not_hold() { - let mut test = LossBasedBweV2Test::default(); - let config = LossBasedBweV2Config { - hold_duration_factor: 0.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 - ); - 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=*/ DataRate::plus_infinity(), - /*in_alr=*/ false, - ); - assert_eq!( - loss_based_bandwidth_estimator.get_loss_based_result().state, - LossBasedState::Increasing - ); - assert!( - loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate - > estimate - ); - } - - #[test] - fn increase_estimate_after_hold_duration() { - let mut test = LossBasedBweV2Test::default(); - let config = LossBasedBweV2Config { - hold_duration_factor: 10.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 - ); - let estimate: DataRate = loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate; - - // During the hold duration, e.g. first 300ms, the estimate cannot increase. - 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=*/ 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, - estimate - ); - - // After the hold duration, the estimate can increase. - 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, - ); - assert_eq!( - loss_based_bandwidth_estimator.get_loss_based_result().state, - LossBasedState::Increasing - ); - assert!( - loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate - >= estimate - ); - - // Get another 50p packet loss. - 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 * 3, - ), - /*delay_based_estimate=*/ DataRate::plus_infinity(), - /*in_alr=*/ false, - ); - assert_eq!( - loss_based_bandwidth_estimator.get_loss_based_result().state, - LossBasedState::Decreasing - ); - let estimate_at_hold: DataRate = loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate; - - // In the hold duration, e.g. next 3s, the estimate cannot increase above the - // hold rate. Get some lost packets to get lower estimate than the HOLD rate. - for i in 4..=6 { - loss_based_bandwidth_estimator.update_bandwidth_estimate( - &test.create_packet_results_with_100p_loss_rate( - /*first_packet_timestamp=*/ - Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * i, - 2, - ), - /*delay_based_estimate=*/ DataRate::plus_infinity(), - /*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 - < estimate_at_hold - ); - } - - let mut feedback_id: i64 = 7; - while loss_based_bandwidth_estimator.get_loss_based_result().state - != LossBasedState::Increasing - { - 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, - ); - if loss_based_bandwidth_estimator.get_loss_based_result().state - == LossBasedState::Decreasing - { - // In the hold duration, the estimate can not go higher than estimate at - // hold. - assert!( - loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate - <= estimate_at_hold - ); - } else if loss_based_bandwidth_estimator.get_loss_based_result().state - == LossBasedState::Increasing - { - // After the hold duration, the estimate can increase again. - assert!( - loss_based_bandwidth_estimator - .get_loss_based_result() - .bandwidth_estimate - > estimate_at_hold - ); - } - feedback_id += 1; - } - } - #[test] fn hold_rate_not_lower_than_acked_rate() { let mut test = LossBasedBweV2Test::default(); @@ -3632,10 +3446,7 @@ mod test { #[test] fn end_hold_duration_if_delay_based_estimate_works() { let mut test = LossBasedBweV2Test::default(); - let config = LossBasedBweV2Config { - hold_duration_factor: 3.0, - ..short_observation_config() - }; + 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)); @@ -3662,6 +3473,14 @@ mod test { /*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 @@ -3701,7 +3520,7 @@ mod test { loss_based_bandwidth_estimator .get_loss_based_result() .bandwidth_estimate - < DataRate::from_kilobits_per_sec(150) + < DataRate::from_kilobits_per_sec(160) ); } @@ -3711,7 +3530,7 @@ mod test { let config = LossBasedBweV2Config { use_byte_loss_rate: true, observation_window_size: 5, - ..short_observation_config() + ..Default::default() }; let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); const DELAY_BASED_ESTIMATE: DataRate = DataRate::from_kilobits_per_sec(500); @@ -3784,7 +3603,7 @@ mod test { let config = LossBasedBweV2Config { use_byte_loss_rate: true, observation_window_size: 5, - ..short_observation_config() + ..Default::default() }; let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); const DELAY_BASED_ESTIMATE: DataRate = DataRate::from_kilobits_per_sec(500); @@ -3826,116 +3645,39 @@ mod test { } #[test] - fn pace_at_loss_based_estimate() { + fn select_higher_candidate_if_having_same_objective() { let mut test = LossBasedBweV2Test::default(); - let config = LossBasedBweV2Config { - pace_at_loss_based_estimate: true, - padding_duration: TimeDelta::from_millis(1000), - ..short_observation_config() - }; + 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(1000)); - loss_based_bandwidth_estimator.update_bandwidth_estimate( - &test.create_packet_results_with_received_packets( + 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(), - ), - /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(1000), - /*in_alr=*/ false, - ); - assert_eq!( - loss_based_bandwidth_estimator.get_loss_based_result().state, - LossBasedState::DelayBasedEstimate - ); - assert!(!loss_based_bandwidth_estimator.pace_at_loss_based_estimate()); - - loss_based_bandwidth_estimator.update_bandwidth_estimate( - &test.create_packet_results_with_100p_loss_rate( + ); + let enough_feedback_02: Vec = test + .create_packet_results_with_received_packets( /*first_packet_timestamp=*/ Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND, - 2, - ), - /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(1000), + ); + loss_based_bandwidth_estimator.update_bandwidth_estimate( + &enough_feedback_01, + START_BITRATE, /*in_alr=*/ false, ); - assert_eq!( - loss_based_bandwidth_estimator.get_loss_based_result().state, - LossBasedState::Decreasing - ); - assert!(loss_based_bandwidth_estimator.pace_at_loss_based_estimate()); - + 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( - &test.create_packet_results_with_received_packets( - /*first_packet_timestamp=*/ - Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND * 2, - ), - /*delay_based_estimate=*/ DataRate::from_kilobits_per_sec(1000), + &enough_feedback_02, + delay_based_estimate, /*in_alr=*/ false, ); - assert_eq!( - loss_based_bandwidth_estimator.get_loss_based_result().state, - LossBasedState::IncreaseUsingPadding - ); - assert!(loss_based_bandwidth_estimator.pace_at_loss_based_estimate()); - } - - #[test] - fn estimate_does_not_back_off_due_to_packet_reordering_between_feedback() { - let config = short_observation_config(); - let mut loss_based_bandwidth_estimator = LossBasedBweV2::new(config); - const START_BITRATE: DataRate = DataRate::from_kilobits_per_sec(2500); - loss_based_bandwidth_estimator.set_bandwidth_estimate(START_BITRATE); - let mut feedback_1 = vec![PacketResult::default(); 3]; - feedback_1[0].sent_packet.sequence_number = 1; - feedback_1[0].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); - feedback_1[0].sent_packet.send_time = Timestamp::zero(); - feedback_1[0].receive_time = - feedback_1[0].sent_packet.send_time + TimeDelta::from_millis(10); - feedback_1[1].sent_packet.sequence_number = 2; - feedback_1[1].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); - feedback_1[1].sent_packet.send_time = Timestamp::zero(); - // Lost or reordered - feedback_1[1].receive_time = Timestamp::plus_infinity(); - - feedback_1[2].sent_packet.sequence_number = 3; - feedback_1[2].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); - feedback_1[2].sent_packet.send_time = Timestamp::zero(); - feedback_1[2].receive_time = - feedback_1[2].sent_packet.send_time + TimeDelta::from_millis(10); - - let mut feedback_2 = vec![PacketResult::default(); 3]; - feedback_2[0].sent_packet.sequence_number = 2; - feedback_2[0].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); - feedback_2[0].sent_packet.send_time = Timestamp::zero(); - feedback_2[0].receive_time = - feedback_1[0].sent_packet.send_time + TimeDelta::from_millis(10); - feedback_2[1].sent_packet.sequence_number = 4; - feedback_2[1].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); - feedback_2[1].sent_packet.send_time = Timestamp::zero() + OBSERVATION_DURATION_LOWER_BOUND; - feedback_2[1].receive_time = - feedback_2[1].sent_packet.send_time + TimeDelta::from_millis(10); - feedback_2[2].sent_packet.sequence_number = 5; - feedback_2[2].sent_packet.size = DataSize::from_bytes(PACKET_SIZE); - feedback_2[2].sent_packet.send_time = Timestamp::zero(); - feedback_2[2].receive_time = - feedback_2[2].sent_packet.send_time + TimeDelta::from_millis(10); - - loss_based_bandwidth_estimator.update_bandwidth_estimate( - &feedback_1, - /*delay_based_estimate=*/ START_BITRATE, - /*in_alr=*/ false, - ); - loss_based_bandwidth_estimator.update_bandwidth_estimate( - &feedback_2, - /*delay_based_estimate=*/ START_BITRATE, - /*in_alr=*/ false, - ); assert_eq!( loss_based_bandwidth_estimator .get_loss_based_result() .bandwidth_estimate, - START_BITRATE + delay_based_estimate ); } } // namespace diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 2569c3b8827..1c17bae51ec 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -16,25 +16,17 @@ macro_rules! assert_relative_eq { } mod api; - -/// [FieldTrials](experiments::FieldTrials) and dynamic configuration. -pub mod experiments; - -// We inline goog_cc modules to avoid goog_cc::goog_cc logging prefix. +mod experiments; mod pacing; - mod remote_bitrate_estimator; -mod acknowledged_bitrate_estimator; -mod acknowledged_bitrate_estimator_interface; + mod alr_detector; -mod bitrate_estimator; 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_bandwidth_estimation; mod loss_based_bwe_v2; mod probe_bitrate_estimator; mod probe_controller; @@ -42,16 +34,12 @@ mod robust_throughput_estimator; mod send_side_bandwidth_estimation; mod trendline_estimator; -use acknowledged_bitrate_estimator::*; -use acknowledged_bitrate_estimator_interface::*; use alr_detector::*; -use bitrate_estimator::*; 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_bandwidth_estimation::*; use loss_based_bwe_v2::*; use probe_bitrate_estimator::*; use probe_controller::*; @@ -59,16 +47,399 @@ use robust_throughput_estimator::*; use send_side_bandwidth_estimation::*; use trendline_estimator::*; -// Selectively export the public API. -pub use api::{network_control, transport, units}; -pub use goog_cc_network_control::{GoogCcConfig, GoogCcNetworkController}; -pub use { - alr_detector::AlrDetectorConfig, bitrate_estimator::BitrateEstimatorConfig, - send_side_bandwidth_estimation::BweLossExperiment, - delay_based_bwe::BweSeparateAudioPacketsSettings, - loss_based_bwe_v2::LossBasedBweV2Config, - loss_based_bandwidth_estimation::LossBasedControlConfig, - probe_controller::ProbeControllerConfig, - robust_throughput_estimator::RobustThroughputEstimatorSettings, - send_side_bandwidth_estimation::RttBasedBackoffConfig, goog_cc_network_control::SafeResetOnRouteChange, trendline_estimator::TrendlineEstimatorSettings, +use goog_cc_network_control::{GoogCcNetworkController, NetworkControllerConfig}; + +use std::{any::Any, collections::VecDeque, num::NonZeroU16, sync::Arc, time::Duration}; + +use kio::clock::Instant; + +use crate::{ + Ack, Ce, Controller, ControllerFactory, ControllerMetrics, Init, Loss, Packet, Sent, + pacer::{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); + +/// Configuration for the [`Gcc`] congestion controller. +#[derive(Debug, Clone)] +pub struct GccConfig { + initial_rate: u64, +} + +impl GccConfig { + /// The rate GoogCC starts at and first probes from, in bytes per second. + /// + /// Defaults to WebRTC's 300 kbit/s. + pub fn initial_rate(&mut self, bytes_per_second: u64) -> &mut Self { + self.initial_rate = bytes_per_second; + self + } +} + +impl Default for GccConfig { + fn default() -> Self { + Self { + initial_rate: 300_000 / 8, + } + } +} + +impl ControllerFactory for GccConfig { + fn build(self: Arc, init: Init) -> Box { + Box::new(Gcc::new(&self, 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 +/// [`ControllerMetrics::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, + 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, + /// Reused for each ACK's and loss's feedback + feedback: TransportPacketsFeedback, +} + +impl Gcc { + fn new(config: &GccConfig, init: Init) -> Self { + let now = timestamp(init.now); + let starting_rate = DataRate::from_bytes_per_sec(config.initial_rate.max(1) as i64); + let cc = GoogCcNetworkController::new(NetworkControllerConfig { + constraints: TargetRateConstraints { + at_time: now, + min_data_rate: None, + max_data_rate: init + .max_pacing_rate + .map(|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: config.initial_rate.max(1), + target_rate: config.initial_rate, + initial_window, + in_flight: 0, + next_process: init.now, + receive_basis: None, + clusters: VecDeque::new(), + feedback: TransportPacketsFeedback::default(), + }; + let update = gcc.cc.on_network_availability(NetworkAvailability { + at_time: now, + network_available: true, + }); + gcc.apply(update); + gcc.process(init.now); + gcc + } + + /// Runs GoogCC's process timer if it is due. + fn process(&mut self, now: Instant) { + 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); + let received = i64::try_from(received.as_micros()).unwrap_or(i64::MAX); + let basis = *self + .receive_basis + .get_or_insert_with(|| now.us().saturating_sub(received)); + let mapped = Timestamp::from_micros(received.saturating_add(basis)); + mapped.clamp(now - RECEIVE_SKEW, now + RECEIVE_SKEW) + } + + /// 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. + fn probe_inflight(&self) -> Option { + let (bytes, packets, _) = self.prober.remaining()?; + let packets = u64::try_from(packets).unwrap_or(0) * u64::from(self.mtu); + let bytes = u64::try_from(bytes.bytes()).unwrap_or(0); + 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()); + + 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 !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 { + // 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 is left out rather than mix the two clocks. + let receive_time = match packet.received { + Some(received) => self.receive_time(received, ack.now), + None if self.receive_basis.is_none() => timestamp(ack.now), + None => continue, + }; + 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, + }; + 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(), + }; + 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 window(&self) -> u64 { + self.congestion_window() + } + + fn pace(&mut self, now: Instant, bytes: u64) -> Option { + self.process(now); + let at = timestamp(now); + // A packet is ready, which starts a pending probe cluster + self.prober + .on_incoming_packet(DataSize::from_bytes(bytes.min(i64::MAX as u64) as i64)); + 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) + } + + fn metrics(&self) -> ControllerMetrics { + ControllerMetrics { + 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 + } + + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } + + fn into_any(self: Box) -> Box { + self + } +} + +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: GoogCC's, which counts up from one, wrapped to +/// sixteen bits without zero. +fn short_id(id: i32) -> Option { + let id = u32::try_from(id).ok().filter(|&id| id > 0)?; + NonZeroU16::new(((id - 1) % u32::from(u16::MAX) + 1) 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..4639a574d7e --- /dev/null +++ b/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs @@ -0,0 +1,368 @@ +/* + * 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, +} + +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), + } + } +} + +impl BitrateProber { + const PROBE_CLUSTER_TIMEOUT: TimeDelta = TimeDelta::from_seconds(5); + const MAX_PENDING_PROBE_CLUSTERS: usize = 5; + + // 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; + } + } + + 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 index 8b841b9a1dc..a00423d8a6e 100644 --- a/rs/moq-congestion/src/gcc/pacing/interval_budget.rs +++ b/rs/moq-congestion/src/gcc/pacing/interval_budget.rs @@ -1,3 +1,4 @@ +#[derive(Clone)] pub struct IntervalBudget { target_rate_kbps: i64, max_bytes_in_budget: i64, diff --git a/rs/moq-congestion/src/gcc/pacing/mod.rs b/rs/moq-congestion/src/gcc/pacing/mod.rs index 1e02c11a213..5606d7034da 100644 --- a/rs/moq-congestion/src/gcc/pacing/mod.rs +++ b/rs/moq-congestion/src/gcc/pacing/mod.rs @@ -1,2 +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 index 3243ed2fe98..dadcf21fec3 100644 --- a/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs +++ b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs @@ -15,6 +15,7 @@ use crate::gcc::api::{ units::{DataRate, DataSize, TimeDelta, Timestamp}, }; +#[derive(Clone)] struct AggregatedCluster { pub num_probes: i64, pub first_send: Timestamp, @@ -41,7 +42,7 @@ impl Default for AggregatedCluster { } } -#[derive(Default)] +#[derive(Default, Clone)] pub struct ProbeBitrateEstimator { clusters: HashMap, estimated_data_rate: Option, @@ -146,8 +147,12 @@ impl ProbeBitrateEstimator { || 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); + 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 @@ -166,13 +171,31 @@ impl ProbeBitrateEstimator { 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); + 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); + 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, @@ -467,10 +490,11 @@ mod test { #[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()); + assert!( + test.probe_bitrate_estimator + .fetch_and_reset_last_estimated_bitrate() + .is_none() + ); } #[test] @@ -490,9 +514,10 @@ mod test { 800000.0, epsilon = 10.0 ); - assert!(test - .probe_bitrate_estimator - .fetch_and_reset_last_estimated_bitrate() - .is_none()); + 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 index 48d7a697703..50d99836b36 100644 --- a/rs/moq-congestion/src/gcc/probe_controller.rs +++ b/rs/moq-congestion/src/gcc/probe_controller.rs @@ -9,7 +9,7 @@ */ use crate::gcc::api::{ - transport::{NetworkAvailability, NetworkStateEstimate, ProbeClusterConfig}, + transport::{NetworkAvailability, ProbeClusterConfig}, units::{DataRate, TimeDelta, Timestamp}, }; @@ -39,24 +39,10 @@ pub struct ProbeControllerConfig { // Configures how often we send ALR probes and how big they are. pub alr_interval: TimeDelta, pub alr_scale: f64, - // Configures how often we send probes if NetworkStateEstimate is available. - pub network_state_interval: TimeDelta, - // Periodically probe as long as the ratio between current estimate and - // NetworkStateEstimate is lower then this. - pub est_lower_than_network_ratio: f64, - pub est_lower_than_network_interval: TimeDelta, - pub network_state_scale: f64, - // Overrides min_probe_duration if network_state_estimate_probing_interval - // is set and a network state estimate is known and equal or higher than the - // probe target. - pub network_state_probe_duration: TimeDelta, - // Overrides min_probe_delta if network_state_estimate_probing_interval - // is set and a network state estimate is known and equal or higher than the - // probe target. - pub network_state_min_probe_delta: TimeDelta, // 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, @@ -89,13 +75,8 @@ impl Default for ProbeControllerConfig { initial_min_probe_delta: TimeDelta::from_millis(20), alr_interval: TimeDelta::from_seconds(5), alr_scale: 2.0, - network_state_interval: TimeDelta::plus_infinity(), - est_lower_than_network_ratio: 0.0, - est_lower_than_network_interval: TimeDelta::from_seconds(3), - network_state_scale: 1.0, - network_state_probe_duration: TimeDelta::from_millis(15), - network_state_min_probe_delta: TimeDelta::from_millis(20), probe_max_allocation: true, + probe_max_allocation_without_alr: true, alloc_p1: 1.0, alloc_p2: 2.0, alloc_current_bwe_limit: 2.0, @@ -121,6 +102,7 @@ pub enum BandwidthLimitedCause { RttBasedBackOffHighRtt = 4, } +#[derive(Clone)] enum State { // Initial state where no probing has been triggered yet. Init, @@ -133,6 +115,7 @@ enum State { // 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, @@ -142,7 +125,6 @@ pub struct ProbeController { min_bitrate_to_probe_further: DataRate, time_last_probing_initiated: Timestamp, estimated_bitrate: DataRate, - network_estimate: Option, start_bitrate: DataRate, max_bitrate: DataRate, last_bwe_drop_probing_time: Timestamp, @@ -167,7 +149,6 @@ impl Default for ProbeController { min_bitrate_to_probe_further: DataRate::plus_infinity(), time_last_probing_initiated: Timestamp::minus_infinity(), estimated_bitrate: DataRate::zero(), - network_estimate: None, start_bitrate: DataRate::zero(), max_bitrate: DataRate::plus_infinity(), last_bwe_drop_probing_time: Timestamp::zero(), @@ -278,10 +259,10 @@ impl ProbeController { at_time: Timestamp, ) -> Vec { let in_alr: bool = self.alr_start_time.is_some(); - let allow_allocation_probe: bool = in_alr; + 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 + && 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 @@ -366,25 +347,13 @@ impl ProbeController { // No need to continue probing. self.min_bitrate_to_probe_further = DataRate::plus_infinity(); } - let network_state_estimate_probe_further_limit: DataRate = match self.network_estimate { - Some(network_estimate) - if self.config.network_state_interval.is_finite() - && network_estimate.link_capacity_upper.is_finite() => - { - network_estimate.link_capacity_upper * self.config.further_probe_threshold - } - _ => DataRate::plus_infinity(), - }; tracing::debug!( - "Measured bitrate: {:?} Minimum to probe further: {:?} upper limit: {:?}", + "Measured bitrate: {:?} Minimum to probe further: {:?}", bitrate, self.min_bitrate_to_probe_further, - network_state_estimate_probe_further_limit ); - if bitrate > self.min_bitrate_to_probe_further - && bitrate <= network_state_estimate_probe_further_limit - { + if bitrate > self.min_bitrate_to_probe_further { return self.initiate_probing(at_time, &[self.config.step_size * bitrate], true); } } @@ -404,11 +373,11 @@ impl ProbeController { self.repeated_initial_probing_enabled = enable; } - pub fn set_alr_start_time_ms(&mut self, alr_start_time: Option) { - self.alr_start_time = alr_start_time.map(Timestamp::from_millis); + 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_ms(&mut self, alr_end_time: i64) { - self.alr_end_time = Some(Timestamp::from_millis(alr_end_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 { @@ -446,10 +415,6 @@ impl ProbeController { vec![] } - pub fn set_network_state_estimate(&mut self, estimate: NetworkStateEstimate) { - self.network_estimate = Some(estimate); - } - // 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 @@ -460,7 +425,6 @@ impl ProbeController { self.min_bitrate_to_probe_further = DataRate::plus_infinity(); self.time_last_probing_initiated = Timestamp::zero(); self.estimated_bitrate = DataRate::zero(); - self.network_estimate = None; self.start_bitrate = DataRate::zero(); self.max_bitrate = Self::DEFAULT_MAX_PROBING_BITRATE; let now: Timestamp = at_time; @@ -487,7 +451,7 @@ impl ProbeController { true, ); } - if self.time_for_alr_probe(at_time) || self.time_for_network_state_probe(at_time) { + if self.time_for_alr_probe(at_time) { return self.initiate_probing( at_time, &[self.estimated_bitrate * self.config.alr_scale], @@ -541,10 +505,6 @@ impl ProbeController { mut probe_further: bool, ) -> Vec { if self.config.skip_if_est_larger_than_fraction_of_max > 0.0 { - let network_estimate: DataRate = self - .network_estimate - .map(|x| x.link_capacity_upper) - .unwrap_or(DataRate::plus_infinity()); let max_probe_rate: DataRate = if self.max_total_allocated_bitrate.is_zero() { self.max_bitrate } else { @@ -553,7 +513,7 @@ impl ProbeController { self.max_bitrate, ) }; - if std::cmp::min(network_estimate, self.estimated_bitrate) + if self.estimated_bitrate > self.config.skip_if_est_larger_than_fraction_of_max * max_probe_rate { self.update_state(State::ProbingComplete); @@ -592,25 +552,6 @@ impl ProbeController { _ => (), }; - match self.network_estimate { - Some(network_estimate) if network_estimate.link_capacity_upper.is_finite() => { - if self.config.network_state_interval.is_finite() { - if network_estimate.link_capacity_upper.is_zero() { - tracing::debug!("Not sending probe, Network state estimate is zero"); - return vec![]; - } - max_probe_bitrate = std::cmp::min( - max_probe_bitrate, - std::cmp::max( - self.estimated_bitrate, - network_estimate.link_capacity_upper * self.config.network_state_scale, - ), - ); - } - } - _ => (), - }; - let mut pending_probes: Vec = vec![]; for mut bitrate in bitrates_to_probe.iter().cloned() { assert!(!bitrate.is_zero()); @@ -643,33 +584,6 @@ impl ProbeController { } false } - fn time_for_network_state_probe(&self, at_time: Timestamp) -> bool { - let network_estimate = match self.network_estimate.as_ref() { - Some(network_estimate) if !network_estimate.link_capacity_upper.is_infinite() => { - network_estimate - } - _ => return false, - }; - - let probe_due_to_low_estimate: bool = self.bandwidth_limited_cause - == BandwidthLimitedCause::DelayBasedLimited - && self.estimated_bitrate - < self.config.est_lower_than_network_ratio * network_estimate.link_capacity_upper; - if probe_due_to_low_estimate && self.config.est_lower_than_network_interval.is_finite() { - let next_probe_time: Timestamp = - self.time_last_probing_initiated + self.config.est_lower_than_network_interval; - return at_time >= next_probe_time; - } - - let periodic_probe: bool = self.estimated_bitrate < network_estimate.link_capacity_upper; - if periodic_probe && self.config.network_state_interval.is_finite() { - let next_probe_time: Timestamp = - self.time_last_probing_initiated + self.config.network_state_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 @@ -697,24 +611,13 @@ impl ProbeController { self.next_probe_cluster_id += 1; - match self.network_estimate { - Some(network_estimate) - if network_estimate.link_capacity_upper.is_finite() - && network_estimate.link_capacity_upper >= bitrate => - { - config.target_duration = self.config.network_state_probe_duration; - config.min_probe_delta = self.config.network_state_min_probe_delta; - } - _ => { - 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; - } - } - }; + 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 } @@ -726,6 +629,7 @@ mod test { 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); @@ -739,12 +643,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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); @@ -773,12 +679,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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); @@ -795,12 +703,14 @@ mod test { min_probe_duration: TimeDelta::from_millis(123), ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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); @@ -836,12 +746,14 @@ mod test { p2: 3.0, ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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); @@ -856,12 +768,14 @@ mod test { p2: 0.0, ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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); @@ -873,12 +787,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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; @@ -899,17 +815,20 @@ mod test { } #[test] - fn probes_on_max_allocated_bitrate_increase_only_when_in_alr() { + 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()); + 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), @@ -923,17 +842,130 @@ mod test { assert!(probes.is_empty()); // Probe when in alr. - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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); + } - // Do not probe when not in alr. - probe_controller.set_alr_start_time_ms(None); - let probes = probe_controller - .on_max_total_allocated_bitrate(MAX_BITRATE + DataRate::from_bits_per_sec(2), clock); + #[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] @@ -944,12 +976,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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, @@ -963,7 +997,7 @@ mod test { assert!(probes.is_empty()); // Probe when in alr. - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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); @@ -985,12 +1019,14 @@ mod test { probe_max_allocation: false, ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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), @@ -1000,10 +1036,10 @@ mod test { clock += EXPONENTIAL_PROBING_TIMEOUT; let probes = probe_controller.process(clock); assert!(probes.is_empty()); - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + probe_controller.set_alr_start_time(Some(clock)); // Do no probe, since probe_max_allocation:false. - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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()); @@ -1015,12 +1051,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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; @@ -1054,12 +1092,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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 @@ -1087,12 +1127,14 @@ mod test { abort_further: true, ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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()); @@ -1122,12 +1164,14 @@ mod test { abort_further: true, ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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()); @@ -1151,12 +1195,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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()); @@ -1182,12 +1228,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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. @@ -1209,12 +1257,14 @@ mod test { ..Default::default() }); probe_controller.enable_repeated_initial_probing(true); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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; @@ -1244,12 +1294,14 @@ mod test { ..Default::default() }); probe_controller.enable_repeated_initial_probing(true); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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()); @@ -1268,12 +1320,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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( @@ -1282,7 +1336,7 @@ mod test { clock, ); - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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( @@ -1302,12 +1356,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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( @@ -1316,7 +1372,7 @@ mod test { clock, ); - probe_controller.set_alr_start_time_ms(None); + probe_controller.set_alr_start_time(None); clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); probe_controller.process(clock); probe_controller.set_estimated_bitrate( @@ -1324,7 +1380,7 @@ mod test { BandwidthLimitedCause::DelayBasedLimited, clock, ); - probe_controller.set_alr_ended_time_ms(clock.ms()); + probe_controller.set_alr_ended_time(clock); clock += ALR_ENDED_TIMEOUT - TimeDelta::from_millis(1); let probes = probe_controller.request_probe(clock); @@ -1338,12 +1394,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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( @@ -1352,7 +1410,7 @@ mod test { clock, ); - probe_controller.set_alr_start_time_ms(None); + probe_controller.set_alr_start_time(None); clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); probe_controller.process(clock); probe_controller.set_estimated_bitrate( @@ -1360,7 +1418,7 @@ mod test { BandwidthLimitedCause::DelayBasedLimited, clock, ); - probe_controller.set_alr_ended_time_ms(clock.ms()); + 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()); @@ -1372,12 +1430,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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( @@ -1386,7 +1446,7 @@ mod test { clock, ); - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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( @@ -1405,12 +1465,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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); @@ -1423,7 +1485,7 @@ mod test { let start_time: Timestamp = clock; // Expect the controller to send a new probe after 5s has passed. - probe_controller.set_alr_start_time_ms(Some(start_time.ms())); + 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); @@ -1436,7 +1498,7 @@ mod test { ); // The following probe should be sent at 10s into ALR. - probe_controller.set_alr_start_time_ms(Some(start_time.ms())); + 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( @@ -1446,7 +1508,7 @@ mod test { ); assert!(probes.is_empty()); - probe_controller.set_alr_start_time_ms(Some(start_time.ms())); + 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); @@ -1464,15 +1526,17 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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_ms(Some(alr_start_time.ms())); + 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); @@ -1508,9 +1572,11 @@ mod test { ..Default::default() }); assert!(probes.is_empty()); - assert!(probe_controller - .set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock) - .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()); @@ -1519,9 +1585,11 @@ mod test { // 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()); + 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()); } @@ -1532,13 +1600,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); - const MBPS_MULTIPLIER: DataRate = DataRate::from_kilobits_per_sec(1000); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); probe_controller.set_bitrates( MIN_BITRATE, 10 * MBPS_MULTIPLIER, @@ -1568,22 +1637,27 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); - const MBPS_MULTIPLIER: DataRate = DataRate::from_kilobits_per_sec(1000); - const MAX_BITRATE: DataRate = DataRate::from_kilobits_per_sec(100 * 1000); - probe_controller.set_bitrates(MIN_BITRATE, 10 * MBPS_MULTIPLIER, MAX_BITRATE, clock); + 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_ms(Some(alr_start_time.ms())); + probe_controller.set_alr_start_time(Some(alr_start_time)); - let estimated_bitrate: DataRate = MAX_BITRATE / 10; + let estimated_bitrate: DataRate = 10 * MBPS_MULTIPLIER; probe_controller.set_estimated_bitrate( estimated_bitrate, BandwidthLimitedCause::DelayBasedLimited, @@ -1602,9 +1676,11 @@ mod test { 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()); + 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); @@ -1627,12 +1703,14 @@ mod test { min_probe_packets_sent: 2, ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() + ); let probes = probe_controller.set_bitrates( MIN_BITRATE, @@ -1666,7 +1744,7 @@ mod test { clock += TimeDelta::from_seconds(5); probe_controller.process(clock); - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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); @@ -1679,12 +1757,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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( @@ -1693,7 +1773,7 @@ mod test { clock, ); // Expect the controller to send a new probe after 5s has passed. - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + probe_controller.set_alr_start_time(Some(clock)); clock += TimeDelta::from_seconds(5); let probes = probe_controller.process(clock); assert_eq!(probes.len(), 1); @@ -1723,593 +1803,147 @@ mod test { } #[test] - fn periodic_probe_at_upper_network_state_estimate() { + fn probe_in_alr_if_loss_based_increasing() { let mut clock = Timestamp::zero(); let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); - + 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( - DataRate::from_bits_per_sec(5000), - BandwidthLimitedCause::DelayBasedLimited, + START_BITRATE, + BandwidthLimitedCause::LossLimitedBweIncreasing, clock, ); - // Expect the controller to send a new probe after 5s has passed. - let state_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_kilobits_per_sec(6), - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - clock += TimeDelta::from_seconds(5); + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; let probes = probe_controller.process(clock); - assert_eq!(probes.len(), 1); - assert_eq!( - probes[0].target_data_rate, - state_estimate.link_capacity_upper - ); - clock += TimeDelta::from_seconds(5); + 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, - state_estimate.link_capacity_upper - ); + assert_eq!(probes[0].target_data_rate, 1.5 * START_BITRATE); } #[test] - fn limit_probe_at_upper_network_state_estimate_if_loss_based_limited() { + fn not_probe_when_in_alr_if_loss_based_decreases() { let mut clock = Timestamp::zero(); let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); - + 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( - DataRate::from_bits_per_sec(500), - BandwidthLimitedCause::DelayBasedLimited, + START_BITRATE, + BandwidthLimitedCause::LossLimitedBwe, clock, ); - // Expect the controller to send a new probe after 5s has passed. - let state_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_bits_per_sec(700), - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - clock += TimeDelta::from_seconds(5); + + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; let probes = probe_controller.process(clock); - assert_eq!(probes.len(), 1); + assert!(probes.is_empty()); - probe_controller.set_estimated_bitrate( - DataRate::from_bits_per_sec(500), - BandwidthLimitedCause::LossLimitedBweIncreasing, - clock, - ); - // Expect the controller to send a new probe after 5s has passed. - clock += TimeDelta::from_seconds(5); + // 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()); - assert_eq!(probes[0].target_data_rate, DataRate::from_bits_per_sec(700)); + assert!(probes.is_empty()); } #[test] - fn alr_probes_limited_by_network_state_estimate() { + fn not_probe_if_loss_based_increasing_outside_alr() { let mut clock = Timestamp::zero(); let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); - probe_controller.enable_periodic_alr_probing(true); + 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( - DataRate::from_kilobits_per_sec(6), - BandwidthLimitedCause::DelayBasedLimited, + START_BITRATE, + BandwidthLimitedCause::LossLimitedBweIncreasing, clock, ); - probe_controller.set_alr_start_time_ms(Some(clock.ms())); - clock += TimeDelta::from_seconds(5); + // Wait long enough to time out exponential probing. + clock += EXPONENTIAL_PROBING_TIMEOUT; let probes = probe_controller.process(clock); - assert_eq!(probes.len(), 1); - assert_eq!(probes[0].target_data_rate, MAX_BITRATE); + assert!(probes.is_empty()); - let state_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_bits_per_sec(8000), - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - clock += TimeDelta::from_seconds(5); + probe_controller.set_alr_start_time(None); + 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, - state_estimate.link_capacity_upper - ); + assert!(probes.is_empty()); } #[test] - fn can_set_longer_probe_duration_after_network_state_estimate() { - let mut clock = Timestamp::zero(); + fn max_allocated_bitrate_not_reset() { + let clock = Timestamp::zero(); let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), - network_state_probe_duration: TimeDelta::from_millis(100), ..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); - let probes = probe_controller.set_estimated_bitrate( - DataRate::from_kilobits_per_sec(5), - BandwidthLimitedCause::DelayBasedLimited, - clock, + 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()); - assert!(probes[0].target_duration < TimeDelta::from_millis(100)); - let state_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_kilobits_per_sec(6), - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - clock += TimeDelta::from_seconds(5); - let probes = probe_controller.process(clock); - assert_eq!(probes.len(), 1); - assert_eq!(probes[0].target_duration, TimeDelta::from_millis(100)); + 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 probe_in_alr_if_loss_based_increasing() { + 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.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_ms(Some(clock.ms())); - 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_ms(Some(clock.ms())); - 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_ms(None); - clock += ALR_PROBE_INTERVAL + TimeDelta::from_millis(1); - let probes = probe_controller.process(clock); - assert!(probes.is_empty()); - } - - #[test] - fn probe_further_when_loss_based_is_same_as_delay_based_estimate() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), - ..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 state_estimate = NetworkStateEstimate { - link_capacity_upper: 5 * START_BITRATE, - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - clock += TimeDelta::from_seconds(5); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - - let probe_target_rate: DataRate = probes[0].target_data_rate; - assert!(probe_target_rate < state_estimate.link_capacity_upper); - // Expect that more probes are sent if BWE is the same as delay based - // estimate. - let probes = probe_controller.set_estimated_bitrate( - probe_target_rate, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(!probes.is_empty()); - assert_eq!(probes[0].target_data_rate, 2 * probe_target_rate); - } - - #[test] - fn probe_if_estimate_lower_than_network_state_estimate() { - // Periodic probe every 1 second if estimate is lower than 50% of the - // NetworkStateEstimate. - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - est_lower_than_network_interval: TimeDelta::from_seconds(1), - est_lower_than_network_ratio: 0.5, - // limit_probe_target_rate_to_loss_bwe: true, - ..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, - ); - // 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 mut state_estimate = NetworkStateEstimate { - link_capacity_upper: START_BITRATE, - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - let probes = probe_controller.process(clock); - assert!(probes.is_empty()); - - state_estimate.link_capacity_upper = START_BITRATE * 3; - probe_controller.set_network_state_estimate(state_estimate); - let probes = probe_controller.process(clock); - assert_eq!(probes.len(), 1); - assert!(probes[0].target_data_rate > START_BITRATE); - - // If network state not increased, send another probe. - clock += TimeDelta::from_millis(1100); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - - // Stop probing if estimate increase. We might probe further here though. - probe_controller.set_estimated_bitrate( - 2 * START_BITRATE, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - // No more periodic probes. - clock += TimeDelta::from_millis(1100); - let probes = probe_controller.process(clock); - assert!(probes.is_empty()); - } - - #[test] - fn dont_probe_further_when_loss_limited() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), - ..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 state_estimate = NetworkStateEstimate { - link_capacity_upper: 3 * START_BITRATE, - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - clock += TimeDelta::from_seconds(5); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - assert!(probes[0].target_data_rate < state_estimate.link_capacity_upper); - // Expect that no more probes are sent immediately if BWE is loss limited. - let probes = probe_controller.set_estimated_bitrate( - probes[0].target_data_rate, - BandwidthLimitedCause::LossLimitedBwe, - clock, - ); - assert!(probes.is_empty()); - } - - #[test] - fn probe_further_when_delay_based_limited() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), - ..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 state_estimate = NetworkStateEstimate { - link_capacity_upper: 3 * START_BITRATE, - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - clock += TimeDelta::from_seconds(5); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - assert!(probes[0].target_data_rate < state_estimate.link_capacity_upper); - // Since the probe was successfull, expect to continue probing. - let probes = probe_controller.set_estimated_bitrate( - probes[0].target_data_rate, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(!probes.is_empty()); - assert_eq!( - probes[0].target_data_rate, - state_estimate.link_capacity_upper - ); - } - - #[test] - fn probe_after_timeout_if_network_state_estimate_increase_after_probe_sent() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), - est_lower_than_network_interval: TimeDelta::from_seconds(3), - est_lower_than_network_ratio: 0.7, - ..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()); - let mut state_estimate = NetworkStateEstimate { - link_capacity_upper: 1.2 * probes[0].target_data_rate / 2.0, - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); - // No immediate further probing since probe result is low. - let probes = probe_controller.set_estimated_bitrate( - probes[0].target_data_rate / 2, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(probes.is_empty()); - - clock += TimeDelta::from_seconds(5); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - assert!(probes[0].target_data_rate <= state_estimate.link_capacity_upper); - // If the network state estimate increase, even before the probe result, - // expect a new probe after `est_lower_than_network_interval` timeout. - state_estimate.link_capacity_upper = 3 * START_BITRATE; - probe_controller.set_network_state_estimate(state_estimate); - let probes = probe_controller.set_estimated_bitrate( - probes[0].target_data_rate, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(probes.is_empty()); - - clock += TimeDelta::from_seconds(3); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - - // But no more probes if estimate is close to the link capacity. - let probes = probe_controller.set_estimated_bitrate( - state_estimate.link_capacity_upper * 0.9, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(probes.is_empty()); - } - - #[test] - fn skip_probe_further_if_already_probed_to_max_rate() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(2), - 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()); - let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); - assert!(!probes.is_empty()); - - probe_controller.set_network_state_estimate(NetworkStateEstimate { - link_capacity_upper: 2 * MAX_BITRATE, - ..Default::default() - }); - - // Attempt to probe up to max rate. - let probes = probe_controller.set_estimated_bitrate( - MAX_BITRATE * 0.8, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(!probes.is_empty()); - assert_eq!(probes[0].target_data_rate, MAX_BITRATE); - - // If the probe result arrives, dont expect a new probe immediately since we - // already tried to probe at the max rate. - let probes = probe_controller.set_estimated_bitrate( - MAX_BITRATE * 0.8, - BandwidthLimitedCause::DelayBasedLimited, - clock, + assert!( + probe_controller + .on_network_availability(NetworkAvailability { + network_available: true, + ..Default::default() + }) + .is_empty() ); - assert!(probes.is_empty()); - - clock += TimeDelta::from_millis(1000); - let probes = probe_controller.process(clock); - assert!(probes.is_empty()); - // But when enough time has passed, expect a new probe. - clock += TimeDelta::from_millis(1000); - 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()); @@ -2321,7 +1955,7 @@ mod test { ); assert!(probes.is_empty()); - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + probe_controller.set_alr_start_time(Some(clock)); clock += TimeDelta::from_seconds(10); let probes = probe_controller.process(clock); assert!(probes.is_empty()); @@ -2339,12 +1973,14 @@ mod test { 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()); + 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()); @@ -2355,7 +1991,7 @@ mod test { ); clock += TimeDelta::from_seconds(10); - probe_controller.set_alr_start_time_ms(Some(clock.ms())); + 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()); @@ -2371,148 +2007,20 @@ mod test { assert!(!probes.is_empty()); } - #[test] - fn skip_network_state_probe_if_estimate_larger_than_max_probe() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(2), - 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()); - let probes = probe_controller.set_bitrates(MIN_BITRATE, START_BITRATE, MAX_BITRATE, clock); - assert!(!probes.is_empty()); - - probe_controller.set_network_state_estimate(NetworkStateEstimate { - link_capacity_upper: 2 * MAX_BITRATE, - ..Default::default() - }); - let probes = probe_controller.set_estimated_bitrate( - MAX_BITRATE, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(probes.is_empty()); - - clock += TimeDelta::from_seconds(10); - let probes = probe_controller.process(clock); - assert!(probes.is_empty()); - } - - #[test] - fn sends_probe_if_network_state_estimate_lower_than_max_probe() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(2), - skip_if_est_larger_than_fraction_of_max: 0.9, - network_state_probe_duration: TimeDelta::from_millis(100), - network_state_min_probe_delta: TimeDelta::from_millis(20), - ..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.set_network_state_estimate(NetworkStateEstimate { - link_capacity_upper: 2 * MAX_BITRATE, - ..Default::default() - }); - let probes = probe_controller.set_estimated_bitrate( - MAX_BITRATE, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(probes.is_empty()); - - // Need to wait at least two seconds before process can trigger a new probe. - clock += TimeDelta::from_millis(2100); - - let probes = probe_controller.set_estimated_bitrate( - START_BITRATE, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - assert!(probes.is_empty()); - probe_controller.set_network_state_estimate(NetworkStateEstimate { - link_capacity_upper: 2 * START_BITRATE, - ..Default::default() - }); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - assert!(probes[0].target_data_rate <= 2 * START_BITRATE); - // Expect probe durations to be picked from field trial probe target is lower - // or equal to the network state estimate. - assert_eq!(probes[0].min_probe_delta, TimeDelta::from_millis(20)); - assert_eq!(probes[0].target_duration, TimeDelta::from_millis(100)); - } - - #[test] - fn probe_not_limited_by_network_state_esimate_if_lower_thant_current() { - let mut clock = Timestamp::zero(); - let mut probe_controller = ProbeController::new(ProbeControllerConfig { - network_state_interval: TimeDelta::from_seconds(5), - network_state_probe_duration: TimeDelta::from_millis(100), - network_state_min_probe_delta: TimeDelta::from_millis(20), - ..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( - START_BITRATE, - BandwidthLimitedCause::DelayBasedLimited, - clock, - ); - probe_controller.set_network_state_estimate(NetworkStateEstimate { - link_capacity_upper: START_BITRATE, - ..Default::default() - }); - // 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()); - - probe_controller.set_alr_start_time_ms(Some(clock.ms())); - probe_controller.set_network_state_estimate(NetworkStateEstimate { - link_capacity_upper: START_BITRATE / 2, - ..Default::default() - }); - clock += TimeDelta::from_seconds(6); - let probes = probe_controller.process(clock); - assert!(!probes.is_empty()); - assert_eq!(probes[0].target_data_rate, START_BITRATE); - // Expect probe durations to be default since network state estimate is lower - // than the probe rate. - assert_eq!(probes[0].min_probe_delta, TimeDelta::from_millis(2)); - assert_eq!(probes[0].target_duration, TimeDelta::from_millis(15)); - } - #[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()); + 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()); @@ -2521,12 +2029,6 @@ mod test { clock += TimeDelta::from_millis(1100); let probes = probe_controller.process(clock); assert!(probes.is_empty()); - - let state_estimate = NetworkStateEstimate { - link_capacity_upper: 3 * START_BITRATE, - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); let probes = probe_controller.set_estimated_bitrate( START_BITRATE, BandwidthLimitedCause::DelayBasedLimitedDelayIncreased, @@ -2545,12 +2047,14 @@ mod test { let mut probe_controller = ProbeController::new(ProbeControllerConfig { ..Default::default() }); - assert!(probe_controller - .on_network_availability(NetworkAvailability { - network_available: true, - ..Default::default() - }) - .is_empty()); + 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()); @@ -2559,12 +2063,6 @@ mod test { clock += TimeDelta::from_millis(1100); let probes = probe_controller.process(clock); assert!(probes.is_empty()); - - let state_estimate = NetworkStateEstimate { - link_capacity_upper: 3 * START_BITRATE, - ..Default::default() - }; - probe_controller.set_network_state_estimate(state_estimate); let probes = probe_controller.set_estimated_bitrate( START_BITRATE, BandwidthLimitedCause::RttBasedBackOffHighRtt, 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 index 725ffe234e0..5256dfe195b 100644 --- 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 @@ -9,16 +9,16 @@ */ use crate::gcc::{ + LinkCapacityEstimator, api::{ - transport::{BandwidthUsage, NetworkStateEstimate}, + transport::BandwidthUsage, units::{DataRate, DataSize, TimeDelta, Timestamp}, }, experiments::FieldTrials, - LinkCapacityEstimator, remote_bitrate_estimator::BITRATE_WINDOW, }; -use super::{RateControlInput, CONGESTION_CONTROLLER_MIN_BITRATE}; +use super::{CONGESTION_CONTROLLER_MIN_BITRATE, RateControlInput}; // WebRTC-BweBackOffFactor #[derive(Debug, Clone)] @@ -46,27 +46,13 @@ impl BweBackOffFactor { return; } - tracing::warn!("Failed to parse parameters for AimdRateControl experiment from field trial string. Using default."); + tracing::warn!( + "Failed to parse parameters for AimdRateControl experiment from field trial string. Using default." + ); self.backoff_factor = Self::DEFAULT_BACKOFF_FACTOR } } -// WebRTC-Bwe-EstimateBoundedIncrease -#[derive(Debug, Clone)] -pub struct EstimateBoundedIncrease { - pub disable_estimate_bounded_increase: bool, - pub use_current_estimate_as_min_upper_bound: bool, -} - -impl Default for EstimateBoundedIncrease { - fn default() -> Self { - Self { - disable_estimate_bounded_increase: false, - use_current_estimate_as_min_upper_bound: true, - } - } -} - #[derive(Debug, PartialEq, Clone, Copy)] enum RateControlState { Hold, @@ -79,12 +65,12 @@ enum RateControlState { // 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, - network_estimate: Option, rate_control_state: RateControlState, time_last_bitrate_change: Timestamp, time_last_bitrate_decrease: Timestamp, @@ -98,10 +84,6 @@ pub struct AimdRateControl { // limited region (alr) is not detected. no_bitrate_increase_in_alr: bool, last_decrease: Option, - - // Field trials - disable_estimate_bounded_increase: bool, - use_current_estimate_as_min_upper_bound: bool, } impl Default for AimdRateControl { @@ -122,10 +104,7 @@ impl Default for AimdRateControl { rtt: Self::DEFAULT_RTT, send_side: false, no_bitrate_increase_in_alr: false, - network_estimate: None, last_decrease: None, - disable_estimate_bounded_increase: false, - use_current_estimate_as_min_upper_bound: true, } } } @@ -135,21 +114,17 @@ impl AimdRateControl { 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 {}", - field_trials.bwe_back_off_factor.backoff_factor + backoff.backoff_factor ); Self { send_side, - beta: field_trials.bwe_back_off_factor.backoff_factor, + beta: backoff.backoff_factor, no_bitrate_increase_in_alr: field_trials.no_bitrate_increase_in_alr, - use_current_estimate_as_min_upper_bound: field_trials - .estimate_bounded_increase - .use_current_estimate_as_min_upper_bound, - disable_estimate_bounded_increase: field_trials - .estimate_bounded_increase - .disable_estimate_bounded_increase, ..Default::default() } } @@ -259,11 +234,6 @@ impl AimdRateControl { } } - #[cfg(test)] - pub fn set_network_state_estimate(&mut self, estimate: Option) { - self.network_estimate = estimate; - } - // 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()); @@ -281,23 +251,6 @@ impl AimdRateControl { const MIN_INCREASE_RATE_BPS_PER_SECOND: f64 = 4000.0; increase_rate_bps_per_second.max(MIN_INCREASE_RATE_BPS_PER_SECOND) } - // Returns the expected time between overuse signals (assuming steady state). - pub fn get_expected_bandwidth_period(&self) -> TimeDelta { - const MIN_PERIOD: TimeDelta = TimeDelta::from_seconds(2); - const DEFAULT_PERIOD: TimeDelta = TimeDelta::from_seconds(3); - const MAX_PERIOD: TimeDelta = TimeDelta::from_seconds(50); - - let increase_rate_bps_per_second: f64 = self.get_near_max_increase_rate_bps_per_second(); - if let Some(last_decrease) = self.last_decrease { - let time_to_recover_decrease_seconds: f64 = - last_decrease.bps_float() / increase_rate_bps_per_second; - let period: TimeDelta = TimeDelta::from_seconds_float(time_to_recover_decrease_seconds); - period.clamp(MIN_PERIOD, MAX_PERIOD) - } else { - DEFAULT_PERIOD - } - } - // 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 @@ -414,27 +367,6 @@ impl AimdRateControl { } fn clamp_bitrate(&self, mut new_bitrate: DataRate) -> DataRate { - if let Some(network_estimate) = self.network_estimate.as_ref() { - if !self.disable_estimate_bounded_increase - && network_estimate.link_capacity_upper.is_finite() - { - let upper_bound: DataRate = if self.use_current_estimate_as_min_upper_bound { - network_estimate - .link_capacity_upper - .max(self.current_bitrate) - } else { - network_estimate.link_capacity_upper - }; - new_bitrate = upper_bound.min(new_bitrate); - } - if network_estimate.link_capacity_lower.is_finite() - && new_bitrate < self.current_bitrate - { - new_bitrate = self - .current_bitrate - .min(new_bitrate.max(network_estimate.link_capacity_lower * self.beta)); - } - } new_bitrate = new_bitrate.max(self.min_configured_bitrate); new_bitrate } @@ -486,10 +418,6 @@ mod test { const INITIAL_TIME: Timestamp = Timestamp::from_millis(123_456); - const MIN_BWE_PERIOD: TimeDelta = TimeDelta::from_seconds(2); - const DEFAULT_PERIOD: TimeDelta = TimeDelta::from_seconds(3); - const MAX_BWE_PERIOD: TimeDelta = TimeDelta::from_seconds(50); - // After an overuse, we back off to 85% to the received bitrate. const FRACTION_AFTER_OVERUSE: f64 = 0.85; @@ -525,7 +453,7 @@ mod test { } #[test] - fn get_increase_rate_and_bandwidth_period() { + 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); @@ -537,10 +465,6 @@ mod test { (aimd_rate_control.get_near_max_increase_rate_bps_per_second() - 14_000.0).abs() <= 1_000.0, ); - assert_eq!( - aimd_rate_control.get_expected_bandwidth_period(), - DEFAULT_PERIOD - ); } #[test] @@ -595,33 +519,9 @@ mod test { } #[test] - fn default_period_until_first_overuse() { - let mut aimd_rate_control = AimdRateControl::default(); - aimd_rate_control.set_start_bitrate(DataRate::from_kilobits_per_sec(300)); - assert_eq!( - aimd_rate_control.get_expected_bandwidth_period(), - DEFAULT_PERIOD - ); - aimd_rate_control.update( - RateControlInput::new( - BandwidthUsage::Overusing, - Some(DataRate::from_kilobits_per_sec(280)), - ), - INITIAL_TIME, - ); - assert_ne!( - aimd_rate_control.get_expected_bandwidth_period(), - DEFAULT_PERIOD - ); - } - - #[test] - fn expected_period_after_typical_drop() { + fn typical_drop() { let mut aimd_rate_control = AimdRateControl::default(); - // The rate increase at 216 kbps should be 12 kbps. If we drop from - // 216 + 4*12 = 264 kbps, it should take 4 seconds to recover. Since we - // back off to 0.85*acked_rate-5kbps, the acked bitrate needs to be 260 - // kbps to end up at 216 kbps. + // 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 = @@ -638,50 +538,6 @@ mod test { aimd_rate_control.get_near_max_increase_rate_bps_per_second(), 12_000.0 ); - assert_eq!( - aimd_rate_control.get_expected_bandwidth_period(), - TimeDelta::from_seconds(4) - ); - } - - #[test] - fn bandwidth_period_is_not_below_min() { - let mut aimd_rate_control = AimdRateControl::default(); - const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(10_000); - let mut now: Timestamp = INITIAL_TIME; - aimd_rate_control.set_estimate(INITIAL_BITRATE, now); - now += TimeDelta::from_millis(100); - // Make a small (1.5 kbps) bitrate drop to 8.5 kbps. - aimd_rate_control.update( - RateControlInput::new( - BandwidthUsage::Overusing, - Some(INITIAL_BITRATE - DataRate::from_bits_per_sec(1)), - ), - now, - ); - assert_eq!( - aimd_rate_control.get_expected_bandwidth_period(), - MIN_BWE_PERIOD - ); - } - - #[test] - fn bandwidth_period_is_not_above_max_no_smoothing_exp() { - let mut aimd_rate_control = AimdRateControl::default(); - const INITIAL_BITRATE: DataRate = DataRate::from_bits_per_sec(10_010_000); - let mut now: Timestamp = INITIAL_TIME; - aimd_rate_control.set_estimate(INITIAL_BITRATE, now); - now += TimeDelta::from_millis(100); - // Make a large (10 Mbps) bitrate drop to 10 kbps. - let acked_bitrate: DataRate = DataRate::from_bits_per_sec(10_000) / FRACTION_AFTER_OVERUSE; - aimd_rate_control.update( - RateControlInput::new(BandwidthUsage::Overusing, Some(acked_bitrate)), - now, - ); - assert_eq!( - aimd_rate_control.get_expected_bandwidth_period(), - MAX_BWE_PERIOD - ); } #[test] @@ -757,110 +613,6 @@ mod test { assert_eq!(aimd_rate_control.latest_estimate(), 2 * INITIAL_BITRATE); } - #[test] - fn set_estimate_upper_limited_by_network_estimate() { - let field_trials = Default::default(); - let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); - aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(300_000), INITIAL_TIME); - let network_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_bits_per_sec(400_000), - ..Default::default() - }; - aimd_rate_control.set_network_state_estimate(Some(network_estimate)); - aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(500_000), INITIAL_TIME); - assert_eq!( - aimd_rate_control.latest_estimate(), - network_estimate.link_capacity_upper - ); - } - - #[test] - fn set_estimate_default_upper_limited_by_current_bitrate_if_network_estimate_is_low() { - let field_trials = Default::default(); - let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); - aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(500_000), INITIAL_TIME); - assert_eq!( - aimd_rate_control.latest_estimate(), - DataRate::from_bits_per_sec(500_000) - ); - - let network_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_bits_per_sec(300_000), - ..Default::default() - }; - - aimd_rate_control.set_network_state_estimate(Some(network_estimate)); - aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(700_000), INITIAL_TIME); - assert_eq!( - aimd_rate_control.latest_estimate(), - DataRate::from_bits_per_sec(500_000) - ); - } - - #[test] - fn set_estimate_not_upper_limited_by_current_bitrate_if_network_estimate_is_low_if() { - // WebRTC-Bwe-EstimateBoundedIncrease/c_upper:false/ - let field_trials = FieldTrials { - estimate_bounded_increase: EstimateBoundedIncrease { - use_current_estimate_as_min_upper_bound: false, - ..Default::default() - }, - ..Default::default() - }; - let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); - - aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(500_000), INITIAL_TIME); - assert_eq!( - aimd_rate_control.latest_estimate(), - DataRate::from_bits_per_sec(500_000) - ); - - let network_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_bits_per_sec(300_000), - ..Default::default() - }; - aimd_rate_control.set_network_state_estimate(Some(network_estimate)); - aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(700_000), INITIAL_TIME); - assert_eq!( - aimd_rate_control.latest_estimate(), - DataRate::from_bits_per_sec(300_000) - ); - } - - #[test] - fn set_estimate_lower_limited_by_network_estimate() { - let field_trials = Default::default(); - let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); - let network_estimate = NetworkStateEstimate { - link_capacity_lower: DataRate::from_bits_per_sec(400_000), - ..Default::default() - }; - aimd_rate_control.set_network_state_estimate(Some(network_estimate)); - aimd_rate_control.set_estimate(DataRate::from_bits_per_sec(100_000), INITIAL_TIME); - // 0.85 is default backoff factor. (`beta_`) - assert_eq!( - aimd_rate_control.latest_estimate(), - network_estimate.link_capacity_lower * 0.85 - ); - } - - #[test] - fn set_estimate_ignored_if_lower_than_network_estimate_and_current() { - let field_trials = Default::default(); - let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); - aimd_rate_control.set_estimate(DataRate::from_kilobits_per_sec(200), INITIAL_TIME); - assert_eq!(aimd_rate_control.latest_estimate().kbps(), 200); - let network_estimate = NetworkStateEstimate { - link_capacity_lower: DataRate::from_kilobits_per_sec(400), - ..Default::default() - }; - aimd_rate_control.set_network_state_estimate(Some(network_estimate)); - // Ignore the next SetEstimate, since the estimate is lower than 85% of - // the network estimate. - aimd_rate_control.set_estimate(DataRate::from_kilobits_per_sec(100), INITIAL_TIME); - assert_eq!(aimd_rate_control.latest_estimate().kbps(), 200); - } - #[test] fn estimate_increase_while_not_in_alr() { // Allow the estimate to increase as long as alr is not detected to ensure @@ -886,34 +638,4 @@ mod test { } assert!(aimd_rate_control.latest_estimate() > INITIAL_BITRATE); } - - #[test] - fn estimate_not_limited_by_network_estimate_if_disabled() { - // WebRTC-Bwe-EstimateBoundedIncrease/Disabled/ - let field_trials = FieldTrials { - estimate_bounded_increase: EstimateBoundedIncrease { - disable_estimate_bounded_increase: true, - ..Default::default() - }, - ..Default::default() - }; - let mut aimd_rate_control = AimdRateControl::new(&field_trials, true); - aimd_rate_control.disable_estimate_bounded_increase = 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); - let network_estimate = NetworkStateEstimate { - link_capacity_upper: DataRate::from_bits_per_sec(150_000), - ..Default::default() - }; - aimd_rate_control.set_network_state_estimate(Some(network_estimate)); - - 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() > network_estimate.link_capacity_upper); - } } diff --git a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs index d5793ac11c9..5983bbf1558 100644 --- a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs +++ b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs @@ -8,23 +8,21 @@ * 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}, - }, - AcknowledgedBitrateEstimatorInterface, +use crate::gcc::api::{ + transport::PacketResult, + units::{DataRate, DataSize, TimeDelta, Timestamp}, }; // WebRTC-Bwe-RobustThroughputEstimatorSettings #[derive(Debug, Clone)] pub struct RobustThroughputEstimatorSettings { - // Set `enabled` to true to use the RobustThroughputEstimator, false to use - // the AcknowledgedBitrateEstimator. - pub enabled: bool, - // The estimator keeps the smallest window containing at least // `window_packets` and at least the packets received during the last // `min_window_duration` milliseconds. @@ -41,37 +39,22 @@ pub struct RobustThroughputEstimatorSettings { // The estimator window requires at least `required_packets` packets // to produce an estimate. pub required_packets: usize, - - // If audio packets aren't included in allocation (i.e. the - // estimated available bandwidth is divided only among the video - // streams), then `unacked_weight` should be set to 0. - // If audio packets are included in allocation, but not in bandwidth - // estimation (i.e. they don't have transport-wide sequence numbers, - // but we nevertheless divide the estimated available bandwidth among - // both audio and video streams), then `unacked_weight` should be set to 1. - // If all packets have transport-wide sequence numbers, then the value - // of `unacked_weight` doesn't matter. - pub unacked_weight: f64, } impl Default for RobustThroughputEstimatorSettings { fn default() -> Self { Self { - enabled: true, window_packets: 20, max_window_packets: 500, window_duration: TimeDelta::from_seconds(1), max_window_duration: TimeDelta::from_seconds(5), required_packets: 10, - unacked_weight: 1.0, } } } impl RobustThroughputEstimatorSettings { pub fn validate(&mut self) { - assert!(self.enabled); - if self.window_packets < 10 || 1000 < self.window_packets { tracing::warn!("Window size must be between 10 and 1000 packets"); self.window_packets = 20; @@ -103,14 +86,10 @@ impl RobustThroughputEstimatorSettings { self.max_window_duration = TimeDelta::from_seconds(5); } self.window_duration = self.window_duration.min(self.max_window_duration); - - if self.unacked_weight < 0.0 || 1.0 < self.unacked_weight { - tracing::warn!("Weight for prior unacked size must be between 0 and 1."); - self.unacked_weight = 1.0; - } } } +#[derive(Clone)] pub struct RobustThroughputEstimator { settings: RobustThroughputEstimatorSettings, window: VecDeque, @@ -153,10 +132,8 @@ impl RobustThroughputEstimator { } false } -} -impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { - fn incoming_packet_feedback(&mut self, packet_feedback: &[PacketResult]) { + 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. @@ -170,18 +147,13 @@ impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { // Insert the new packet. self.window.push_back(*packet); - self.window - .back_mut() - .unwrap() - .sent_packet - .prior_unacked_data *= self.settings.unacked_weight; // 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. - for i in (1..self.window.len()).rev() { - if self.window[i].receive_time < self.window[i - 1].receive_time { - self.window.swap(i, i - 1); - } + 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); @@ -207,7 +179,7 @@ impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { } } - fn bitrate(&self) -> Option { + pub fn bitrate(&self) -> Option { if self.window.is_empty() || self.window.len() < self.settings.required_packets { return None; } @@ -237,11 +209,10 @@ impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { 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 + packet.sent_packet.prior_unacked_data; + 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; - recv_size += packet.sent_packet.prior_unacked_data; if packet.sent_packet.send_time < self.latest_discarded_send_time { // If we have dropped packets from the window that were sent after @@ -254,12 +225,11 @@ impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { } if packet.sent_packet.send_time > last_send_time { last_send_time = packet.sent_packet.send_time; - last_send_size = packet.sent_packet.size + packet.sent_packet.prior_unacked_data; + 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; - send_size += packet.sent_packet.prior_unacked_data; num_sent_packets_in_window += 1; } @@ -308,18 +278,6 @@ impl AcknowledgedBitrateEstimatorInterface for RobustThroughputEstimator { recv_size / recv_duration, )) } - - fn peek_rate(&self) -> Option { - self.bitrate() - } - - fn set_alr(&mut self, _in_alr: bool) { - // noop - } - - fn set_alr_ended_time(&mut self, _alr_ended_time: crate::gcc::api::units::Timestamp) { - // noop - } } #[cfg(test)] @@ -331,7 +289,6 @@ mod test { struct FeedbackGenerator { send_clock: Timestamp, recv_clock: Timestamp, - sequence_number: i64, } impl Default for FeedbackGenerator { @@ -339,7 +296,6 @@ mod test { Self { send_clock: Timestamp::from_millis(100000), recv_clock: Timestamp::from_millis(10000), - sequence_number: 100, } } } @@ -359,7 +315,6 @@ mod test { packet_feedback.push(PacketResult { sent_packet: SentPacket { send_time: self.send_clock, - sequence_number: self.sequence_number, size: packet_size, ..Default::default() }, @@ -367,7 +322,6 @@ mod test { ..Default::default() }); self.send_clock += packet_size / send_rate; - self.sequence_number += 1; } packet_feedback @@ -554,7 +508,6 @@ mod test { // This test uses a 500ms window to amplify the effect // of a delay spike. let settings = RobustThroughputEstimatorSettings { - enabled: true, window_duration: TimeDelta::from_millis(500), ..Default::default() }; @@ -711,7 +664,6 @@ mod test { // This test uses a 500ms window to amplify the // effect of reordering. let settings = RobustThroughputEstimatorSettings { - enabled: true, window_duration: TimeDelta::from_millis(500), ..Default::default() }; diff --git a/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs b/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs index e337a1b0787..52593f949e2 100644 --- a/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs +++ b/rs/moq-congestion/src/gcc/send_side_bandwidth_estimation.rs @@ -10,105 +10,21 @@ * FEC and NACK added bitrate is handled outside class */ -use std::collections::VecDeque; +//! 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, - LossBasedBandwidthEstimation, LossBasedBweV2, LossBasedState, }; -#[derive(Clone, Debug)] -pub struct BweLossExperiment { - pub enabled: bool, - pub low_loss_threshold: f32, - pub high_loss_threshold: f32, - pub bitrate_threshold_kbps: f64, -} - -impl Default for BweLossExperiment { - fn default() -> Self { - Self { - enabled: false, - low_loss_threshold: 0.02, - high_loss_threshold: 0.1, - bitrate_threshold_kbps: 0.0, - } - } -} - -pub struct LinkCapacityTracker { - capacity_estimate_bps: f64, - last_link_capacity_update: Timestamp, // = Timestamp::minus_infinity(); - last_delay_based_estimate: DataRate, // = DataRate::plus_infinity(); -} - -impl Default for LinkCapacityTracker { - fn default() -> Self { - Self { - capacity_estimate_bps: 0.0, - last_link_capacity_update: Timestamp::minus_infinity(), - last_delay_based_estimate: DataRate::plus_infinity(), - } - } -} - -impl LinkCapacityTracker { - // Call when a new delay-based estimate is available. - pub fn update_delay_based_estimate( - &mut self, - at_time: Timestamp, - delay_based_bitrate: DataRate, - ) { - if delay_based_bitrate < self.last_delay_based_estimate { - self.capacity_estimate_bps = self - .capacity_estimate_bps - .min(delay_based_bitrate.bps_float()); - self.last_link_capacity_update = at_time; - } - self.last_delay_based_estimate = delay_based_bitrate; - } - pub fn on_starting_rate(&mut self, start_rate: DataRate) { - if self.last_link_capacity_update.is_infinite() { - self.capacity_estimate_bps = start_rate.bps_float(); - } - } - pub fn on_rate_update( - &mut self, - acknowledged: Option, - target: DataRate, - at_time: Timestamp, - ) { - let acknowledged = match acknowledged { - Some(ack) => ack, - None => return, - }; - let acknowledged_target: DataRate = std::cmp::min(acknowledged, target); - if acknowledged_target.bps_float() > self.capacity_estimate_bps { - let delta: TimeDelta = at_time - self.last_link_capacity_update; - let alpha: f64 = if delta.is_finite() { - (-(delta / TimeDelta::from_seconds(10))).exp() - } else { - 0.0 - }; - self.capacity_estimate_bps = alpha * self.capacity_estimate_bps - + (1.0 - alpha) * acknowledged_target.bps_float(); - } - self.last_link_capacity_update = at_time; - } - pub fn on_rtt_backoff(&mut self, backoff_rate: DataRate, at_time: Timestamp) { - self.capacity_estimate_bps = self.capacity_estimate_bps.min(backoff_rate.bps_float()); - self.last_link_capacity_update = at_time; - } - pub fn estimate(&self) -> DataRate { - DataRate::from_bits_per_sec_float(self.capacity_estimate_bps) - } -} - // WebRTC-Bwe-MaxRttLimit #[derive(Clone, Debug)] pub struct RttBasedBackoffConfig { @@ -131,6 +47,7 @@ impl Default for RttBasedBackoffConfig { } } +#[derive(Clone)] pub struct RttBasedBackoff { pub drop_fraction: f64, pub drop_interval: TimeDelta, @@ -176,167 +93,57 @@ impl RttBasedBackoff { } } +#[derive(Clone)] pub struct SendSideBandwidthEstimation { - field_trials: FieldTrials, rtt_backoff: RttBasedBackoff, - link_capacity: LinkCapacityTracker, - min_bitrate_history: VecDeque<(Timestamp, DataRate)>, - - // incoming filters - lost_packets_since_last_loss_update: i64, - expected_packets_since_last_loss_update: i64, - - acknowledged_rate: Option, current_target: DataRate, min_bitrate_configured: DataRate, max_bitrate_configured: DataRate, last_low_bitrate_log: Timestamp, - has_decreased_since_last_fraction_loss: bool, - last_loss_feedback: Timestamp, - last_loss_packet_report: Timestamp, - last_fraction_loss: u8, - last_logged_fraction_loss: u8, last_round_trip_time: TimeDelta, - // The max bitrate as set by the receiver in the call. This is typically - // signalled using the REMB RTCP message and is used when we don't have any - // send side delay based estimate. - receiver_limit: DataRate, delay_based_limit: DataRate, time_last_decrease: Timestamp, - first_report_time: Timestamp, - initially_lost_packets: i64, - bitrate_at_2_seconds: DataRate, - last_rtc_event_log: Timestamp, - low_loss_threshold: f32, - high_loss_threshold: f32, - bitrate_threshold: DataRate, - loss_based_bandwidth_estimator_v1: LossBasedBandwidthEstimation, loss_based_bandwidth_estimator_v2: LossBasedBweV2, loss_based_state: LossBasedState, - disable_receiver_limit_caps_only: bool, } impl Default for SendSideBandwidthEstimation { fn default() -> Self { - Self::new(FieldTrials::default()) + Self::new(&FieldTrials::default()) } } impl SendSideBandwidthEstimation { - const BWE_INCREASE_INTERVAL: TimeDelta = TimeDelta::from_millis(1000); - const BWE_DECREASE_INTERVAL: TimeDelta = TimeDelta::from_millis(300); - const START_PHASE: TimeDelta = TimeDelta::from_millis(2000); - const LIMIT_NUM_PACKETS: i64 = 20; const DEFAULT_MAX_BITRATE: DataRate = DataRate::from_bits_per_sec(1000000000); const LOW_BITRATE_LOG_PERIOD: TimeDelta = TimeDelta::from_millis(10000); - // Expecting that RTCP feedback is sent uniformly within [0.5, 1.5]s intervals. - const MAX_RTCP_FEEDBACK_INTERVAL: TimeDelta = TimeDelta::from_millis(5000); - - pub fn new(field_trials: FieldTrials) -> Self { - let mut low_loss_threshold = 0.02; - let mut high_loss_threshold = 0.1; - let mut bitrate_threshold_kbps = 0.0; - - if field_trials.loss_experiment.enabled { - low_loss_threshold = field_trials.loss_experiment.low_loss_threshold; - high_loss_threshold = field_trials.loss_experiment.high_loss_threshold; - bitrate_threshold_kbps = field_trials.loss_experiment.bitrate_threshold_kbps; - assert!(low_loss_threshold > 0.0); - assert!(low_loss_threshold <= 1.0); - assert!(high_loss_threshold > 0.0); - assert!(high_loss_threshold <= 1.0); - assert!(low_loss_threshold <= high_loss_threshold); - assert!(bitrate_threshold_kbps >= 0.0); - tracing::debug!( - "Enabled BweLossExperiment with parameters {:?}, {:?}, {:?}", - low_loss_threshold, - high_loss_threshold, - bitrate_threshold_kbps - ); - } - let bitrate_threshold = DataRate::from_kilobits_per_sec_float(bitrate_threshold_kbps); + pub fn new(field_trials: &FieldTrials) -> Self { let mut loss_based_bandwidth_estimator_v2 = LossBasedBweV2::new(field_trials.loss_based_bwe_v2.clone()); - if field_trials.loss_based_bwe_v2.enabled { + 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), - link_capacity: LinkCapacityTracker::default(), - min_bitrate_history: VecDeque::new(), - lost_packets_since_last_loss_update: 0, - expected_packets_since_last_loss_update: 0, 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(), - has_decreased_since_last_fraction_loss: false, - last_loss_feedback: Timestamp::minus_infinity(), - last_loss_packet_report: Timestamp::minus_infinity(), - last_fraction_loss: 0, - last_logged_fraction_loss: 0, last_round_trip_time: TimeDelta::zero(), - receiver_limit: DataRate::plus_infinity(), delay_based_limit: DataRate::plus_infinity(), time_last_decrease: Timestamp::minus_infinity(), - first_report_time: Timestamp::minus_infinity(), - initially_lost_packets: 0, - bitrate_at_2_seconds: DataRate::zero(), - last_rtc_event_log: Timestamp::minus_infinity(), - low_loss_threshold, - high_loss_threshold, - bitrate_threshold, - loss_based_bandwidth_estimator_v1: LossBasedBandwidthEstimation::new( - field_trials.loss_based_control.clone(), - ), loss_based_bandwidth_estimator_v2, loss_based_state: LossBasedState::DelayBasedEstimate, - disable_receiver_limit_caps_only: field_trials.receiver_limit_caps_only, - acknowledged_rate: None, - field_trials, - } - } - - pub fn on_route_change(&mut self) { - self.lost_packets_since_last_loss_update = 0; - self.expected_packets_since_last_loss_update = 0; - self.current_target = DataRate::zero(); - self.min_bitrate_configured = CONGESTION_CONTROLLER_MIN_BITRATE; - self.max_bitrate_configured = Self::DEFAULT_MAX_BITRATE; - self.last_low_bitrate_log = Timestamp::minus_infinity(); - self.has_decreased_since_last_fraction_loss = false; - self.last_loss_feedback = Timestamp::minus_infinity(); - self.last_loss_packet_report = Timestamp::minus_infinity(); - self.last_fraction_loss = 0; - self.last_logged_fraction_loss = 0; - self.last_round_trip_time = TimeDelta::zero(); - self.receiver_limit = DataRate::plus_infinity(); - self.delay_based_limit = DataRate::plus_infinity(); - self.time_last_decrease = Timestamp::minus_infinity(); - self.first_report_time = Timestamp::minus_infinity(); - self.initially_lost_packets = 0; - self.bitrate_at_2_seconds = DataRate::zero(); - self.last_rtc_event_log = Timestamp::minus_infinity(); - if self.loss_based_bandwidth_estimator_v2_enabled() - && self.loss_based_bandwidth_estimator_v2.use_in_start_phase() - { - self.loss_based_bandwidth_estimator_v2 = - LossBasedBweV2::new(self.field_trials.loss_based_bwe_v2.clone()); } } pub fn target_rate(&self) -> DataRate { - let mut target: DataRate = self.current_target; - if !self.disable_receiver_limit_caps_only { - target = std::cmp::min(target, self.receiver_limit); - } - std::cmp::max(self.min_bitrate_configured, target) + std::cmp::max(self.min_bitrate_configured, self.current_target) } pub fn loss_based_state(&self) -> LossBasedState { self.loss_based_state @@ -347,17 +154,10 @@ impl SendSideBandwidthEstimation { self.rtt_backoff.is_rtt_above_limit() } - pub fn fraction_loss(&self) -> u8 { - self.last_fraction_loss - } - pub fn round_trip_time(&self) -> TimeDelta { self.last_round_trip_time } - pub fn get_estimated_link_capacity(&self) -> DataRate { - self.link_capacity.estimate() - } // Call periodically to update estimate. pub fn update_estimate(&mut self, at_time: Timestamp) { if self.rtt_backoff.is_rtt_above_limit() { @@ -369,7 +169,6 @@ impl SendSideBandwidthEstimation { self.current_target * self.rtt_backoff.drop_fraction, self.rtt_backoff.bandwidth_floor, ); - self.link_capacity.on_rtt_backoff(new_bitrate, at_time); self.update_target_bitrate(new_bitrate, at_time); return; } @@ -378,56 +177,15 @@ impl SendSideBandwidthEstimation { return; } - // We trust the REMB and/or delay-based estimate during the first 2 seconds if - // we haven't had any packet loss reported, to allow startup bitrate probing. - if self.last_fraction_loss == 0 - && self.is_in_start_phase(at_time) - && !self - .loss_based_bandwidth_estimator_v2 - .ready_to_use_in_start_phase() + // 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 { - let mut new_bitrate: DataRate = self.current_target; - // TODO(srte): We should not allow the new_bitrate to be larger than the - // receiver limit here. - if self.receiver_limit.is_finite() { - new_bitrate = std::cmp::max(self.receiver_limit, new_bitrate); - } - if self.delay_based_limit.is_finite() { - new_bitrate = std::cmp::max(self.delay_based_limit, new_bitrate); - } - if self.loss_based_bandwidth_estimator_v1_enabled() { - self.loss_based_bandwidth_estimator_v1 - .initialize(new_bitrate); - } - - if new_bitrate != self.current_target { - self.min_bitrate_history.clear(); - if self.loss_based_bandwidth_estimator_v1_enabled() { - self.min_bitrate_history.push_back((at_time, new_bitrate)); - } else { - self.min_bitrate_history - .push_back((at_time, self.current_target)); - } - self.update_target_bitrate(new_bitrate, at_time); - return; - } - } - self.update_min_history(at_time); - if self.last_loss_packet_report.is_infinite() { - // No feedback received. - // TODO(srte): This is likely redundant in most cases. - self.apply_target_limits(at_time); - return; - } - - if self.loss_based_bandwidth_estimator_v1_ready_for_use() { - let new_bitrate: DataRate = self.loss_based_bandwidth_estimator_v1.update( - at_time, - self.min_bitrate_history.front().unwrap().1, - self.delay_based_limit, - self.last_round_trip_time, - ); - self.update_target_bitrate(new_bitrate, at_time); + self.update_target_bitrate(self.delay_based_limit, at_time); return; } @@ -440,65 +198,10 @@ impl SendSideBandwidthEstimation { return; } - let time_since_loss_packet_report: TimeDelta = at_time - self.last_loss_packet_report; - if time_since_loss_packet_report < 1.2 * Self::MAX_RTCP_FEEDBACK_INTERVAL { - // We only care about loss above a given bitrate threshold. - let loss: f32 = self.last_fraction_loss as f32 / 256.0; - // We only make decisions based on loss when the bitrate is above a - // threshold. This is a crude way of handling loss which is uncorrelated - // to congestion. - if self.current_target < self.bitrate_threshold || loss <= self.low_loss_threshold { - // Loss < 2%: Increase rate by 8% of the min bitrate in the last - // BweIncreaseInterval. - // Note that by remembering the bitrate over the last second one can - // rampup up one second faster than if only allowed to start ramping - // at 8% per second rate now. E.g.: - // If sending a constant 100kbps it can rampup immediately to 108kbps - // whenever a receiver report is received with lower packet loss. - // If instead one would do: self.current_bitrate *= 1.08^(delta time), - // it would take over one second since the lower packet loss to achieve - // 108kbps. - let mut new_bitrate: DataRate = DataRate::from_bits_per_sec_float( - self.min_bitrate_history.front().unwrap().1.bps_float() * 1.08 + 0.5, - ); - - // Add 1 kbps extra, just to make sure that we do not get stuck - // (gives a little extra increase at low rates, negligible at higher - // rates). - new_bitrate += DataRate::from_bits_per_sec(1000); - self.update_target_bitrate(new_bitrate, at_time); - return; - } else if self.current_target > self.bitrate_threshold { - if loss <= self.high_loss_threshold { - // Loss between 2% - 10%: Do nothing. - } else { - // Loss > 10%: Limit the rate decreases to once a BweDecreaseInterval - // + rtt. - if !self.has_decreased_since_last_fraction_loss - && (at_time - self.time_last_decrease) - >= (Self::BWE_DECREASE_INTERVAL + self.last_round_trip_time) - { - self.time_last_decrease = at_time; - - // Reduce rate: - // newRate = rate * (1 - 0.5*lossRate); - // where packetLoss = 256*lossRate; - let new_bitrate: DataRate = DataRate::from_bits_per_sec_float( - (self.current_target.bps_float() - * (512.0 - self.last_fraction_loss as f64)) - / 512.0, - ); - self.has_decreased_since_last_fraction_loss = true; - self.update_target_bitrate(new_bitrate, 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) { + 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; } @@ -506,21 +209,8 @@ impl SendSideBandwidthEstimation { self.rtt_backoff .update_propagation_rtt(at_time, propagation_rtt); } - // Call when we receive a RTCP message with TMMBR or REMB. - pub fn update_receiver_estimate(&mut self, at_time: Timestamp, bandwidth: DataRate) { - // TODO(srte): Ensure caller passes plus_infinity, not zero, to represent no - // limitation. - self.receiver_limit = if bandwidth.is_zero() { - DataRate::plus_infinity() - } else { - bandwidth - }; - self.apply_target_limits(at_time); - } // Call when a new delay-based estimate is available. pub fn update_delay_based_estimate(&mut self, at_time: Timestamp, bitrate: DataRate) { - self.link_capacity - .update_delay_based_estimate(at_time, bitrate); // TODO(srte): Ensure caller passes plus_infinity, not zero, to represent no // limitation. self.delay_based_limit = if bitrate.is_zero() { @@ -530,46 +220,7 @@ impl SendSideBandwidthEstimation { }; self.apply_target_limits(at_time); } - // Call when we receive a RTCP message with a ReceiveBlock. - pub fn update_packets_lost( - &mut self, - packets_lost: i64, - number_of_packets: i64, - at_time: Timestamp, - ) { - self.last_loss_feedback = at_time; - if self.first_report_time.is_infinite() { - self.first_report_time = at_time; - } - - // Check sequence number diff and weight loss report - if number_of_packets > 0 { - let expected: i64 = self.expected_packets_since_last_loss_update + number_of_packets; - - // Don't generate a loss rate until it can be based on enough packets. - if expected < Self::LIMIT_NUM_PACKETS { - // Accumulate reports. - self.expected_packets_since_last_loss_update = expected; - self.lost_packets_since_last_loss_update += packets_lost; - return; - } - - self.has_decreased_since_last_fraction_loss = false; - let lost_q8: i64 = - std::cmp::max(self.lost_packets_since_last_loss_update + packets_lost, 0) << 8; - self.last_fraction_loss = (lost_q8 / expected).min(255) as u8; - - // Reset accumulators. - self.lost_packets_since_last_loss_update = 0; - self.expected_packets_since_last_loss_update = 0; - self.last_loss_packet_report = at_time; - self.update_estimate(at_time); - } - } - // Call when we receive a RTCP message with a ReceiveBlock. - pub fn update_rtt(&mut self, rtt: TimeDelta, _at_time: Timestamp) { - // Update RTT if we were able to compute an RTT based on this RTCP. - // FlexFEC doesn't send RTCP SR, which means we won't be able to compute RTT. + pub fn update_rtt(&mut self, rtt: TimeDelta) { if rtt > TimeDelta::zero() { self.last_round_trip_time = rtt; } @@ -583,18 +234,14 @@ impl SendSideBandwidthEstimation { ) { self.set_min_max_bitrate(min_bitrate, max_bitrate); if let Some(send_bitrate) = send_bitrate { - self.link_capacity.on_starting_rate(send_bitrate); self.set_send_bitrate(send_bitrate, at_time); } } pub fn set_send_bitrate(&mut self, bitrate: DataRate, at_time: Timestamp) { - assert!(bitrate > DataRate::zero()); + 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); - // Clear last sent bitrate history so the new value can be used directly - // and not capped. - self.min_bitrate_history.clear(); } 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); @@ -611,21 +258,11 @@ impl SendSideBandwidthEstimation { self.min_bitrate_configured.bps() } - pub fn set_acknowledged_rate( - &mut self, - acknowledged_rate: Option, - at_time: Timestamp, - ) { - self.acknowledged_rate = acknowledged_rate; - let acknowledged_rate = match acknowledged_rate { - Some(rate) => rate, - None => return, + pub fn set_acknowledged_rate(&mut self, acknowledged_rate: Option) { + let Some(acknowledged_rate) = acknowledged_rate else { + return; }; - if self.loss_based_bandwidth_estimator_v1_enabled() { - self.loss_based_bandwidth_estimator_v1 - .update_acknowledged_bitrate(acknowledged_rate, at_time); - } - if self.loss_based_bandwidth_estimator_v2_enabled() { + if self.loss_based_bandwidth_estimator_v2.is_enabled() { self.loss_based_bandwidth_estimator_v2 .set_acknowledged_bitrate(acknowledged_rate); } @@ -637,11 +274,7 @@ impl SendSideBandwidthEstimation { _probe_bitrate: Option, in_alr: bool, ) { - if self.loss_based_bandwidth_estimator_v1_enabled() { - self.loss_based_bandwidth_estimator_v1 - .update_loss_statistics(&report.packet_feedbacks, report.feedback_time); - } - if self.loss_based_bandwidth_estimator_v2_enabled() { + if self.loss_based_bandwidth_estimator_v2.is_enabled() { self.loss_based_bandwidth_estimator_v2 .update_bandwidth_estimate( &report.packet_feedbacks, @@ -651,57 +284,17 @@ impl SendSideBandwidthEstimation { self.update_estimate(report.feedback_time); } } - pub fn pace_at_loss_based_estimate(&self) -> bool { - self.loss_based_bandwidth_estimator_v2_ready_for_use() - && self - .loss_based_bandwidth_estimator_v2 - .pace_at_loss_based_estimate() - } - - fn is_in_start_phase(&self, at_time: Timestamp) -> bool { - self.first_report_time.is_infinite() || at_time - self.first_report_time < Self::START_PHASE - } - - // Updates history of min bitrates. - // After this method returns self.min_bitrate_history.front().second contains the - // min bitrate used during last BweIncreaseIntervalMs. - fn update_min_history(&mut self, at_time: Timestamp) { - // Remove old data points from history. - // Since history precision is in ms, add one so it is able to increase - // bitrate if it is off by as little as 0.5ms. - while !self.min_bitrate_history.is_empty() - && at_time - self.min_bitrate_history.front().unwrap().0 + TimeDelta::from_millis(1) - > Self::BWE_INCREASE_INTERVAL - { - self.min_bitrate_history.pop_front(); - } - - // Typical minimum sliding-window algorithm: Pop values higher than current - // bitrate before pushing it. - while !self.min_bitrate_history.is_empty() - && self.current_target <= self.min_bitrate_history.back().unwrap().1 - { - self.min_bitrate_history.pop_back(); - } - - self.min_bitrate_history - .push_back((at_time, self.current_target)); - } // Gets the upper limit for the target bitrate. This is the minimum of the - // delay based limit, the receiver limit and the loss based controller limit. + // delay based limit and the configured max. fn get_upper_limit(&self) -> DataRate { - let mut upper_limit: DataRate = self.delay_based_limit; - if self.disable_receiver_limit_caps_only { - upper_limit = std::cmp::min(upper_limit, self.receiver_limit); - } - std::cmp::min(upper_limit, self.max_bitrate_configured) + 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::warn!( + tracing::debug!( "Estimated available bandwidth {:?} is below configured min bitrate {:?}.", bitrate, self.min_bitrate_configured @@ -711,7 +304,7 @@ impl SendSideBandwidthEstimation { } // Cap `bitrate` to [self.min_bitrate_configured, max_bitrate_configured_] and - // set `current_bitrate_` to the capped value and updates the event log. + // 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 { @@ -719,8 +312,6 @@ impl SendSideBandwidthEstimation { new_bitrate = self.min_bitrate_configured; } self.current_target = new_bitrate; - self.link_capacity - .on_rate_update(self.acknowledged_rate, self.current_target, at_time); } // Applies lower and upper bounds to the current target rate. // TODO(srte): This seems to be called even when limits haven't changed, that @@ -729,17 +320,6 @@ impl SendSideBandwidthEstimation { self.update_target_bitrate(self.current_target, at_time); } - fn loss_based_bandwidth_estimator_v1_enabled(&self) -> bool { - self.field_trials.loss_based_control.enabled && !self.field_trials.loss_based_bwe_v2.enabled - } - fn loss_based_bandwidth_estimator_v2_enabled(&self) -> bool { - self.field_trials.loss_based_bwe_v2.enabled - } - - fn loss_based_bandwidth_estimator_v1_ready_for_use(&self) -> bool { - self.loss_based_bandwidth_estimator_v1_enabled() - && self.loss_based_bandwidth_estimator_v1.in_use() - } fn loss_based_bandwidth_estimator_v2_ready_for_use(&self) -> bool { self.loss_based_bandwidth_estimator_v2.is_ready() } @@ -749,133 +329,27 @@ impl SendSideBandwidthEstimation { mod test { use super::*; - fn test_probing(use_delay_based: bool) { - let mut bwe = SendSideBandwidthEstimation::new(Default::default()); - let mut now_ms: i64 = 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), - Timestamp::from_millis(now_ms), - ); - - const REMB_BPS: i64 = 1000000; - const SECOND_REMB_BPS: i64 = REMB_BPS + 500000; - - bwe.update_packets_lost( - /*packets_lost=*/ 0, - /*number_of_packets=*/ 1, - Timestamp::from_millis(now_ms), - ); - bwe.update_rtt(TimeDelta::from_millis(50), Timestamp::from_millis(now_ms)); - - // Initial REMB applies immediately. - if use_delay_based { - bwe.update_delay_based_estimate( - Timestamp::from_millis(now_ms), - DataRate::from_bits_per_sec(REMB_BPS), - ); - } else { - bwe.update_receiver_estimate( - Timestamp::from_millis(now_ms), - DataRate::from_bits_per_sec(REMB_BPS), - ); - } - bwe.update_estimate(Timestamp::from_millis(now_ms)); - assert_eq!(REMB_BPS, bwe.target_rate().bps()); - - // Second REMB doesn't apply immediately. - now_ms += 2001; - if use_delay_based { - bwe.update_delay_based_estimate( - Timestamp::from_millis(now_ms), - DataRate::from_bits_per_sec(SECOND_REMB_BPS), - ); - } else { - bwe.update_receiver_estimate( - Timestamp::from_millis(now_ms), - DataRate::from_bits_per_sec(SECOND_REMB_BPS), - ); - } - bwe.update_estimate(Timestamp::from_millis(now_ms)); - assert_eq!(REMB_BPS, bwe.target_rate().bps()); - } - - #[test] - fn initial_remb_with_probing() { - test_probing(false); - } - #[test] fn initial_delay_based_bwe_with_probing() { - test_probing(true); - } - - #[test] - fn doesnt_reapply_bitrate_decrease_without_following_remb() { - let mut bwe = SendSideBandwidthEstimation::new(Default::default()); - const MIN_BITRATE_BPS: i64 = 100000; - const INITIAL_BITRATE_BPS: i64 = 1000000; - let mut now_ms: i64 = 1000; + let mut bwe = SendSideBandwidthEstimation::default(); + let now = Timestamp::from_millis(0); bwe.set_min_max_bitrate( - DataRate::from_bits_per_sec(MIN_BITRATE_BPS), + DataRate::from_bits_per_sec(100000), DataRate::from_bits_per_sec(1500000), ); - bwe.set_send_bitrate( - DataRate::from_bits_per_sec(INITIAL_BITRATE_BPS), - Timestamp::from_millis(now_ms), - ); - - const FRACTION_LOSS: u8 = 128; - const RTT_MS: i64 = 50; - now_ms += 10000; - - assert_eq!(INITIAL_BITRATE_BPS, bwe.target_rate().bps()); - assert_eq!(0, bwe.fraction_loss()); - assert_eq!(0, bwe.round_trip_time().ms()); - - // Signal heavy loss to go down in bitrate. - bwe.update_packets_lost( - /*packets_lost=*/ 50, - /*number_of_packets=*/ 100, - Timestamp::from_millis(now_ms), - ); - bwe.update_rtt( - TimeDelta::from_millis(RTT_MS), - Timestamp::from_millis(now_ms), - ); - - // Trigger an update 2 seconds later to not be rate limited. - now_ms += 1000; - bwe.update_estimate(Timestamp::from_millis(now_ms)); - assert!(bwe.target_rate().bps() < INITIAL_BITRATE_BPS); - // Verify that the obtained bitrate isn't hitting the min bitrate, or this - // test doesn't make sense. If this ever happens, update the thresholds or - // loss rates so that it doesn't hit min bitrate after one bitrate update. - assert!(bwe.target_rate().bps() > MIN_BITRATE_BPS); - assert_eq!(FRACTION_LOSS, bwe.fraction_loss()); - assert_eq!(RTT_MS, bwe.round_trip_time().ms()); - - // Triggering an update shouldn't apply further downgrade nor upgrade since - // there's no intermediate receiver block received indicating whether this is - // currently good or not. - let last_bitrate_bps: i64 = bwe.target_rate().bps(); - // Trigger an update 2 seconds later to not be rate limited (but it still - // shouldn't update). - now_ms += 1000; - bwe.update_estimate(Timestamp::from_millis(now_ms)); + bwe.set_send_bitrate(DataRate::from_bits_per_sec(200000), now); + bwe.update_rtt(TimeDelta::from_millis(50)); - assert_eq!(last_bitrate_bps, bwe.target_rate().bps()); - // The old loss rate should still be applied though. - assert_eq!(FRACTION_LOSS, bwe.fraction_loss()); - assert_eq!(RTT_MS, bwe.round_trip_time().ms()); + // 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::new(Default::default()); + let mut bwe = SendSideBandwidthEstimation::default(); const MIN_BITRATE_BPS: i64 = 10000; const MAX_BITRATE_BPS: i64 = 10000000; const INITIAL_BITRATE_BPS: i64 = 300000; @@ -925,38 +399,9 @@ mod test { assert!(rtt_backoff.rtt_limit.is_plus_infinity()); } - #[test] - fn fraction_loss_is_not_overflowed() { - let mut bwe = SendSideBandwidthEstimation::new(Default::default()); - const MIN_BITRATE_BPS: i64 = 100000; - const INITIAL_BITRATE_BPS: i64 = 1000000; - let mut now_ms: i64 = 1000; - bwe.set_min_max_bitrate( - DataRate::from_bits_per_sec(MIN_BITRATE_BPS), - DataRate::from_bits_per_sec(1500000), - ); - bwe.set_send_bitrate( - DataRate::from_bits_per_sec(INITIAL_BITRATE_BPS), - Timestamp::from_millis(now_ms), - ); - - now_ms += 10000; - - assert_eq!(INITIAL_BITRATE_BPS, bwe.target_rate().bps()); - assert_eq!(0, bwe.fraction_loss()); - - // Signal negative loss. - bwe.update_packets_lost( - /*packets_lost=*/ -1, - /*number_of_packets=*/ 100, - Timestamp::from_millis(now_ms), - ); - assert_eq!(0, bwe.fraction_loss()); - } - #[test] fn rtt_is_above_limit_if_rtt_greater_than_limit() { - let mut bwe = SendSideBandwidthEstimation::new(Default::default()); + let mut bwe = SendSideBandwidthEstimation::default(); const MIN_BITRATE_BPS: i64 = 10000; const MAX_BITRATE_BPS: i64 = 10000000; const INITIAL_BITRATE_BPS: i64 = 300000; @@ -978,7 +423,7 @@ mod test { #[test] fn rtt_is_below_limit_if_rtt_less_than_limit() { - let mut bwe = SendSideBandwidthEstimation::new(Default::default()); + let mut bwe = SendSideBandwidthEstimation::default(); const MIN_BITRATE_BPS: i64 = 10000; const MAX_BITRATE_BPS: i64 = 10000000; const INITIAL_BITRATE_BPS: i64 = 300000; diff --git a/rs/moq-congestion/src/gcc/trendline_estimator.rs b/rs/moq-congestion/src/gcc/trendline_estimator.rs index 1c108d1ec2f..fe500320b9f 100644 --- a/rs/moq-congestion/src/gcc/trendline_estimator.rs +++ b/rs/moq-congestion/src/gcc/trendline_estimator.rs @@ -10,7 +10,7 @@ use std::collections::VecDeque; -use crate::gcc::{api::transport::BandwidthUsage, DelayIncreaseDetectorInterface}; +use crate::gcc::{DelayIncreaseDetectorInterface, api::transport::BandwidthUsage}; // WebRTC-Bwe-TrendlineEstimatorSettings #[derive(Debug, Clone)] @@ -81,6 +81,7 @@ impl Default for TrendlineEstimatorSettings { } } +#[derive(Clone)] pub struct TrendlineEstimator { // Parameters. settings: TrendlineEstimatorSettings, @@ -108,6 +109,7 @@ pub struct TrendlineEstimator { hypothesis: BandwidthUsage, } +#[derive(Clone)] struct PacketTiming { pub arrival_time_ms: f64, pub smoothed_delay_ms: f64, @@ -211,7 +213,10 @@ impl TrendlineEstimator { 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); + 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, diff --git a/rs/moq-congestion/src/lib.rs b/rs/moq-congestion/src/lib.rs index 806ef220381..0ad98b93368 100644 --- a/rs/moq-congestion/src/lib.rs +++ b/rs/moq-congestion/src/lib.rs @@ -13,13 +13,14 @@ use std::sync::Arc; mod bbr3; mod cubic; +mod gcc; mod pacer; mod rate; mod unlimited; -mod gcc; pub use bbr3::{Bbr3, Bbr3Config}; pub use cubic::{Cubic, CubicConfig}; +pub use gcc::{Gcc, GccConfig}; pub use rate::Packet; pub use unlimited::Unlimited; diff --git a/rs/moq-congestion/src/rate.rs b/rs/moq-congestion/src/rate.rs index abb57f61bba..686e132fcb6 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,9 @@ 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, and a cluster's results are kept for a second, far fewer than wrap. + pub(crate) probe_cluster: Option, } /// An empty state, for a controller that keeps none. @@ -42,6 +46,7 @@ impl Default for Packet { lost: 0, tx_in_flight: 0, is_app_limited: false, + probe_cluster: None, } } } @@ -115,6 +120,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, } } From b62e538b940fdb7013089ab7d8fdfb782f42dd80 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 04:21:16 -0800 Subject: [PATCH 05/15] fix(congestion): repay pacing debt instead of forgetting it moq-quic checks a datagram's pace while the packet before it is still being built, asking for both. Under 2 Mbit/s a rate's burst budget is a single packet, so both left on one packet's credit and the second's cost saturated at zero: a rate-paced controller sent at twice its pacing rate. The pacer now carries the overshoot as debt, repaid before new credit counts, bounded to one burst so unpaced loss probes cost no more. Co-Authored-By: Claude Opus 5.5 --- rs/moq-congestion/src/pacer.rs | 77 ++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/rs/moq-congestion/src/pacer.rs b/rs/moq-congestion/src/pacer.rs index b295b739b01..cc26fc07064 100644 --- a/rs/moq-congestion/src/pacer.rs +++ b/rs/moq-congestion/src/pacer.rs @@ -17,6 +17,12 @@ pub(crate) struct Pacer { /// Inputs [`Self::capacity`] was derived from, or `None` if it was derived from a pacing rate last_window_inputs: Option, tokens: u64, + /// Bytes sent past the tokens on hand, repaid before any new credit counts + /// + /// A transport checks the pace of a datagram while the packet before it is still being + /// built, so a burst can release two packets on one packet's credit. Without a debt the + /// second would go free, doubling the rate whenever the burst budget is a single packet. + debt: u64, max_bytes_per_second: Option, prev: Instant, } @@ -45,6 +51,7 @@ impl Pacer { capacity, last_window_inputs: Some(WindowInputs { window, mtu }), tokens: capacity, + debt: 0, max_bytes_per_second, prev: now, } @@ -52,7 +59,26 @@ impl Pacer { /// Record that a packet has been transmitted. pub(crate) fn on_transmit(&mut self, packet_length: u16) { - self.tokens = self.tokens.saturating_sub(packet_length.into()) + let bytes = u64::from(packet_length); + match self.tokens.checked_sub(bytes) { + Some(tokens) => self.tokens = tokens, + None => { + // Bounded, so packets sent unpaced, as loss probes are, cost one burst at most + let debt = self.debt + bytes - self.tokens; + self.debt = debt.min(self.capacity.max(bytes)); + self.tokens = 0; + } + } + } + + /// Adds `earned` bytes of credit, repaying any debt first + fn refill(&mut self, earned: u64) { + let repaid = earned.min(self.debt); + self.debt -= repaid; + self.tokens = self + .tokens + .saturating_add(earned - repaid) + .min(self.capacity); } /// Return how long we need to wait before sending `bytes_to_send` @@ -94,7 +120,7 @@ impl Pacer { } // if we can already send a packet, there is no need for delay - if self.tokens >= bytes_to_send { + if self.debt == 0 && self.tokens >= bytes_to_send { return None; } @@ -113,7 +139,7 @@ impl Pacer { let elapsed_rtts = time_elapsed.as_secs_f64() / smoothed_rtt.as_secs_f64(); let new_tokens = (window as f64 * 1.25 * elapsed_rtts).round() as u64; - self.tokens = self.tokens.saturating_add(new_tokens).min(self.capacity); + self.refill(new_tokens); // In the unlikely event that we're getting polled faster than tokens are generated, ensure // that `elapsed_rtts` can grow until we make progress. @@ -122,12 +148,12 @@ impl Pacer { } // if we can already send a packet, there is no need for delay - if self.tokens >= bytes_to_send { + if self.debt == 0 && self.tokens >= bytes_to_send { return None; } let unscaled_delay = smoothed_rtt - .checked_mul((bytes_to_send.max(self.capacity) - self.tokens) as _) + .checked_mul((bytes_to_send.max(self.capacity) - self.tokens + self.debt) as _) .unwrap_or(Duration::MAX) / window; @@ -175,14 +201,14 @@ impl Pacer { // pay for a single byte is carried over rather than discarded. Without this, a slow // rate polled frequently would never accumulate anything. if new_tokens > 0 { - self.tokens = self.tokens.saturating_add(new_tokens).min(self.capacity); + self.refill(new_tokens); self.prev = now; } // Capped at the burst budget so that a `bytes_to_send` exceeding the whole bucket is // still released eventually, rather than waiting for a level the bucket never reaches. let target = Ord::min(bytes_to_send, self.capacity); - if self.tokens >= target { + if self.debt == 0 && self.tokens >= target { return None; } @@ -190,7 +216,7 @@ impl Pacer { // the same interval on every poll and never retire, stalling the connection. // Round up, so the wait always earns the whole shortfall. At a high rate it can be under // a nanosecond, and truncating that to zero would re-arm the timer at `now` forever. - let deficit = target - self.tokens; + let deficit = target.saturating_sub(self.tokens) + self.debt; let nanos = (u128::from(deficit) * 1_000_000_000).div_ceil(u128::from(rate)); Some(now + Duration::from_nanos(u64::try_from(nanos).unwrap_or(u64::MAX))) } @@ -704,6 +730,41 @@ mod tests { ); } + /// A transport checks a datagram's pace while the packet before it is unfinished, asking for + /// both. Under 2 Mbit/s the burst budget is one packet, so both leave on its credit, and the + /// second is repaid before the next leaves rather than going free at twice the rate. + #[test] + fn repays_a_packet_sent_past_the_burst_budget() { + // 100 KB/s, whose 10ms burst budget is under one packet + const RATE: u64 = 100_000; + let mtu = 1200; + let rtt = Duration::from_millis(50); + let now = Instant::now(); + let metrics = paced_metrics(1_000_000, RATE); + let mut pacer = Pacer::new(rtt, 1_000_000, mtu, None, now); + assert_eq!(pacer.delay(rtt, u64::from(mtu), mtu, now, metrics), None); + assert_eq!(pacer.capacity, u64::from(mtu)); + + let mut now = now; + let mut sent = 0u64; + let start = now; + while now - start < Duration::from_secs(1) { + match pacer.delay(rtt, 2 * u64::from(mtu), mtu, now, metrics) { + Some(resume) => now = resume, + None => { + pacer.on_transmit(mtu); + pacer.on_transmit(mtu); + sent += 2 * u64::from(mtu); + } + } + } + // One packet of credit on hand at the start, then the rate + assert!( + sent <= RATE + 2 * u64::from(mtu), + "sent {sent} bytes in a second at {RATE} B/s" + ); + } + #[test] fn burst_is_bounded_by_the_target_interval() { let window = 2_000_000; From beefdb356e2fcee475ee831a21951b38f9dfe3ab Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 04:21:16 -0800 Subject: [PATCH 06/15] feat(congestion): GCC probing, network availability, and mocked-time tests - The network becomes available to GoogCC at the first ACK, and a probe cluster starts with a large enough sent packet, so the initial probes pace data rather than the handshake. - A probe cluster the sender runs out of data for restarts under a new id with its next packet, since only the transport's duplicates can pad it; ALR probing is on so an application-limited sender probes. - Receive times map through i128 and clamp within an hour of the local clock, so no peer value overflows GoogCC's units. - gcc::Config builds it, namespaced as the congestion API audit settled. - Unit tests over a simulated uplink on mocked time, and a nightly measurement of a stopped rung under native probing. Co-Authored-By: Claude Opus 5.5 --- rs/moq-congestion/src/gcc/delay_based_bwe.rs | 2 +- rs/moq-congestion/src/gcc/mod.rs | 106 +++--- .../src/gcc/pacing/bitrate_prober.rs | 31 ++ rs/moq-congestion/src/gcc/tests.rs | 356 ++++++++++++++++++ rs/moq-congestion/src/lib.rs | 4 +- rs/moq-quic/src/tests/probe.rs | 68 +++- 6 files changed, 512 insertions(+), 55 deletions(-) create mode 100644 rs/moq-congestion/src/gcc/tests.rs diff --git a/rs/moq-congestion/src/gcc/delay_based_bwe.rs b/rs/moq-congestion/src/gcc/delay_based_bwe.rs index aa01d3d6e29..e92d6f4d7d7 100644 --- a/rs/moq-congestion/src/gcc/delay_based_bwe.rs +++ b/rs/moq-congestion/src/gcc/delay_based_bwe.rs @@ -96,7 +96,7 @@ impl DelayBasedBwe { // 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::warn!("Very late feedback received."); + tracing::debug!("Very late feedback received."); return DelayBasedBweResult::default(); } diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 1c17bae51ec..3d89f255c97 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -1,4 +1,22 @@ -//! libwebrtc's Google Congestion Control, vendored from kixelated/goog_cc. +//! 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 ([`experiments::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)] @@ -49,6 +67,9 @@ use trendline_estimator::*; use goog_cc_network_control::{GoogCcNetworkController, NetworkControllerConfig}; +#[cfg(test)] +mod tests; + use std::{any::Any, collections::VecDeque, num::NonZeroU16, sync::Arc, time::Duration}; use kio::clock::Instant; @@ -78,33 +99,18 @@ const MIN_WINDOW: u64 = 2 * 1500; /// `ProbeBitrateEstimator`'s own one second of history const CLUSTER_HISTORY: TimeDelta = TimeDelta::from_seconds(2); -/// Configuration for the [`Gcc`] congestion controller. -#[derive(Debug, Clone)] -pub struct GccConfig { - initial_rate: u64, -} +/// The rate GoogCC starts at and first probes from, WebRTC's default of 300 kbit/s +const INITIAL_RATE: DataRate = DataRate::from_kilobits_per_sec(300); -impl GccConfig { - /// The rate GoogCC starts at and first probes from, in bytes per second. - /// - /// Defaults to WebRTC's 300 kbit/s. - pub fn initial_rate(&mut self, bytes_per_second: u64) -> &mut Self { - self.initial_rate = bytes_per_second; - self - } +/// Configuration for the [`Gcc`] congestion controller, which has no settings yet. +#[derive(Debug, Clone, Default)] +pub struct Config { + _private: (), } -impl Default for GccConfig { - fn default() -> Self { - Self { - initial_rate: 300_000 / 8, - } - } -} - -impl ControllerFactory for GccConfig { +impl ControllerFactory for Config { fn build(self: Arc, init: Init) -> Box { - Box::new(Gcc::new(&self, init)) + Box::new(Gcc::new(init)) } } @@ -135,10 +141,14 @@ pub struct Gcc { /// 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, + receive_basis: Option, /// Recently sent probe clusters, oldest first clusters: VecDeque, /// Reused for each ACK's and loss's feedback @@ -146,9 +156,9 @@ pub struct Gcc { } impl Gcc { - fn new(config: &GccConfig, init: Init) -> Self { + fn new(init: Init) -> Self { let now = timestamp(init.now); - let starting_rate = DataRate::from_bytes_per_sec(config.initial_rate.max(1) as i64); + let starting_rate = INITIAL_RATE; let cc = GoogCcNetworkController::new(NetworkControllerConfig { constraints: TargetRateConstraints { at_time: now, @@ -181,20 +191,16 @@ impl Gcc { init.now, ), mtu: init.mtu, - pacing_rate: config.initial_rate.max(1), - target_rate: config.initial_rate, + 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(), feedback: TransportPacketsFeedback::default(), }; - let update = gcc.cc.on_network_availability(NetworkAvailability { - at_time: now, - network_available: true, - }); - gcc.apply(update); gcc.process(init.now); gcc } @@ -230,13 +236,15 @@ impl Gcc { /// 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); - let received = i64::try_from(received.as_micros()).unwrap_or(i64::MAX); + 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(|| now.us().saturating_sub(received)); - let mapped = Timestamp::from_micros(received.saturating_add(basis)); - mapped.clamp(now - RECEIVE_SKEW, now + RECEIVE_SKEW) + .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 pacing info of the probe cluster `id` was sent in. @@ -287,6 +295,10 @@ impl Controller for Gcc { 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); @@ -325,6 +337,14 @@ impl Controller for Gcc { 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( @@ -380,6 +400,11 @@ impl Controller for Gcc { 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() } @@ -387,9 +412,6 @@ impl Controller for Gcc { fn pace(&mut self, now: Instant, bytes: u64) -> Option { self.process(now); let at = timestamp(now); - // A packet is ready, which starts a pending probe cluster - self.prober - .on_incoming_packet(DataSize::from_bytes(bytes.min(i64::MAX as u64) as i64)); if self.prober.current_cluster(at).is_some() { let next = self.prober.next_probe_time(); if next <= at { diff --git a/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs b/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs index 4639a574d7e..7a4018ec75e 100644 --- a/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs +++ b/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs @@ -59,6 +59,9 @@ pub struct BitrateProber { max_probe_delay: TimeDelta, min_packet_size: DataSize, + + /// Clusters restarted so far, numbering their new ids + restarts: i32, } impl Default for BitrateProber { @@ -69,6 +72,7 @@ impl Default for BitrateProber { next_probe_time: Timestamp::plus_infinity(), max_probe_delay: TimeDelta::from_millis(10), min_packet_size: DataSize::from_bytes(200), + restarts: 0, } } } @@ -76,6 +80,8 @@ impl Default for BitrateProber { 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 + 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 @@ -219,6 +225,31 @@ impl BitrateProber { } } + /// 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(); diff --git a/rs/moq-congestion/src/gcc/tests.rs b/rs/moq-congestion/src/gcc/tests.rs new file mode 100644 index 00000000000..0f8a60e8310 --- /dev/null +++ b/rs/moq-congestion/src/gcc/tests.rs @@ -0,0 +1,356 @@ +//! [`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" + ); +} + +/// An application-limited sender gets probe clusters to fill: duplicates filling them carry the +/// estimate far past the media's rate, which GoogCC's increase alone holds within 1.5x of it +#[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!( + alone.estimate() <= MEDIA * 2, + "estimate {} B/s from {MEDIA} B/s of media alone", + alone.estimate() + ); + assert!( + filled.estimate() >= 4 * MEDIA, + "estimate {} B/s filling the probes, {} B/s without", + filled.estimate(), + alone.estimate() + ); +} + +/// 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/lib.rs b/rs/moq-congestion/src/lib.rs index 0ad98b93368..4635bb4e6f4 100644 --- a/rs/moq-congestion/src/lib.rs +++ b/rs/moq-congestion/src/lib.rs @@ -13,14 +13,14 @@ use std::sync::Arc; mod bbr3; mod cubic; -mod gcc; +pub mod gcc; mod pacer; mod rate; mod unlimited; pub use bbr3::{Bbr3, Bbr3Config}; pub use cubic::{Cubic, CubicConfig}; -pub use gcc::{Gcc, GccConfig}; +pub use gcc::Gcc; pub use rate::Packet; pub use unlimited::Unlimited; diff --git a/rs/moq-quic/src/tests/probe.rs b/rs/moq-quic/src/tests/probe.rs index 3937ef7cbe6..5499b77ce25 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::{Bbr3, CubicConfig}; +use crate::congestion::{Bbr3, CubicConfig, 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, } } } @@ -267,6 +270,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); @@ -370,15 +377,11 @@ fn run(scenario: &Scenario) -> Outcome { if now >= next_sample { let estimate = conn.stats().path.bandwidth_estimate.unwrap_or(0); outcome.probing += conn.congestion_state().metrics().probe_inflight.is_some() as u64; - let bbr = conn - .congestion_state() - .clone_box() - .into_any() - .downcast::() - .expect("BBR3"); - outcome.probe_rtts += (bbr.probing_rtt() && !probing_rtt) as u64; - probing_rtt = bbr.probing_rtt(); - outcome.min_rtt = outcome.min_rtt.max(bbr.min_rtt()); + if let Ok(bbr) = conn.congestion_state().clone_box().into_any().downcast::() { + outcome.probe_rtts += (bbr.probing_rtt() && !probing_rtt) as u64; + probing_rtt = bbr.probing_rtt(); + outcome.min_rtt = outcome.min_rtt.max(bbr.min_rtt()); + } let read: u64 = readings.values().map(|r| r.read).sum(); outcome.unread = outcome.unread.max(outcome.unique - read); outcome.samples.push(Sample { @@ -692,6 +695,40 @@ fn rises_to_target() { rises(&compare("rises", scenario, GATE), Duration::from_secs(5)); } +/// The same under GCC, which probes every five seconds from the application-limited region at +/// twice its estimate, and reads a probe at the slower of its send and receive rates. The +/// duplicates filling a probe leave no faster than the target in all, so past a frame's media +/// they trail the cluster and the reading falls short of the target. This measures how far the +/// estimate gets, holding the probe's own budgets, until GCC can raise it to the target. +#[test] +#[ignore = "nightly: just rs quic-sim"] +fn rises_toward_target_under_gcc() { + let _guard = subscribe(); + let scenario = Scenario { + latency: Duration::from_millis(50), + buffer: 170_000, + keyframe: 0, + rates: vec![(Duration::ZERO, 375_000), (Duration::from_secs(5), 1_500_000)], + duration: Duration::from_secs(15), + gcc: true, + ..Scenario::default() + }; + for Trial { + seed, + probing, + baseline, + } in compare("rises gcc", scenario, SEEDS) + { + info!( + seed, + rise = ?probing.rise(Duration::from_secs(5)), + probing = probing.samples.last().expect("sampled").estimate, + baseline = baseline.samples.last().expect("sampled").estimate, + "estimate after the path opened up, in B/s" + ); + } +} + /// 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] @@ -862,6 +899,17 @@ fn steps_keyframes() { falls(&trials, Duration::from_secs(40), 375_000); } +/// Under GCC, as the keyframed steps under BBR3 +#[test] +#[ignore = "nightly: just rs quic-sim"] +fn steps_gcc() { + let _guard = subscribe(); + let scenario = Scenario { gcc: true, ..steps() }; + let trials = compare("steps gcc", scenario, SEEDS); + rises(&trials, Duration::from_secs(20)); + falls(&trials, Duration::from_secs(40), 375_000); +} + #[test] #[ignore = "nightly: just rs quic-sim"] fn steps_constant() { From c581107d04d093bc882d7235d0aa76dc347015be Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 04:23:41 -0800 Subject: [PATCH 07/15] Revert "fix(congestion): repay pacing debt instead of forgetting it" This reverts commit b62e538b940fdb7013089ab7d8fdfb782f42dd80. --- rs/moq-congestion/src/pacer.rs | 77 ++++------------------------------ 1 file changed, 8 insertions(+), 69 deletions(-) diff --git a/rs/moq-congestion/src/pacer.rs b/rs/moq-congestion/src/pacer.rs index cc26fc07064..b295b739b01 100644 --- a/rs/moq-congestion/src/pacer.rs +++ b/rs/moq-congestion/src/pacer.rs @@ -17,12 +17,6 @@ pub(crate) struct Pacer { /// Inputs [`Self::capacity`] was derived from, or `None` if it was derived from a pacing rate last_window_inputs: Option, tokens: u64, - /// Bytes sent past the tokens on hand, repaid before any new credit counts - /// - /// A transport checks the pace of a datagram while the packet before it is still being - /// built, so a burst can release two packets on one packet's credit. Without a debt the - /// second would go free, doubling the rate whenever the burst budget is a single packet. - debt: u64, max_bytes_per_second: Option, prev: Instant, } @@ -51,7 +45,6 @@ impl Pacer { capacity, last_window_inputs: Some(WindowInputs { window, mtu }), tokens: capacity, - debt: 0, max_bytes_per_second, prev: now, } @@ -59,26 +52,7 @@ impl Pacer { /// Record that a packet has been transmitted. pub(crate) fn on_transmit(&mut self, packet_length: u16) { - let bytes = u64::from(packet_length); - match self.tokens.checked_sub(bytes) { - Some(tokens) => self.tokens = tokens, - None => { - // Bounded, so packets sent unpaced, as loss probes are, cost one burst at most - let debt = self.debt + bytes - self.tokens; - self.debt = debt.min(self.capacity.max(bytes)); - self.tokens = 0; - } - } - } - - /// Adds `earned` bytes of credit, repaying any debt first - fn refill(&mut self, earned: u64) { - let repaid = earned.min(self.debt); - self.debt -= repaid; - self.tokens = self - .tokens - .saturating_add(earned - repaid) - .min(self.capacity); + self.tokens = self.tokens.saturating_sub(packet_length.into()) } /// Return how long we need to wait before sending `bytes_to_send` @@ -120,7 +94,7 @@ impl Pacer { } // if we can already send a packet, there is no need for delay - if self.debt == 0 && self.tokens >= bytes_to_send { + if self.tokens >= bytes_to_send { return None; } @@ -139,7 +113,7 @@ impl Pacer { let elapsed_rtts = time_elapsed.as_secs_f64() / smoothed_rtt.as_secs_f64(); let new_tokens = (window as f64 * 1.25 * elapsed_rtts).round() as u64; - self.refill(new_tokens); + self.tokens = self.tokens.saturating_add(new_tokens).min(self.capacity); // In the unlikely event that we're getting polled faster than tokens are generated, ensure // that `elapsed_rtts` can grow until we make progress. @@ -148,12 +122,12 @@ impl Pacer { } // if we can already send a packet, there is no need for delay - if self.debt == 0 && self.tokens >= bytes_to_send { + if self.tokens >= bytes_to_send { return None; } let unscaled_delay = smoothed_rtt - .checked_mul((bytes_to_send.max(self.capacity) - self.tokens + self.debt) as _) + .checked_mul((bytes_to_send.max(self.capacity) - self.tokens) as _) .unwrap_or(Duration::MAX) / window; @@ -201,14 +175,14 @@ impl Pacer { // pay for a single byte is carried over rather than discarded. Without this, a slow // rate polled frequently would never accumulate anything. if new_tokens > 0 { - self.refill(new_tokens); + self.tokens = self.tokens.saturating_add(new_tokens).min(self.capacity); self.prev = now; } // Capped at the burst budget so that a `bytes_to_send` exceeding the whole bucket is // still released eventually, rather than waiting for a level the bucket never reaches. let target = Ord::min(bytes_to_send, self.capacity); - if self.debt == 0 && self.tokens >= target { + if self.tokens >= target { return None; } @@ -216,7 +190,7 @@ impl Pacer { // the same interval on every poll and never retire, stalling the connection. // Round up, so the wait always earns the whole shortfall. At a high rate it can be under // a nanosecond, and truncating that to zero would re-arm the timer at `now` forever. - let deficit = target.saturating_sub(self.tokens) + self.debt; + let deficit = target - self.tokens; let nanos = (u128::from(deficit) * 1_000_000_000).div_ceil(u128::from(rate)); Some(now + Duration::from_nanos(u64::try_from(nanos).unwrap_or(u64::MAX))) } @@ -730,41 +704,6 @@ mod tests { ); } - /// A transport checks a datagram's pace while the packet before it is unfinished, asking for - /// both. Under 2 Mbit/s the burst budget is one packet, so both leave on its credit, and the - /// second is repaid before the next leaves rather than going free at twice the rate. - #[test] - fn repays_a_packet_sent_past_the_burst_budget() { - // 100 KB/s, whose 10ms burst budget is under one packet - const RATE: u64 = 100_000; - let mtu = 1200; - let rtt = Duration::from_millis(50); - let now = Instant::now(); - let metrics = paced_metrics(1_000_000, RATE); - let mut pacer = Pacer::new(rtt, 1_000_000, mtu, None, now); - assert_eq!(pacer.delay(rtt, u64::from(mtu), mtu, now, metrics), None); - assert_eq!(pacer.capacity, u64::from(mtu)); - - let mut now = now; - let mut sent = 0u64; - let start = now; - while now - start < Duration::from_secs(1) { - match pacer.delay(rtt, 2 * u64::from(mtu), mtu, now, metrics) { - Some(resume) => now = resume, - None => { - pacer.on_transmit(mtu); - pacer.on_transmit(mtu); - sent += 2 * u64::from(mtu); - } - } - } - // One packet of credit on hand at the start, then the rate - assert!( - sent <= RATE + 2 * u64::from(mtu), - "sent {sent} bytes in a second at {RATE} B/s" - ); - } - #[test] fn burst_is_bounded_by_the_target_interval() { let window = 2_000_000; From dcac0bb4d474192c70760db74530077400a4d22b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 04:26:48 -0800 Subject: [PATCH 08/15] feat(congestion): bound GCC's bursts by its pacer, document, and benchmark it - burst() is the pacer's budget at the pacing rate, or one packet while a probe cluster runs: moq-quic checks a datagram's pace while the packet before it is still being built, and a longer batch sends a packet the budget never pays for, twice the rate under 2 Mbit/s. - The crate's license gains BSD-3-Clause for the vendored GoogCC; the READMEs list GCC. - A per-packet benchmark beside BBR3's, run by the nightly's benchmark_per_packet filter. - The quest keeps what remains: a stopped rung does not yet recover under GCC's native probing. Co-Authored-By: Claude Opus 5.5 --- quest/m2/quic-realtime/README.md | 2 +- quest/m2/quic-realtime/gcc.md | 48 +++++++-------- rs/moq-congestion/Cargo.toml | 2 +- rs/moq-congestion/README.md | 4 +- rs/moq-congestion/src/gcc/bench.rs | 94 ++++++++++++++++++++++++++++++ rs/moq-congestion/src/gcc/mod.rs | 18 +++++- rs/moq-congestion/src/pacer.rs | 2 +- rs/moq-quic/README.md | 2 +- rs/moq-quic/src/tests/probe.rs | 11 ---- 9 files changed, 142 insertions(+), 41 deletions(-) create mode 100644 rs/moq-congestion/src/gcc/bench.rs diff --git a/quest/m2/quic-realtime/README.md b/quest/m2/quic-realtime/README.md index 2192afeb0b0..b32524355ff 100644 --- a/quest/m2/quic-realtime/README.md +++ b/quest/m2/quic-realtime/README.md @@ -84,7 +84,7 @@ owns that feedback. ## Required -- [GCC candidate](/quest/m2/quic-realtime/gcc.md) - libwebrtc's GoogCC vendored into `moq-congestion` and refreshed to current WebRTC +- [GCC candidate](/quest/m2/quic-realtime/gcc.md) - native probing raises GCC's estimate to the probe target, so a stopped rung recovers under it - [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` - [moqsink encoder feedback](/quest/m2/quic-realtime/moqsink-feedback.md) - GStreamer publishers retune their encoder to the target rate diff --git a/quest/m2/quic-realtime/gcc.md b/quest/m2/quic-realtime/gcc.md index f4cdb97519c..46b7950a2fd 100644 --- a/quest/m2/quic-realtime/gcc.md +++ b/quest/m2/quic-realtime/gcc.md @@ -1,31 +1,31 @@ -# [L] GCC candidate +# [S] 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. +Under `moq_congestion::Gcc`, moq-quic's native probing raises the estimate to +the session's probe target once the path allows it, so a rung stopped for want +of an estimate comes back, as it does under BBR3. ## 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. +GoogCC is vendored in `rs/moq-congestion/src/gcc/`, refreshed to WebRTC +556f7eec27, trimmed to what QUIC has, and driven as a `Controller` from +receive timestamps. Its probe clusters report `probe_inflight`, and its +queuing delay, throughput, and reaction time on the sim replicas of the +cellular presets are in the PR that landed it. -- 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 `ControllerMetrics::{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. +What remains: `tests::probe::rises_toward_target_under_gcc` ends at +3.2 Mbit/s against a 4 Mbit/s target on every seed, while GCC without the +target reaches 4.9 Mbit/s. GoogCC probes at twice its estimate and reads a +probe at the slower of its send and receive rates, while the transport sends +the duplicates filling a probe at no more than the target in all, so past a +frame's media they trail the cluster and the reading falls short. Which side +changes is undecided: the controller learning the target (GoogCC's +`OnMaxTotalAllocatedBitrate` takes it, and caps its probes at twice it), or +the transport letting a probe's duplicates keep the controller's probe rate. +Once one lands, `rises_toward_target_under_gcc` asserts the rise as +`rises_to_target` does for BBR3. + +## Related + +- [Realtime uplink congestion control](/quest/m2/quic-realtime/README.md) - the bake-off GCC is a candidate in diff --git a/rs/moq-congestion/Cargo.toml b/rs/moq-congestion/Cargo.toml index 579b4a37736..4e0f0df84f9 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 `Bbr3Config::probe_rng_seed`, for moq-quic's deterministic transport tests. diff --git a/rs/moq-congestion/README.md b/rs/moq-congestion/README.md index 898d009fb12..b899ca0bb3c 100644 --- a/rs/moq-congestion/README.md +++ b/rs/moq-congestion/README.md @@ -1,11 +1,13 @@ # 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. 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/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/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 3d89f255c97..6841ef1d763 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -70,13 +70,16 @@ use goog_cc_network_control::{GoogCcNetworkController, NetworkControllerConfig}; #[cfg(test)] mod tests; +#[cfg(test)] +mod bench; + use std::{any::Any, collections::VecDeque, num::NonZeroU16, sync::Arc, time::Duration}; use kio::clock::Instant; use crate::{ Ack, Ce, Controller, ControllerFactory, ControllerMetrics, Init, Loss, Packet, Sent, - pacer::{Pacer, Rate}, + pacer::{self, Pacer, Rate}, }; use api::{ transport::{ @@ -427,6 +430,19 @@ impl Controller for Gcc { 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) -> ControllerMetrics { ControllerMetrics { congestion_window: self.congestion_window(), 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-quic/README.md b/rs/moq-quic/README.md index 0fcebe1fd53..b702151e094 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/tests/probe.rs b/rs/moq-quic/src/tests/probe.rs index 5499b77ce25..9de34351afa 100644 --- a/rs/moq-quic/src/tests/probe.rs +++ b/rs/moq-quic/src/tests/probe.rs @@ -899,17 +899,6 @@ fn steps_keyframes() { falls(&trials, Duration::from_secs(40), 375_000); } -/// Under GCC, as the keyframed steps under BBR3 -#[test] -#[ignore = "nightly: just rs quic-sim"] -fn steps_gcc() { - let _guard = subscribe(); - let scenario = Scenario { gcc: true, ..steps() }; - let trials = compare("steps gcc", scenario, SEEDS); - rises(&trials, Duration::from_secs(20)); - falls(&trials, Duration::from_secs(40), 375_000); -} - #[test] #[ignore = "nightly: just rs quic-sim"] fn steps_constant() { From 37a678439507d84c9ba39ea783d471013ebdccac Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 04:33:39 -0800 Subject: [PATCH 09/15] chore(congestion): satisfy the workspace lints in the vendored GoogCC Allows say why, the unused std::time conversions go, and clippy's mechanical fixes apply. The module docs no longer link a private item. Co-Authored-By: Claude Opus 5.5 --- .../src/gcc/api/units/timestamp.rs | 41 ++------- .../src/gcc/api/units/unit_base.rs | 18 ++-- .../src/gcc/inter_arrival_delta.rs | 5 +- .../src/gcc/loss_based_bwe_v2.rs | 88 +++++++++---------- rs/moq-congestion/src/gcc/mod.rs | 2 +- .../src/gcc/probe_bitrate_estimator.rs | 3 +- rs/moq-congestion/src/gcc/probe_controller.rs | 14 +-- .../src/gcc/robust_throughput_estimator.rs | 1 - .../src/gcc/trendline_estimator.rs | 9 +- 9 files changed, 77 insertions(+), 104 deletions(-) diff --git a/rs/moq-congestion/src/gcc/api/units/timestamp.rs b/rs/moq-congestion/src/gcc/api/units/timestamp.rs index cf85a900f73..5f7dd5f43dd 100644 --- a/rs/moq-congestion/src/gcc/api/units/timestamp.rs +++ b/rs/moq-congestion/src/gcc/api/units/timestamp.rs @@ -14,15 +14,15 @@ //! difference of two Timestamps results in a TimeDelta. super::unit_base!(Timestamp); +use std::fmt; use std::ops::*; -use std::{ - fmt, - sync::LazyLock, - time::{Duration, Instant}, -}; 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; @@ -166,37 +166,6 @@ impl fmt::Debug for Timestamp { } } -// Custom -impl From for Timestamp { - fn from(instant: Instant) -> Self { - let r = *REF; - if let Some(after) = instant.checked_duration_since(r) { - Timestamp::from_micros(after.as_micros() as i64) - } else if let Some(before) = r.checked_duration_since(instant) { - Timestamp::from_micros(-(before.as_micros() as i64)) - } else { - Timestamp::zero() - } - } -} - -impl From for Instant { - fn from(timestamp: Timestamp) -> Self { - let r = *REF; - let duration = Duration::from_micros(timestamp.us().unsigned_abs()); - - if timestamp.0 > 0 { - r.checked_add(duration) - .expect("Timestamp is too far in the future") - } else { - r.checked_sub(duration) - .expect("Timestamp is from before the program started") - } - } -} - -static REF: LazyLock = LazyLock::new(|| std::time::Instant::now()); - #[cfg(test)] mod test { use super::*; diff --git a/rs/moq-congestion/src/gcc/api/units/unit_base.rs b/rs/moq-congestion/src/gcc/api/units/unit_base.rs index 384296ce272..76cb88c44c7 100644 --- a/rs/moq-congestion/src/gcc/api/units/unit_base.rs +++ b/rs/moq-congestion/src/gcc/api/units/unit_base.rs @@ -5,6 +5,10 @@ macro_rules! unit_base { #[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) @@ -59,18 +63,18 @@ macro_rules! unit_base { Self::from_value((self.0 / resolution.0) * resolution.0) } - #[allow(dead_code)] + #[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)] + #[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)] + #[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) } @@ -92,13 +96,13 @@ macro_rules! unit_base { result } - #[allow(dead_code)] + #[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)] + #[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() { @@ -150,7 +154,7 @@ macro_rules! unit_base { self.0 } - #[allow(dead_code)] + #[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 @@ -698,7 +702,7 @@ mod test { #[test] #[should_panic] fn crashes_when_created_from_nan2() { - #[allow(clippy::zero_divided_by_zero)] + #[allow(clippy::zero_divided_by_zero, reason = "builds a NaN")] TestUnit::from_value_float(0.0 / 0.0); } diff --git a/rs/moq-congestion/src/gcc/inter_arrival_delta.rs b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs index 8f05e23da92..11026b60b1e 100644 --- a/rs/moq-congestion/src/gcc/inter_arrival_delta.rs +++ b/rs/moq-congestion/src/gcc/inter_arrival_delta.rs @@ -72,7 +72,10 @@ impl InterArrivalDelta { // `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)] + #[allow( + clippy::too_many_arguments, + reason = "upstream's ComputeDeltas, with its outputs as arguments" + )] pub fn compute_deltas( &mut self, send_time: Timestamp, diff --git a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs index 6b67bf4fb57..ebcd5008046 100644 --- a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs +++ b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs @@ -717,34 +717,35 @@ impl LossBasedBweV2 { /*new_estimate=*/ best_candidate.loss_limited_bandwidth, ); // Bound the best candidate by the acked bitrate. - if let Some(acknowledged_bitrate) = self.acknowledged_bitrate { - if 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); - } + 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); } } } @@ -992,15 +993,14 @@ impl LossBasedBweV2 { bandwidths.push(*candidate_factor * best_estimate.loss_limited_bandwidth); } - if let Some(acknowledged_bitrate) = self.acknowledged_bitrate { - if 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 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() @@ -1220,11 +1220,11 @@ impl LossBasedBweV2 { fn calculate_instant_lower_bound(&mut self) { let mut instance_lower_bound: DataRate = DataRate::zero(); - if let Some(acknowledged_bitrate) = self.acknowledged_bitrate { - if self.config.lower_bound_by_acked_rate_factor > 0.0 { - instance_lower_bound = - self.config.lower_bound_by_acked_rate_factor * acknowledged_bitrate; - } + 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() { @@ -1348,7 +1348,7 @@ impl LossBasedBweV2 { return DataRate::zero(); } sending_rates.sort(); - if sending_rates.len() % 2 == 0 { + if sending_rates.len().is_multiple_of(2) { return (sending_rates[sending_rates.len() / 2 - 1] + sending_rates[sending_rates.len() / 2]) / 2; @@ -1507,7 +1507,6 @@ mod test { ..Default::default() }, receive_time: Timestamp::plus_infinity(), - ..Default::default() }); } @@ -1518,7 +1517,6 @@ mod test { ..Default::default() }, receive_time: Timestamp::plus_infinity(), - ..Default::default() }); enough_feedback diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 6841ef1d763..32d9af5e701 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -12,7 +12,7 @@ //! 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 ([`experiments::FieldTrials`]). +//! 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 diff --git a/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs index dadcf21fec3..a1c04584358 100644 --- a/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs +++ b/rs/moq-congestion/src/gcc/probe_bitrate_estimator.rs @@ -322,10 +322,9 @@ mod test { const MIN_PROBES: i64 = 30; const MIN_BYTES: i64 = 312500; - let mut receive_time: i64 = 5; 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); - receive_time += 1; } assert_relative_eq!( test.measured_data_rate.unwrap().bps_float(), diff --git a/rs/moq-congestion/src/gcc/probe_controller.rs b/rs/moq-congestion/src/gcc/probe_controller.rs index 50d99836b36..63f15692e7e 100644 --- a/rs/moq-congestion/src/gcc/probe_controller.rs +++ b/rs/moq-congestion/src/gcc/probe_controller.rs @@ -574,13 +574,13 @@ impl ProbeController { pending_probes } fn time_for_alr_probe(&self, at_time: Timestamp) -> bool { - if let Some(alr_start_time) = self.alr_start_time { - if 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; - } + 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 } diff --git a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs index 5983bbf1558..5b329f6211c 100644 --- a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs +++ b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs @@ -319,7 +319,6 @@ mod test { ..Default::default() }, receive_time: self.recv_clock, - ..Default::default() }); self.send_clock += packet_size / send_rate; } diff --git a/rs/moq-congestion/src/gcc/trendline_estimator.rs b/rs/moq-congestion/src/gcc/trendline_estimator.rs index fe500320b9f..06de9322db1 100644 --- a/rs/moq-congestion/src/gcc/trendline_estimator.rs +++ b/rs/moq-congestion/src/gcc/trendline_estimator.rs @@ -291,10 +291,11 @@ impl TrendlineEstimator { 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 { - if trend >= 0.0 && trend > cap { - trend = cap; - } + if let Some(cap) = cap + && trend >= 0.0 + && trend > cap + { + trend = cap; } } } From cedc8fed3f51f28ce499af3d72dbb86f70452ead Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 04:39:07 -0800 Subject: [PATCH 10/15] fix(congestion): count packets acknowledged without a receive time as received A packet whose receive time never arrived, as when the ACK carrying it was lost, was left out of GoogCC's feedback once the peer had reported times, while every loss was reported, so the loss-based estimate read too high a loss rate. It now reaches the feedback as received with an ambiguous receive time, upstream's flag for a time not to read, which the delay-based estimate and the RTT skip. Co-Authored-By: Claude Opus 5.5 --- .../src/gcc/api/transport/network_types.rs | 10 ++++- rs/moq-congestion/src/gcc/delay_based_bwe.rs | 1 + .../src/gcc/goog_cc_network_control.rs | 1 + .../src/gcc/loss_based_bwe_v2.rs | 2 + rs/moq-congestion/src/gcc/mod.rs | 42 +++++++++++-------- .../src/gcc/robust_throughput_estimator.rs | 1 + rs/moq-congestion/src/gcc/tests.rs | 35 ++++++++++++++++ 7 files changed, 74 insertions(+), 18 deletions(-) diff --git a/rs/moq-congestion/src/gcc/api/transport/network_types.rs b/rs/moq-congestion/src/gcc/api/transport/network_types.rs index 3e5ed63feea..8ba16240e35 100644 --- a/rs/moq-congestion/src/gcc/api/transport/network_types.rs +++ b/rs/moq-congestion/src/gcc/api/transport/network_types.rs @@ -130,6 +130,9 @@ pub struct RoundTripTimeUpdate { 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 { @@ -143,6 +146,7 @@ impl Default for PacketResult { Self { sent_packet: SentPacket::default(), receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, } } } @@ -165,8 +169,12 @@ impl Default for TransportPacketsFeedback { } 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()) + self.packet_feedbacks + .iter() + .filter(|fb| fb.is_received() && !fb.ambiguous_receive_time) } pub fn sorted_by_receive_time(&self) -> Vec { diff --git a/rs/moq-congestion/src/gcc/delay_based_bwe.rs b/rs/moq-congestion/src/gcc/delay_based_bwe.rs index e92d6f4d7d7..f461af14759 100644 --- a/rs/moq-congestion/src/gcc/delay_based_bwe.rs +++ b/rs/moq-congestion/src/gcc/delay_based_bwe.rs @@ -519,6 +519,7 @@ mod test { let packet = PacketResult { receive_time, + ambiguous_receive_time: false, sent_packet: SentPacket { send_time, size: DataSize::from_bytes(payload_size as _), diff --git a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs index c0d736caad0..c27f72bbcaf 100644 --- a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs +++ b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs @@ -631,6 +631,7 @@ mod test { ..Default::default() }, receive_time: arrival_time, + ambiguous_receive_time: false, } } diff --git a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs index ebcd5008046..92f971fa3a0 100644 --- a/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs +++ b/rs/moq-congestion/src/gcc/loss_based_bwe_v2.rs @@ -1507,6 +1507,7 @@ mod test { ..Default::default() }, receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, }); } @@ -1517,6 +1518,7 @@ mod test { ..Default::default() }, receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, }); enough_feedback diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 32d9af5e701..94186dd1048 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -250,6 +250,29 @@ impl Gcc { 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| { @@ -356,23 +379,7 @@ impl Controller for Gcc { }); } for packet in ack.packets { - // 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 is left out rather than mix the two clocks. - let receive_time = match packet.received { - Some(received) => self.receive_time(received, ack.now), - None if self.receive_basis.is_none() => timestamp(ack.now), - None => continue, - }; - 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, - }; + let result = self.acked(packet, ack.now); self.feedback.packet_feedbacks.push(result); } self.feed(ack.now, ack.in_flight); @@ -390,6 +397,7 @@ impl Controller for Gcc { data_in_flight: DataSize::zero(), }, receive_time: Timestamp::plus_infinity(), + ambiguous_receive_time: false, }; self.feedback.packet_feedbacks.push(result); } diff --git a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs index 5b329f6211c..411cffa0b11 100644 --- a/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs +++ b/rs/moq-congestion/src/gcc/robust_throughput_estimator.rs @@ -319,6 +319,7 @@ mod test { ..Default::default() }, receive_time: self.recv_clock, + ambiguous_receive_time: false, }); self.send_clock += packet_size / send_rate; } diff --git a/rs/moq-congestion/src/gcc/tests.rs b/rs/moq-congestion/src/gcc/tests.rs index 0f8a60e8310..f3e6dee639f 100644 --- a/rs/moq-congestion/src/gcc/tests.rs +++ b/rs/moq-congestion/src/gcc/tests.rs @@ -285,6 +285,41 @@ fn falls_back_to_ack_arrival() { ); } +/// 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 the media's rate, which GoogCC's increase alone holds within 1.5x of it #[test] From b84964e6dc8a06dff7e465496edef754ffbee44c Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 14:13:05 -0800 Subject: [PATCH 11/15] feat(congestion): fill GCC's probe clusters at its own pace GoogCC reads a probe at the slower of its send and receive rates, so the transport holding a cluster's duplicates to the session's probe target capped the reading. A controller now says it paces its own probe (`Controller::paces_probe`), and the transport fills its clusters at that pace, bounded by the cluster's size instead of the target. BBR3 is unchanged. GCC's `probe_inflight` also rounds a cluster's remaining bytes up to whole packets: a cluster short of one packet's bytes held the sender back and was left unfinished, and its stretched reading pulled the estimate down. `rises_toward_target_under_gcc` now ends at 5.0 Mbit/s against the 4 Mbit/s target on every seed, 7.9s after the path opens. Co-Authored-By: Claude Opus 5.5 --- quest/m2/quic-realtime/gcc.md | 31 +++-- rs/moq-congestion/src/gcc/mod.rs | 16 ++- rs/moq-congestion/src/gcc/tests.rs | 44 +++++++ rs/moq-congestion/src/lib.rs | 12 ++ rs/moq-quic/src/connection/bandwidth_probe.rs | 12 +- rs/moq-quic/src/connection/mod.rs | 3 +- rs/moq-quic/src/tests/probe.rs | 109 +++++++++++++++++- 7 files changed, 205 insertions(+), 22 deletions(-) diff --git a/quest/m2/quic-realtime/gcc.md b/quest/m2/quic-realtime/gcc.md index 46b7950a2fd..36b6c41be3d 100644 --- a/quest/m2/quic-realtime/gcc.md +++ b/quest/m2/quic-realtime/gcc.md @@ -14,17 +14,26 @@ receive timestamps. Its probe clusters report `probe_inflight`, and its queuing delay, throughput, and reaction time on the sim replicas of the cellular presets are in the PR that landed it. -What remains: `tests::probe::rises_toward_target_under_gcc` ends at -3.2 Mbit/s against a 4 Mbit/s target on every seed, while GCC without the -target reaches 4.9 Mbit/s. GoogCC probes at twice its estimate and reads a -probe at the slower of its send and receive rates, while the transport sends -the duplicates filling a probe at no more than the target in all, so past a -frame's media they trail the cluster and the reading falls short. Which side -changes is undecided: the controller learning the target (GoogCC's -`OnMaxTotalAllocatedBitrate` takes it, and caps its probes at twice it), or -the transport letting a probe's duplicates keep the controller's probe rate. -Once one lands, `rises_toward_target_under_gcc` asserts the rise as -`rises_to_target` does for BBR3. +Decided 2026-11-01: + +- ✅ The transport fills a probe cluster at the controller's own pace rather + than capping the duplicates at the target, so GoogCC's probe reads the path + (`Controller::paces_probe`). GoogCC's clusters bound the overhead. +- ✅ The shared comparison's sender model is an encoder retuned to + `bandwidth_estimate`, as moq-cli and moq-ffi do, rather than bulk alone. + +What remains: `tests::probe::rises_toward_target_under_gcc` now reaches +5.0 Mbit/s against the 4 Mbit/s target on all 8 seeds, but 7.9s after the +path opens, against BBR3's 5s budget, and the baseline without a target +reaches 4.9 Mbit/s at the same moment. GoogCC probes in the +application-limited region only five seconds after it last probed or +entered that region, and caps every probe at its default 5 Mbit/s maximum, +so with an 8 Mbit/s target the estimate stops at 5.0 Mbit/s. Whether to hand +GoogCC the session's probe target (its `OnMaxTotalAllocatedBitrate` probes +when the allocation rises and caps probes at twice it), raise its maximum, +or accept GoogCC's cadence is undecided. Once that lands, +`rises_toward_target_under_gcc` asserts the rise as `rises_to_target` does +for BBR3, if the rise budget applies to GCC. ## Related diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 94186dd1048..4ad96376604 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -305,10 +305,16 @@ impl Gcc { } /// 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 packets = u64::try_from(packets).unwrap_or(0) * u64::from(self.mtu); - let bytes = u64::try_from(bytes.bytes()).unwrap_or(0); + 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)) } } @@ -465,6 +471,12 @@ impl Controller for Gcc { 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 clone_box(&self) -> Box { Box::new(self.clone()) } diff --git a/rs/moq-congestion/src/gcc/tests.rs b/rs/moq-congestion/src/gcc/tests.rs index f3e6dee639f..86a8e74da8e 100644 --- a/rs/moq-congestion/src/gcc/tests.rs +++ b/rs/moq-congestion/src/gcc/tests.rs @@ -343,6 +343,50 @@ fn filled_probes_raise_the_estimate() { ); } +/// 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"); +} + /// Receive times a peer controls, however far off, regressing, or unreported, never panic or /// leave the estimate outside GoogCC's bounds #[test] diff --git a/rs/moq-congestion/src/lib.rs b/rs/moq-congestion/src/lib.rs index 4635bb4e6f4..1563a3a0b26 100644 --- a/rs/moq-congestion/src/lib.rs +++ b/rs/moq-congestion/src/lib.rs @@ -129,6 +129,18 @@ pub trait Controller: Send + Sync { false } + /// Whether each probe is a cluster the controller sizes and paces itself, so a transport + /// filling [`ControllerMetrics::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 + } + /// Duplicate the controller's state fn clone_box(&self) -> Box; diff --git a/rs/moq-quic/src/connection/bandwidth_probe.rs b/rs/moq-quic/src/connection/bandwidth_probe.rs index 7e5fe79dda7..58324934219 100644 --- a/rs/moq-quic/src/connection/bandwidth_probe.rs +++ b/rs/moq-quic/src/connection/bandwidth_probe.rs @@ -5,7 +5,8 @@ //! 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 +//! 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. //! @@ -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 @@ -194,8 +196,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 e87c0212714..436d79c83b3 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1597,7 +1597,8 @@ impl Connection { /// fills the inflight it allows /// ([`probe_inflight`](crate::congestion::ControllerMetrics::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 + /// measures the path. A controller that paces its own probe cluster + /// ([`Controller::paces_probe`]) gets it filled at its pace instead. 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`]) diff --git a/rs/moq-quic/src/tests/probe.rs b/rs/moq-quic/src/tests/probe.rs index 9de34351afa..8b4b26f85e0 100644 --- a/rs/moq-quic/src/tests/probe.rs +++ b/rs/moq-quic/src/tests/probe.rs @@ -695,11 +695,12 @@ fn rises_to_target() { rises(&compare("rises", scenario, GATE), Duration::from_secs(5)); } -/// The same under GCC, which probes every five seconds from the application-limited region at -/// twice its estimate, and reads a probe at the slower of its send and receive rates. The -/// duplicates filling a probe leave no faster than the target in all, so past a frame's media -/// they trail the cluster and the reading falls short of the target. This measures how far the -/// estimate gets, holding the probe's own budgets, until GCC can raise it to the target. +/// The same under GCC, which probes five seconds after it last probed or entered the +/// application-limited region, at twice its estimate but no more than 5 Mbit/s, and reads a probe +/// at the slower of its send and receive rates, so the duplicates fill each cluster at its pace. +/// The estimate reaches the target only once GCC next probes, later than [`RISE`] here, and never +/// past its 5 Mbit/s cap, so this measures the rise, holding the probe's own budgets, rather than +/// asserting it. #[test] #[ignore = "nightly: just rs quic-sim"] fn rises_toward_target_under_gcc() { @@ -890,6 +891,104 @@ 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::ControllerMetrics { + congestion::ControllerMetrics { + 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()) + } + + fn into_any(self: Box) -> Box { + self + } +} + +impl ControllerFactory 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() { From b29b8f4e9b65a1046241c7ddf5402493a4e2a363 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 14:17:28 -0800 Subject: [PATCH 12/15] quest(gcc): record why GoogCC's probe cap still blocks a high rung Co-Authored-By: Claude Opus 5.5 --- quest/m2/quic-realtime/gcc.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/quest/m2/quic-realtime/gcc.md b/quest/m2/quic-realtime/gcc.md index 36b6c41be3d..547d2a3f273 100644 --- a/quest/m2/quic-realtime/gcc.md +++ b/quest/m2/quic-realtime/gcc.md @@ -28,10 +28,13 @@ path opens, against BBR3's 5s budget, and the baseline without a target reaches 4.9 Mbit/s at the same moment. GoogCC probes in the application-limited region only five seconds after it last probed or entered that region, and caps every probe at its default 5 Mbit/s maximum, -so with an 8 Mbit/s target the estimate stops at 5.0 Mbit/s. Whether to hand -GoogCC the session's probe target (its `OnMaxTotalAllocatedBitrate` probes -when the allocation rises and caps probes at twice it), raise its maximum, -or accept GoogCC's cadence is undecided. Once that lands, +so with an 8 Mbit/s target the estimate stops at 5.0 Mbit/s. Feeding GoogCC +the session's probe target through `OnMaxTotalAllocatedBitrate` alone does +not lift that cap, since it caps probes at the lesser of the maximum and +twice the target, and a 1 Gbit/s maximum broke GoogCC's initial probing. +Whether to configure the maximum (tuning the initial probes), hand GoogCC +the target as well, or accept the cap is undecided, and so is whether BBR3's +5s rise budget applies to GoogCC's 5s probe cadence. Once that lands, `rises_toward_target_under_gcc` asserts the rise as `rises_to_target` does for BBR3, if the rise budget applies to GCC. From 511d75ec7a2add6cf2d406184859d34ce7263666 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 14:49:26 -0800 Subject: [PATCH 13/15] feat(congestion): aim GCC's probes at the session's probe target Settled by the maintainer on 2026-11-01: - GoogCC gets a maximum rate (1 Gbit/s when the transport sets none), so its probes are no longer capped at 5 Mbit/s, and the session's probe target reaches it through a new `Controller::on_probe_target` hook, fed to `OnMaxTotalAllocatedBitrate` through a port of `OnStreamsConfig`. GoogCC then caps its probes at twice the target. - `rises_to_target_under_gcc` asserts that the estimate reaches the target, at 4 and 8 Mbit/s, and reports the rise time rather than holding GCC to BBR3's 5s budget. Lifting the 5 Mbit/s cap exposed a crawling start: from 300 kbit/s the initial probes let 2 Mbit/s of media queue behind the pacer, and its drain triggered an overuse cut to 85% of a startup throughput. GoogCC now starts at 1 Mbit/s, so its initial probes go out at 3 and 6 Mbit/s. moq-quic reports the target on every change and to each controller it builds on migration. The GCC quest is complete, so it is deleted. Co-Authored-By: Claude Opus 5.5 --- quest/m2/quic-realtime/README.md | 1 - quest/m2/quic-realtime/gcc.md | 43 --------- quest/m2/webrtc/README.md | 3 +- .../src/gcc/api/transport/network_types.rs | 1 + .../src/gcc/goog_cc_network_control.rs | 47 +++++++++- rs/moq-congestion/src/gcc/mod.rs | 46 ++++++++-- rs/moq-congestion/src/gcc/tests.rs | 38 ++++++-- rs/moq-congestion/src/lib.rs | 11 +++ rs/moq-quic/src/connection/bandwidth_probe.rs | 11 ++- rs/moq-quic/src/connection/mod.rs | 14 ++- rs/moq-quic/src/tests/probe.rs | 91 +++++++++++-------- 11 files changed, 200 insertions(+), 106 deletions(-) delete mode 100644 quest/m2/quic-realtime/gcc.md diff --git a/quest/m2/quic-realtime/README.md b/quest/m2/quic-realtime/README.md index f1678c6669a..7a3918faa09 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) - native probing raises GCC's estimate to the probe target, so a stopped rung recovers under it - [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 547d2a3f273..00000000000 --- a/quest/m2/quic-realtime/gcc.md +++ /dev/null @@ -1,43 +0,0 @@ -# [S] GCC candidate - -## Goal - -Under `moq_congestion::Gcc`, moq-quic's native probing raises the estimate to -the session's probe target once the path allows it, so a rung stopped for want -of an estimate comes back, as it does under BBR3. - -## Plan - -GoogCC is vendored in `rs/moq-congestion/src/gcc/`, refreshed to WebRTC -556f7eec27, trimmed to what QUIC has, and driven as a `Controller` from -receive timestamps. Its probe clusters report `probe_inflight`, and its -queuing delay, throughput, and reaction time on the sim replicas of the -cellular presets are in the PR that landed it. - -Decided 2026-11-01: - -- ✅ The transport fills a probe cluster at the controller's own pace rather - than capping the duplicates at the target, so GoogCC's probe reads the path - (`Controller::paces_probe`). GoogCC's clusters bound the overhead. -- ✅ The shared comparison's sender model is an encoder retuned to - `bandwidth_estimate`, as moq-cli and moq-ffi do, rather than bulk alone. - -What remains: `tests::probe::rises_toward_target_under_gcc` now reaches -5.0 Mbit/s against the 4 Mbit/s target on all 8 seeds, but 7.9s after the -path opens, against BBR3's 5s budget, and the baseline without a target -reaches 4.9 Mbit/s at the same moment. GoogCC probes in the -application-limited region only five seconds after it last probed or -entered that region, and caps every probe at its default 5 Mbit/s maximum, -so with an 8 Mbit/s target the estimate stops at 5.0 Mbit/s. Feeding GoogCC -the session's probe target through `OnMaxTotalAllocatedBitrate` alone does -not lift that cap, since it caps probes at the lesser of the maximum and -twice the target, and a 1 Gbit/s maximum broke GoogCC's initial probing. -Whether to configure the maximum (tuning the initial probes), hand GoogCC -the target as well, or accept the cap is undecided, and so is whether BBR3's -5s rise budget applies to GoogCC's 5s probe cadence. Once that lands, -`rises_toward_target_under_gcc` asserts the rise as `rises_to_target` does -for BBR3, if the rise budget applies to GCC. - -## Related - -- [Realtime uplink congestion control](/quest/m2/quic-realtime/README.md) - the bake-off GCC is a candidate in 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/src/gcc/api/transport/network_types.rs b/rs/moq-congestion/src/gcc/api/transport/network_types.rs index 8ba16240e35..9e99091042c 100644 --- a/rs/moq-congestion/src/gcc/api/transport/network_types.rs +++ b/rs/moq-congestion/src/gcc/api/transport/network_types.rs @@ -20,6 +20,7 @@ use crate::gcc::api::units::{DataRate, DataSize, TimeDelta, Timestamp}; /// 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 diff --git a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs index c27f72bbcaf..78a1e499781 100644 --- a/rs/moq-congestion/src/gcc/goog_cc_network_control.rs +++ b/rs/moq-congestion/src/gcc/goog_cc_network_control.rs @@ -9,10 +9,11 @@ */ //! Upstream's `NetworkControllerInterface` also takes route changes, REMB, RTCP loss reports, -//! received packets, runtime stream and constraint changes, and a network state estimate. A QUIC -//! sender has none of them: a path change rebuilds the controller, and the constraints and stream -//! settings come once, at construction. What remains is the network becoming available, the -//! process timer, the RTT, each sent packet, and each ACK's feedback. +//! 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; @@ -166,6 +167,44 @@ impl GoogCcNetworkController { } } + 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() { diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 4ad96376604..6b950e65d4f 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -102,8 +102,22 @@ const MIN_WINDOW: u64 = 2 * 1500; /// `ProbeBitrateEstimator`'s own one second of history const CLUSTER_HISTORY: TimeDelta = TimeDelta::from_seconds(2); -/// The rate GoogCC starts at and first probes from, WebRTC's default of 300 kbit/s -const INITIAL_RATE: DataRate = DataRate::from_kilobits_per_sec(300); +/// 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)] @@ -154,6 +168,9 @@ pub struct Gcc { 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, } @@ -166,9 +183,9 @@ impl Gcc { constraints: TargetRateConstraints { at_time: now, min_data_rate: None, - max_data_rate: init - .max_pacing_rate - .map(|rate| DataRate::from_bytes_per_sec(rate.min(i64::MAX as u64) as i64)), + 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 { @@ -202,14 +219,27 @@ impl Gcc { next_process: init.now, receive_basis: None, clusters: VecDeque::new(), + probe_target: None, feedback: TransportPacketsFeedback::default(), }; gcc.process(init.now); gcc } - /// Runs GoogCC's process timer if it is due. + /// 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; } @@ -477,6 +507,10 @@ impl Controller for Gcc { 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()) } diff --git a/rs/moq-congestion/src/gcc/tests.rs b/rs/moq-congestion/src/gcc/tests.rs index 86a8e74da8e..b73b7bc24b8 100644 --- a/rs/moq-congestion/src/gcc/tests.rs +++ b/rs/moq-congestion/src/gcc/tests.rs @@ -321,7 +321,7 @@ fn counts_packets_acknowledged_without_times() { } /// An application-limited sender gets probe clusters to fill: duplicates filling them carry the -/// estimate far past the media's rate, which GoogCC's increase alone holds within 1.5x of it +/// estimate far past what the media alone reads from the same clusters #[test] fn filled_probes_raise_the_estimate() { const MEDIA: u64 = 125_000; @@ -331,18 +331,42 @@ fn filled_probes_raise_the_estimate() { filled.fill_probes = true; filled.run(Duration::from_secs(10)); assert!( - alone.estimate() <= MEDIA * 2, - "estimate {} B/s from {MEDIA} B/s of media alone", - alone.estimate() - ); - assert!( - filled.estimate() >= 4 * MEDIA, + 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] diff --git a/rs/moq-congestion/src/lib.rs b/rs/moq-congestion/src/lib.rs index 1563a3a0b26..ac11fc5980e 100644 --- a/rs/moq-congestion/src/lib.rs +++ b/rs/moq-congestion/src/lib.rs @@ -141,6 +141,17 @@ pub trait Controller: Send + Sync { 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-quic/src/connection/bandwidth_probe.rs b/rs/moq-quic/src/connection/bandwidth_probe.rs index 58324934219..094de2bc47c 100644 --- a/rs/moq-quic/src/connection/bandwidth_probe.rs +++ b/rs/moq-quic/src/connection/bandwidth_probe.rs @@ -6,9 +6,9 @@ //! 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 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. +//! 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 @@ -99,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 { diff --git a/rs/moq-quic/src/connection/mod.rs b/rs/moq-quic/src/connection/mod.rs index 436d79c83b3..fc70125cb22 100644 --- a/rs/moq-quic/src/connection/mod.rs +++ b/rs/moq-quic/src/connection/mod.rs @@ -1598,13 +1598,21 @@ impl Connection { /// ([`probe_inflight`](crate::congestion::ControllerMetrics::probe_inflight)) with duplicates /// of STREAM data still in flight, sending no more than the target in all, so the probe /// measures the path. A controller that paces its own probe cluster - /// ([`Controller::paces_probe`]) gets it filled at its pace instead. Duplicates count against congestion control, as - /// [`PathStats::sent_bandwidth_probe_bytes`]. A reset stream's data is never sent again. An + /// ([`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. @@ -1620,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() { @@ -3371,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 8b4b26f85e0..828242eb49a 100644 --- a/rs/moq-quic/src/tests/probe.rs +++ b/rs/moq-quic/src/tests/probe.rs @@ -173,9 +173,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) } @@ -621,20 +621,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 @@ -695,38 +703,45 @@ fn rises_to_target() { rises(&compare("rises", scenario, GATE), Duration::from_secs(5)); } -/// The same under GCC, which probes five seconds after it last probed or entered the -/// application-limited region, at twice its estimate but no more than 5 Mbit/s, and reads a probe -/// at the slower of its send and receive rates, so the duplicates fill each cluster at its pace. -/// The estimate reaches the target only once GCC next probes, later than [`RISE`] here, and never -/// past its 5 Mbit/s cap, so this measures the rise, holding the probe's own budgets, rather than -/// asserting it. +/// 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_toward_target_under_gcc() { +fn rises_to_target_under_gcc() { let _guard = subscribe(); - let scenario = Scenario { - latency: Duration::from_millis(50), - buffer: 170_000, - keyframe: 0, - rates: vec![(Duration::ZERO, 375_000), (Duration::from_secs(5), 1_500_000)], - duration: Duration::from_secs(15), - gcc: true, - ..Scenario::default() - }; - for Trial { - seed, - probing, - baseline, - } in compare("rises gcc", scenario, SEEDS) - { - info!( + 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, - rise = ?probing.rise(Duration::from_secs(5)), - probing = probing.samples.last().expect("sampled").estimate, - baseline = baseline.samples.last().expect("sampled").estimate, - "estimate after the path opened up, in B/s" - ); + 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); } } @@ -757,7 +772,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"); } From d09d1dcbf0059b39b41ffd9db82f3eae93f6c574 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 14:59:08 -0800 Subject: [PATCH 14/15] fix(congestion): keep GCC's ALR budget refilling between close sends The ALR detector rounded each interval between sends to whole milliseconds, so packets under a millisecond apart never refilled its budget and a fast sender below 80% of the estimate never entered the application-limited region. Subtract the whole-millisecond send times, as upstream's integer times do. Co-Authored-By: Claude Opus 5.5 --- rs/moq-congestion/src/gcc/alr_detector.rs | 26 +++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/rs/moq-congestion/src/gcc/alr_detector.rs b/rs/moq-congestion/src/gcc/alr_detector.rs index 70fa35ed766..e944f3d0ccf 100644 --- a/rs/moq-congestion/src/gcc/alr_detector.rs +++ b/rs/moq-congestion/src/gcc/alr_detector.rs @@ -79,11 +79,13 @@ impl AlrDetector { self.last_send_time = Some(send_time); return; }; - let delta_time = send_time - last_send_time; + // In whole milliseconds of each time, as upstream's `int64_t` send times are, so packets + // under a millisecond apart still add up to the time between them + 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()); + 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() { @@ -112,6 +114,7 @@ impl AlrDetector { #[cfg(test)] mod test { use super::*; + use crate::gcc::api::units::TimeDelta; const ESTIMATED_BITRATE_BPS: i64 = 300000; @@ -341,4 +344,23 @@ mod test { .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() + ); + } } From 88dad74746e22855cc1aeac3abc9efd368891ef3 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 1 Nov 2026 15:23:32 -0800 Subject: [PATCH 15/15] fix(gcc): keep restart cluster ids apart from GoogCC's; say the ALR interval departs from upstream A restarted probe cluster's id, 1<<30 plus its count, wrapped to the same 16-bit short id as GoogCC's cluster 16384 plus that count, so a packet could resolve to the wrong cluster. GoogCC's ids now wrap within the lower half of the short ids and restarts within the upper half. The ALR detector's comment claimed upstream rounds each send time; at 556f7eec27 upstream rounds each interval. Say the departure is deliberate. Co-Authored-By: Claude Opus 5.5 --- rs/moq-congestion/src/gcc/alr_detector.rs | 7 +++-- rs/moq-congestion/src/gcc/mod.rs | 13 ++++++--- .../src/gcc/pacing/bitrate_prober.rs | 2 +- rs/moq-congestion/src/gcc/tests.rs | 27 +++++++++++++++++++ rs/moq-congestion/src/rate.rs | 3 ++- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/rs/moq-congestion/src/gcc/alr_detector.rs b/rs/moq-congestion/src/gcc/alr_detector.rs index e944f3d0ccf..eba328f6dec 100644 --- a/rs/moq-congestion/src/gcc/alr_detector.rs +++ b/rs/moq-congestion/src/gcc/alr_detector.rs @@ -79,8 +79,11 @@ impl AlrDetector { self.last_send_time = Some(send_time); return; }; - // In whole milliseconds of each time, as upstream's `int64_t` send times are, so packets - // under a millisecond apart still add up to the time between them + // 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); diff --git a/rs/moq-congestion/src/gcc/mod.rs b/rs/moq-congestion/src/gcc/mod.rs index 42c26028e18..fd777386cac 100644 --- a/rs/moq-congestion/src/gcc/mod.rs +++ b/rs/moq-congestion/src/gcc/mod.rs @@ -525,9 +525,16 @@ fn size(bytes: u64) -> DataSize { DataSize::from_bytes(bytes.min(i64::MAX as u64 / 2) as i64) } -/// The probe cluster id a [`Packet`] stores: GoogCC's, which counts up from one, wrapped to -/// sixteen bits without zero. +/// 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)?; - NonZeroU16::new(((id - 1) % u32::from(u16::MAX) + 1) as u16) + 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 index 7a4018ec75e..89263d1a3be 100644 --- a/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs +++ b/rs/moq-congestion/src/gcc/pacing/bitrate_prober.rs @@ -81,7 +81,7 @@ 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 - const RESTART_ID_BASE: i32 = 1 << 30; + 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 diff --git a/rs/moq-congestion/src/gcc/tests.rs b/rs/moq-congestion/src/gcc/tests.rs index b73b7bc24b8..e3b87ff6e14 100644 --- a/rs/moq-congestion/src/gcc/tests.rs +++ b/rs/moq-congestion/src/gcc/tests.rs @@ -411,6 +411,33 @@ fn probe_inflight_rounds_up_to_a_packet() { 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] diff --git a/rs/moq-congestion/src/rate.rs b/rs/moq-congestion/src/rate.rs index 686e132fcb6..8dd15e86773 100644 --- a/rs/moq-congestion/src/rate.rs +++ b/rs/moq-congestion/src/rate.rs @@ -32,7 +32,8 @@ pub struct Packet { /// 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, and a cluster's results are kept for a second, far fewer than wrap. + /// 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, }