mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +00:00
fix(webui): implement dynamic stall threshold for requestVideoFrameCallback
This commit is contained in:
@@ -19,7 +19,6 @@ export function useFramePlayer(
|
|||||||
videoRef: React.RefObject<HTMLVideoElement | null>,
|
videoRef: React.RefObject<HTMLVideoElement | null>,
|
||||||
frameRate: number,
|
frameRate: number,
|
||||||
totalFrames: number,
|
totalFrames: number,
|
||||||
videoTrackFrames?: number,
|
|
||||||
): UseFramePlayerResult {
|
): UseFramePlayerResult {
|
||||||
const [currentFrame, setCurrentFrame] = useState<number>(0)
|
const [currentFrame, setCurrentFrame] = useState<number>(0)
|
||||||
const isSeekingRef = useRef<boolean>(false)
|
const isSeekingRef = useRef<boolean>(false)
|
||||||
@@ -60,6 +59,7 @@ export function useFramePlayer(
|
|||||||
let rVfcId: number | null = null
|
let rVfcId: number | null = null
|
||||||
let rafId: number | null = null
|
let rafId: number | null = null
|
||||||
let active = true
|
let active = true
|
||||||
|
let lastVfcTime = Date.now()
|
||||||
|
|
||||||
const updateFrameLoop = () => {
|
const updateFrameLoop = () => {
|
||||||
if (!active) return
|
if (!active) return
|
||||||
@@ -69,22 +69,35 @@ export function useFramePlayer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const videoWithCallback = video as unknown as HtmlVideoElementWithCallback
|
const videoWithCallback = video as unknown as HtmlVideoElementWithCallback
|
||||||
const isPastVideoTrack =
|
|
||||||
videoTrackFrames !== undefined && video.currentTime >= (videoTrackFrames - 1) / frameRate
|
|
||||||
|
|
||||||
// Drive playhead update using requestAnimationFrame
|
// Calculate dynamic stall threshold (minimum 100ms, or 3 frames of duration)
|
||||||
const frame = Math.round(video.currentTime * frameRate)
|
const stallThreshold = Math.max(100, 3000 / frameRate)
|
||||||
const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
|
const isVfcStalled = Date.now() - lastVfcTime > stallThreshold
|
||||||
setCurrentFrame(clamped)
|
|
||||||
|
|
||||||
// Always schedule next update tick via requestAnimationFrame (avoids deadlocks when compositor freezes)
|
// Print debug log roughly once per second during playback
|
||||||
|
if (Math.round(video.currentTime * frameRate) % 30 === 0) {
|
||||||
|
console.log(
|
||||||
|
`[useFramePlayer] currentTime: ${video.currentTime}, isVfcStalled: ${isVfcStalled}, stallThreshold: ${stallThreshold}, totalFrames: ${totalFrames}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only update playhead via currentTime (RAF) if rVFC has stalled or isn't supported
|
||||||
|
if (!videoWithCallback.requestVideoFrameCallback || isVfcStalled) {
|
||||||
|
const frame = Math.round(video.currentTime * frameRate)
|
||||||
|
const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
|
||||||
|
setCurrentFrame(clamped)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always schedule the next update tick via requestAnimationFrame to keep the loop alive
|
||||||
rafId = requestAnimationFrame(updateFrameLoop)
|
rafId = requestAnimationFrame(updateFrameLoop)
|
||||||
|
|
||||||
// Schedule video frame callback for frame presentation synchronization if active
|
// Schedule video frame callback for compositor-accurate synchronization
|
||||||
if (videoWithCallback.requestVideoFrameCallback && !isPastVideoTrack && rVfcId === null) {
|
if (videoWithCallback.requestVideoFrameCallback && rVfcId === null) {
|
||||||
rVfcId = videoWithCallback.requestVideoFrameCallback(
|
rVfcId = videoWithCallback.requestVideoFrameCallback(
|
||||||
(_now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => {
|
(_now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => {
|
||||||
rVfcId = null
|
rVfcId = null
|
||||||
|
lastVfcTime = Date.now() // Reset stall timer
|
||||||
|
|
||||||
const compositorFrame = Math.round(metadata.mediaTime * frameRate)
|
const compositorFrame = Math.round(metadata.mediaTime * frameRate)
|
||||||
const compositorClamped = Math.max(0, Math.min(compositorFrame, totalFrames - 1))
|
const compositorClamped = Math.max(0, Math.min(compositorFrame, totalFrames - 1))
|
||||||
setCurrentFrame(compositorClamped)
|
setCurrentFrame(compositorClamped)
|
||||||
@@ -146,7 +159,7 @@ export function useFramePlayer(
|
|||||||
cancelAnimationFrame(rafId)
|
cancelAnimationFrame(rafId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [videoRef.current, frameRate, totalFrames, videoTrackFrames])
|
}, [videoRef.current, frameRate, totalFrames])
|
||||||
|
|
||||||
const seekToFrame = useCallback(
|
const seekToFrame = useCallback(
|
||||||
async (targetFrame: number) => {
|
async (targetFrame: number) => {
|
||||||
|
|||||||
@@ -520,12 +520,7 @@ const VideoPlayer: React.FC<VideoPlayerProps> = ({
|
|||||||
containerDuration - videoDuration > 0.5 * frameDuration
|
containerDuration - videoDuration > 0.5 * frameDuration
|
||||||
? Math.round(containerDuration * frameRate)
|
? Math.round(containerDuration * frameRate)
|
||||||
: dbTotalFrames
|
: dbTotalFrames
|
||||||
const { currentFrame, seekToFrame } = useFramePlayer(
|
const { currentFrame, seekToFrame } = useFramePlayer(videoRef, frameRate, totalFrames)
|
||||||
videoRef,
|
|
||||||
frameRate,
|
|
||||||
totalFrames,
|
|
||||||
dbTotalFrames,
|
|
||||||
)
|
|
||||||
|
|
||||||
const currentTime = currentFrame / frameRate
|
const currentTime = currentFrame / frameRate
|
||||||
const duration = totalFrames / frameRate
|
const duration = totalFrames / frameRate
|
||||||
|
|||||||
Reference in New Issue
Block a user