// Shared multi-video sync rules (native-playback and the evidence review page). // Drift beyond SEEK_DRIFT_S is re-seeked; smaller drift is chased by nudging the // playback rate, because every seek re-buffers and a seek per frame starves a // slow link. A route that is seeking or not yet able to play holds every route. export const SEEK_DRIFT_S = .060; export const RATE_DEADBAND_S = .008; export const MAX_RATE_NUDGE = .04; /** Playback rate for a follower that is `error` seconds behind (positive) or ahead (negative). */ export function nudgedRate(error: number, speed: number) { return speed * (Math.abs(error) > RATE_DEADBAND_S ? 1 + Math.max(-MAX_RATE_NUDGE, Math.min(MAX_RATE_NUDGE, error * .8)) : 1); } /** What a follower does about `drift` = target - current seconds. */ export function syncStep(drift: number, speed: number): {seek: boolean; rate: number} { return Math.abs(drift) > SEEK_DRIFT_S ? {seek: true, rate: speed} : {seek: false, rate: nudgedRate(drift, speed)}; } /** A route that cannot present its next frame now; while any recording route does, all routes wait. */ export function mustWait(video: {seeking: boolean; readyState: number}) { return video.seeking || video.readyState < 3; }