From f439844e80beb26aec0a8713e82dab1d8c5f3144 Mon Sep 17 00:00:00 2001 From: Hugo Tunius Date: Wed, 30 Oct 2024 10:36:14 +0000 Subject: [PATCH] Guard against NaN results --- src/rtp/bandwidth.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/rtp/bandwidth.rs b/src/rtp/bandwidth.rs index 35656fd3..e90726ce 100644 --- a/src/rtp/bandwidth.rs +++ b/src/rtp/bandwidth.rs @@ -189,8 +189,13 @@ impl Div for DataSize { fn div(self, rhs: Duration) -> Self::Output { let bytes = self.as_bytes_f64(); + let s = rhs.as_secs_f64(); - let bps = (bytes * 8.0) / rhs.as_secs_f64(); + if s == 0.0 { + return Bitrate::ZERO; + } + + let bps = (bytes * 8.0) / s; bps.into() } @@ -201,7 +206,13 @@ impl Div for DataSize { fn div(self, rhs: Bitrate) -> Self::Output { let bits = self.as_bytes_f64() * 8.0; - let seconds = bits / rhs.as_f64(); + let rhs = rhs.as_f64(); + + if rhs == 0.0 { + return Duration::ZERO; + } + + let seconds = bits / rhs; Duration::from_secs_f64(seconds) } @@ -211,6 +222,10 @@ impl Div for Bitrate { type Output = Bitrate; fn div(self, rhs: f64) -> Self::Output { + if rhs == 0.0 { + return Self::ZERO; + } + Self(self.0 / rhs) } }