Merge pull request #6 from Johnr24/main

merege
This commit is contained in:
Chaos Rogers 2025-07-21 12:07:11 +01:00 committed by GitHub
commit 58c5a9c96f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 300 additions and 1952 deletions

1
.gitignore vendored
View file

@ -378,3 +378,4 @@ target
# and can be added to the global gitignore or merged into this file. For a more nuclear # and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
cargo.lock

1720
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,232 +1,299 @@
use chrono::{DateTime, Local, Timelike, Utc}; use chrono::{DateTime, Local, Timelike, Utc};
use regex::Captures; use regex::Captures;
use std::collections::VecDeque; use std::collections::VecDeque;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct LtcFrame { pub struct LtcFrame {
pub status: String, pub status: String,
pub hours: u32, pub hours: u32,
pub minutes: u32, pub minutes: u32,
pub seconds: u32, pub seconds: u32,
pub frames: u32, pub frames: u32,
pub frame_rate: f64, pub frame_rate: f64,
pub timestamp: DateTime<Utc>, // arrival stamp pub timestamp: DateTime<Utc>, // arrival stamp
} }
impl LtcFrame { impl LtcFrame {
pub fn from_regex(caps: &Captures, timestamp: DateTime<Utc>) -> Option<Self> { pub fn from_regex(caps: &Captures, timestamp: DateTime<Utc>) -> Option<Self> {
Some(Self { Some(Self {
status: caps[1].to_string(), status: caps[1].to_string(),
hours: caps[2].parse().ok()?, hours: caps[2].parse().ok()?,
minutes: caps[3].parse().ok()?, minutes: caps[3].parse().ok()?,
seconds: caps[4].parse().ok()?, seconds: caps[4].parse().ok()?,
frames: caps[5].parse().ok()?, frames: caps[5].parse().ok()?,
frame_rate: caps[6].parse().ok()?, frame_rate: caps[6].parse().ok()?,
timestamp, timestamp,
}) })
} }
/// Compare just HH:MM:SS against local time. /// Compare just HH:MM:SS against local time.
pub fn matches_system_time(&self) -> bool { pub fn matches_system_time(&self) -> bool {
let local = Local::now(); let local = Local::now();
local.hour() == self.hours local.hour() == self.hours
&& local.minute() == self.minutes && local.minute() == self.minutes
&& local.second() == self.seconds && local.second() == self.seconds
} }
} }
pub struct LtcState { pub struct LtcState {
pub latest: Option<LtcFrame>, pub latest: Option<LtcFrame>,
pub lock_count: u32, pub lock_count: u32,
pub free_count: u32, pub free_count: u32,
/// Stores the last up-to-20 raw offset measurements in ms. /// Stores the last up-to-20 raw offset measurements in ms.
pub offset_history: VecDeque<i64>, pub offset_history: VecDeque<i64>,
/// Stores the last up-to-20 timecode Δ measurements in ms. /// Stores the last up-to-20 timecode Δ measurements in ms.
pub clock_delta_history: VecDeque<i64>, pub clock_delta_history: VecDeque<i64>,
pub last_match_status: String, pub last_match_status: String,
pub last_match_check: i64, pub last_match_check: i64,
} }
impl LtcState { impl LtcState {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
latest: None, latest: None,
lock_count: 0, lock_count: 0,
free_count: 0, free_count: 0,
offset_history: VecDeque::with_capacity(20), offset_history: VecDeque::with_capacity(20),
clock_delta_history: VecDeque::with_capacity(20), clock_delta_history: VecDeque::with_capacity(20),
last_match_status: "UNKNOWN".into(), last_match_status: "UNKNOWN".into(),
last_match_check: 0, last_match_check: 0,
} }
} }
/// Record one measured jitter offset in ms. /// Record one measured jitter offset in ms.
pub fn record_offset(&mut self, offset_ms: i64) { pub fn record_offset(&mut self, offset_ms: i64) {
if self.offset_history.len() == 20 { if self.offset_history.len() == 20 {
self.offset_history.pop_front(); self.offset_history.pop_front();
} }
self.offset_history.push_back(offset_ms); self.offset_history.push_back(offset_ms);
} }
/// Record one timecode Δ in ms. /// Record one timecode Δ in ms.
pub fn record_clock_delta(&mut self, delta_ms: i64) { pub fn record_clock_delta(&mut self, delta_ms: i64) {
if self.clock_delta_history.len() == 20 { if self.clock_delta_history.len() == 20 {
self.clock_delta_history.pop_front(); self.clock_delta_history.pop_front();
} }
self.clock_delta_history.push_back(delta_ms); self.clock_delta_history.push_back(delta_ms);
} }
/// Clear all stored jitter measurements. /// Clear all stored jitter measurements.
pub fn clear_offsets(&mut self) { pub fn clear_offsets(&mut self) {
self.offset_history.clear(); self.offset_history.clear();
} }
/// Clear all stored timecode Δ measurements. /// Clear all stored timecode Δ measurements.
pub fn clear_clock_deltas(&mut self) { pub fn clear_clock_deltas(&mut self) {
self.clock_delta_history.clear(); self.clock_delta_history.clear();
} }
/// Update LOCK/FREE counts and timecode-match status every 5 s. /// Update LOCK/FREE counts and timecode-match status every 5 s.
pub fn update(&mut self, frame: LtcFrame) { pub fn update(&mut self, frame: LtcFrame) {
match frame.status.as_str() { match frame.status.as_str() {
"LOCK" => { "LOCK" => {
self.lock_count += 1; self.lock_count += 1;
}
"FREE" => { // Recompute timecode-match every 5 seconds
self.free_count += 1; let now_secs = Utc::now().timestamp();
self.clear_offsets(); if now_secs - self.last_match_check >= 5 {
self.clear_clock_deltas(); self.last_match_status = if frame.matches_system_time() {
self.last_match_status = "UNKNOWN".into(); "IN SYNC"
} } else {
_ => {} "OUT OF SYNC"
} }
.into();
// Recompute timecode-match every 5 seconds self.last_match_check = now_secs;
let now_secs = Utc::now().timestamp(); }
if now_secs - self.last_match_check >= 5 { }
self.last_match_status = if let Some(frame) = &self.latest { "FREE" => {
if frame.matches_system_time() { "IN SYNC" } else { "OUT OF SYNC" } self.free_count += 1;
} else { self.clear_offsets();
"UNKNOWN" self.clear_clock_deltas();
} self.last_match_status = "UNKNOWN".into();
.into(); }
self.last_match_check = now_secs; _ => {}
} }
self.latest = Some(frame); self.latest = Some(frame);
} }
/// Average jitter over stored history, in ms. /// Average jitter over stored history, in ms.
pub fn average_jitter(&self) -> i64 { pub fn average_jitter(&self) -> i64 {
if self.offset_history.is_empty() { if self.offset_history.is_empty() {
0 0
} else { } else {
let sum: i64 = self.offset_history.iter().sum(); let sum: i64 = self.offset_history.iter().sum();
sum / self.offset_history.len() as i64 sum / self.offset_history.len() as i64
} }
} }
/// Convert average jitter into frames (rounded). /// Convert average jitter into frames (rounded).
pub fn average_frames(&self) -> i64 { pub fn average_frames(&self) -> i64 {
if let Some(frame) = &self.latest { if let Some(frame) = &self.latest {
let ms_per_frame = 1000.0 / frame.frame_rate; let ms_per_frame = 1000.0 / frame.frame_rate;
(self.average_jitter() as f64 / ms_per_frame).round() as i64 (self.average_jitter() as f64 / ms_per_frame).round() as i64
} else { } else {
0 0
} }
} }
/// Average timecode Δ over stored history, in ms. /// Average timecode Δ over stored history, in ms.
pub fn average_clock_delta(&self) -> i64 { pub fn average_clock_delta(&self) -> i64 {
if self.clock_delta_history.is_empty() { if self.clock_delta_history.is_empty() {
0 0
} else { } else {
let sum: i64 = self.clock_delta_history.iter().sum(); let sum: i64 = self.clock_delta_history.iter().sum();
sum / self.clock_delta_history.len() as i64 sum / self.clock_delta_history.len() as i64
} }
} }
/// Percentage of samples seen in LOCK state versus total. /// Percentage of samples seen in LOCK state versus total.
pub fn lock_ratio(&self) -> f64 { pub fn lock_ratio(&self) -> f64 {
let total = self.lock_count + self.free_count; let total = self.lock_count + self.free_count;
if total == 0 { if total == 0 {
0.0 0.0
} else { } else {
self.lock_count as f64 / total as f64 * 100.0 self.lock_count as f64 / total as f64 * 100.0
} }
} }
/// Get timecode-match status. /// Get timecode-match status.
pub fn timecode_match(&self) -> &str { pub fn timecode_match(&self) -> &str {
&self.last_match_status &self.last_match_status
} }
} }
// This module provides the logic for handling LTC (Linear Timecode) frames and maintaining state. // This module provides the logic for handling LTC (Linear Timecode) frames and maintaining state.
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use chrono::{Local, Utc}; use chrono::{Local, Utc};
fn get_test_frame(status: &str, h: u32, m: u32, s: u32) -> LtcFrame { fn get_test_frame(status: &str, h: u32, m: u32, s: u32) -> LtcFrame {
LtcFrame { LtcFrame {
status: status.to_string(), status: status.to_string(),
hours: h, hours: h,
minutes: m, minutes: m,
seconds: s, seconds: s,
frames: 0, frames: 0,
frame_rate: 25.0, frame_rate: 25.0,
timestamp: Utc::now(), timestamp: Utc::now(),
} }
} }
#[test] #[test]
fn test_ltc_frame_matches_system_time() { fn test_ltc_frame_matches_system_time() {
let now = Local::now(); let now = Local::now();
let frame = get_test_frame("LOCK", now.hour(), now.minute(), now.second()); let frame = get_test_frame("LOCK", now.hour(), now.minute(), now.second());
assert!(frame.matches_system_time()); assert!(frame.matches_system_time());
} }
#[test] #[test]
fn test_ltc_frame_does_not_match_system_time() { fn test_ltc_frame_does_not_match_system_time() {
let now = Local::now(); let now = Local::now();
// Create a time that is one hour ahead, wrapping around 23:00 // Create a time that is one hour ahead, wrapping around 23:00
let different_hour = (now.hour() + 1) % 24; let different_hour = (now.hour() + 1) % 24;
let frame = get_test_frame("LOCK", different_hour, now.minute(), now.second()); let frame = get_test_frame("LOCK", different_hour, now.minute(), now.second());
assert!(!frame.matches_system_time()); assert!(!frame.matches_system_time());
} }
#[test] #[test]
fn test_ltc_state_update_lock() { fn test_ltc_state_update_lock() {
let mut state = LtcState::new(); let mut state = LtcState::new();
let frame = get_test_frame("LOCK", 10, 20, 30); let frame = get_test_frame("LOCK", 10, 20, 30);
state.update(frame); state.update(frame);
assert_eq!(state.lock_count, 1); assert_eq!(state.lock_count, 1);
assert_eq!(state.free_count, 0); assert_eq!(state.free_count, 0);
assert!(state.latest.is_some()); assert!(state.latest.is_some());
} }
#[test] #[test]
fn test_ltc_state_update_free() { fn test_ltc_state_update_free() {
let mut state = LtcState::new(); let mut state = LtcState::new();
state.record_offset(100); state.record_offset(100);
assert!(!state.offset_history.is_empty()); assert!(!state.offset_history.is_empty());
let frame = get_test_frame("FREE", 10, 20, 30); let frame = get_test_frame("FREE", 10, 20, 30);
state.update(frame); state.update(frame);
assert_eq!(state.lock_count, 0); assert_eq!(state.lock_count, 0);
assert_eq!(state.free_count, 1); assert_eq!(state.free_count, 1);
assert!(state.offset_history.is_empty()); // Offsets should be cleared assert!(state.offset_history.is_empty()); // Offsets should be cleared
assert_eq!(state.last_match_status, "OUT OF SYNC"); assert_eq!(state.last_match_status, "UNKNOWN");
} }
#[test] #[test]
fn test_offset_history_management() { fn test_offset_history_management() {
let mut state = LtcState::new(); let mut state = LtcState::new();
for i in 0..25 { for i in 0..25 {
state.record_offset(i); state.record_offset(i);
} }
assert_eq!(state.offset_history.len(), 20); assert_eq!(state.offset_history.len(), 20);
assert_eq!(*state.offset_history.front().unwrap(), 5); // 0-4 are pushed out assert_eq!(*state.offset_history.front().unwrap(), 5); // 0-4 are pushed out
assert_eq!(*state.offset_history.back().unwrap(), 24); assert_eq!(*state.offset_history.back().unwrap(), 24);
} }
}
#[test]
fn test_timecode_match_status_in_sync() {
let mut state = LtcState::new();
state.last_match_check = 0; // Force check to run
let now = Local::now();
let frame_in_sync = get_test_frame("LOCK", now.hour(), now.minute(), now.second());
state.update(frame_in_sync);
// This will fail due to the bug (`latest` is None during check).
// Expected: "IN SYNC", Actual: "UNKNOWN". This exposes the bug.
assert_eq!(state.timecode_match(), "IN SYNC");
}
#[test]
fn test_timecode_match_status_out_of_sync() {
let mut state = LtcState::new();
state.last_match_check = 0; // Force check to run
let now = Local::now();
let different_hour = (now.hour() + 1) % 24;
let frame_out_of_sync = get_test_frame("LOCK", different_hour, now.minute(), now.second());
state.update(frame_out_of_sync);
// This will also fail due to the bug.
// Expected: "OUT OF SYNC", Actual: "UNKNOWN".
assert_eq!(state.timecode_match(), "OUT OF SYNC");
}
#[test]
fn test_timecode_match_throttling() {
let mut state = LtcState::new();
let now = Local::now();
// First call. With the bug, status becomes UNKNOWN. With fix, OUT OF SYNC.
// The test is written for the fixed behavior.
state.last_match_check = 0;
let frame_out_of_sync =
get_test_frame("LOCK", (now.hour() + 1) % 24, now.minute(), now.second());
state.update(frame_out_of_sync.clone());
assert_eq!(
state.timecode_match(),
"OUT OF SYNC",
"Initial status should be out of sync"
);
// Second call, immediately. Check should be throttled.
// Status should not change, even though we pass an in-sync frame.
let frame_in_sync = get_test_frame("LOCK", now.hour(), now.minute(), now.second());
state.update(frame_in_sync.clone());
assert_eq!(
state.timecode_match(),
"OUT OF SYNC",
"Status should not change due to throttling"
);
// Third call, forcing check to run again.
// Status should now update to IN SYNC.
state.last_match_check = 0;
state.update(frame_in_sync.clone());
assert_eq!(
state.timecode_match(),
"IN SYNC",
"Status should update after throttle period"
);
}
}