mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 12:54:55 +00:00
fix(viewer): synchronize 3D turntable playback via frame callback to prevent skipped degrees
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { cleanup, fireEvent, render } from '@testing-library/react'
|
||||
import { act, cleanup, fireEvent, render } from '@testing-library/react'
|
||||
import React, { createRef } from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { ModelViewer } from './model-viewer'
|
||||
@@ -191,4 +191,84 @@ describe('ModelViewer', () => {
|
||||
fireEvent.click(minusBtn)
|
||||
expect(onTimeUpdate).toHaveBeenCalledWith(0)
|
||||
})
|
||||
|
||||
it('updates playback smoothly on each frame via requestVideoFrameCallback without skipping degrees', () => {
|
||||
let frameCallback: ((now: number, metadata: { mediaTime: number }) => void) | null = null
|
||||
const rvfcMock = vi.fn().mockImplementation((cb) => {
|
||||
frameCallback = cb
|
||||
return 101
|
||||
})
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(HTMLVideoElement.prototype as any).requestVideoFrameCallback = rvfcMock
|
||||
|
||||
const onTimeUpdate = vi.fn()
|
||||
const { container, getByTestId } = render(
|
||||
<ModelViewer file={mockAsset} onTimeUpdate={onTimeUpdate} />,
|
||||
)
|
||||
|
||||
const video = container.querySelector('video')
|
||||
expect(video).toBeDefined()
|
||||
|
||||
// Start playing
|
||||
fireEvent.play(video!)
|
||||
expect(rvfcMock).toHaveBeenCalled()
|
||||
|
||||
// Simulate decoded frames arriving sequentially at 6 FPS (every 1/6 second)
|
||||
const degreeReadout = getByTestId('degree-readout')
|
||||
|
||||
// Frame 0: 0.083s -> 0°
|
||||
act(() => {
|
||||
frameCallback!(100, { mediaTime: 0.083 })
|
||||
})
|
||||
expect(degreeReadout.textContent).toContain('0° / 360°')
|
||||
|
||||
// Frame 1: 0.25s -> 15°
|
||||
act(() => {
|
||||
frameCallback!(267, { mediaTime: 0.25 })
|
||||
})
|
||||
expect(degreeReadout.textContent).toContain('15° / 360°')
|
||||
|
||||
// Frame 2: 0.417s -> 30°
|
||||
act(() => {
|
||||
frameCallback!(433, { mediaTime: 0.417 })
|
||||
})
|
||||
expect(degreeReadout.textContent).toContain('30° / 360°')
|
||||
|
||||
// Frame 3: 0.583s -> 45°
|
||||
act(() => {
|
||||
frameCallback!(600, { mediaTime: 0.583 })
|
||||
})
|
||||
expect(degreeReadout.textContent).toContain('45° / 360°')
|
||||
|
||||
// Frame 4: 0.75s -> 60°
|
||||
act(() => {
|
||||
frameCallback!(767, { mediaTime: 0.75 })
|
||||
})
|
||||
expect(degreeReadout.textContent).toContain('60° / 360°')
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
delete (HTMLVideoElement.prototype as any).requestVideoFrameCallback
|
||||
})
|
||||
|
||||
it('cancels frame callback loop when video is paused', () => {
|
||||
const cancelMock = vi.fn()
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(HTMLVideoElement.prototype as any).requestVideoFrameCallback = vi.fn().mockReturnValue(123)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(HTMLVideoElement.prototype as any).cancelVideoFrameCallback = cancelMock
|
||||
|
||||
const { container } = render(<ModelViewer file={mockAsset} />)
|
||||
const video = container.querySelector('video')
|
||||
|
||||
fireEvent.play(video!)
|
||||
fireEvent.pause(video!)
|
||||
|
||||
expect(cancelMock).toHaveBeenCalledWith(123)
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
delete (HTMLVideoElement.prototype as any).requestVideoFrameCallback
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
delete (HTMLVideoElement.prototype as any).cancelVideoFrameCallback
|
||||
})
|
||||
})
|
||||
|
||||
@@ -97,6 +97,67 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
// Current angle
|
||||
const currentDegree = secondToDegree(currentTime)
|
||||
|
||||
const onTimeUpdateRef = useRef(onTimeUpdate)
|
||||
useEffect(() => {
|
||||
onTimeUpdateRef.current = onTimeUpdate
|
||||
}, [onTimeUpdate])
|
||||
|
||||
// Synchronized playback frame loop (via requestVideoFrameCallback / requestAnimationFrame)
|
||||
useEffect(() => {
|
||||
if (!isPlaying) return
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
let animationFrameId: number | null = null
|
||||
let rvfcId: number | null = null
|
||||
let isCancelled = false
|
||||
|
||||
type VideoWithRvfc = HTMLVideoElement & {
|
||||
requestVideoFrameCallback?: (
|
||||
callback: (now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => void,
|
||||
) => number
|
||||
cancelVideoFrameCallback?: (id: number) => void
|
||||
}
|
||||
|
||||
const videoWithRvfc = video as VideoWithRvfc
|
||||
const rvfcSupported = typeof videoWithRvfc.requestVideoFrameCallback === 'function'
|
||||
|
||||
const updateTime = (time: number) => {
|
||||
if (!isDraggingRotation) {
|
||||
setCurrentTime(time)
|
||||
onTimeUpdateRef.current?.(time)
|
||||
}
|
||||
}
|
||||
|
||||
if (rvfcSupported) {
|
||||
const onFrame = (_now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => {
|
||||
if (isCancelled) return
|
||||
updateTime(metadata.mediaTime)
|
||||
rvfcId = videoWithRvfc.requestVideoFrameCallback!(onFrame)
|
||||
}
|
||||
rvfcId = videoWithRvfc.requestVideoFrameCallback!(onFrame)
|
||||
} else {
|
||||
const loop = () => {
|
||||
if (isCancelled) return
|
||||
if (videoRef.current) {
|
||||
updateTime(videoRef.current.currentTime)
|
||||
}
|
||||
animationFrameId = requestAnimationFrame(loop)
|
||||
}
|
||||
animationFrameId = requestAnimationFrame(loop)
|
||||
}
|
||||
|
||||
return () => {
|
||||
isCancelled = true
|
||||
if (rvfcId !== null && typeof videoWithRvfc.cancelVideoFrameCallback === 'function') {
|
||||
videoWithRvfc.cancelVideoFrameCallback(rvfcId)
|
||||
}
|
||||
if (animationFrameId !== null) {
|
||||
cancelAnimationFrame(animationFrameId)
|
||||
}
|
||||
}
|
||||
}, [isPlaying, isDraggingRotation])
|
||||
|
||||
// Seek helper
|
||||
const seekToFrame = useCallback(
|
||||
(frameIndex: number) => {
|
||||
@@ -322,10 +383,13 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
setIsPlaying(false)
|
||||
onPause?.()
|
||||
}}
|
||||
onEnded={() => {
|
||||
setIsPlaying(false)
|
||||
}}
|
||||
onTimeUpdate={() => {
|
||||
if (videoRef.current && !isDraggingRotation && isPlaying) {
|
||||
if (videoRef.current && !isDraggingRotation && !isPlaying) {
|
||||
setCurrentTime(videoRef.current.currentTime)
|
||||
onTimeUpdate?.(videoRef.current.currentTime)
|
||||
onTimeUpdateRef.current?.(videoRef.current.currentTime)
|
||||
}
|
||||
}}
|
||||
onError={() => {
|
||||
|
||||
Reference in New Issue
Block a user