diff --git a/packages/webui/components/viewers/video/use-frame-player.ts b/packages/webui/components/viewers/video/use-frame-player.ts index 2db01cce..98dd96ea 100644 --- a/packages/webui/components/viewers/video/use-frame-player.ts +++ b/packages/webui/components/viewers/video/use-frame-player.ts @@ -35,6 +35,7 @@ export function useFramePlayer( // Track playing state of the HTML video element useEffect(() => { + setCurrentFrame(0) const video = videoRef.current if (!video) return diff --git a/packages/webui/components/viewers/video/video-viewer.test.tsx b/packages/webui/components/viewers/video/video-viewer.test.tsx new file mode 100644 index 00000000..4180089b --- /dev/null +++ b/packages/webui/components/viewers/video/video-viewer.test.tsx @@ -0,0 +1,122 @@ +// @vitest-environment happy-dom +import { cleanup, render } from '@testing-library/react' +import React from 'react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import VideoViewer from './video-viewer' +import type { AssetInfo } from '@shumai/dtos' + +const videojsMock = vi.fn((...args: unknown[]) => { + void args + return { + on: vi.fn(), + volume: vi.fn(), + duration: vi.fn(() => 10), + currentTime: vi.fn(() => 0), + bufferedEnd: vi.fn(() => 0), + playbackRate: vi.fn(() => 1), + ready: vi.fn(), + dispose: vi.fn(), + isDisposed: vi.fn(() => false), + } +}) + +vi.mock('video.js', () => ({ + default: (...args: unknown[]) => videojsMock(...args), +})) + +vi.mock('@/ui/components/drawing-canvas', () => ({ + default: () =>
, +})) + +vi.mock('@/ui/paraglide/messages.js', () => ({ + m: new Proxy({}, { get: () => () => '' }), +})) + +describe('VideoViewer', () => { + beforeEach(() => { + videojsMock.mockClear() + }) + + afterEach(() => { + cleanup() + }) + + it('reloads video player with new video source when file prop changes without unmounting', () => { + const videoA: AssetInfo = { + id: 'video-a', + name: 'video-a.mp4', + proxyType: 'video', + media: { + metadata: { + originalWidth: 1280, + originalHeight: 720, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '720p', + url: 'https://cdn.example.com/videoA-720p.mp4', + width: 1280, + height: 720, + }, + ], + }, + } as unknown as AssetInfo + + const videoB: AssetInfo = { + id: 'video-b', + name: 'video-b.mp4', + proxyType: 'video', + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 25, + frameRate: 30, + totalFrames: 750, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/videoB-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const { rerender } = render() + + // Initial render should initialize videojs with video A URL + expect(videojsMock).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ + sources: [ + { + src: 'https://cdn.example.com/videoA-720p.mp4', + type: 'video/mp4', + }, + ], + }), + ) + + // Rerender with video B (simulating selecting another video from the carousel) + rerender() + + // VideoJS should be re-initialized with video B's URL + expect(videojsMock).toHaveBeenLastCalledWith( + expect.anything(), + expect.objectContaining({ + sources: [ + { + src: 'https://cdn.example.com/videoB-1080p.mp4', + type: 'video/mp4', + }, + ], + }), + ) + }) +}) diff --git a/packages/webui/components/viewers/video/video-viewer.tsx b/packages/webui/components/viewers/video/video-viewer.tsx index 1c850717..83165318 100644 --- a/packages/webui/components/viewers/video/video-viewer.tsx +++ b/packages/webui/components/viewers/video/video-viewer.tsx @@ -128,7 +128,20 @@ const VideoViewer = React.forwardRef( const [containerSize, setContainerSize] = useState({ width: 0, height: 0 }) useEffect(() => { + const res = getInitialResolution() + setState((prev) => ({ + ...prev, + isPlaying: false, + progress: 0, + currentTime: 0, + duration: data.media?.metadata?.duration || 0, + currentResolution: res?.resolution ?? '', + currentSrc: res?.url ?? '', + })) setHasManuallyZoomed(false) + setIsPlayerReady(false) + setBuffered(0) + lastProcessedStartTimeRef.current = null }, [data.id]) const vidW = data.media?.metadata?.originalWidth || 1920 @@ -252,6 +265,17 @@ const VideoViewer = React.forwardRef( useEffect(() => { if (!videoContainerRef.current) return + const initialRes = getInitialResolution() + const targetSrc = initialRes?.url ?? '' + if (!targetSrc) return + + // Clean up previous player if exists + if (playerRef.current && !playerRef.current.isDisposed()) { + playerRef.current.dispose() + playerRef.current = null + videoRef.current = null + } + // 1. Create the video element manually // This avoids conflicts with React Strict Mode where the DOM might not match React's virtual DOM expectations after Video.js modifies it. const videoElement = document.createElement('video-js') @@ -272,7 +296,7 @@ const VideoViewer = React.forwardRef( playsinline: true, sources: [ { - src: state.currentSrc, + src: targetSrc, type: 'video/mp4', }, ], @@ -352,7 +376,7 @@ const VideoViewer = React.forwardRef( videoRef.current = null } } - }, [data]) + }, [data.id]) // Handle changes to startTime (e.g., clicking different chunks in search results) useEffect(() => {