Skip to content

Commit

Permalink
fix: bitrate calc crash
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Nov 28, 2023
1 parent beda14d commit 2832f26
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Sources/LiveKit/Track/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,24 @@ public extension OutboundRtpStreamStatistics {
guard let previous,
let currentBytesSent = bytesSent,
let previousBytesSent = previous.bytesSent else { return 0 }

// Calculate the difference in seconds, ensuring it's not zero
let secondsDiff = (timestamp - previous.timestamp) / (1000 * 1000)
return UInt64(Double((currentBytesSent - previousBytesSent) * 8) / abs(secondsDiff))
if secondsDiff == 0 {
// Handle the case where secondsDiff is zero to avoid division by zero
return 0
}

// Calculate the rate
let rate = Double((currentBytesSent - previousBytesSent) * 8) / abs(secondsDiff)

// Check if the rate is a finite number before converting to UInt64
if rate.isFinite {
return UInt64(rate)
} else {
// Handle the case where rate is not finite (NaN or infinity)
return 0
}
}
}

Expand Down

0 comments on commit 2832f26

Please sign in to comment.