diff --git a/packages/webui/components/compare/compare-control-bar.tsx b/packages/webui/components/compare/compare-control-bar.tsx index e4a19691..32359aeb 100644 --- a/packages/webui/components/compare/compare-control-bar.tsx +++ b/packages/webui/components/compare/compare-control-bar.tsx @@ -68,6 +68,7 @@ export function CompareControlBar({ isCurrentHdr: v.isCurrentHdr, activeAutoResolution: v.activeAutoResolution, isHlsManualSupported: v.isHlsManualSupported, + isLoading: v.isLoading, } // The active-side asset carries the metadata used for timecode display and diff --git a/packages/webui/components/compare/types.ts b/packages/webui/components/compare/types.ts index 33574719..b3b48690 100644 --- a/packages/webui/components/compare/types.ts +++ b/packages/webui/components/compare/types.ts @@ -35,6 +35,7 @@ export interface PaneReportedState { buffered: number activeAutoResolution?: string isHlsManualSupported?: boolean + isLoading?: boolean } } diff --git a/packages/webui/components/viewers/video/compare-video-pane.test.tsx b/packages/webui/components/viewers/video/compare-video-pane.test.tsx index a0d1b4b4..a96d8b8c 100644 --- a/packages/webui/components/viewers/video/compare-video-pane.test.tsx +++ b/packages/webui/components/viewers/video/compare-video-pane.test.tsx @@ -305,4 +305,198 @@ describe('CompareVideoPane', () => { const lastState = reportedStates[reportedStates.length - 1] expect(lastState?.video?.resolutions?.[0]?.resolution).toBe('1080p') }) + + it('sets poster on video element from file.preview.thumbnailUrl', () => { + const videoWithThumb: AssetInfo = { + id: 'compare-thumb', + name: 'thumb.mp4', + proxyType: 'video', + preview: { + thumbnailUrl: 'https://cdn.example.com/compare-poster.jpg', + }, + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/compare-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const { container } = render( + , + ) + const video = container.querySelector( + '[data-testid="compare-video-area"] video', + ) as HTMLVideoElement + expect(video.getAttribute('poster')).toBe('https://cdn.example.com/compare-poster.jpg') + }) + + it('shows loading spinner initially, transitions on loadeddata, and blocks play while loading', () => { + const testVideo: AssetInfo = { + id: 'compare-loading-test', + name: 'loading.mp4', + proxyType: 'video', + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/compare-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const onRequestTogglePlay = vi.fn() + const { container } = render( + , + ) + + const area = container.querySelector('[data-testid="compare-video-area"]') as HTMLDivElement + const video = area.querySelector('video') as HTMLVideoElement + + // Initially loading spinner is visible + expect(container.querySelector('[data-testid="compare-video-loading-spinner"]')).not.toBeNull() + expect(container.querySelector('[data-testid="compare-video-play-overlay"]')).toBeNull() + + // Clicking while loading should not trigger play + act(() => { + area.click() + }) + expect(onRequestTogglePlay).not.toHaveBeenCalled() + + // Dispatch loadeddata + act(() => { + video.dispatchEvent(new Event('loadeddata')) + }) + + // Spinner gone, play overlay visible + expect(container.querySelector('[data-testid="compare-video-loading-spinner"]')).toBeNull() + expect(container.querySelector('[data-testid="compare-video-play-overlay"]')).not.toBeNull() + + // Clicking now triggers toggle play + act(() => { + area.click() + }) + expect(onRequestTogglePlay).toHaveBeenCalledTimes(1) + }) + + it('does not cancel waiting stall debounce when progress events fire during stall', () => { + vi.useFakeTimers() + try { + const testVideo: AssetInfo = { + id: 'compare-stall-progress-test', + name: 'compare-stall-progress.mp4', + proxyType: 'video', + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/compare-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const { container } = render( + , + ) + + const video = container.querySelector( + '[data-testid="compare-video-area"] video', + ) as HTMLVideoElement + + // 1. Initial ready state + act(() => { + video.dispatchEvent(new Event('loadeddata')) + }) + expect(container.querySelector('[data-testid="compare-video-loading-spinner"]')).toBeNull() + + // 2. Play starts (paused === false) + Object.defineProperty(video, 'paused', { value: false, configurable: true, writable: true }) + act(() => { + video.dispatchEvent(new Event('playing')) + }) + + // 3. Stalls: dispatches waiting + act(() => { + video.dispatchEvent(new Event('waiting')) + }) + // Spinner not visible immediately (within 200ms debounce) + expect(container.querySelector('[data-testid="compare-video-loading-spinner"]')).toBeNull() + + // 4. Progress event fires at 100ms while paused is false (data still trickling in) + act(() => { + vi.advanceTimersByTime(100) + video.dispatchEvent(new Event('progress')) + }) + // Should NOT clear debounce or mark loading false + expect(container.querySelector('[data-testid="compare-video-loading-spinner"]')).toBeNull() + + // 5. Advance past 200ms debounce (advance remaining 110ms) + act(() => { + vi.advanceTimersByTime(110) + }) + // Spinner should now appear despite the progress event! + expect( + container.querySelector('[data-testid="compare-video-loading-spinner"]'), + ).not.toBeNull() + + // 6. Playback resumes: dispatch playing + act(() => { + video.dispatchEvent(new Event('playing')) + }) + // Spinner disappears + expect(container.querySelector('[data-testid="compare-video-loading-spinner"]')).toBeNull() + } finally { + vi.useRealTimers() + } + }) }) diff --git a/packages/webui/components/viewers/video/compare-video-pane.tsx b/packages/webui/components/viewers/video/compare-video-pane.tsx index 1049a9b5..63a86dbb 100644 --- a/packages/webui/components/viewers/video/compare-video-pane.tsx +++ b/packages/webui/components/viewers/video/compare-video-pane.tsx @@ -4,7 +4,7 @@ import { cn } from '@/ui/lib/utils' import { useAnnotationStore } from '@/ui/stores/annotation-store' import type { Annotation } from '@/ui/types' import type { AssetInfo } from '@shumai/dtos' -import { Play, AudioLines } from 'lucide-react' +import { Play, AudioLines, Loader2 } from 'lucide-react' import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react' import Hls from 'hls.js' import { useFramePlayer } from './use-frame-player' @@ -107,6 +107,8 @@ export const CompareVideoPane = forwardRef | null>(null) const metadata = file.media?.metadata const frameRate = metadata?.frameRate || 30 @@ -168,6 +170,12 @@ export const CompareVideoPane = forwardRef { - setIsPlayerReady(true) - }) - hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => { const level = hls?.levels[eventData.level] if (level) { @@ -244,7 +253,17 @@ export const CompareVideoPane = forwardRef { + if (waitingTimeoutRef.current) { + clearTimeout(waitingTimeoutRef.current) + waitingTimeoutRef.current = null + } + } + const handlePlay = () => { + clearWaitingTimeout() + setIsLoading(false) + setIsPlayerReady(true) if (pendingResolutionCorrectionRef.current) { pendingResolutionCorrectionRef.current() pendingResolutionCorrectionRef.current = null @@ -254,9 +273,36 @@ export const CompareVideoPane = forwardRef setIsPlaying(false) - const handleEnded = () => setIsPlaying(false) - const handleLoadedMetadata = () => setIsPlayerReady(true) - const handleLoadStart = () => setIsPlayerReady(false) + const handleEnded = () => { + clearWaitingTimeout() + setIsLoading(false) + setIsPlaying(false) + } + const handleLoadedData = () => { + clearWaitingTimeout() + setIsLoading(false) + setIsPlayerReady(true) + } + const handleCanPlay = () => { + clearWaitingTimeout() + setIsLoading(false) + setIsPlayerReady(true) + } + const handlePlaying = () => { + clearWaitingTimeout() + setIsLoading(false) + } + const handleWaiting = () => { + clearWaitingTimeout() + waitingTimeoutRef.current = setTimeout(() => { + setIsLoading(true) + }, 200) + } + const handleLoadStart = () => { + clearWaitingTimeout() + setIsLoading(true) + setIsPlayerReady(false) + } const handleProgress = () => { const vidDuration = video.duration || containerDuration || 0 if (vidDuration > 0 && video.buffered.length > 0) { @@ -282,10 +328,18 @@ export const CompareVideoPane = forwardRef setPlaybackRate(video.playbackRate || 1) + if (video.readyState >= 2) { + setIsLoading(false) + setIsPlayerReady(true) + } + video.addEventListener('play', handlePlay) video.addEventListener('pause', handlePause) video.addEventListener('ended', handleEnded) - video.addEventListener('loadedmetadata', handleLoadedMetadata) + video.addEventListener('loadeddata', handleLoadedData) + video.addEventListener('canplay', handleCanPlay) + video.addEventListener('playing', handlePlaying) + video.addEventListener('waiting', handleWaiting) video.addEventListener('loadstart', handleLoadStart) video.addEventListener('timeupdate', handleProgress) video.addEventListener('progress', handleProgress) @@ -293,10 +347,14 @@ export const CompareVideoPane = forwardRef { + clearWaitingTimeout() video.removeEventListener('play', handlePlay) video.removeEventListener('pause', handlePause) video.removeEventListener('ended', handleEnded) - video.removeEventListener('loadedmetadata', handleLoadedMetadata) + video.removeEventListener('loadeddata', handleLoadedData) + video.removeEventListener('canplay', handleCanPlay) + video.removeEventListener('playing', handlePlaying) + video.removeEventListener('waiting', handleWaiting) video.removeEventListener('loadstart', handleLoadStart) video.removeEventListener('timeupdate', handleProgress) video.removeEventListener('progress', handleProgress) @@ -343,6 +401,7 @@ export const CompareVideoPane = forwardRef )} @@ -712,8 +774,23 @@ export const CompareVideoPane = forwardRef + {/* Loading Spinner Overlay */} + {isLoading && ( +
+
+ +
+
+ )} + + {!isLoading && !hasStartedPlaying && !isDrawing && ( +
diff --git a/packages/webui/components/viewers/video/mobile-video-control-bar.tsx b/packages/webui/components/viewers/video/mobile-video-control-bar.tsx index f5f2def4..32a83de8 100644 --- a/packages/webui/components/viewers/video/mobile-video-control-bar.tsx +++ b/packages/webui/components/viewers/video/mobile-video-control-bar.tsx @@ -45,7 +45,9 @@ export const MobileVideoControlBar: React.FC = ({ floatOverlayInFullScreen = true, onMouseEnter, onMouseLeave, + isLoading: propIsLoading, }) => { + const isLoading = propIsLoading ?? state.isLoading ?? false const { videoTimeDisplayMode, setVideoTimeDisplayMode } = useUiStore() if (!data.media?.metadata) { @@ -78,241 +80,248 @@ export const MobileVideoControlBar: React.FC = ({ onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} > - {/* Progress Bar */} -
- -
+
+ {/* Progress Bar */} +
+ +
- {/* Control Row */} -
- {/* Left Side: Play, Mute, Time */} -
- - - - -
- + {/* Left Side: Play, Mute, Time */} +
+ + + + +
+ + {displayString} + + + + + + + + {m.format()} + + setVideoTimeDisplayMode('standard')} + className="flex items-center justify-between cursor-pointer text-xs" + > + {m.standard_time()} + {videoTimeDisplayMode === 'standard' && ( + + )} + + setVideoTimeDisplayMode('frames')} + className="flex items-center justify-between cursor-pointer text-xs" + > + {m.frames()} + {videoTimeDisplayMode === 'frames' && ( + + )} + + setVideoTimeDisplayMode('timecode')} + className="flex items-center justify-between cursor-pointer text-xs" + > + {m.timecode()} + {videoTimeDisplayMode === 'timecode' && ( + + )} + + + +
+
+ + {/* Right Side: Settings (Quality + Speed) & Fullscreen */} +
- + + {!isAudio && resolutions.length > 0 && ( + <> + + {m.quality()} + + {data.media?.isHls && ( + + changeResolution({ + resolution: 'Auto', + width: data.media?.metadata?.originalWidth ?? 0, + height: data.media?.metadata?.originalHeight ?? 0, + } as DisplayTranscode) + } + className={cn( + 'flex w-full items-center justify-between text-xs cursor-pointer', + state.currentResolution === 'Auto' + ? 'text-primary font-medium' + : 'text-foreground', + )} + > + {m.quality_auto()} + + )} + {resolutions.map((res) => { + const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}` + const isSelected = + state.currentResolution === res.resolution && + Boolean(state.isCurrentHdr) === Boolean(res.hdr) + const isManualDisabled = + Boolean(data.media?.isHls) && state.isHlsManualSupported === false + return ( + !isManualDisabled && changeResolution(res)} + className={cn( + 'flex w-full items-center justify-between text-xs', + isSelected ? 'text-primary font-medium' : 'text-foreground', + isManualDisabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer', + )} + > +
+ + {res.resolution === 'Original' ? m.original() : res.resolution} + + {res.hdr && ( + + {m.hdr()} + + )} +
+ + {res.width}x{res.height} + +
+ ) + })} + + + )} + - {m.format()} + {m.playback_speed()} + {[0.5, 1, 1.5, 2].map((rate) => ( + changePlaybackRate(rate)} + className={cn( + 'flex items-center justify-between text-xs cursor-pointer', + state.playbackRate === rate ? 'text-primary font-medium' : 'text-foreground', + )} + > + {rate}x + {state.playbackRate === rate && } + + ))} + + setVideoTimeDisplayMode('standard')} - className="flex items-center justify-between cursor-pointer text-xs" + onClick={toggleLoop} + className="flex items-center justify-between text-xs cursor-pointer" > - {m.standard_time()} - {videoTimeDisplayMode === 'standard' && ( - - )} - - setVideoTimeDisplayMode('frames')} - className="flex items-center justify-between cursor-pointer text-xs" - > - {m.frames()} - {videoTimeDisplayMode === 'frames' && ( - - )} - - setVideoTimeDisplayMode('timecode')} - className="flex items-center justify-between cursor-pointer text-xs" - > - {m.timecode()} - {videoTimeDisplayMode === 'timecode' && ( - - )} + + + {m.loop()} + + {state.isLooping && }
-
-
- {/* Right Side: Settings (Quality + Speed) & Fullscreen */} -
- - - - - - {!isAudio && resolutions.length > 0 && ( - <> - - {m.quality()} - - {data.media?.isHls && ( - - changeResolution({ - resolution: 'Auto', - width: data.media?.metadata?.originalWidth ?? 0, - height: data.media?.metadata?.originalHeight ?? 0, - } as DisplayTranscode) - } - className={cn( - 'flex w-full items-center justify-between text-xs cursor-pointer', - state.currentResolution === 'Auto' - ? 'text-primary font-medium' - : 'text-foreground', - )} - > - {m.quality_auto()} - - )} - {resolutions.map((res) => { - const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}` - const isSelected = - state.currentResolution === res.resolution && - Boolean(state.isCurrentHdr) === Boolean(res.hdr) - const isManualDisabled = - Boolean(data.media?.isHls) && state.isHlsManualSupported === false - return ( - !isManualDisabled && changeResolution(res)} - className={cn( - 'flex w-full items-center justify-between text-xs', - isSelected ? 'text-primary font-medium' : 'text-foreground', - isManualDisabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer', - )} - > -
- - {res.resolution === 'Original' ? m.original() : res.resolution} - - {res.hdr && ( - - {m.hdr()} - - )} -
- - {res.width}x{res.height} - -
- ) - })} - - + {/* Fullscreen */} + + +
diff --git a/packages/webui/components/viewers/video/video-control-bar.tsx b/packages/webui/components/viewers/video/video-control-bar.tsx index c5581ca6..4f7ea27e 100644 --- a/packages/webui/components/viewers/video/video-control-bar.tsx +++ b/packages/webui/components/viewers/video/video-control-bar.tsx @@ -47,6 +47,7 @@ export interface PlayerState { isCurrentHdr?: boolean activeAutoResolution?: string isHlsManualSupported?: boolean + isLoading?: boolean } export interface DisplayTranscode { @@ -93,6 +94,7 @@ export interface ControlBarProps { onMouseLeave?: () => void /** When false, hides the download affordance. Defaults to true. */ allowDownload?: boolean + isLoading?: boolean } export const VideoControlBar: React.FC = ({ @@ -120,7 +122,9 @@ export const VideoControlBar: React.FC = ({ onMouseEnter, onMouseLeave, allowDownload = true, + isLoading: propIsLoading, }) => { + const isLoading = propIsLoading ?? state.isLoading ?? false const { videoTimeDisplayMode, setVideoTimeDisplayMode } = useUiStore() if (!data.media?.metadata) { @@ -156,332 +160,341 @@ export const VideoControlBar: React.FC = ({ onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} > - {/* Progress Bar */} -
- -
+
+ {/* Progress Bar */} +
+ +
- {/* Lower Controls Row */} -
- {/* Left Side: Play, Vol, Time */} -
- - - - -
- -
-
- handleVolumeChange(v[0])} - max={1} - step={0.05} - /> + + +
+ + +
+
+ handleVolumeChange(v[0])} + max={1} + step={0.05} + /> +
+ +
+ + {displayString} + + + + + + + + Format + + setVideoTimeDisplayMode('standard')} + className="flex items-center justify-between cursor-pointer text-xs" + > + Standard Time + {videoTimeDisplayMode === 'standard' && ( + + )} + + setVideoTimeDisplayMode('frames')} + className="flex items-center justify-between cursor-pointer text-xs" + > + Frames + {videoTimeDisplayMode === 'frames' && ( + + )} + + setVideoTimeDisplayMode('timecode')} + className="flex items-center justify-between cursor-pointer text-xs" + > + Timecode + {videoTimeDisplayMode === 'timecode' && ( + + )} + + + +
-
- - {displayString} - - - - - - - - Format - - setVideoTimeDisplayMode('standard')} - className="flex items-center justify-between cursor-pointer text-xs" - > - Standard Time - {videoTimeDisplayMode === 'standard' && ( - - )} - - setVideoTimeDisplayMode('frames')} - className="flex items-center justify-between cursor-pointer text-xs" - > - Frames - {videoTimeDisplayMode === 'frames' && ( - - )} - - setVideoTimeDisplayMode('timecode')} - className="flex items-center justify-between cursor-pointer text-xs" - > - Timecode - {videoTimeDisplayMode === 'timecode' && ( - - )} - - - -
-
- - {/* Right Side: Zoom, Speed, Res, Download, Fullscreen */} -
- {/* Zoom Controls */} - {!isAudio && ( -
-
+ {/* Right Side: Zoom, Speed, Res, Download, Fullscreen */} +
+ {/* Zoom Controls */} + {!isAudio && ( +
+
+ + + {Math.round(zoom * 100)}% + + +
- - {Math.round(zoom * 100)}% - -
- -
- )} + )} - {/* Playback Rate */} - - - - - - {[0.5, 1, 1.5, 2].map((rate) => ( - changePlaybackRate(rate)} - className={cn( - 'rounded py-1 px-2 text-left text-xs', - state.playbackRate === rate - ? 'bg-primary text-primary-foreground focus:bg-primary focus:text-primary-foreground' - : 'text-foreground', - )} - > - {rate}x - - ))} - - - - {/* Settings / Resolution */} - {!isAudio && resolutions.length > 0 && ( + {/* Playback Rate */} - - - {m.quality()} - {data.media?.isHls && ( + {[0.5, 1, 1.5, 2].map((rate) => ( - changeResolution({ - resolution: 'Auto', - width: data.media?.metadata?.originalWidth ?? 0, - height: data.media?.metadata?.originalHeight ?? 0, - } as DisplayTranscode) - } + key={rate} + onClick={() => changePlaybackRate(rate)} className={cn( - 'flex w-full items-center justify-between cursor-pointer', - state.currentResolution === 'Auto' - ? 'text-primary font-medium' + 'rounded py-1 px-2 text-left text-xs', + state.playbackRate === rate + ? 'bg-primary text-primary-foreground focus:bg-primary focus:text-primary-foreground' : 'text-foreground', )} > - {m.quality_auto()} + {rate}x - )} - {resolutions.map((res) => { - const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}` - const isSelected = - state.currentResolution === res.resolution && - Boolean(state.isCurrentHdr) === Boolean(res.hdr) - const isManualDisabled = - Boolean(data.media?.isHls) && state.isHlsManualSupported === false - return ( - !isManualDisabled && changeResolution(res)} - className={cn( - 'flex w-full items-center justify-between', - isSelected ? 'text-primary font-medium' : 'text-foreground', - isManualDisabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer', - )} - > -
- {res.resolution === 'Original' ? m.original() : res.resolution} - {res.hdr && ( - - {m.hdr()} - - )} -
- - {res.width}x{res.height} - -
- ) - })} + ))}
- )} - {/* Download */} - {allowDownload && - (isAudio ? ( - - ) : ( + {/* Settings / Resolution */} + {!isAudio && resolutions.length > 0 && ( - + - {m.download()} - {(data.media?.videoTranscodes && data.media.videoTranscodes.length > 0 - ? data.media.videoTranscodes - : resolutions - ).map((res) => { - const resLabel = getVideoResolutionLabel(res) - const itemKey = res.key || `${resLabel}-${res.hdr ? 'hdr' : 'sdr'}` + {m.quality()} + {data.media?.isHls && ( + + changeResolution({ + resolution: 'Auto', + width: data.media?.metadata?.originalWidth ?? 0, + height: data.media?.metadata?.originalHeight ?? 0, + } as DisplayTranscode) + } + className={cn( + 'flex w-full items-center justify-between cursor-pointer', + state.currentResolution === 'Auto' + ? 'text-primary font-medium' + : 'text-foreground', + )} + > + {m.quality_auto()} + + )} + {resolutions.map((res) => { + const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}` + const isSelected = + state.currentResolution === res.resolution && + Boolean(state.isCurrentHdr) === Boolean(res.hdr) + const isManualDisabled = + Boolean(data.media?.isHls) && state.isHlsManualSupported === false return ( handleDownload(res.key ?? '')} - className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground cursor-pointer" + disabled={isManualDisabled} + onClick={() => !isManualDisabled && changeResolution(res)} + className={cn( + 'flex w-full items-center justify-between', + isSelected ? 'text-primary font-medium' : 'text-foreground', + isManualDisabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer', + )} >
- {resLabel} + + {res.resolution === 'Original' ? m.original() : res.resolution} + {res.hdr && ( {m.hdr()} )}
- MP4 + + {res.width}x{res.height} +
) })} - {data.media?.original?.key && ( - <> - - handleDownload(data.media?.original?.key ?? '')} - className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground" - > - {m.original()} - - {data.name?.split('.').pop()?.toUpperCase() || 'RAW'} - - - - )}
- ))} - - {/* Fullscreen */} - + + {/* Download */} + {allowDownload && + (isAudio ? ( + + ) : ( + + + + + + {m.download()} + {(data.media?.videoTranscodes && data.media.videoTranscodes.length > 0 + ? data.media.videoTranscodes + : resolutions + ).map((res) => { + const resLabel = getVideoResolutionLabel(res) + const itemKey = res.key || `${resLabel}-${res.hdr ? 'hdr' : 'sdr'}` + return ( + handleDownload(res.key ?? '')} + className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground cursor-pointer" + > +
+ {resLabel} + {res.hdr && ( + + {m.hdr()} + + )} +
+ MP4 +
+ ) + })} + {data.media?.original?.key && ( + <> + + handleDownload(data.media?.original?.key ?? '')} + className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground" + > + {m.original()} + + {data.name?.split('.').pop()?.toUpperCase() || 'RAW'} + + + + )} +
+
+ ))} + + {/* Fullscreen */} + +
diff --git a/packages/webui/components/viewers/video/video-viewer.test.tsx b/packages/webui/components/viewers/video/video-viewer.test.tsx index 394fb85d..04d685b0 100644 --- a/packages/webui/components/viewers/video/video-viewer.test.tsx +++ b/packages/webui/components/viewers/video/video-viewer.test.tsx @@ -566,4 +566,229 @@ describe('VideoViewer', () => { expect(settingsButton?.textContent).toContain('1080p') expect(settingsButton?.textContent).not.toContain('1920p') }) + + it('sets poster on video element from file.preview.thumbnailUrl', () => { + const videoWithThumb: AssetInfo = { + id: 'video-thumb', + name: 'thumb.mp4', + proxyType: 'video', + preview: { + thumbnailUrl: 'https://cdn.example.com/poster.jpg', + }, + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/video-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const { container } = render() + const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement + expect(video.getAttribute('poster')).toBe('https://cdn.example.com/poster.jpg') + }) + + it('shows loading spinner initially and transitions to play overlay on loadeddata', () => { + const testVideo: AssetInfo = { + id: 'video-loading-test', + name: 'loading.mp4', + proxyType: 'video', + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/video-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const { container } = render() + const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement + + // 1. Initially loading spinner is visible, play overlay is not + expect(container.querySelector('[data-testid="video-loading-spinner"]')).not.toBeNull() + expect(container.querySelector('[data-testid="video-play-overlay"]')).toBeNull() + + // 2. Control bar inner wrapper is dimmed & non-interactive + const controlBarWrapper = container.querySelector('.pointer-events-none.opacity-60') + expect(controlBarWrapper).not.toBeNull() + + // 3. Dispatch loadeddata + act(() => { + video.dispatchEvent(new Event('loadeddata')) + }) + + // Spinner is gone, play overlay is visible, controls are interactive + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + expect(container.querySelector('[data-testid="video-play-overlay"]')).not.toBeNull() + expect(container.querySelector('.pointer-events-none.opacity-60')).toBeNull() + }) + + it('debounces waiting stall with 200ms delay and cancels spinner on quick recovery', () => { + vi.useFakeTimers() + try { + const testVideo: AssetInfo = { + id: 'video-stall-test', + name: 'stall.mp4', + proxyType: 'video', + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/video-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const { container } = render() + const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement + + // Become ready first + act(() => { + video.dispatchEvent(new Event('loadeddata')) + }) + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + + // Stalls briefly (<200ms) + act(() => { + video.dispatchEvent(new Event('waiting')) + }) + // Immediately after waiting, spinner should NOT be shown yet (debounced) + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + + // Recovers quickly at 100ms + act(() => { + vi.advanceTimersByTime(100) + video.dispatchEvent(new Event('playing')) + }) + act(() => { + vi.advanceTimersByTime(150) + }) + // Spinner still not shown + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + + // Now a long stall (>200ms) + act(() => { + video.dispatchEvent(new Event('waiting')) + }) + act(() => { + vi.advanceTimersByTime(250) + }) + // Spinner should now appear! + expect(container.querySelector('[data-testid="video-loading-spinner"]')).not.toBeNull() + + // Resume playing + act(() => { + video.dispatchEvent(new Event('playing')) + }) + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + } finally { + vi.useRealTimers() + } + }) + + it('does not cancel waiting stall debounce when progress events fire during stall', () => { + vi.useFakeTimers() + try { + const testVideo: AssetInfo = { + id: 'video-stall-progress-test', + name: 'stall-progress.mp4', + proxyType: 'video', + media: { + metadata: { + originalWidth: 1920, + originalHeight: 1080, + duration: 10, + frameRate: 30, + totalFrames: 300, + }, + videoTranscodes: [ + { + resolution: '1080p', + url: 'https://cdn.example.com/video-1080p.mp4', + width: 1920, + height: 1080, + }, + ], + }, + } as unknown as AssetInfo + + const { container } = render() + const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement + + // 1. Initial ready state + act(() => { + video.dispatchEvent(new Event('loadeddata')) + }) + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + + // 2. Play starts (paused === false) + Object.defineProperty(video, 'paused', { value: false, configurable: true, writable: true }) + act(() => { + video.dispatchEvent(new Event('playing')) + }) + + // 3. Stalls: dispatches waiting + act(() => { + video.dispatchEvent(new Event('waiting')) + }) + // Spinner not visible immediately (within 200ms debounce) + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + + // 4. Progress event fires at 100ms while paused is false (data still trickling in) + act(() => { + vi.advanceTimersByTime(100) + video.dispatchEvent(new Event('progress')) + }) + // Should NOT clear debounce or mark loading false + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + + // 5. Advance past 200ms debounce (advance remaining 110ms) + act(() => { + vi.advanceTimersByTime(110) + }) + // Spinner should now appear despite the progress event! + expect(container.querySelector('[data-testid="video-loading-spinner"]')).not.toBeNull() + + // 6. Playback resumes: dispatch playing + act(() => { + video.dispatchEvent(new Event('playing')) + }) + // Spinner disappears + expect(container.querySelector('[data-testid="video-loading-spinner"]')).toBeNull() + } finally { + vi.useRealTimers() + } + }) }) diff --git a/packages/webui/components/viewers/video/video-viewer.tsx b/packages/webui/components/viewers/video/video-viewer.tsx index cb62faaf..d9ea1235 100644 --- a/packages/webui/components/viewers/video/video-viewer.tsx +++ b/packages/webui/components/viewers/video/video-viewer.tsx @@ -1,6 +1,6 @@ import { client } from '@/ui/api/client' import { cn } from '@/ui/lib/utils' -import { Play, AudioLines } from 'lucide-react' +import { Play, AudioLines, Loader2 } from 'lucide-react' import React, { useCallback, useEffect, useRef, useState, useImperativeHandle } from 'react' import Hls from 'hls.js' import { useFramePlayer } from './use-frame-player' @@ -157,7 +157,12 @@ const VideoViewer = React.forwardRef( })) setActiveAutoResolution(undefined) setHasManuallyZoomed(false) + if (waitingTimeoutRef.current) { + clearTimeout(waitingTimeoutRef.current) + waitingTimeoutRef.current = null + } setIsPlayerReady(false) + setIsLoading(true) setBuffered(0) lastProcessedStartTimeRef.current = null setHasStartedPlaying(false) @@ -250,6 +255,8 @@ const VideoViewer = React.forwardRef( const [buffered, setBuffered] = useState(0) const [isControlsVisible, setIsControlsVisible] = useState(true) const [isPlayerReady, setIsPlayerReady] = useState(false) + const [isLoading, setIsLoading] = useState(true) + const waitingTimeoutRef = useRef | null>(null) const lastProcessedStartTimeRef = useRef(null) // Frame-accurate hook and derived state @@ -285,6 +292,7 @@ const VideoViewer = React.forwardRef( progress, activeAutoResolution, isHlsManualSupported, + isLoading, } // Trigger time update event reactively @@ -309,7 +317,12 @@ const VideoViewer = React.forwardRef( hlsRef.current = null } + if (waitingTimeoutRef.current) { + clearTimeout(waitingTimeoutRef.current) + waitingTimeoutRef.current = null + } setIsPlayerReady(false) + setIsLoading(true) setActiveAutoResolution(undefined) const startAutoPlay = () => { @@ -339,7 +352,6 @@ const VideoViewer = React.forwardRef( hls.loadSource(targetSrc) hls.on(Hls.Events.MANIFEST_PARSED, () => { - setIsPlayerReady(true) if (autoPlay) { if (video.readyState >= 2) { startAutoPlay() @@ -406,7 +418,17 @@ const VideoViewer = React.forwardRef( } } + const clearWaitingTimeout = () => { + if (waitingTimeoutRef.current) { + clearTimeout(waitingTimeoutRef.current) + waitingTimeoutRef.current = null + } + } + const handlePlay = () => { + clearWaitingTimeout() + setIsLoading(false) + setIsPlayerReady(true) if (pendingResolutionCorrectionRef.current) { pendingResolutionCorrectionRef.current() pendingResolutionCorrectionRef.current = null @@ -420,12 +442,33 @@ const VideoViewer = React.forwardRef( onPause?.() } const handleEnded = () => { + clearWaitingTimeout() + setIsLoading(false) setState((p) => ({ ...p, isPlaying: false })) } - const handleLoadedMetadata = () => { + const handleLoadedData = () => { + clearWaitingTimeout() + setIsLoading(false) setIsPlayerReady(true) } + const handleCanPlay = () => { + clearWaitingTimeout() + setIsLoading(false) + setIsPlayerReady(true) + } + const handlePlaying = () => { + clearWaitingTimeout() + setIsLoading(false) + } + const handleWaiting = () => { + clearWaitingTimeout() + waitingTimeoutRef.current = setTimeout(() => { + setIsLoading(true) + }, 200) + } const handleLoadStart = () => { + clearWaitingTimeout() + setIsLoading(true) setIsPlayerReady(false) } const handleProgress = () => { @@ -461,13 +504,23 @@ const VideoViewer = React.forwardRef( })) } const handleError = () => { + clearWaitingTimeout() + setIsLoading(false) console.error('Video Error:', video.error) } + if (video.readyState >= 2) { + setIsLoading(false) + setIsPlayerReady(true) + } + video.addEventListener('play', handlePlay) video.addEventListener('pause', handlePause) video.addEventListener('ended', handleEnded) - video.addEventListener('loadedmetadata', handleLoadedMetadata) + video.addEventListener('loadeddata', handleLoadedData) + video.addEventListener('canplay', handleCanPlay) + video.addEventListener('playing', handlePlaying) + video.addEventListener('waiting', handleWaiting) video.addEventListener('loadstart', handleLoadStart) video.addEventListener('timeupdate', handleProgress) video.addEventListener('progress', handleProgress) @@ -480,10 +533,14 @@ const VideoViewer = React.forwardRef( // Cleanup return () => { + clearWaitingTimeout() video.removeEventListener('play', handlePlay) video.removeEventListener('pause', handlePause) video.removeEventListener('ended', handleEnded) - video.removeEventListener('loadedmetadata', handleLoadedMetadata) + video.removeEventListener('loadeddata', handleLoadedData) + video.removeEventListener('canplay', handleCanPlay) + video.removeEventListener('playing', handlePlaying) + video.removeEventListener('waiting', handleWaiting) video.removeEventListener('loadstart', handleLoadStart) video.removeEventListener('timeupdate', handleProgress) video.removeEventListener('progress', handleProgress) @@ -570,6 +627,7 @@ const VideoViewer = React.forwardRef( // -- Event Handlers -- const togglePlay = useCallback(() => { + if (isLoading) return // Disable click-to-play if drawing if (useAnnotationStore.getState().isDrawing) return @@ -593,7 +651,7 @@ const VideoViewer = React.forwardRef( } else { video.pause() } - }, []) + }, [isLoading]) useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { @@ -949,6 +1007,7 @@ const VideoViewer = React.forwardRef( className="w-full h-full block object-contain pointer-events-none" playsInline preload="auto" + poster={data.preview?.thumbnailUrl || undefined} />
)} @@ -996,9 +1055,24 @@ const VideoViewer = React.forwardRef( ) )} + {/* Loading Spinner Overlay */} + {isLoading && ( +
+
+ +
+
+ )} + {/* Initial Big Play Button Overlay (shown only before playback starts) */} - {!hasStartedPlaying && !isDrawing && ( -
+ {!isLoading && !hasStartedPlaying && !isDrawing && ( +
@@ -1032,6 +1106,7 @@ const VideoViewer = React.forwardRef( onMouseEnter={handleControlsMouseEnter} onMouseLeave={handleControlsMouseLeave} allowDownload={allowDownload} + isLoading={isLoading} /> ) : ( ( onMouseEnter={handleControlsMouseEnter} onMouseLeave={handleControlsMouseLeave} allowDownload={allowDownload} + isLoading={isLoading} /> )}