record-daemon: refactor and cleanup

This commit is contained in:
2026-03-21 00:05:46 +01:00
parent 1166424c29
commit 03e36d0f1b
12 changed files with 140 additions and 139 deletions

View File

@@ -11,6 +11,8 @@ pub struct EventMapper {
start_time: Option<DateTime<Utc>>,
/// Game start time (from game event).
game_start_time: Option<DateTime<Utc>>,
/// Synchronizer for handling time drift.
synchronizer: EventSynchronizer,
}
impl EventMapper {
@@ -19,12 +21,14 @@ impl EventMapper {
Self {
start_time: None,
game_start_time: None,
synchronizer: EventSynchronizer::new(),
}
}
/// Start the mapper (recording started).
pub fn start(&mut self) {
self.start_time = Some(Utc::now());
self.synchronizer.reset();
debug!("Event mapper started at {:?}", self.start_time);
}
@@ -32,6 +36,7 @@ impl EventMapper {
pub fn stop(&mut self) {
self.start_time = None;
self.game_start_time = None;
self.synchronizer.reset();
debug!("Event mapper stopped");
}
@@ -49,7 +54,11 @@ impl EventMapper {
let video_timestamp = now - start_time;
// Calculate game timestamp if we have game start time
let game_timestamp = self.game_start_time.map(|game_start| now - game_start);
let game_timestamp = self.game_start_time.map(|game_start| {
let raw_ts = now - game_start;
// Apply drift correction if synchronizer has data
self.synchronizer.adjust_game_timestamp(raw_ts)
});
// Update game start time if this is a game start event
// (handled separately in handle_event)
@@ -69,7 +78,14 @@ impl EventMapper {
debug!("Game start time recorded: {:?}", self.game_start_time);
}
self.map_event(event)
let result = self.map_event(event);
// Add sync point if we have both timestamps
if let Some((video_ts, Some(game_ts))) = result {
self.synchronizer.add_sync_point(video_ts, game_ts);
}
result
}
/// Get the current video timestamp.
@@ -79,7 +95,10 @@ impl EventMapper {
/// Get the current game timestamp.
pub fn current_game_timestamp(&self) -> Option<Duration> {
self.game_start_time.map(|start| Utc::now() - start)
self.game_start_time.map(|start| {
let raw_ts = Utc::now() - start;
self.synchronizer.adjust_game_timestamp(raw_ts)
})
}
/// Get the recording duration so far.
@@ -87,10 +106,21 @@ impl EventMapper {
self.current_video_timestamp()
}
/// Check if time drift is within acceptable bounds.
pub fn is_time_sync_healthy(&self) -> bool {
self.synchronizer.is_drift_acceptable()
}
/// Get the current time drift, if any.
pub fn time_drift(&self) -> Option<Duration> {
self.synchronizer.calculate_drift()
}
/// Reset the mapper.
pub fn reset(&mut self) {
self.start_time = None;
self.game_start_time = None;
self.synchronizer.reset();
debug!("Event mapper reset");
}
}

View File

@@ -299,6 +299,7 @@ mod tests {
start_time: Utc::now(),
end_time: Utc::now() + Duration::seconds(60),
duration: Duration::seconds(60),
stats: None,
};
let id = store.add_recording(result).unwrap();