fix(webui): fallback to requestAnimationFrame when video track freezes

This commit is contained in:
yiling
2026-06-29 20:16:06 +08:00
parent 14f6752de2
commit 9fd707b23f
2 changed files with 13 additions and 4 deletions
@@ -19,6 +19,7 @@ export function useFramePlayer(
videoRef: React.RefObject<HTMLVideoElement | null>,
frameRate: number,
totalFrames: number,
videoTrackFrames?: number,
): UseFramePlayerResult {
const [currentFrame, setCurrentFrame] = useState<number>(0)
const isSeekingRef = useRef<boolean>(false)
@@ -68,7 +69,10 @@ export function useFramePlayer(
}
const videoWithCallback = video as unknown as HtmlVideoElementWithCallback
if (videoWithCallback.requestVideoFrameCallback) {
const isPastVideoTrack =
videoTrackFrames !== undefined && video.currentTime >= (videoTrackFrames - 1) / frameRate
if (videoWithCallback.requestVideoFrameCallback && !isPastVideoTrack) {
rVfcId = videoWithCallback.requestVideoFrameCallback(
(_now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => {
const frame = Math.round(metadata.mediaTime * frameRate)
@@ -79,7 +83,7 @@ export function useFramePlayer(
},
)
} else {
// Fallback for browsers without requestVideoFrameCallback
// Fallback for browsers without requestVideoFrameCallback or when video track is frozen
const frame = Math.round(video.currentTime * frameRate)
const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
setCurrentFrame(clamped)
@@ -140,7 +144,7 @@ export function useFramePlayer(
cancelAnimationFrame(rafId)
}
}
}, [videoRef.current, frameRate, totalFrames])
}, [videoRef.current, frameRate, totalFrames, videoTrackFrames])
const seekToFrame = useCallback(
async (targetFrame: number) => {
@@ -520,7 +520,12 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
containerDuration - videoDuration > 0.5 * frameDuration
? Math.round(containerDuration * frameRate)
: dbTotalFrames
const { currentFrame, seekToFrame } = useFramePlayer(videoRef, frameRate, totalFrames)
const { currentFrame, seekToFrame } = useFramePlayer(
videoRef,
frameRate,
totalFrames,
dbTotalFrames,
)
const currentTime = currentFrame / frameRate
const duration = totalFrames / frameRate