webrtcsink: fix calculation of fec_ratio with multiple encoders

In this context, the bitrate variable is for all encoders, but the
max_bitrate field is per encoder. To calculate a proper FEC ratio, we
need to scale max_bitrate to the number of encoders.

+ Also clamp the fec-percentage that we set on the transceiver for extra
  safety

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/merge_requests/1151>
This commit is contained in:
Mathieu Duponchelle 2023-03-29 18:40:06 +02:00 committed by GStreamer Marge Bot
parent 23c165dee1
commit 15e1844956

View file

@ -2376,19 +2376,21 @@ impl WebRTCSink {
let mut state = element.imp().state.lock().unwrap(); let mut state = element.imp().state.lock().unwrap();
if let Some(session) = state.sessions.get_mut(peer_id) { if let Some(session) = state.sessions.get_mut(peer_id) {
let n_encoders = session.encoders.len();
let fec_ratio = { let fec_ratio = {
if settings.do_fec && bitrate > DO_FEC_THRESHOLD { if settings.do_fec && bitrate > DO_FEC_THRESHOLD {
(bitrate as f64 - DO_FEC_THRESHOLD as f64) (bitrate as f64 - DO_FEC_THRESHOLD as f64)
/ (session.cc_info.max_bitrate as f64 - DO_FEC_THRESHOLD as f64) / ((session.cc_info.max_bitrate as usize * n_encoders) as f64
- DO_FEC_THRESHOLD as f64)
} else { } else {
0f64 0f64
} }
}; };
let fec_percentage = fec_ratio * 50f64; let fec_percentage = fec_ratio * 50f64;
let encoders_bitrate = ((bitrate as f64) let encoders_bitrate =
/ (1. + (fec_percentage / 100.)) ((bitrate as f64) / (1. + (fec_percentage / 100.)) / (n_encoders as f64)) as i32;
/ (session.encoders.len() as f64)) as i32;
if let Some(rtpxsend) = session.rtprtxsend.as_ref() { if let Some(rtpxsend) = session.rtprtxsend.as_ref() {
rtpxsend.set_property("stuffing-kbps", (bitrate as f64 / 1000.) as i32); rtpxsend.set_property("stuffing-kbps", (bitrate as f64 / 1000.) as i32);
@ -2398,7 +2400,7 @@ impl WebRTCSink {
encoder.set_bitrate(element, encoders_bitrate); encoder.set_bitrate(element, encoders_bitrate);
encoder encoder
.transceiver .transceiver
.set_property("fec-percentage", fec_percentage as u32); .set_property("fec-percentage", (fec_percentage as u32).min(100));
} }
} }
} }