# Frame-Accurate Video Player & Comment System: Technical Design Document
This document outlines the technical design, mathematical formulas, synchronization loops, and database schemes that guarantee **100% frame-accurate playback, scrubbing, stepping, and comment navigation** in Shumai.
---
## 1. Architectural Overview
Shumai's video player is a decoupled, frame-locked system that coordinates three primary components around a single state coordinator (`useFramePlayer`):
```
┌───────────────────────────┐
│ useFramePlayer Hook │
│ (State: currentFrame) │
└──────┬─────────────▲──────┘
│ │
1. Set currentTime │ │ 2. Sync frame index
(Frame Center Nudge)│ │ (Floor-with-Epsilon)
▼ │
┌────────────────────────────┴──┐
│ Native HTML5 Video Player │
│ (VideoJS Engine Wrapper) │
└───────────────────────────────┘
▲ ▲
│ │
Stepping / │ │ Comment Selection /
Scrubbing │ │ Timestamp Navigation
│ │
┌───────┴──────┐ ┌──────┴───────┐
│ Seekbar │ │ Comments │
│ (Progress) │ │ (Panel/List) │
└──────────────┘ └──────────────┘
```
1.**Native HTML5 Video Player (VideoJS Engine Wrapper):** Manages the underlying browser media stream, buffering, audio clock, and presentation.
2.**Seekbar (Scrubber & Progress Bar):** Visualizes playback progress and maps horizontal screen clicks/drags to playhead percentages.
3.**Comment Area (Sidebar):** Renders comment list, displays timestamp labels, captures drawings, and triggers seeks to target frames.
---
## 2. Core Mathematical Foundations
To achieve frame-accurate synchronization across different browsers (which use floating-point double precision for playhead timestamps), we utilize three key mathematical formulas:
### A. Frame-to-Time Boundary Formula
When generating timestamps for external storage (such as database comments), we calculate the exact **start boundary time** of the frame. This avoids carrying floating-point errors into the database:
### B. Time-to-Frame Conversion (Floor with 0.45 Offset)
When converting a raw player timestamp ($t$) back to an integer frame index, we use a **Floor with 0.45 Offset** formula to absorb sub-millisecond clock drift, compositor presentation offsets, and V-Sync boundaries:
* **Compositor Lag Guard:** Since the browser compositor typically presents a frame slightly before the playhead crosses the exact mathematical start time (due to V-Sync ticks and decoding latency), the browser transitions visually around the half-frame mark. An offset of `0.45` shifts the transition boundary to `0.55` frames, matching user perception.
* **Seek Buffer Guard:** When we seek to the safe center of a frame ($N + 0.5$ frames), using a `0.45` offset yields $N + 0.95$, which floors back to the correct frame index $N$ even if the browser has minor floating-point seek target deviations.
Browsers can occasionally render the previous frame if the playhead lands exactly on a frame boundary due to float rounding. To guarantee that seeking always lands squarely inside the target frame's presentation window, we seek to the **center of the frame**:
1.**`requestVideoFrameCallback` (rVFC):** The primary driver when the video is actively decoding. It fires only when a new video frame is sent to the compositor, updating the UI frame-accurately based on `metadata.mediaTime`.
2.**`requestAnimationFrame` (RAF):** Runs continuously at 60Hz. It acts as the fallback driver.
3.**Stall Detection:** The playhead tracks when the last rVFC callback was fired. If it has been stalled for longer than the **dynamic stall threshold**:
* Restores playback state (`play()` if `wasPlaying` was true) and playback rate.
4. The seek triggered by `player.currentTime(currentT)` fires a `seeked` event, which automatically calls `handleExternalSeeked`, recalculating the frame index and nudging the playhead to the safe frame center on the new media stream.
* **Precision:** The `second` column uses `Decimal(10, 4)` to store sub-millisecond timestamps accurately without rounding or floating-point truncation.