[Video] use dynamic import for hls.js (#5429)
Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import React, {useEffect, useId, useRef, useState} from 'react'
|
import React, {useEffect, useId, useRef, useState} from 'react'
|
||||||
import {View} from 'react-native'
|
import {View} from 'react-native'
|
||||||
import {AppBskyEmbedVideo} from '@atproto/api'
|
import {AppBskyEmbedVideo} from '@atproto/api'
|
||||||
import Hls, {Events, FragChangedData, Fragment} from 'hls.js'
|
import type * as HlsTypes from 'hls.js'
|
||||||
|
|
||||||
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
|
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
|
||||||
import {atoms as a} from '#/alf'
|
import {atoms as a} from '#/alf'
|
||||||
@@ -23,6 +23,7 @@ export function VideoEmbedInnerWeb({
|
|||||||
const videoRef = useRef<HTMLVideoElement>(null)
|
const videoRef = useRef<HTMLVideoElement>(null)
|
||||||
const [focused, setFocused] = useState(false)
|
const [focused, setFocused] = useState(false)
|
||||||
const [hasSubtitleTrack, setHasSubtitleTrack] = useState(false)
|
const [hasSubtitleTrack, setHasSubtitleTrack] = useState(false)
|
||||||
|
const [hlsLoading, setHlsLoading] = React.useState(false)
|
||||||
const figId = useId()
|
const figId = useId()
|
||||||
|
|
||||||
// send error up to error boundary
|
// send error up to error boundary
|
||||||
@@ -37,6 +38,7 @@ export function VideoEmbedInnerWeb({
|
|||||||
setHasSubtitleTrack,
|
setHasSubtitleTrack,
|
||||||
setError,
|
setError,
|
||||||
videoRef,
|
videoRef,
|
||||||
|
setHlsLoading,
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -77,6 +79,7 @@ export function VideoEmbedInnerWeb({
|
|||||||
setActive={setActive}
|
setActive={setActive}
|
||||||
focused={focused}
|
focused={focused}
|
||||||
setFocused={setFocused}
|
setFocused={setFocused}
|
||||||
|
hlsLoading={hlsLoading}
|
||||||
onScreen={onScreen}
|
onScreen={onScreen}
|
||||||
fullscreenRef={containerRef}
|
fullscreenRef={containerRef}
|
||||||
hasSubtitleTrack={hasSubtitleTrack}
|
hasSubtitleTrack={hasSubtitleTrack}
|
||||||
@@ -99,31 +102,62 @@ export class VideoNotFoundError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CachedPromise<T> = Promise<T> & {value: undefined | T}
|
||||||
|
const promiseForHls = import(
|
||||||
|
// @ts-ignore
|
||||||
|
'hls.js/dist/hls.min'
|
||||||
|
).then(mod => mod.default) as CachedPromise<typeof HlsTypes.default>
|
||||||
|
promiseForHls.value = undefined
|
||||||
|
promiseForHls.then(Hls => {
|
||||||
|
promiseForHls.value = Hls
|
||||||
|
})
|
||||||
|
|
||||||
function useHLS({
|
function useHLS({
|
||||||
focused,
|
focused,
|
||||||
playlist,
|
playlist,
|
||||||
setHasSubtitleTrack,
|
setHasSubtitleTrack,
|
||||||
setError,
|
setError,
|
||||||
videoRef,
|
videoRef,
|
||||||
|
setHlsLoading,
|
||||||
}: {
|
}: {
|
||||||
focused: boolean
|
focused: boolean
|
||||||
playlist: string
|
playlist: string
|
||||||
setHasSubtitleTrack: (v: boolean) => void
|
setHasSubtitleTrack: (v: boolean) => void
|
||||||
setError: (v: Error | null) => void
|
setError: (v: Error | null) => void
|
||||||
videoRef: React.RefObject<HTMLVideoElement>
|
videoRef: React.RefObject<HTMLVideoElement>
|
||||||
|
setHlsLoading: (v: boolean) => void
|
||||||
}) {
|
}) {
|
||||||
const hlsRef = useRef<Hls | undefined>(undefined)
|
const [Hls, setHls] = useState<typeof HlsTypes.default | undefined>(
|
||||||
const [lowQualityFragments, setLowQualityFragments] = useState<Fragment[]>([])
|
() => promiseForHls.value,
|
||||||
|
)
|
||||||
|
useEffect(() => {
|
||||||
|
if (!Hls) {
|
||||||
|
setHlsLoading(true)
|
||||||
|
promiseForHls.then(loadedHls => {
|
||||||
|
setHls(() => loadedHls)
|
||||||
|
setHlsLoading(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, [Hls, setHlsLoading])
|
||||||
|
|
||||||
|
const hlsRef = useRef<HlsTypes.default | undefined>(undefined)
|
||||||
|
const [lowQualityFragments, setLowQualityFragments] = useState<
|
||||||
|
HlsTypes.Fragment[]
|
||||||
|
>([])
|
||||||
|
|
||||||
// purge low quality segments from buffer on next frag change
|
// purge low quality segments from buffer on next frag change
|
||||||
const handleFragChange = useNonReactiveCallback(
|
const handleFragChange = useNonReactiveCallback(
|
||||||
(_event: Events.FRAG_CHANGED, {frag}: FragChangedData) => {
|
(
|
||||||
|
_event: HlsTypes.Events.FRAG_CHANGED,
|
||||||
|
{frag}: HlsTypes.FragChangedData,
|
||||||
|
) => {
|
||||||
|
if (!Hls) return
|
||||||
if (!hlsRef.current) return
|
if (!hlsRef.current) return
|
||||||
const hls = hlsRef.current
|
const hls = hlsRef.current
|
||||||
|
|
||||||
if (focused && hls.nextAutoLevel > 0) {
|
if (focused && hls.nextAutoLevel > 0) {
|
||||||
// if the current quality level goes above 0, flush the low quality segments
|
// if the current quality level goes above 0, flush the low quality segments
|
||||||
const flushed: Fragment[] = []
|
const flushed: HlsTypes.Fragment[] = []
|
||||||
|
|
||||||
for (const lowQualFrag of lowQualityFragments) {
|
for (const lowQualFrag of lowQualityFragments) {
|
||||||
// avoid if close to the current fragment
|
// avoid if close to the current fragment
|
||||||
@@ -147,12 +181,15 @@ function useHLS({
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!videoRef.current) return
|
if (!videoRef.current) return
|
||||||
if (!Hls.isSupported()) throw new HLSUnsupportedError()
|
if (!Hls) return
|
||||||
|
if (!Hls.isSupported()) {
|
||||||
|
throw new HLSUnsupportedError()
|
||||||
|
}
|
||||||
|
|
||||||
const hls = new Hls({
|
const hls = new Hls({
|
||||||
maxMaxBufferLength: 10, // only load 10s ahead
|
maxMaxBufferLength: 10, // only load 10s ahead
|
||||||
// note: the amount buffered is affected by both maxBufferLength and maxBufferSize
|
// note: the amount buffered is affected by both maxBufferLength and maxBufferSize
|
||||||
// it will buffer until it it's greater than *both* of those values
|
// it will buffer until it is greater than *both* of those values
|
||||||
// so we use maxMaxBufferLength to set the actual maximum amount of buffering instead
|
// so we use maxMaxBufferLength to set the actual maximum amount of buffering instead
|
||||||
})
|
})
|
||||||
hlsRef.current = hls
|
hlsRef.current = hls
|
||||||
@@ -211,7 +248,7 @@ function useHLS({
|
|||||||
hls.destroy()
|
hls.destroy()
|
||||||
abortController.abort()
|
abortController.abort()
|
||||||
}
|
}
|
||||||
}, [playlist, setError, setHasSubtitleTrack, videoRef, handleFragChange])
|
}, [playlist, setError, setHasSubtitleTrack, videoRef, handleFragChange, Hls])
|
||||||
|
|
||||||
return hlsRef
|
return hlsRef
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ export function Controls({
|
|||||||
setFocused,
|
setFocused,
|
||||||
onScreen,
|
onScreen,
|
||||||
fullscreenRef,
|
fullscreenRef,
|
||||||
|
hlsLoading,
|
||||||
hasSubtitleTrack,
|
hasSubtitleTrack,
|
||||||
}: {
|
}: {
|
||||||
videoRef: React.RefObject<HTMLVideoElement>
|
videoRef: React.RefObject<HTMLVideoElement>
|
||||||
@@ -53,6 +54,7 @@ export function Controls({
|
|||||||
setFocused: (focused: boolean) => void
|
setFocused: (focused: boolean) => void
|
||||||
onScreen: boolean
|
onScreen: boolean
|
||||||
fullscreenRef: React.RefObject<HTMLDivElement>
|
fullscreenRef: React.RefObject<HTMLDivElement>
|
||||||
|
hlsLoading: boolean
|
||||||
hasSubtitleTrack: boolean
|
hasSubtitleTrack: boolean
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
@@ -80,6 +82,7 @@ export function Controls({
|
|||||||
const [isFullscreen, toggleFullscreen] = useFullscreen(fullscreenRef)
|
const [isFullscreen, toggleFullscreen] = useFullscreen(fullscreenRef)
|
||||||
const {state: hasFocus, onIn: onFocus, onOut: onBlur} = useInteractionState()
|
const {state: hasFocus, onIn: onFocus, onOut: onBlur} = useInteractionState()
|
||||||
const [interactingViaKeypress, setInteractingViaKeypress] = useState(false)
|
const [interactingViaKeypress, setInteractingViaKeypress] = useState(false)
|
||||||
|
const showSpinner = hlsLoading || buffering
|
||||||
const {
|
const {
|
||||||
state: volumeHovered,
|
state: volumeHovered,
|
||||||
onIn: onVolumeHover,
|
onIn: onVolumeHover,
|
||||||
@@ -409,11 +412,11 @@ export function Controls({
|
|||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
{(buffering || error) && (
|
{(showSpinner || error) && (
|
||||||
<View
|
<View
|
||||||
pointerEvents="none"
|
pointerEvents="none"
|
||||||
style={[a.absolute, a.inset_0, a.justify_center, a.align_center]}>
|
style={[a.absolute, a.inset_0, a.justify_center, a.align_center]}>
|
||||||
{buffering && <Loader fill={t.palette.white} size="lg" />}
|
{showSpinner && <Loader fill={t.palette.white} size="lg" />}
|
||||||
{error && (
|
{error && (
|
||||||
<Text style={{color: t.palette.white}}>
|
<Text style={{color: t.palette.white}}>
|
||||||
<Trans>An error occurred</Trans>
|
<Trans>An error occurred</Trans>
|
||||||
|
|||||||
Reference in New Issue
Block a user