use std::time::Duration;
#[derive(Clone, Debug)]
pub struct RtpClock {
rate: u64,
last_pts: Option<Duration>,
extended_ticks: u64,
}
impl RtpClock {
pub fn new(rate: u32) -> Self {
Self {
rate: u64::from(rate.max(1)),
last_pts: None,
extended_ticks: 0,
}
}
pub fn timestamp(&mut self, pts: Duration) -> u32 {
match self.last_pts {
None => {
self.last_pts = Some(pts);
self.extended_ticks = 0;
0
}
Some(last) => {
let delta = pts.saturating_sub(last);
self.last_pts = Some(pts);
let delta_ticks =
(delta.as_nanos() * u128::from(self.rate) / 1_000_000_000u128) as u64;
self.extended_ticks = self.extended_ticks.wrapping_add(delta_ticks);
self.extended_ticks as u32
}
}
}
pub fn timestamp_ticks(&mut self, ticks: u64) -> u32 {
let ticks = ticks.max(self.extended_ticks);
self.extended_ticks = ticks;
ticks as u32
}
pub fn position_ticks(&self) -> u64 {
self.extended_ticks
}
}
pub fn duration_to_ticks(d: Duration, rate: u32) -> u64 {
let rate = u64::from(rate);
d.as_secs().saturating_mul(rate)
+ u64::from(d.subsec_nanos()).saturating_mul(rate) / 1_000_000_000
}
pub fn duration_to_ticks_rounded(d: Duration, rate: u32) -> u64 {
let rate = u64::from(rate);
d.as_secs().saturating_mul(rate)
+ (u64::from(d.subsec_nanos()).saturating_mul(rate) + 500_000_000) / 1_000_000_000
}
pub fn ticks_to_duration(ticks: u64, rate: u32) -> Duration {
let rate = u64::from(rate).max(1);
Duration::new(ticks / rate, ((ticks % rate) * 1_000_000_000 / rate) as u32)
}
pub const VIDEO_CLOCK_RATE: u32 = 90_000;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn first_frame_starts_at_zero_and_advances_at_rate() {
let mut clock = RtpClock::new(90_000);
assert_eq!(clock.timestamp(Duration::ZERO), 0);
assert_eq!(
clock.timestamp(Duration::from_micros(33_333)),
2999 );
assert_eq!(clock.timestamp(Duration::from_micros(66_666)), 5998);
}
#[test]
fn regression_is_clamped() {
let mut clock = RtpClock::new(90_000);
clock.timestamp(Duration::ZERO);
clock.timestamp(Duration::from_millis(40));
assert_eq!(clock.timestamp(Duration::from_millis(40)), 3600);
assert_eq!(clock.timestamp(Duration::from_millis(80)), 7200);
}
#[test]
fn audio_rate_44_1k() {
let mut clock = RtpClock::new(44_100);
clock.timestamp(Duration::ZERO);
let ts = clock.timestamp(Duration::from_micros(23_220));
assert_eq!(ts, 1024);
}
#[test]
fn wraps_at_32_bits_while_staying_monotonic() {
let mut clock = RtpClock::new(90_000);
let mut prev = 0u32;
for frame in 0..2_000_000u64 {
let pts = Duration::from_micros(frame * 33_333);
let ts = clock.timestamp(pts);
assert!(
ts.wrapping_sub(prev) < 1 << 30,
"timestamp jumped: {prev} -> {ts}"
);
prev = ts;
}
assert!(clock.position_ticks() > (1u64 << 32));
}
}
#[cfg(test)]
mod conversion_tests {
use super::*;
#[test]
fn duration_to_ticks_truncates_int_exactly() {
assert_eq!(
duration_to_ticks(Duration::from_micros(33_333), 90_000),
2999
);
assert_eq!(
duration_to_ticks(Duration::from_micros(66_666), 90_000),
5999
);
assert_eq!(duration_to_ticks(Duration::from_secs(2), 90_000), 180_000);
assert_eq!(duration_to_ticks(Duration::from_nanos(1), 44_100), 0);
}
#[test]
fn duration_to_ticks_rounded_half_tick_up() {
assert_eq!(
duration_to_ticks_rounded(Duration::from_micros(33_333), 90_000),
3000
);
assert_eq!(
duration_to_ticks_rounded(Duration::from_millis(40), 90_000),
3600
);
assert_eq!(
duration_to_ticks_rounded(Duration::from_nanos(5_555_555), 90_000),
500
);
}
#[test]
fn ticks_to_duration_round_trips() {
let cases = [
(0u64, 90_000u32),
(1, 90_000),
(2999, 90_000),
(1024, 44_100),
(u32::MAX as u64, 8_000),
];
for (ticks, rate) in cases {
let d = ticks_to_duration(ticks, rate);
let back = duration_to_ticks(d, rate);
assert!(
back == ticks || back + 1 == ticks,
"round trip {ticks}@{rate}: got {back}"
);
}
assert_eq!(ticks_to_duration(90_000, 90_000), Duration::from_secs(1));
assert_eq!(ticks_to_duration(44_100, 44_100), Duration::from_secs(1));
}
#[test]
fn duration_to_ticks_never_loses_a_tick_to_float() {
assert_eq!(
duration_to_ticks(Duration::from_millis(703), 90_000),
63_270
);
for ms in 0..10_000u32 {
let d = Duration::from_millis(u64::from(ms));
let int = duration_to_ticks(d, 90_000);
let float = (d.as_secs_f64() * 90_000.0) as u64;
assert!(
int == float || int == float + 1,
"ms={ms}: int {int} vs float {float}"
);
}
}
}