From c08f24f8dc35a05717197c1c1bed02aa41382273 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 4 Apr 2021 17:59:46 +0000 Subject: [PATCH] Reset send timing if lag becomes too high This happens always if the input stream stops (for example, if the MPD is paused). If the timing is not reset in this case, the program starts spamming the target with UDP packets, resulting in unpredictable animation behaviour. --- src/main.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 0c24719..4578a17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,6 +81,8 @@ fn main() let block_period = Duration::from_nanos((0.95 * (config::SAMPLES_PER_UPDATE as f32) * 1e9 / config::SAMP_RATE) as u64); let send_period = Duration::from_nanos(1000000000 / 60); + let max_lag = Duration::from_millis(1000); + let mut next_block_instant = Instant::now() + block_period; let mut next_send_instant = Instant::now() + send_period; @@ -166,7 +168,13 @@ fn main() Err(e) => panic!(e), } - next_send_instant += send_period; + let now = Instant::now(); + if now - next_send_instant > max_lag { + println!("Warning! Lag exceeds {:?}. Resetting sender timing.", max_lag); + next_send_instant = now + send_period; + } else { + next_send_instant += send_period; + } } let now = Instant::now();