mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 21:04:59 +00:00
feat(transcode): add HLS live streaming support (#436)
* feat(transcode): add HLS live streaming support * fix(hls): resolve version stacks for public HLS and add workflow E2E test
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import type { VideoTranscode } from '@shumai/dtos'
|
||||
|
||||
export type PaneKind = 'image' | 'video' | 'unsupported'
|
||||
|
||||
export type CompareSide = 'left' | 'right'
|
||||
|
||||
export type DisplayTranscode = VideoTranscode & { resolution: string }
|
||||
export interface DisplayTranscode {
|
||||
id?: string
|
||||
url?: string
|
||||
key?: string
|
||||
width: number
|
||||
height: number
|
||||
size?: number
|
||||
hdr?: boolean
|
||||
resolution: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Reactive state a pane reports upward so the shared control bar can render.
|
||||
|
||||
@@ -32,15 +32,23 @@ interface CompareVideoPaneProps {
|
||||
function computeResolutions(file: AssetInfo): DisplayTranscode[] {
|
||||
// Only transcoded proxy versions are ever displayed; the raw original file
|
||||
// is never used as a playback source.
|
||||
return (file.media?.videoTranscodes ?? []).map((t) => {
|
||||
const isHls = Boolean(file.media?.isHls && file.media.hls?.url)
|
||||
const baseTranscodes = isHls
|
||||
? (file.media?.hls?.resolutions ?? [])
|
||||
: (file.media?.videoTranscodes ?? [])
|
||||
|
||||
return baseTranscodes.map((t) => {
|
||||
const longSide = Math.max(t.width, t.height)
|
||||
let resolution = `${t.height}p`
|
||||
if (longSide >= 3840) resolution = '2160p'
|
||||
else if (longSide >= 1920) resolution = '1080p'
|
||||
else if (longSide >= 1280) resolution = '720p'
|
||||
else if (longSide >= 960) resolution = '540p'
|
||||
else if (longSide >= 640) resolution = '360p'
|
||||
else if (longSide >= 320) resolution = '180p'
|
||||
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
|
||||
let resolution = resCandidate || `${t.height}p`
|
||||
if (!resCandidate) {
|
||||
if (longSide >= 3840) resolution = '2160p'
|
||||
else if (longSide >= 1920) resolution = '1080p'
|
||||
else if (longSide >= 1280) resolution = '720p'
|
||||
else if (longSide >= 960) resolution = '540p'
|
||||
else if (longSide >= 640) resolution = '360p'
|
||||
else if (longSide >= 320) resolution = '180p'
|
||||
}
|
||||
return { ...t, resolution }
|
||||
})
|
||||
}
|
||||
@@ -117,11 +125,14 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
const containerDuration = metadata?.duration || 0
|
||||
const totalFrames = resolveTotalFrames({ dbTotalFrames, containerDuration, frameRate })
|
||||
|
||||
const isHls = Boolean(file.media?.isHls && file.media.hls?.url)
|
||||
const resolutions = computeResolutions(file)
|
||||
const initialRes = getInitialResolution(resolutions)
|
||||
const [currentResolution, setCurrentResolution] = useState(initialRes?.resolution ?? '')
|
||||
const initialRes = isHls ? null : getInitialResolution(resolutions)
|
||||
const [currentResolution, setCurrentResolution] = useState(
|
||||
isHls ? 'Auto' : (initialRes?.resolution ?? ''),
|
||||
)
|
||||
const [isCurrentHdr, setIsCurrentHdr] = useState(initialRes?.hdr)
|
||||
const currentSrcRef = useRef(initialRes?.url)
|
||||
const currentSrcRef = useRef(isHls ? file.media!.hls!.url : initialRes?.url)
|
||||
|
||||
const isAudio = file.proxyType === 'audio'
|
||||
const { currentFrame, seekToFrame } = useFramePlayer(videoRef, frameRate, totalFrames, isAudio)
|
||||
@@ -160,12 +171,12 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
// if the parent renders this pane without a per-asset `key`. Runs before the
|
||||
// video.js init effect below (declaration order) so the ref is fresh.
|
||||
useEffect(() => {
|
||||
const res = getInitialResolution(resolutions)
|
||||
setCurrentResolution(res?.resolution ?? '')
|
||||
const res = isHls ? null : getInitialResolution(resolutions)
|
||||
setCurrentResolution(isHls ? 'Auto' : (res?.resolution ?? ''))
|
||||
setIsCurrentHdr(res?.hdr)
|
||||
currentSrcRef.current = res?.url
|
||||
currentSrcRef.current = isHls ? file.media!.hls!.url : res?.url
|
||||
setHasStartedPlaying(false)
|
||||
}, [file.id])
|
||||
}, [file.id, Boolean(isHls ? file.media?.hls?.url : initialRes?.url)])
|
||||
|
||||
// Initialize video.js
|
||||
useEffect(() => {
|
||||
@@ -175,12 +186,15 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
videoElement.style.pointerEvents = 'none'
|
||||
videoContainerRef.current.appendChild(videoElement)
|
||||
|
||||
const targetSrc = isHls ? file.media!.hls!.url! : (currentSrcRef.current ?? '')
|
||||
const targetType = isHls ? 'application/x-mpegURL' : 'video/mp4'
|
||||
|
||||
const player = (playerRef.current = videojs(videoElement, {
|
||||
controls: false,
|
||||
autoplay: false,
|
||||
preload: 'auto',
|
||||
playsinline: true,
|
||||
sources: [{ src: currentSrcRef.current ?? '', type: 'video/mp4' }],
|
||||
sources: [{ src: targetSrc, type: targetType }],
|
||||
}))
|
||||
|
||||
const htmlVid = videoElement.querySelector('video')
|
||||
@@ -325,6 +339,30 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
(resolution: string, hdr?: boolean) => {
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
|
||||
if (isHls) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const ql = (player as any).qualityLevels?.()
|
||||
if (resolution === 'Auto') {
|
||||
if (ql) {
|
||||
for (let i = 0; i < ql.length; i++) {
|
||||
ql[i].enabled = true
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
setCurrentResolution(resolution)
|
||||
setIsCurrentHdr(target?.hdr)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const target =
|
||||
hdr !== undefined
|
||||
? resolutions.find(
|
||||
@@ -346,7 +384,7 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
|
||||
}
|
||||
})
|
||||
},
|
||||
[resolutions],
|
||||
[resolutions, isHls],
|
||||
)
|
||||
|
||||
useImperativeHandle(
|
||||
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
} from '../../ui/dropdown-menu'
|
||||
import ProgressBar from './progress-bar'
|
||||
import { formatTime, formatTimecode } from './utils'
|
||||
import type { ControlBarProps } from './video-control-bar'
|
||||
import type { ControlBarProps, DisplayTranscode } from './video-control-bar'
|
||||
|
||||
export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
|
||||
state,
|
||||
@@ -189,9 +189,11 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
|
||||
<span className="flex items-center gap-1 text-[11px]">
|
||||
<span>
|
||||
{!isAudio && state.currentResolution
|
||||
? state.currentResolution === 'Original'
|
||||
? m.original()
|
||||
: state.currentResolution
|
||||
? state.currentResolution === 'Auto'
|
||||
? m.quality_auto()
|
||||
: state.currentResolution === 'Original'
|
||||
? m.original()
|
||||
: state.currentResolution
|
||||
: `${state.playbackRate}x`}
|
||||
</span>
|
||||
{!isAudio && state.isCurrentHdr && (
|
||||
@@ -208,6 +210,25 @@ export const MobileVideoControlBar: React.FC<ControlBarProps> = ({
|
||||
<DropdownMenuLabel className="text-[10px] font-bold text-muted-foreground uppercase tracking-wider">
|
||||
{m.quality()}
|
||||
</DropdownMenuLabel>
|
||||
{data.media?.isHls && (
|
||||
<DropdownMenuItem
|
||||
onClick={() =>
|
||||
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',
|
||||
)}
|
||||
>
|
||||
<span>{m.quality_auto()}</span>
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
{resolutions.map((res) => {
|
||||
const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}`
|
||||
const isSelected =
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
import { useUiStore } from '@/ui/stores/ui'
|
||||
import type { AssetInfo, VideoTranscode } from '@shumai/dtos'
|
||||
import type { AssetInfo } from '@shumai/dtos'
|
||||
import {
|
||||
Check,
|
||||
ChevronDown,
|
||||
@@ -46,7 +46,16 @@ export interface PlayerState {
|
||||
isCurrentHdr?: boolean
|
||||
}
|
||||
|
||||
export type DisplayTranscode = VideoTranscode & { resolution: string }
|
||||
export interface DisplayTranscode {
|
||||
id?: string
|
||||
url?: string
|
||||
key?: string
|
||||
width: number
|
||||
height: number
|
||||
size?: number
|
||||
hdr?: boolean
|
||||
resolution: string
|
||||
}
|
||||
|
||||
export interface ControlBarProps {
|
||||
state: PlayerState
|
||||
@@ -322,9 +331,11 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
||||
<Settings className="h-3.5 w-3.5" />
|
||||
<span className="flex items-center gap-1">
|
||||
<span>
|
||||
{state.currentResolution === 'Original'
|
||||
? m.original()
|
||||
: state.currentResolution}
|
||||
{state.currentResolution === 'Auto'
|
||||
? m.quality_auto()
|
||||
: state.currentResolution === 'Original'
|
||||
? m.original()
|
||||
: state.currentResolution}
|
||||
</span>
|
||||
{state.isCurrentHdr && (
|
||||
<span className="px-1 py-0.2 rounded text-[10px] font-bold bg-amber-500/20 text-amber-500 border border-amber-500/30">
|
||||
@@ -337,6 +348,25 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
||||
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuLabel>{m.quality()}</DropdownMenuLabel>
|
||||
{data.media?.isHls && (
|
||||
<DropdownMenuItem
|
||||
onClick={() =>
|
||||
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',
|
||||
)}
|
||||
>
|
||||
<span>{m.quality_auto()}</span>
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
{resolutions.map((res) => {
|
||||
const itemKey = `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}`
|
||||
const isSelected =
|
||||
@@ -389,8 +419,13 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuLabel>{m.download()}</DropdownMenuLabel>
|
||||
{resolutions.map((res) => {
|
||||
const itemKey = res.key || `${res.resolution}-${res.hdr ? 'hdr' : 'sdr'}`
|
||||
{(data.media?.videoTranscodes && data.media.videoTranscodes.length > 0
|
||||
? data.media.videoTranscodes
|
||||
: resolutions
|
||||
).map((res) => {
|
||||
const resLabel =
|
||||
'resolution' in res && res.resolution ? res.resolution : `${res.height}p`
|
||||
const itemKey = res.key || `${resLabel}-${res.hdr ? 'hdr' : 'sdr'}`
|
||||
return (
|
||||
<DropdownMenuItem
|
||||
key={itemKey}
|
||||
@@ -398,7 +433,7 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
|
||||
className="flex w-full items-center justify-between px-3 py-2 text-left text-sm text-foreground cursor-pointer"
|
||||
>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span>{res.resolution}</span>
|
||||
<span>{resLabel}</span>
|
||||
{res.hdr && (
|
||||
<span className="px-1 py-0.2 rounded text-[10px] font-bold bg-amber-500/20 text-amber-500 border border-amber-500/30">
|
||||
{m.hdr()}
|
||||
|
||||
@@ -33,15 +33,23 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
) => {
|
||||
const localPlayerRef = useRef<Player | null>(null)
|
||||
const playerRef = localPlayerRef
|
||||
const resolutions: DisplayTranscode[] = (data.media?.videoTranscodes ?? []).map((t) => {
|
||||
const isHls = Boolean(data.media?.isHls && data.media.hls?.url)
|
||||
const baseTranscodes = isHls
|
||||
? (data.media?.hls?.resolutions ?? [])
|
||||
: (data.media?.videoTranscodes ?? [])
|
||||
|
||||
const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => {
|
||||
const longSide = Math.max(t.width, t.height)
|
||||
let resolution = `${t.height}p`
|
||||
if (longSide >= 3840) resolution = '2160p'
|
||||
else if (longSide >= 1920) resolution = '1080p'
|
||||
else if (longSide >= 1280) resolution = '720p'
|
||||
else if (longSide >= 960) resolution = '540p'
|
||||
else if (longSide >= 640) resolution = '360p'
|
||||
else if (longSide >= 320) resolution = '180p'
|
||||
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
|
||||
let resolution = resCandidate || `${t.height}p`
|
||||
if (!resCandidate) {
|
||||
if (longSide >= 3840) resolution = '2160p'
|
||||
else if (longSide >= 1920) resolution = '1080p'
|
||||
else if (longSide >= 1280) resolution = '720p'
|
||||
else if (longSide >= 960) resolution = '540p'
|
||||
else if (longSide >= 640) resolution = '360p'
|
||||
else if (longSide >= 320) resolution = '180p'
|
||||
}
|
||||
|
||||
return {
|
||||
...t,
|
||||
@@ -50,7 +58,7 @@ 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 = resolutions.length > 0 && !!data.media?.metadata
|
||||
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)
|
||||
|
||||
@@ -90,7 +98,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
return bestFit || sortedResolutions[sortedResolutions.length - 1]
|
||||
}
|
||||
|
||||
const initialRes = getInitialResolution()
|
||||
const initialRes = isHls ? null : getInitialResolution()
|
||||
|
||||
// State
|
||||
const [state, setState] = useState<PlayerState>({
|
||||
@@ -104,8 +112,8 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
playbackRate: 1,
|
||||
isFullScreen: false,
|
||||
showFrames: false,
|
||||
currentResolution: initialRes?.resolution ?? '',
|
||||
currentSrc: initialRes?.url ?? '',
|
||||
currentResolution: isHls ? 'Auto' : (initialRes?.resolution ?? ''),
|
||||
currentSrc: isHls ? data.media!.hls!.url : (initialRes?.url ?? ''),
|
||||
isCurrentHdr: initialRes?.hdr ?? false,
|
||||
})
|
||||
const [hasStartedPlaying, setHasStartedPlaying] = useState(false)
|
||||
@@ -147,15 +155,15 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
|
||||
|
||||
useEffect(() => {
|
||||
const res = getInitialResolution()
|
||||
const res = isHls ? null : getInitialResolution()
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
isPlaying: false,
|
||||
progress: 0,
|
||||
currentTime: 0,
|
||||
duration: data.media?.metadata?.duration || 0,
|
||||
currentResolution: res?.resolution ?? '',
|
||||
currentSrc: res?.url ?? '',
|
||||
currentResolution: isHls ? 'Auto' : (res?.resolution ?? ''),
|
||||
currentSrc: isHls ? (data.media?.hls?.url ?? '') : (res?.url ?? ''),
|
||||
isCurrentHdr: res?.hdr ?? false,
|
||||
}))
|
||||
setHasManuallyZoomed(false)
|
||||
@@ -163,7 +171,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
setBuffered(0)
|
||||
lastProcessedStartTimeRef.current = null
|
||||
setHasStartedPlaying(false)
|
||||
}, [data.id, Boolean(initialRes?.url)])
|
||||
}, [data.id, Boolean(isHls ? data.media?.hls?.url : initialRes?.url)])
|
||||
|
||||
const vidW = data.media?.metadata?.originalWidth || 1920
|
||||
const vidH = data.media?.metadata?.originalHeight || 1080
|
||||
@@ -294,8 +302,8 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
useEffect(() => {
|
||||
if (!videoContainerRef.current) return
|
||||
|
||||
const initialRes = getInitialResolution()
|
||||
const targetSrc = initialRes?.url ?? ''
|
||||
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
|
||||
@@ -325,7 +333,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
sources: [
|
||||
{
|
||||
src: targetSrc,
|
||||
type: 'video/mp4',
|
||||
type: targetType,
|
||||
},
|
||||
],
|
||||
}))
|
||||
@@ -429,7 +437,7 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
videoRef.current = null
|
||||
}
|
||||
}
|
||||
}, [data.id, Boolean(initialRes?.url), autoPlay])
|
||||
}, [data.id, Boolean(isHls ? data.media?.hls?.url : initialRes?.url), autoPlay])
|
||||
|
||||
// Handle changes to startTime (e.g., clicking different chunks in search results)
|
||||
useEffect(() => {
|
||||
@@ -620,6 +628,34 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const player = playerRef.current
|
||||
if (!player) return
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
currentResolution: 'Auto',
|
||||
}))
|
||||
} else {
|
||||
if (ql) {
|
||||
for (let i = 0; i < ql.length; i++) {
|
||||
ql[i].enabled = ql[i].height === res.height
|
||||
}
|
||||
}
|
||||
setState((prev) => ({
|
||||
...prev,
|
||||
currentResolution: res.resolution,
|
||||
isCurrentHdr: res.hdr ?? false,
|
||||
}))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const wasPlaying = !player.paused()
|
||||
const currentT = player.currentTime()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user