mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 12:54:55 +00:00
feat(webui): switch from video.js to hls.js and native video player (#437)
* feat(webui): switch from video.js to hls.js and native video player * fix(webui): prevent stale seek hijacking and guard native HLS compare mode
This commit is contained in:
@@ -66,6 +66,8 @@ export function CompareControlBar({
|
||||
showFrames: false,
|
||||
currentResolution: v.currentResolution,
|
||||
isCurrentHdr: v.isCurrentHdr,
|
||||
activeAutoResolution: v.activeAutoResolution,
|
||||
isHlsManualSupported: v.isHlsManualSupported,
|
||||
}
|
||||
|
||||
// The active-side asset carries the metadata used for timecode display and
|
||||
|
||||
@@ -33,6 +33,8 @@ export interface PaneReportedState {
|
||||
isCurrentHdr?: boolean
|
||||
resolutions: DisplayTranscode[]
|
||||
buffered: number
|
||||
activeAutoResolution?: string
|
||||
isHlsManualSupported?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { act, cleanup, render } from '@testing-library/react'
|
||||
import React, { createRef } from 'react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { CompareVideoPane } from './compare-video-pane'
|
||||
import type { ComparePaneHandle, PaneReportedState } from '../../compare/types'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
|
||||
const { mockHlsInstance, mockHlsConstructor } = vi.hoisted(() => {
|
||||
const instance = {
|
||||
attachMedia: vi.fn(),
|
||||
loadSource: vi.fn(),
|
||||
on: vi.fn(),
|
||||
off: vi.fn(),
|
||||
once: vi.fn(),
|
||||
destroy: vi.fn(),
|
||||
levels: [
|
||||
{ height: 1080, width: 1920, bitrate: 5000000 },
|
||||
{ height: 720, width: 1280, bitrate: 2500000 },
|
||||
],
|
||||
currentLevel: -1,
|
||||
startLoad: vi.fn(),
|
||||
recoverMediaError: vi.fn(),
|
||||
}
|
||||
const constructor = vi.fn(function () {
|
||||
return instance
|
||||
})
|
||||
return { mockHlsInstance: instance, mockHlsConstructor: constructor }
|
||||
})
|
||||
|
||||
vi.mock('hls.js', () => {
|
||||
const MockHls = mockHlsConstructor
|
||||
// @ts-expect-error adding static mock property
|
||||
MockHls.isSupported = vi.fn(() => true)
|
||||
// @ts-expect-error adding static mock property
|
||||
MockHls.Events = {
|
||||
MANIFEST_PARSED: 'hlsManifestParsed',
|
||||
LEVEL_SWITCHED: 'hlsLevelSwitched',
|
||||
ERROR: 'hlsError',
|
||||
}
|
||||
// @ts-expect-error adding static mock property
|
||||
MockHls.ErrorTypes = {
|
||||
NETWORK_ERROR: 'networkError',
|
||||
MEDIA_ERROR: 'mediaError',
|
||||
OTHER_ERROR: 'otherError',
|
||||
}
|
||||
return {
|
||||
default: MockHls,
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@/ui/components/drawing-canvas', () => ({
|
||||
default: () => <div data-testid="drawing-canvas" />,
|
||||
}))
|
||||
|
||||
vi.mock('@/ui/paraglide/messages.js', () => ({
|
||||
m: new Proxy({}, { get: () => () => '' }),
|
||||
}))
|
||||
|
||||
describe('CompareVideoPane', () => {
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks()
|
||||
mockHlsInstance.currentLevel = -1
|
||||
const Hls = (await import('hls.js')).default as unknown as {
|
||||
isSupported: ReturnType<typeof vi.fn>
|
||||
}
|
||||
Hls.isSupported.mockReturnValue(true)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
})
|
||||
|
||||
it('safely handles native HLS fallback without clearing video.src when manual resolution is invoked', async () => {
|
||||
const Hls = (await import('hls.js')).default as unknown as {
|
||||
isSupported: ReturnType<typeof vi.fn>
|
||||
}
|
||||
Hls.isSupported.mockReturnValue(false)
|
||||
|
||||
// Mock HTMLMediaElement.prototype.canPlayType to support native HLS
|
||||
const originalCanPlayType = HTMLMediaElement.prototype.canPlayType
|
||||
HTMLMediaElement.prototype.canPlayType = vi.fn((type: string) =>
|
||||
type === 'application/vnd.apple.mpegurl' ? 'maybe' : '',
|
||||
)
|
||||
|
||||
try {
|
||||
const hlsVideo: AssetInfo = {
|
||||
id: 'hls-compare',
|
||||
name: 'hls-compare.m3u8',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
isHls: true,
|
||||
hls: {
|
||||
key: 'hls-key',
|
||||
url: 'https://cdn.example.com/master.m3u8',
|
||||
resolutions: [
|
||||
{ width: 1920, height: 1080, resolution: '1080p' },
|
||||
{ width: 1280, height: 720, resolution: '720p' },
|
||||
],
|
||||
},
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
},
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const reportedStates: PaneReportedState[] = []
|
||||
const ref = createRef<ComparePaneHandle>()
|
||||
|
||||
const { container } = render(
|
||||
<CompareVideoPane
|
||||
ref={ref}
|
||||
file={hlsVideo}
|
||||
isActive={true}
|
||||
annotations={[]}
|
||||
onStateChange={(state) => reportedStates.push(state)}
|
||||
onActivate={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
|
||||
const video = container.querySelector('video') as HTMLVideoElement
|
||||
expect(video.src).toBe('https://cdn.example.com/master.m3u8')
|
||||
|
||||
// Check reported state: isHlsManualSupported should be false
|
||||
const lastState = reportedStates[reportedStates.length - 1]
|
||||
expect(lastState?.video?.isHlsManualSupported).toBe(false)
|
||||
|
||||
// Invoke changeResolution('1080p')
|
||||
act(() => {
|
||||
ref.current?.changeResolution('1080p')
|
||||
})
|
||||
|
||||
// In native fallback, changeResolution should NO-OP and NOT clear video.src to empty string
|
||||
expect(video.src).toBe('https://cdn.example.com/master.m3u8')
|
||||
} finally {
|
||||
HTMLMediaElement.prototype.canPlayType = originalCanPlayType
|
||||
}
|
||||
})
|
||||
|
||||
it('switches HLS resolution immediately using currentLevel and does not hijack subsequent seeks', async () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const hlsVideo: AssetInfo = {
|
||||
id: 'hls-compare-switch',
|
||||
name: 'hls-compare-switch.m3u8',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
isHls: true,
|
||||
hls: {
|
||||
key: 'hls-key',
|
||||
url: 'https://cdn.example.com/master.m3u8',
|
||||
resolutions: [
|
||||
{ width: 1920, height: 1080, resolution: '1080p' },
|
||||
{ width: 1280, height: 720, resolution: '720p' },
|
||||
],
|
||||
},
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
},
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const reportedStates: PaneReportedState[] = []
|
||||
const ref = createRef<ComparePaneHandle>()
|
||||
|
||||
const { container } = render(
|
||||
<CompareVideoPane
|
||||
ref={ref}
|
||||
file={hlsVideo}
|
||||
isActive={true}
|
||||
annotations={[]}
|
||||
onStateChange={(state) => reportedStates.push(state)}
|
||||
onActivate={vi.fn()}
|
||||
/>,
|
||||
)
|
||||
|
||||
const video = container.querySelector('video') as HTMLVideoElement
|
||||
Object.defineProperty(video, 'paused', { value: true, configurable: true })
|
||||
video.currentTime = 0
|
||||
|
||||
// In Hls.js supported mode, isHlsManualSupported should be true
|
||||
const lastState = reportedStates[reportedStates.length - 1]
|
||||
expect(lastState?.video?.isHlsManualSupported).toBe(true)
|
||||
|
||||
// Switch to 720p (index 1)
|
||||
act(() => {
|
||||
ref.current?.changeResolution('720p')
|
||||
})
|
||||
expect(mockHlsInstance.currentLevel).toBe(1)
|
||||
|
||||
// Advance timers past the 1500ms safety timeout without seeked event
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(2000)
|
||||
})
|
||||
|
||||
// Now user performs an unrelated seek to frame 150 (currentTime = 5.0)
|
||||
video.currentTime = 5.0
|
||||
act(() => {
|
||||
video.dispatchEvent(new Event('seeked'))
|
||||
})
|
||||
|
||||
// video.currentTime must remain at frame 150, NOT rewound to frame 0
|
||||
expect(Math.floor(video.currentTime * 30 + 0.001)).toBe(150)
|
||||
expect(video.currentTime).toBeCloseTo(5.0167, 3)
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -6,10 +6,9 @@ import type { Annotation } from '@/ui/types'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import { Play, AudioLines } from 'lucide-react'
|
||||
import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
|
||||
import videojs from 'video.js'
|
||||
import type Player from 'video.js/dist/types/player'
|
||||
import Hls from 'hls.js'
|
||||
import { useFramePlayer } from './use-frame-player'
|
||||
import { resolveTotalFrames } from './utils'
|
||||
import { calculateFrameCenterTime, resolveTotalFrames } from './utils'
|
||||
import { clampFrame as clampFrameUtil } from '../../compare/compare-utils'
|
||||
import type { ComparePaneHandle, DisplayTranscode, PaneReportedState } from '../../compare/types'
|
||||
|
||||
@@ -92,8 +91,7 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
const playerRef = useRef<Player | null>(null)
|
||||
const videoContainerRef = useRef<HTMLDivElement>(null)
|
||||
const hlsRef = useRef<Hls | null>(null)
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const videoRef = useRef<HTMLVideoElement | null>(null)
|
||||
|
||||
@@ -132,6 +130,9 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
isHls ? 'Auto' : (initialRes?.resolution ?? ''),
|
||||
)
|
||||
const [isCurrentHdr, setIsCurrentHdr] = useState(initialRes?.hdr)
|
||||
const [activeAutoResolution, setActiveAutoResolution] = useState<string | undefined>(undefined)
|
||||
const pendingResolutionCorrectionRef = useRef<(() => void) | null>(null)
|
||||
const isHlsManualSupported = !isHls || Boolean(Hls.isSupported())
|
||||
const currentSrcRef = useRef(isHls ? file.media!.hls!.url : initialRes?.url)
|
||||
|
||||
const isAudio = file.proxyType === 'audio'
|
||||
@@ -174,78 +175,150 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
const res = isHls ? null : getInitialResolution(resolutions)
|
||||
setCurrentResolution(isHls ? 'Auto' : (res?.resolution ?? ''))
|
||||
setIsCurrentHdr(res?.hdr)
|
||||
setActiveAutoResolution(undefined)
|
||||
currentSrcRef.current = isHls ? file.media!.hls!.url : res?.url
|
||||
setHasStartedPlaying(false)
|
||||
}, [file.id, Boolean(isHls ? file.media?.hls?.url : initialRes?.url)])
|
||||
|
||||
// Initialize video.js
|
||||
// Initialize player (Hls.js or native HTML5 video)
|
||||
useEffect(() => {
|
||||
if (!videoContainerRef.current) return
|
||||
const videoElement = document.createElement('video-js')
|
||||
videoElement.classList.add('vjs-big-play-centered', '!h-full', '!w-full')
|
||||
videoElement.style.pointerEvents = 'none'
|
||||
videoContainerRef.current.appendChild(videoElement)
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
const targetSrc = isHls ? file.media!.hls!.url! : (currentSrcRef.current ?? '')
|
||||
const targetType = isHls ? 'application/x-mpegURL' : 'video/mp4'
|
||||
if (!targetSrc) return
|
||||
|
||||
const player = (playerRef.current = videojs(videoElement, {
|
||||
controls: false,
|
||||
autoplay: false,
|
||||
preload: 'auto',
|
||||
playsinline: true,
|
||||
sources: [{ src: targetSrc, type: targetType }],
|
||||
}))
|
||||
|
||||
const htmlVid = videoElement.querySelector('video')
|
||||
if (htmlVid) {
|
||||
videoRef.current = htmlVid
|
||||
} else {
|
||||
player.ready(() => {
|
||||
const techEl = player.tech({ iWillNotUseThisInPlugins: true })?.el() as HTMLVideoElement
|
||||
if (techEl) {
|
||||
videoRef.current = techEl
|
||||
}
|
||||
})
|
||||
if (hlsRef.current) {
|
||||
hlsRef.current.destroy()
|
||||
hlsRef.current = null
|
||||
}
|
||||
|
||||
player.on('play', () => {
|
||||
setIsPlayerReady(false)
|
||||
|
||||
let hls: Hls | null = null
|
||||
|
||||
if (isHls) {
|
||||
if (Hls.isSupported()) {
|
||||
hls = new Hls({
|
||||
autoStartLoad: true,
|
||||
startLevel: -1,
|
||||
capLevelToPlayerSize: false,
|
||||
enableWorker: true,
|
||||
})
|
||||
hlsRef.current = hls
|
||||
hls.attachMedia(video)
|
||||
hls.loadSource(targetSrc)
|
||||
|
||||
hls.on(Hls.Events.MANIFEST_PARSED, () => {
|
||||
setIsPlayerReady(true)
|
||||
})
|
||||
|
||||
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
|
||||
const level = hls?.levels[eventData.level]
|
||||
if (level) {
|
||||
setActiveAutoResolution(`${level.height}p`)
|
||||
}
|
||||
})
|
||||
|
||||
hls.on(Hls.Events.ERROR, (_event, eventData) => {
|
||||
if (eventData.fatal) {
|
||||
switch (eventData.type) {
|
||||
case Hls.ErrorTypes.NETWORK_ERROR:
|
||||
hls?.startLoad()
|
||||
break
|
||||
case Hls.ErrorTypes.MEDIA_ERROR:
|
||||
hls?.recoverMediaError()
|
||||
break
|
||||
default:
|
||||
hls?.destroy()
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
|
||||
video.src = targetSrc
|
||||
}
|
||||
} else {
|
||||
video.src = targetSrc
|
||||
}
|
||||
|
||||
const handlePlay = () => {
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
setIsPlaying(true)
|
||||
setHasStartedPlaying(true)
|
||||
onPlay?.()
|
||||
})
|
||||
player.on('pause', () => setIsPlaying(false))
|
||||
player.on('ended', () => setIsPlaying(false))
|
||||
}
|
||||
const handlePause = () => setIsPlaying(false)
|
||||
const handleEnded = () => setIsPlaying(false)
|
||||
const handleLoadedMetadata = () => setIsPlayerReady(true)
|
||||
const handleLoadStart = () => setIsPlayerReady(false)
|
||||
const handleProgress = () => {
|
||||
const vidDuration = video.duration || containerDuration || 0
|
||||
if (vidDuration > 0 && video.buffered.length > 0) {
|
||||
let bufferedEnd = 0
|
||||
for (let i = 0; i < video.buffered.length; i++) {
|
||||
if (
|
||||
video.buffered.start(i) <= video.currentTime &&
|
||||
video.currentTime <= video.buffered.end(i)
|
||||
) {
|
||||
bufferedEnd = video.buffered.end(i)
|
||||
break
|
||||
}
|
||||
}
|
||||
if (bufferedEnd === 0) {
|
||||
bufferedEnd = video.buffered.end(video.buffered.length - 1)
|
||||
}
|
||||
setBuffered((bufferedEnd / vidDuration) * 100)
|
||||
}
|
||||
}
|
||||
const handleVolumeChange = () => {
|
||||
setPlayerVolume(video.volume || 0)
|
||||
setPlayerMuted(video.muted || false)
|
||||
}
|
||||
const handleRateChange = () => setPlaybackRate(video.playbackRate || 1)
|
||||
|
||||
player.on('timeupdate', () => {
|
||||
setBuffered(player.bufferedPercent())
|
||||
})
|
||||
|
||||
player.on('loadedmetadata', () => {
|
||||
setIsPlayerReady(true)
|
||||
})
|
||||
player.on('loadstart', () => setIsPlayerReady(false))
|
||||
player.on('volumechange', () => {
|
||||
setPlayerVolume(player.volume() || 0)
|
||||
setPlayerMuted(player.muted() || false)
|
||||
})
|
||||
player.on('ratechange', () => setPlaybackRate(player.playbackRate() || 1))
|
||||
video.addEventListener('play', handlePlay)
|
||||
video.addEventListener('pause', handlePause)
|
||||
video.addEventListener('ended', handleEnded)
|
||||
video.addEventListener('loadedmetadata', handleLoadedMetadata)
|
||||
video.addEventListener('loadstart', handleLoadStart)
|
||||
video.addEventListener('timeupdate', handleProgress)
|
||||
video.addEventListener('progress', handleProgress)
|
||||
video.addEventListener('volumechange', handleVolumeChange)
|
||||
video.addEventListener('ratechange', handleRateChange)
|
||||
|
||||
return () => {
|
||||
if (player && !player.isDisposed()) {
|
||||
player.dispose()
|
||||
playerRef.current = null
|
||||
videoRef.current = null
|
||||
video.removeEventListener('play', handlePlay)
|
||||
video.removeEventListener('pause', handlePause)
|
||||
video.removeEventListener('ended', handleEnded)
|
||||
video.removeEventListener('loadedmetadata', handleLoadedMetadata)
|
||||
video.removeEventListener('loadstart', handleLoadStart)
|
||||
video.removeEventListener('timeupdate', handleProgress)
|
||||
video.removeEventListener('progress', handleProgress)
|
||||
video.removeEventListener('volumechange', handleVolumeChange)
|
||||
video.removeEventListener('ratechange', handleRateChange)
|
||||
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
|
||||
if (hlsRef.current) {
|
||||
hlsRef.current.destroy()
|
||||
hlsRef.current = null
|
||||
}
|
||||
}
|
||||
}, [file.id])
|
||||
|
||||
// Apply muted / volume (audio routing: only active side unmuted)
|
||||
useEffect(() => {
|
||||
const player = playerRef.current
|
||||
if (!player || !isPlayerReady) return
|
||||
player.muted(muted)
|
||||
player.volume(volume)
|
||||
const video = videoRef.current
|
||||
if (!video || !isPlayerReady) return
|
||||
video.muted = muted
|
||||
video.volume = volume
|
||||
}, [muted, volume, isPlayerReady])
|
||||
|
||||
// Report state upward
|
||||
@@ -266,6 +339,8 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
isCurrentHdr,
|
||||
resolutions,
|
||||
buffered,
|
||||
activeAutoResolution,
|
||||
isHlsManualSupported,
|
||||
},
|
||||
}
|
||||
onStateChange(state)
|
||||
@@ -282,6 +357,8 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
currentResolution,
|
||||
isCurrentHdr,
|
||||
buffered,
|
||||
activeAutoResolution,
|
||||
isHlsManualSupported,
|
||||
])
|
||||
|
||||
// Propagate active playhead time
|
||||
@@ -337,28 +414,77 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
|
||||
const changeResolution = useCallback(
|
||||
(resolution: string, hdr?: boolean) => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
|
||||
if (isHls) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const ql = (player as any).qualityLevels?.()
|
||||
if (!hlsRef.current) {
|
||||
// Safari native HLS fallback does not support manual rendition switching
|
||||
return
|
||||
}
|
||||
const hls = hlsRef.current
|
||||
const wasPaused = video.paused
|
||||
const targetFrame = currentFrameRef.current
|
||||
|
||||
if (resolution === 'Auto') {
|
||||
if (ql) {
|
||||
for (let i = 0; i < ql.length; i++) {
|
||||
ql[i].enabled = true
|
||||
}
|
||||
if (currentResolution === 'Auto' && hls.currentLevel === -1) {
|
||||
return
|
||||
}
|
||||
hls.currentLevel = -1
|
||||
setCurrentResolution('Auto')
|
||||
} else {
|
||||
const target = resolutions.find((r) => r.resolution === resolution)
|
||||
if (target && ql) {
|
||||
for (let i = 0; i < ql.length; i++) {
|
||||
ql[i].enabled = ql[i].height === target.height
|
||||
if (target) {
|
||||
const targetIndex = hls.levels.findIndex(
|
||||
(lvl) =>
|
||||
lvl.height === target.height ||
|
||||
Math.max(lvl.width, lvl.height) === Math.max(target.width, target.height),
|
||||
)
|
||||
if (targetIndex !== -1) {
|
||||
if (targetIndex === hls.currentLevel && currentResolution === resolution) {
|
||||
return
|
||||
}
|
||||
hls.currentLevel = targetIndex
|
||||
setCurrentResolution(resolution)
|
||||
setIsCurrentHdr(target.hdr)
|
||||
}
|
||||
}
|
||||
setCurrentResolution(resolution)
|
||||
setIsCurrentHdr(target?.hdr)
|
||||
}
|
||||
|
||||
if (wasPaused) {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
const cleanup = () => {
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
timer = null
|
||||
}
|
||||
video.removeEventListener('seeked', onSeeked)
|
||||
hls.off?.(Hls.Events.ERROR, cleanup)
|
||||
if (pendingResolutionCorrectionRef.current === cleanup) {
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
}
|
||||
|
||||
const onSeeked = () => {
|
||||
cleanup()
|
||||
const safeCenterTime = calculateFrameCenterTime(targetFrame, frameRate)
|
||||
const frameDuration = 1 / frameRate
|
||||
if (Math.abs(video.currentTime - safeCenterTime) > frameDuration / 4) {
|
||||
video.currentTime = safeCenterTime
|
||||
}
|
||||
}
|
||||
|
||||
// Safety timeout (1.5s): if no seeked event fires from rendition switch, cleanly detach
|
||||
timer = setTimeout(cleanup, 1500)
|
||||
pendingResolutionCorrectionRef.current = cleanup
|
||||
video.addEventListener('seeked', onSeeked)
|
||||
hls.once?.(Hls.Events.ERROR, cleanup)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -370,21 +496,24 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
)
|
||||
: resolutions.find((r) => r.resolution === resolution)
|
||||
if (!target) return
|
||||
const wasPlaying = !player.paused()
|
||||
const currentT = player.currentTime()
|
||||
const wasPlaying = !video.paused
|
||||
const currentT = video.currentTime
|
||||
setCurrentResolution(resolution)
|
||||
setIsCurrentHdr(target.hdr)
|
||||
currentSrcRef.current = target.url
|
||||
player.src({ type: 'video/mp4', src: target.url })
|
||||
player.one('loadedmetadata', () => {
|
||||
player.currentTime(currentT)
|
||||
video.src = target.url || ''
|
||||
const onLoadedMetadata = () => {
|
||||
video.removeEventListener('loadedmetadata', onLoadedMetadata)
|
||||
video.currentTime = currentT
|
||||
if (wasPlaying) {
|
||||
const p = player.play()
|
||||
const p = video.play()
|
||||
if (p !== undefined) p.catch(() => {})
|
||||
}
|
||||
})
|
||||
video.playbackRate = playbackRate
|
||||
}
|
||||
video.addEventListener('loadedmetadata', onLoadedMetadata)
|
||||
},
|
||||
[resolutions, isHls],
|
||||
[resolutions, isHls, frameRate, playbackRate, currentResolution],
|
||||
)
|
||||
|
||||
useImperativeHandle(
|
||||
@@ -392,47 +521,71 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
(): ComparePaneHandle => ({
|
||||
getKind: () => 'video',
|
||||
play: () => {
|
||||
const p = playerRef.current?.play()
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
const p = videoRef.current?.play()
|
||||
if (p !== undefined) p.catch(() => {})
|
||||
},
|
||||
pause: () => playerRef.current?.pause(),
|
||||
pause: () => videoRef.current?.pause(),
|
||||
togglePlay: () => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
if (player.paused() || player.ended()) {
|
||||
const p = player.play()
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
if (video.paused || video.ended) {
|
||||
const p = video.play()
|
||||
if (p !== undefined) p.catch(() => {})
|
||||
} else {
|
||||
player.pause()
|
||||
video.pause()
|
||||
}
|
||||
},
|
||||
seekToFrame: (frame) => {
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
setHasStartedPlaying(true)
|
||||
return seekToFrame(clampFrame(frame))
|
||||
},
|
||||
seekToSecond: (second) => {
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
setHasStartedPlaying(true)
|
||||
const frame = Math.floor(second * frameRate + 0.45)
|
||||
seekToFrame(clampFrame(frame))
|
||||
},
|
||||
stepFrame: (delta) => {
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
setHasStartedPlaying(true)
|
||||
return seekToFrame(clampFrame(currentFrameRef.current + delta))
|
||||
},
|
||||
setMuted: (m) => playerRef.current?.muted(m),
|
||||
setMuted: (m) => {
|
||||
if (videoRef.current) videoRef.current.muted = m
|
||||
},
|
||||
setVolume: (v) => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
player.volume(v)
|
||||
if (v > 0 && player.muted()) player.muted(false)
|
||||
else if (v === 0 && !player.muted()) player.muted(true)
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
video.volume = v
|
||||
if (v > 0 && video.muted) video.muted = false
|
||||
else if (v === 0 && !video.muted) video.muted = true
|
||||
},
|
||||
setPlaybackRate: (rate) => {
|
||||
if (videoRef.current) videoRef.current.playbackRate = rate
|
||||
},
|
||||
setPlaybackRate: (rate) => playerRef.current?.playbackRate(rate),
|
||||
toggleLoop: () => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
const next = !player.loop()
|
||||
player.loop(next)
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
const next = !video.loop
|
||||
video.loop = next
|
||||
setIsLooping(next)
|
||||
},
|
||||
changeResolution,
|
||||
@@ -512,14 +665,21 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
transformOrigin: '0 0',
|
||||
}}
|
||||
>
|
||||
<div
|
||||
ref={videoContainerRef}
|
||||
className="w-full h-full [&_.video-js]:!w-full [&_.video-js]:!h-full [&_video]:!w-full [&_video]:!h-full [&_video]:!block [&_video]:!object-contain"
|
||||
<video
|
||||
ref={videoRef}
|
||||
className="w-full h-full block object-contain pointer-events-none"
|
||||
playsInline
|
||||
preload="auto"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{isAudio && (
|
||||
<div ref={videoContainerRef} className="absolute inset-0 pointer-events-none opacity-0" />
|
||||
<video
|
||||
ref={videoRef}
|
||||
className="absolute inset-0 pointer-events-none opacity-0"
|
||||
playsInline
|
||||
preload="auto"
|
||||
/>
|
||||
)}
|
||||
|
||||
{isAudio ? (
|
||||
|
||||
@@ -190,7 +190,9 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
|
||||
<span>
|
||||
{!isAudio && state.currentResolution
|
||||
? state.currentResolution === 'Auto'
|
||||
? m.quality_auto()
|
||||
? state.activeAutoResolution
|
||||
? `${m.quality_auto()} (${state.activeAutoResolution})`
|
||||
: m.quality_auto()
|
||||
: state.currentResolution === 'Original'
|
||||
? m.original()
|
||||
: state.currentResolution
|
||||
@@ -234,13 +236,17 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
|
||||
const isSelected =
|
||||
state.currentResolution === res.resolution &&
|
||||
Boolean(state.isCurrentHdr) === Boolean(res.hdr)
|
||||
const isManualDisabled =
|
||||
Boolean(data.media?.isHls) && state.isHlsManualSupported === false
|
||||
return (
|
||||
<DropdownMenuItem
|
||||
key={itemKey}
|
||||
onClick={() => changeResolution(res)}
|
||||
disabled={isManualDisabled}
|
||||
onClick={() => !isManualDisabled && changeResolution(res)}
|
||||
className={cn(
|
||||
'flex w-full items-center justify-between text-xs cursor-pointer',
|
||||
'flex w-full items-center justify-between text-xs',
|
||||
isSelected ? 'text-primary font-medium' : 'text-foreground',
|
||||
isManualDisabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer',
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center gap-1.5">
|
||||
|
||||
@@ -44,6 +44,8 @@ export interface PlayerState {
|
||||
currentResolution: string // 'Original' or '720p', '480p' etc.
|
||||
currentSrc?: string
|
||||
isCurrentHdr?: boolean
|
||||
activeAutoResolution?: string
|
||||
isHlsManualSupported?: boolean
|
||||
}
|
||||
|
||||
export interface DisplayTranscode {
|
||||
@@ -332,7 +334,9 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
||||
<span className="flex items-center gap-1">
|
||||
<span>
|
||||
{state.currentResolution === 'Auto'
|
||||
? m.quality_auto()
|
||||
? state.activeAutoResolution
|
||||
? `${m.quality_auto()} (${state.activeAutoResolution})`
|
||||
: m.quality_auto()
|
||||
: state.currentResolution === 'Original'
|
||||
? m.original()
|
||||
: state.currentResolution}
|
||||
@@ -372,13 +376,17 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
||||
const isSelected =
|
||||
state.currentResolution === res.resolution &&
|
||||
Boolean(state.isCurrentHdr) === Boolean(res.hdr)
|
||||
const isManualDisabled =
|
||||
Boolean(data.media?.isHls) && state.isHlsManualSupported === false
|
||||
return (
|
||||
<DropdownMenuItem
|
||||
key={itemKey}
|
||||
onClick={() => changeResolution(res)}
|
||||
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',
|
||||
)}
|
||||
>
|
||||
<div className="flex items-center gap-1.5">
|
||||
|
||||
@@ -1,28 +1,52 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { act, cleanup, render } from '@testing-library/react'
|
||||
import { act, cleanup, fireEvent, 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 {
|
||||
const { mockHlsInstance, mockHlsConstructor } = vi.hoisted(() => {
|
||||
const instance = {
|
||||
attachMedia: vi.fn(),
|
||||
loadSource: vi.fn(),
|
||||
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),
|
||||
off: vi.fn(),
|
||||
once: vi.fn(),
|
||||
destroy: vi.fn(),
|
||||
levels: [
|
||||
{ height: 1080, width: 1920, bitrate: 5000000 },
|
||||
{ height: 720, width: 1280, bitrate: 2500000 },
|
||||
],
|
||||
currentLevel: -1,
|
||||
startLoad: vi.fn(),
|
||||
recoverMediaError: vi.fn(),
|
||||
}
|
||||
const constructor = vi.fn(function () {
|
||||
return instance
|
||||
})
|
||||
return { mockHlsInstance: instance, mockHlsConstructor: constructor }
|
||||
})
|
||||
|
||||
vi.mock('video.js', () => ({
|
||||
default: (...args: unknown[]) => videojsMock(...args),
|
||||
}))
|
||||
vi.mock('hls.js', () => {
|
||||
const MockHls = mockHlsConstructor
|
||||
// @ts-expect-error adding static mock property
|
||||
MockHls.isSupported = vi.fn(() => true)
|
||||
// @ts-expect-error adding static mock property
|
||||
MockHls.Events = {
|
||||
MANIFEST_PARSED: 'hlsManifestParsed',
|
||||
LEVEL_SWITCHED: 'hlsLevelSwitched',
|
||||
ERROR: 'hlsError',
|
||||
}
|
||||
// @ts-expect-error adding static mock property
|
||||
MockHls.ErrorTypes = {
|
||||
NETWORK_ERROR: 'networkError',
|
||||
MEDIA_ERROR: 'mediaError',
|
||||
OTHER_ERROR: 'otherError',
|
||||
}
|
||||
return {
|
||||
default: MockHls,
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@/ui/components/drawing-canvas', () => ({
|
||||
default: () => <div data-testid="drawing-canvas" />,
|
||||
@@ -34,7 +58,8 @@ vi.mock('@/ui/paraglide/messages.js', () => ({
|
||||
|
||||
describe('VideoViewer', () => {
|
||||
beforeEach(() => {
|
||||
videojsMock.mockClear()
|
||||
vi.clearAllMocks()
|
||||
mockHlsInstance.currentLevel = -1
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -88,39 +113,20 @@ describe('VideoViewer', () => {
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const { rerender } = render(<VideoViewer file={videoA} />)
|
||||
const { container, rerender } = render(<VideoViewer file={videoA} />)
|
||||
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
|
||||
|
||||
// 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',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
// Initial render should set video element src with video A URL
|
||||
expect(video.src).toBe('https://cdn.example.com/videoA-720p.mp4')
|
||||
|
||||
// Rerender with video B (simulating selecting another video from the carousel)
|
||||
rerender(<VideoViewer file={videoB} />)
|
||||
|
||||
// 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',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
// Native video element should now have video B's URL
|
||||
expect(video.src).toBe('https://cdn.example.com/videoB-1080p.mp4')
|
||||
})
|
||||
|
||||
it('initializes VideoJS when transcode URL becomes available after initially empty with same ID', () => {
|
||||
it('initializes video when transcode URL becomes available after initially empty with same ID', () => {
|
||||
const initialVideo: AssetInfo = {
|
||||
id: 'video-pending',
|
||||
name: 'pending.mp4',
|
||||
@@ -136,7 +142,6 @@ describe('VideoViewer', () => {
|
||||
videoTranscodes: [
|
||||
{
|
||||
resolution: '1080p',
|
||||
// URL not yet presigned (e.g. from file list payload)
|
||||
url: undefined as unknown as string,
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
@@ -160,27 +165,17 @@ describe('VideoViewer', () => {
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const { rerender } = render(<VideoViewer file={initialVideo} autoPlay={true} />)
|
||||
const { container, rerender } = render(<VideoViewer file={initialVideo} autoPlay={true} />)
|
||||
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
|
||||
|
||||
// Initially targetSrc is empty, videojs should not be initialized
|
||||
expect(videojsMock).not.toHaveBeenCalled()
|
||||
// Initially targetSrc is empty, video.src should be empty
|
||||
expect(video.src).toBe('')
|
||||
|
||||
// Rerender with detailedVideo (same id, but signed URL now available)
|
||||
rerender(<VideoViewer file={detailedVideo} autoPlay={true} />)
|
||||
|
||||
// VideoJS should now be initialized with the new URL
|
||||
expect(videojsMock).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
autoplay: false,
|
||||
sources: [
|
||||
{
|
||||
src: 'https://cdn.example.com/signed-pending-1080p.mp4',
|
||||
type: 'video/mp4',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
// Video element should now have the newly available signed URL
|
||||
expect(video.src).toBe('https://cdn.example.com/signed-pending-1080p.mp4')
|
||||
})
|
||||
|
||||
it('auto-selects HDR proxy when display supports HDR (dynamic-range: high)', () => {
|
||||
@@ -227,19 +222,10 @@ describe('VideoViewer', () => {
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
render(<VideoViewer file={hdrVideo} />)
|
||||
const { container } = render(<VideoViewer file={hdrVideo} />)
|
||||
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
|
||||
|
||||
expect(videojsMock).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
sources: [
|
||||
{
|
||||
src: 'https://cdn.example.com/video-1080p-hdr.mp4',
|
||||
type: 'video/mp4',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
expect(video.src).toBe('https://cdn.example.com/video-1080p-hdr.mp4')
|
||||
|
||||
window.matchMedia = originalMatchMedia
|
||||
})
|
||||
@@ -288,39 +274,15 @@ describe('VideoViewer', () => {
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
render(<VideoViewer file={hdrVideo} />)
|
||||
const { container } = render(<VideoViewer file={hdrVideo} />)
|
||||
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
|
||||
|
||||
expect(videojsMock).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
expect.objectContaining({
|
||||
sources: [
|
||||
{
|
||||
src: 'https://cdn.example.com/video-1080p.mp4',
|
||||
type: 'video/mp4',
|
||||
},
|
||||
],
|
||||
}),
|
||||
)
|
||||
expect(video.src).toBe('https://cdn.example.com/video-1080p.mp4')
|
||||
|
||||
window.matchMedia = originalMatchMedia
|
||||
})
|
||||
|
||||
it('shows play button and darkened overlay initially, but never again after playing (even when paused or scrubbed back to start)', () => {
|
||||
const listeners: Record<string, () => void> = {}
|
||||
videojsMock.mockImplementation(() => ({
|
||||
on: vi.fn((event: string, cb: () => void) => {
|
||||
listeners[event] = cb
|
||||
}),
|
||||
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),
|
||||
}))
|
||||
|
||||
const testVideo: AssetInfo = {
|
||||
id: 'test-video-lifecycle',
|
||||
name: 'test.mp4',
|
||||
@@ -346,27 +308,191 @@ describe('VideoViewer', () => {
|
||||
|
||||
const { container } = render(<VideoViewer file={testVideo} />)
|
||||
const videoArea = container.querySelector('[data-testid="video-area"]')
|
||||
const video = videoArea?.querySelector('video') as HTMLVideoElement
|
||||
expect(videoArea).toBeTruthy()
|
||||
expect(video).toBeTruthy()
|
||||
|
||||
// 1. Initially (before playing): play button & 20% black tint are present
|
||||
expect(videoArea?.querySelector('.bg-black\\/20')).not.toBeNull()
|
||||
|
||||
// 2. Start playback
|
||||
act(() => {
|
||||
listeners['play']?.()
|
||||
video.dispatchEvent(new Event('play'))
|
||||
})
|
||||
expect(videoArea?.querySelector('.bg-black\\/20')).toBeNull()
|
||||
|
||||
// 3. Pause video: overlay must NOT show again
|
||||
act(() => {
|
||||
listeners['pause']?.()
|
||||
video.dispatchEvent(new Event('pause'))
|
||||
})
|
||||
expect(videoArea?.querySelector('.bg-black\\/20')).toBeNull()
|
||||
|
||||
// 4. Drag seekbar / time back to 0: overlay must STILL NOT show
|
||||
act(() => {
|
||||
listeners['timeupdate']?.()
|
||||
video.dispatchEvent(new Event('timeupdate'))
|
||||
})
|
||||
expect(videoArea?.querySelector('.bg-black\\/20')).toBeNull()
|
||||
})
|
||||
|
||||
it('initializes Hls.js with capLevelToPlayerSize false when video is HLS', () => {
|
||||
const hlsVideo: AssetInfo = {
|
||||
id: 'video-hls',
|
||||
name: 'video-hls.m3u8',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
isHls: true,
|
||||
hls: {
|
||||
key: 'hls-key',
|
||||
url: 'https://cdn.example.com/master.m3u8',
|
||||
resolutions: [
|
||||
{ width: 1920, height: 1080, resolution: '1080p' },
|
||||
{ width: 1280, height: 720, resolution: '720p' },
|
||||
],
|
||||
},
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
},
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const { container } = render(<VideoViewer file={hlsVideo} />)
|
||||
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
|
||||
|
||||
expect(mockHlsConstructor).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
autoStartLoad: true,
|
||||
startLevel: -1,
|
||||
capLevelToPlayerSize: false,
|
||||
enableWorker: true,
|
||||
}),
|
||||
)
|
||||
expect(mockHlsInstance.attachMedia).toHaveBeenCalledWith(video)
|
||||
expect(mockHlsInstance.loadSource).toHaveBeenCalledWith('https://cdn.example.com/master.m3u8')
|
||||
})
|
||||
|
||||
it('switches HLS resolution immediately using currentLevel', () => {
|
||||
const hlsVideo: AssetInfo = {
|
||||
id: 'video-hls-switch',
|
||||
name: 'video-hls-switch.m3u8',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
isHls: true,
|
||||
hls: {
|
||||
key: 'hls-key',
|
||||
url: 'https://cdn.example.com/master.m3u8',
|
||||
resolutions: [
|
||||
{ width: 1920, height: 1080, resolution: '1080p' },
|
||||
{ width: 1280, height: 720, resolution: '720p' },
|
||||
],
|
||||
},
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
},
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const { container } = render(<VideoViewer file={hlsVideo} />)
|
||||
|
||||
// Open settings / quality dropdown
|
||||
const settingsButton = container.querySelector('button:has(.lucide-settings)')
|
||||
expect(settingsButton).toBeTruthy()
|
||||
fireEvent.pointerDown(settingsButton!, { pointerType: 'mouse', button: 0 })
|
||||
fireEvent.click(settingsButton!)
|
||||
|
||||
// Click 720p (index 1 in levels: 1080p is index 0, 720p is index 1)
|
||||
const items = document.querySelectorAll('[role="menuitem"]')
|
||||
const item720p = Array.from(items).find((el) => el.textContent?.includes('720p'))
|
||||
expect(item720p).toBeTruthy()
|
||||
|
||||
act(() => {
|
||||
fireEvent.click(item720p!)
|
||||
})
|
||||
|
||||
// currentLevel should be set immediately to 1 (720p)
|
||||
expect(mockHlsInstance.currentLevel).toBe(1)
|
||||
|
||||
// Click Auto
|
||||
const itemAuto = Array.from(document.querySelectorAll('[role="menuitem"]')).find(
|
||||
(el) => el.textContent?.includes('Auto') || el.textContent === '',
|
||||
)
|
||||
if (itemAuto) {
|
||||
act(() => {
|
||||
fireEvent.click(itemAuto)
|
||||
})
|
||||
// Auto should reset currentLevel to -1
|
||||
expect(mockHlsInstance.currentLevel).toBe(-1)
|
||||
}
|
||||
})
|
||||
|
||||
it('does not hijack subsequent seeks if paused HLS switch did not emit seeked', () => {
|
||||
vi.useFakeTimers()
|
||||
try {
|
||||
const hlsVideo: AssetInfo = {
|
||||
id: 'video-hls-stale-seek',
|
||||
name: 'video-hls-stale-seek.m3u8',
|
||||
proxyType: 'video',
|
||||
media: {
|
||||
isHls: true,
|
||||
hls: {
|
||||
key: 'hls-key',
|
||||
url: 'https://cdn.example.com/master.m3u8',
|
||||
resolutions: [
|
||||
{ width: 1920, height: 1080, resolution: '1080p' },
|
||||
{ width: 1280, height: 720, resolution: '720p' },
|
||||
],
|
||||
},
|
||||
metadata: {
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
duration: 10,
|
||||
frameRate: 30,
|
||||
totalFrames: 300,
|
||||
},
|
||||
},
|
||||
} as unknown as AssetInfo
|
||||
|
||||
const { container } = render(<VideoViewer file={hlsVideo} />)
|
||||
const video = container.querySelector('[data-testid="video-area"] video') as HTMLVideoElement
|
||||
|
||||
// Video is paused at 0s (frame 0)
|
||||
Object.defineProperty(video, 'paused', { value: true, configurable: true })
|
||||
video.currentTime = 0
|
||||
|
||||
// Open settings and click 720p
|
||||
const settingsButton = container.querySelector('button:has(.lucide-settings)')
|
||||
fireEvent.pointerDown(settingsButton!, { pointerType: 'mouse', button: 0 })
|
||||
fireEvent.click(settingsButton!)
|
||||
const item720p = Array.from(document.querySelectorAll('[role="menuitem"]')).find((el) =>
|
||||
el.textContent?.includes('720p'),
|
||||
)
|
||||
act(() => {
|
||||
fireEvent.click(item720p!)
|
||||
})
|
||||
|
||||
// Simulate that no seeked event occurred within the timeout (e.g. 1500ms elapses)
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(2000)
|
||||
})
|
||||
|
||||
// Now user performs an unrelated seek to frame 150 (currentTime = 5.0)
|
||||
video.currentTime = 5.0
|
||||
act(() => {
|
||||
fireEvent(video, new Event('seeked'))
|
||||
})
|
||||
|
||||
// video.currentTime must remain at frame 150, NOT rewound to frame 0
|
||||
expect(Math.floor(video.currentTime * 30 + 0.001)).toBe(150)
|
||||
expect(video.currentTime).toBeCloseTo(5.0167, 3)
|
||||
} finally {
|
||||
vi.useRealTimers()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,10 +2,9 @@ import { client } from '@/ui/api/client'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { Play, AudioLines } from 'lucide-react'
|
||||
import React, { useCallback, useEffect, useRef, useState, useImperativeHandle } from 'react'
|
||||
import videojs from 'video.js'
|
||||
import type Player from 'video.js/dist/types/player'
|
||||
import Hls from 'hls.js'
|
||||
import { useFramePlayer } from './use-frame-player'
|
||||
import { resolveTotalFrames } from './utils'
|
||||
import { calculateFrameCenterTime, resolveTotalFrames } from './utils'
|
||||
import { VideoControlBar, type PlayerState, type DisplayTranscode } from './video-control-bar'
|
||||
import { MobileVideoControlBar } from './mobile-video-control-bar'
|
||||
import { useIsMobile } from '@/ui/hooks/use-mobile'
|
||||
@@ -31,8 +30,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
},
|
||||
ref,
|
||||
) => {
|
||||
const localPlayerRef = useRef<Player | null>(null)
|
||||
const playerRef = localPlayerRef
|
||||
const hlsRef = useRef<Hls | null>(null)
|
||||
const videoRef = useRef<HTMLVideoElement | null>(null)
|
||||
const [activeAutoResolution, setActiveAutoResolution] = useState<string | undefined>(undefined)
|
||||
const [isHlsManualSupported, setIsHlsManualSupported] = useState<boolean>(true)
|
||||
const isHls = Boolean(data.media?.isHls && data.media.hls?.url)
|
||||
const baseTranscodes = isHls
|
||||
? (data.media?.hls?.resolutions ?? [])
|
||||
@@ -59,8 +60,6 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
// Only transcoded proxy versions are ever displayed; the raw original file
|
||||
// is never used as a playback source.
|
||||
const hasMedia = (isHls || resolutions.length > 0) && !!data.media?.metadata
|
||||
// We use a container ref to manually append the video element
|
||||
const videoContainerRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
// Logic to select best resolution based on screen size and dynamic range
|
||||
const getInitialResolution = (): DisplayTranscode | null => {
|
||||
@@ -120,35 +119,38 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const rootRef = useRef<HTMLDivElement>(null)
|
||||
const controlsTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||
const pendingResolutionCorrectionRef = useRef<(() => void) | null>(null)
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
play: () => {
|
||||
if (localPlayerRef.current) {
|
||||
localPlayerRef.current.play()
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
videoRef.current?.play().catch(() => {})
|
||||
},
|
||||
pause: () => {
|
||||
if (localPlayerRef.current) {
|
||||
localPlayerRef.current.pause()
|
||||
}
|
||||
videoRef.current?.pause()
|
||||
},
|
||||
seekTo: (second: number) => {
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
setHasStartedPlaying(true)
|
||||
if (localPlayerRef.current) {
|
||||
localPlayerRef.current.currentTime(second)
|
||||
if (videoRef.current) {
|
||||
videoRef.current.currentTime = second
|
||||
}
|
||||
},
|
||||
getCurrentTime: () => {
|
||||
return (localPlayerRef.current ? localPlayerRef.current.currentTime() : 0) ?? 0
|
||||
return (videoRef.current ? videoRef.current.currentTime : 0) ?? 0
|
||||
},
|
||||
getDuration: () => {
|
||||
return (localPlayerRef.current ? localPlayerRef.current.duration() : 0) ?? 0
|
||||
return (videoRef.current ? videoRef.current.duration : 0) ?? 0
|
||||
},
|
||||
}))
|
||||
const isOverControlsRef = useRef(false)
|
||||
|
||||
const [videoHtmlEl, setVideoHtmlEl] = useState<HTMLVideoElement | undefined>(undefined)
|
||||
const videoRef = useRef<HTMLVideoElement | null>(null)
|
||||
const [zoom, setZoom] = useState(1)
|
||||
const [pan, setPan] = useState({ x: 0, y: 0 })
|
||||
const [hasManuallyZoomed, setHasManuallyZoomed] = useState(false)
|
||||
@@ -166,6 +168,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
currentSrc: isHls ? (data.media?.hls?.url ?? '') : (res?.url ?? ''),
|
||||
isCurrentHdr: res?.hdr ?? false,
|
||||
}))
|
||||
setActiveAutoResolution(undefined)
|
||||
setHasManuallyZoomed(false)
|
||||
setIsPlayerReady(false)
|
||||
setBuffered(0)
|
||||
@@ -274,6 +277,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
|
||||
const handleSeekToFrame = useCallback(
|
||||
(frame: number) => {
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
setHasStartedPlaying(true)
|
||||
return seekToFrame(frame)
|
||||
},
|
||||
@@ -289,6 +296,8 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
currentTime,
|
||||
duration,
|
||||
progress,
|
||||
activeAutoResolution,
|
||||
isHlsManualSupported,
|
||||
}
|
||||
|
||||
// Trigger time update event reactively
|
||||
@@ -298,146 +307,199 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
}
|
||||
}, [currentFrame, frameRate, onTimeUpdate])
|
||||
|
||||
// Initialize Video.js
|
||||
// Initialize player (Hls.js or native HTML5 video)
|
||||
const targetSrc = isHls ? (data.media?.hls?.url ?? '') : (getInitialResolution()?.url ?? '')
|
||||
|
||||
useEffect(() => {
|
||||
if (!videoContainerRef.current) return
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
const targetSrc = isHls ? (data.media?.hls?.url ?? '') : (getInitialResolution()?.url ?? '')
|
||||
const targetType = isHls ? 'application/x-mpegURL' : 'video/mp4'
|
||||
if (!targetSrc) return
|
||||
|
||||
// Clean up previous player if exists
|
||||
if (playerRef.current && !playerRef.current.isDisposed()) {
|
||||
playerRef.current.dispose()
|
||||
playerRef.current = null
|
||||
videoRef.current = null
|
||||
// Clean up previous HLS instance if exists
|
||||
if (hlsRef.current) {
|
||||
hlsRef.current.destroy()
|
||||
hlsRef.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')
|
||||
videoElement.classList.add('vjs-big-play-centered', '!h-full', '!w-full')
|
||||
setIsPlayerReady(false)
|
||||
setActiveAutoResolution(undefined)
|
||||
|
||||
// Keep it in DOM, enable visible rendering
|
||||
videoElement.style.pointerEvents = 'none'
|
||||
const startAutoPlay = () => {
|
||||
const playPromise = video.play()
|
||||
if (playPromise !== undefined) {
|
||||
playPromise.catch(() => {
|
||||
video.muted = true
|
||||
setState((prev) => ({ ...prev, isMuted: true }))
|
||||
video.play()?.catch(() => {})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Append to our container
|
||||
videoContainerRef.current.appendChild(videoElement)
|
||||
let hls: Hls | null = null
|
||||
|
||||
// 2. Initialize the player
|
||||
const player = (playerRef.current = videojs(videoElement, {
|
||||
controls: false,
|
||||
autoplay: false,
|
||||
preload: 'auto',
|
||||
playsinline: true,
|
||||
sources: [
|
||||
{
|
||||
src: targetSrc,
|
||||
type: targetType,
|
||||
},
|
||||
],
|
||||
}))
|
||||
if (isHls) {
|
||||
if (Hls.isSupported()) {
|
||||
setIsHlsManualSupported(true)
|
||||
hls = new Hls({
|
||||
autoStartLoad: true,
|
||||
startLevel: -1,
|
||||
capLevelToPlayerSize: false,
|
||||
enableWorker: true,
|
||||
})
|
||||
hlsRef.current = hls
|
||||
hls.attachMedia(video)
|
||||
hls.loadSource(targetSrc)
|
||||
|
||||
if (autoPlay) {
|
||||
const startAutoPlay = () => {
|
||||
if (player.isDisposed()) return
|
||||
const playPromise = player.play()
|
||||
if (playPromise !== undefined) {
|
||||
playPromise.catch(() => {
|
||||
if (player.isDisposed()) return
|
||||
player.muted(true)
|
||||
setState((prev) => ({ ...prev, isMuted: true }))
|
||||
player.play()?.catch(() => {})
|
||||
})
|
||||
hls.on(Hls.Events.MANIFEST_PARSED, () => {
|
||||
setIsPlayerReady(true)
|
||||
if (autoPlay) {
|
||||
if (video.readyState >= 2) {
|
||||
startAutoPlay()
|
||||
} else {
|
||||
video.addEventListener('canplay', startAutoPlay, { once: true })
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
|
||||
const level = hls?.levels[eventData.level]
|
||||
if (level) {
|
||||
const h = level.height || Math.min(level.width, level.height)
|
||||
setActiveAutoResolution(h ? `${h}p` : undefined)
|
||||
}
|
||||
})
|
||||
|
||||
hls.on(Hls.Events.ERROR, (_event, eventData) => {
|
||||
if (eventData.fatal) {
|
||||
switch (eventData.type) {
|
||||
case Hls.ErrorTypes.NETWORK_ERROR:
|
||||
hls?.startLoad()
|
||||
break
|
||||
case Hls.ErrorTypes.MEDIA_ERROR:
|
||||
hls?.recoverMediaError()
|
||||
break
|
||||
default:
|
||||
hls?.destroy()
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
|
||||
// Native Safari HLS fallback
|
||||
setIsHlsManualSupported(false)
|
||||
video.src = targetSrc
|
||||
if (autoPlay) {
|
||||
if (video.readyState >= 2) {
|
||||
startAutoPlay()
|
||||
} else {
|
||||
video.addEventListener('canplay', startAutoPlay, { once: true })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
player.ready(() => {
|
||||
if (player.isDisposed()) return
|
||||
if (player.readyState() >= 2) {
|
||||
} else {
|
||||
// Native MP4
|
||||
video.src = targetSrc
|
||||
if (autoPlay) {
|
||||
if (video.readyState >= 2) {
|
||||
startAutoPlay()
|
||||
} else {
|
||||
player.one('canplay', startAutoPlay)
|
||||
video.addEventListener('canplay', startAutoPlay, { once: true })
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Extract video element for Konva
|
||||
const htmlVid = videoElement.querySelector('video')
|
||||
if (htmlVid) {
|
||||
setVideoHtmlEl(htmlVid)
|
||||
videoRef.current = htmlVid
|
||||
} else {
|
||||
// Fallback: try player.tech().el()
|
||||
// Wait for ready?
|
||||
player.ready(() => {
|
||||
const techEl = player.tech({ iWillNotUseThisInPlugins: true })?.el() as HTMLVideoElement
|
||||
if (techEl) {
|
||||
setVideoHtmlEl(techEl)
|
||||
videoRef.current = techEl
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
player.on('play', () => {
|
||||
const handlePlay = () => {
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
setState((p) => ({ ...p, isPlaying: true }))
|
||||
setHasStartedPlaying(true)
|
||||
onPlay?.()
|
||||
})
|
||||
player.on('pause', () => {
|
||||
}
|
||||
const handlePause = () => {
|
||||
setState((p) => ({ ...p, isPlaying: false }))
|
||||
onPause?.()
|
||||
})
|
||||
player.on('ended', () => setState((p) => ({ ...p, isPlaying: false })))
|
||||
player.on('loadedmetadata', () => {
|
||||
}
|
||||
const handleEnded = () => {
|
||||
setState((p) => ({ ...p, isPlaying: false }))
|
||||
}
|
||||
const handleLoadedMetadata = () => {
|
||||
setIsPlayerReady(true)
|
||||
})
|
||||
player.on('loadstart', () => {
|
||||
}
|
||||
const handleLoadStart = () => {
|
||||
setIsPlayerReady(false)
|
||||
})
|
||||
|
||||
player.on('timeupdate', () => {
|
||||
const playerDuration = player.duration() || data.media?.metadata?.duration || 0
|
||||
|
||||
const bufferedEnd = player.bufferedEnd()
|
||||
if (playerDuration > 0) {
|
||||
setBuffered((bufferedEnd / playerDuration) * 100)
|
||||
}
|
||||
})
|
||||
|
||||
player.on('volumechange', () => {
|
||||
const vol = player.volume()
|
||||
const muted = player.muted()
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
volume: vol || 0,
|
||||
isMuted: muted || false,
|
||||
}))
|
||||
})
|
||||
|
||||
player.on('ratechange', () => {
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
playbackRate: player.playbackRate() || 1,
|
||||
}))
|
||||
})
|
||||
|
||||
player.on('error', () => {
|
||||
console.error('VideoJS Error:', player.error())
|
||||
})
|
||||
|
||||
// Set initial state
|
||||
player.volume(state.volume)
|
||||
|
||||
// 4. Cleanup
|
||||
return () => {
|
||||
if (player && !player.isDisposed()) {
|
||||
player.dispose()
|
||||
playerRef.current = null
|
||||
videoRef.current = null
|
||||
}
|
||||
const handleProgress = () => {
|
||||
const vidDuration = video.duration || data.media?.metadata?.duration || 0
|
||||
if (vidDuration > 0 && video.buffered.length > 0) {
|
||||
let bufferedEnd = 0
|
||||
for (let i = 0; i < video.buffered.length; i++) {
|
||||
if (
|
||||
video.buffered.start(i) <= video.currentTime &&
|
||||
video.currentTime <= video.buffered.end(i)
|
||||
) {
|
||||
bufferedEnd = video.buffered.end(i)
|
||||
break
|
||||
}
|
||||
}
|
||||
if (bufferedEnd === 0) {
|
||||
bufferedEnd = video.buffered.end(video.buffered.length - 1)
|
||||
}
|
||||
setBuffered((bufferedEnd / vidDuration) * 100)
|
||||
}
|
||||
}
|
||||
}, [data.id, Boolean(isHls ? data.media?.hls?.url : initialRes?.url), autoPlay])
|
||||
const handleVolumeChange = () => {
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
volume: video.volume,
|
||||
isMuted: video.muted,
|
||||
}))
|
||||
}
|
||||
const handleRateChange = () => {
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
playbackRate: video.playbackRate,
|
||||
}))
|
||||
}
|
||||
const handleError = () => {
|
||||
console.error('Video Error:', video.error)
|
||||
}
|
||||
|
||||
video.addEventListener('play', handlePlay)
|
||||
video.addEventListener('pause', handlePause)
|
||||
video.addEventListener('ended', handleEnded)
|
||||
video.addEventListener('loadedmetadata', handleLoadedMetadata)
|
||||
video.addEventListener('loadstart', handleLoadStart)
|
||||
video.addEventListener('timeupdate', handleProgress)
|
||||
video.addEventListener('progress', handleProgress)
|
||||
video.addEventListener('volumechange', handleVolumeChange)
|
||||
video.addEventListener('ratechange', handleRateChange)
|
||||
video.addEventListener('error', handleError)
|
||||
|
||||
// Set initial state
|
||||
video.volume = state.volume
|
||||
|
||||
// Cleanup
|
||||
return () => {
|
||||
video.removeEventListener('play', handlePlay)
|
||||
video.removeEventListener('pause', handlePause)
|
||||
video.removeEventListener('ended', handleEnded)
|
||||
video.removeEventListener('loadedmetadata', handleLoadedMetadata)
|
||||
video.removeEventListener('loadstart', handleLoadStart)
|
||||
video.removeEventListener('timeupdate', handleProgress)
|
||||
video.removeEventListener('progress', handleProgress)
|
||||
video.removeEventListener('volumechange', handleVolumeChange)
|
||||
video.removeEventListener('ratechange', handleRateChange)
|
||||
video.removeEventListener('error', handleError)
|
||||
|
||||
if (hlsRef.current) {
|
||||
hlsRef.current.destroy()
|
||||
hlsRef.current = null
|
||||
}
|
||||
}
|
||||
}, [data.id, targetSrc, isHls, autoPlay])
|
||||
|
||||
// Handle changes to startTime (e.g., clicking different chunks in search results)
|
||||
useEffect(() => {
|
||||
@@ -514,11 +576,16 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
// Disable click-to-play if drawing
|
||||
if (useAnnotationStore.getState().isDrawing) return
|
||||
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
|
||||
if (player.paused() || player.ended()) {
|
||||
const playPromise = player.play()
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
if (video.paused || video.ended) {
|
||||
const playPromise = video.play()
|
||||
if (playPromise !== undefined) {
|
||||
playPromise.catch((error) => {
|
||||
// Ignore AbortError which happens when pausing rapidly after playing
|
||||
@@ -527,7 +594,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
})
|
||||
}
|
||||
} else {
|
||||
player.pause()
|
||||
video.pause()
|
||||
}
|
||||
}, [])
|
||||
|
||||
@@ -573,10 +640,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
}, [togglePlay])
|
||||
|
||||
const toggleLoop = () => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
const newLoop = !state.isLooping
|
||||
player.loop(newLoop)
|
||||
video.loop = newLoop
|
||||
setState((prev) => ({ ...prev, isLooping: newLoop }))
|
||||
}
|
||||
|
||||
@@ -588,76 +655,121 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
isMuted: newVolume === 0, // If dragging to 0, consider it muted
|
||||
}))
|
||||
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
// Set volume first so if we unmute, it is at the correct level
|
||||
player.volume(newVolume)
|
||||
video.volume = newVolume
|
||||
|
||||
// Manage mute state based on volume
|
||||
if (newVolume > 0 && player.muted()) {
|
||||
player.muted(false)
|
||||
} else if (newVolume === 0 && !player.muted()) {
|
||||
player.muted(true)
|
||||
if (newVolume > 0 && video.muted) {
|
||||
video.muted = false
|
||||
} else if (newVolume === 0 && !video.muted) {
|
||||
video.muted = true
|
||||
}
|
||||
}
|
||||
|
||||
const toggleMute = () => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
const isMuted = player.muted()
|
||||
const isMuted = video.muted
|
||||
if (isMuted) {
|
||||
player.muted(false)
|
||||
video.muted = false
|
||||
// If volume was 0 (e.g. user dragged to 0), restore to default 0.5 so they hear something
|
||||
if (player.volume() === 0) {
|
||||
player.volume(0.5)
|
||||
if (video.volume === 0) {
|
||||
video.volume = 0.5
|
||||
}
|
||||
} else {
|
||||
player.muted(true)
|
||||
video.muted = true
|
||||
}
|
||||
}
|
||||
|
||||
const changePlaybackRate = (rate: number) => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
player.playbackRate(rate)
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
video.playbackRate = rate
|
||||
setState((prev) => ({ ...prev, playbackRate: rate }))
|
||||
}
|
||||
|
||||
const changeResolution = (res: DisplayTranscode) => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
const video = videoRef.current
|
||||
if (!video) return
|
||||
|
||||
if (pendingResolutionCorrectionRef.current) {
|
||||
pendingResolutionCorrectionRef.current()
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
|
||||
if (isHls && hlsRef.current) {
|
||||
const hls = hlsRef.current
|
||||
const wasPaused = video.paused
|
||||
const targetFrame = currentFrame
|
||||
|
||||
if (isHls) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const ql = (player as any).qualityLevels?.()
|
||||
if (res.resolution === 'Auto') {
|
||||
if (ql) {
|
||||
for (let i = 0; i < ql.length; i++) {
|
||||
ql[i].enabled = true
|
||||
}
|
||||
if (state.currentResolution === 'Auto' && hls.currentLevel === -1) {
|
||||
return
|
||||
}
|
||||
hls.currentLevel = -1
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
currentResolution: 'Auto',
|
||||
}))
|
||||
} else {
|
||||
if (ql) {
|
||||
for (let i = 0; i < ql.length; i++) {
|
||||
ql[i].enabled = ql[i].height === res.height
|
||||
const targetIndex = hls.levels.findIndex(
|
||||
(lvl) =>
|
||||
lvl.height === res.height ||
|
||||
Math.max(lvl.width, lvl.height) === Math.max(res.width, res.height),
|
||||
)
|
||||
if (targetIndex !== -1) {
|
||||
if (targetIndex === hls.currentLevel && state.currentResolution === res.resolution) {
|
||||
return
|
||||
}
|
||||
hls.currentLevel = targetIndex
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
currentResolution: res.resolution,
|
||||
isCurrentHdr: res.hdr ?? false,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
// Guarantee frame accuracy if paused: re-nudge to safe center time once fragment is decoded
|
||||
if (wasPaused) {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
const cleanup = () => {
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
timer = null
|
||||
}
|
||||
video.removeEventListener('seeked', onSeeked)
|
||||
hls.off?.(Hls.Events.ERROR, cleanup)
|
||||
if (pendingResolutionCorrectionRef.current === cleanup) {
|
||||
pendingResolutionCorrectionRef.current = null
|
||||
}
|
||||
}
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
currentResolution: res.resolution,
|
||||
isCurrentHdr: res.hdr ?? false,
|
||||
}))
|
||||
|
||||
const onSeeked = () => {
|
||||
cleanup()
|
||||
const safeCenterTime = calculateFrameCenterTime(targetFrame, frameRate)
|
||||
const frameDuration = 1 / frameRate
|
||||
if (Math.abs(video.currentTime - safeCenterTime) > frameDuration / 4) {
|
||||
video.currentTime = safeCenterTime
|
||||
}
|
||||
}
|
||||
|
||||
// Safety timeout (1.5s): if no seeked event fires from rendition switch, cleanly detach
|
||||
timer = setTimeout(cleanup, 1500)
|
||||
pendingResolutionCorrectionRef.current = cleanup
|
||||
video.addEventListener('seeked', onSeeked)
|
||||
hls.once?.(Hls.Events.ERROR, cleanup)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const wasPlaying = !player.paused()
|
||||
const currentT = player.currentTime()
|
||||
const wasPlaying = !video.paused
|
||||
const currentT = video.currentTime
|
||||
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
@@ -666,26 +778,27 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
isCurrentHdr: res.hdr ?? false,
|
||||
}))
|
||||
|
||||
player.src({ type: 'video/mp4', src: res.url })
|
||||
video.src = res.url || ''
|
||||
|
||||
player.one('loadedmetadata', () => {
|
||||
player.currentTime(currentT)
|
||||
const onLoadedMetadata = () => {
|
||||
video.removeEventListener('loadedmetadata', onLoadedMetadata)
|
||||
video.currentTime = currentT
|
||||
if (wasPlaying) {
|
||||
const playPromise = player.play()
|
||||
const playPromise = video.play()
|
||||
if (playPromise !== undefined) {
|
||||
playPromise.catch((error) => {
|
||||
if (error.name === 'AbortError') return
|
||||
console.error('Play after seek failed:', error)
|
||||
if (error.name !== 'AbortError') console.error('Play after seek failed:', error)
|
||||
})
|
||||
}
|
||||
}
|
||||
player.playbackRate(state.playbackRate)
|
||||
})
|
||||
video.playbackRate = state.playbackRate
|
||||
}
|
||||
video.addEventListener('loadedmetadata', onLoadedMetadata)
|
||||
}
|
||||
|
||||
const toggleFullScreen = () => {
|
||||
const rootEl = rootRef.current
|
||||
const videoEl = videoHtmlEl || videoRef.current
|
||||
const videoEl = videoRef.current
|
||||
|
||||
// 1. Standard W3C Fullscreen API (Desktop Chrome/Firefox/Safari, Android, iPadOS)
|
||||
if (rootEl && typeof rootEl.requestFullscreen === 'function') {
|
||||
@@ -763,7 +876,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
document.addEventListener('fullscreenchange', onFsChange)
|
||||
|
||||
// iOS WebKit fullscreen events on <video>
|
||||
const videoEl = videoHtmlEl || videoRef.current
|
||||
const videoEl = videoRef.current
|
||||
const onWebkitBeginFs = () => {
|
||||
setState((prev) => ({ ...prev, isFullScreen: true }))
|
||||
}
|
||||
@@ -782,7 +895,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
videoEl.removeEventListener('webkitendfullscreen', onWebkitEndFs)
|
||||
}
|
||||
}
|
||||
}, [videoHtmlEl])
|
||||
}, [])
|
||||
|
||||
// Center pan is owned by `pan` state (set by auto-fit / gestures).
|
||||
const scale = zoom
|
||||
@@ -819,7 +932,6 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
'flex-1 bg-black cursor-pointer relative flex items-center justify-center overflow-hidden min-h-0 touch-none',
|
||||
)}
|
||||
onClick={togglePlay}
|
||||
data-vjs-player
|
||||
data-testid="video-area"
|
||||
>
|
||||
{/* Native Video Layer (Hardware-accelerated, full HDR EDR) */}
|
||||
@@ -835,16 +947,20 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
transformOrigin: '0 0',
|
||||
}}
|
||||
>
|
||||
<div
|
||||
ref={videoContainerRef}
|
||||
className="w-full h-full [&_.video-js]:!w-full [&_.video-js]:!h-full [&_video]:!w-full [&_video]:!h-full [&_video]:!block [&_video]:!object-contain"
|
||||
<video
|
||||
ref={videoRef}
|
||||
className="w-full h-full block object-contain pointer-events-none"
|
||||
playsInline
|
||||
preload="auto"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{isAudio && (
|
||||
<div
|
||||
ref={videoContainerRef}
|
||||
<video
|
||||
ref={videoRef}
|
||||
className="absolute inset-0 pointer-events-none opacity-0"
|
||||
playsInline
|
||||
preload="auto"
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user