mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-05 15:44:28 +00:00
fix(webui): fallback to requestAnimationFrame when video track freezes
This commit is contained in:
@@ -19,6 +19,7 @@ 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)
|
||||||
@@ -68,7 +69,10 @@ export function useFramePlayer(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const videoWithCallback = video as unknown as HtmlVideoElementWithCallback
|
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(
|
rVfcId = videoWithCallback.requestVideoFrameCallback(
|
||||||
(_now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => {
|
(_now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => {
|
||||||
const frame = Math.round(metadata.mediaTime * frameRate)
|
const frame = Math.round(metadata.mediaTime * frameRate)
|
||||||
@@ -79,7 +83,7 @@ export function useFramePlayer(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
} else {
|
} 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 frame = Math.round(video.currentTime * frameRate)
|
||||||
const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
|
const clamped = Math.max(0, Math.min(frame, totalFrames - 1))
|
||||||
setCurrentFrame(clamped)
|
setCurrentFrame(clamped)
|
||||||
@@ -140,7 +144,7 @@ export function useFramePlayer(
|
|||||||
cancelAnimationFrame(rafId)
|
cancelAnimationFrame(rafId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [videoRef.current, frameRate, totalFrames])
|
}, [videoRef.current, frameRate, totalFrames, videoTrackFrames])
|
||||||
|
|
||||||
const seekToFrame = useCallback(
|
const seekToFrame = useCallback(
|
||||||
async (targetFrame: number) => {
|
async (targetFrame: number) => {
|
||||||
|
|||||||
@@ -520,7 +520,12 @@ 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(videoRef, frameRate, totalFrames)
|
const { currentFrame, seekToFrame } = useFramePlayer(
|
||||||
|
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