Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

BWE increases slowly when audio packets are sent before video packets. #5

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/bwe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,16 @@ impl<'a> Bwe<'a> {
pub fn set_desired_bitrate(&mut self, desired_bitrate: Bitrate) {
self.0.session.set_bwe_desired_bitrate(desired_bitrate);
}

/// Reset the BWE with a new init_bitrate
///
/// # Example
///
/// This method is useful when you initially start with only an audio stream. In this case, the BWE will report a very low estimated bitrate.
/// Later, when you start a video stream, the estimated bitrate will be affected by the previous low bitrate, resulting in a very low estimated bitrate, which can cause poor video stream quality.
/// To avoid this, you need to warm up the video stream for a while then calling reset with a provided init_bitrate.
///
pub fn reset(&mut self, init_bitrate: Bitrate) {
self.0.session.reset_bwe(init_bitrate);
}
}
8 changes: 4 additions & 4 deletions src/packet/bwe/acked_bitrate_estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ impl AckedBitrateEstimator {
// current estimate. With low values of uncertainty_symmetry_cap_ we add more
// uncertainty to increases than to decreases. For higher values we approach
// symmetry.
let sample_uncertainty =
scale * (estimate_bps - sample_estimate_bps).abs() / (estimate_bps.max(25_000.0));
let sample_uncertainty = scale * (estimate_bps - sample_estimate_bps).abs() / estimate_bps;
let sample_var = sample_uncertainty.powf(2.0);

// Update a bayesian estimate of the rate, weighting it lower if the sample
Expand All @@ -85,6 +84,7 @@ impl AckedBitrateEstimator {
let mut new_estimate = (sample_var * estimate_bps
+ pred_bitrate_estimate_var * sample_estimate_bps)
/ (sample_var + pred_bitrate_estimate_var);

new_estimate = new_estimate.max(ESTIMATE_FLOOR.as_f64());
self.estimate = Some(Bitrate::bps(new_estimate.ceil() as u64));
self.estimate_var =
Expand Down Expand Up @@ -136,7 +136,7 @@ impl AckedBitrateEstimator {

self.sum += packet_size;

estimate.map(|e| (e, false))
estimate.map(|e| (e, is_small))
}
}

Expand Down Expand Up @@ -200,7 +200,7 @@ mod test {

assert_eq!(
estimate.as_u64(),
99530,
108320,
"AckedBitrateEstiamtor should produce the correct bitrate"
);
}
Expand Down
10 changes: 10 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,12 @@ impl Session {
}
}

pub fn reset_bwe(&mut self, init_bitrate: Bitrate) {
if let Some(bwe) = self.bwe.as_mut() {
bwe.reset(init_bitrate);
}
}

pub fn line_count(&self) -> usize {
self.medias.len() + if self.app.is_some() { 1 } else { 0 }
}
Expand Down Expand Up @@ -898,6 +904,10 @@ impl Bwe {
self.bwe.handle_timeout(now);
}

pub fn reset(&mut self, init_bitrate: Bitrate) {
self.bwe = SendSideBandwithEstimator::new(init_bitrate);
}

pub fn update<'t>(
&mut self,
records: impl Iterator<Item = &'t crate::rtp_::TwccSendRecord>,
Expand Down
Loading