|
|
|
|
@ -1,7 +1,6 @@ |
|
|
|
|
import type { RefObject } from 'react' |
|
|
|
|
import { |
|
|
|
|
useMemo, |
|
|
|
|
useState, |
|
|
|
|
useEffect, |
|
|
|
|
useCallback, |
|
|
|
|
} from 'react' |
|
|
|
|
@ -14,6 +13,9 @@ import find from 'lodash/find' |
|
|
|
|
import uniqBy from 'lodash/uniqBy' |
|
|
|
|
import isEmpty from 'lodash/isEmpty' |
|
|
|
|
import orderBy from 'lodash/orderBy' |
|
|
|
|
import isString from 'lodash/isString' |
|
|
|
|
|
|
|
|
|
import { useLocalStore } from 'hooks' |
|
|
|
|
|
|
|
|
|
const autoQuality = { |
|
|
|
|
label: 'Auto', |
|
|
|
|
@ -38,7 +40,11 @@ const getVideoQualities = (hls: Hls | null) => { |
|
|
|
|
export const useVideoQuality = (ref: RefObject<ReactPlayer>) => { |
|
|
|
|
const hls = ref.current?.getInternalPlayer('hls') as Hls | null |
|
|
|
|
const videoQualities = useMemo(() => getVideoQualities(hls), [hls]) |
|
|
|
|
const [selectedQuality, setSelectedQuality] = useState('') |
|
|
|
|
const [selectedQuality, setSelectedQuality] = useLocalStore({ |
|
|
|
|
defaultValue: autoQuality.label, |
|
|
|
|
key: 'player_quality', |
|
|
|
|
validator: isString, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
const onQualitySelect = useCallback((label: string) => { |
|
|
|
|
const item = find(videoQualities, { label }) |
|
|
|
|
@ -46,11 +52,27 @@ export const useVideoQuality = (ref: RefObject<ReactPlayer>) => { |
|
|
|
|
hls.currentLevel = item.level |
|
|
|
|
setSelectedQuality(item.label) |
|
|
|
|
} |
|
|
|
|
}, [hls, videoQualities]) |
|
|
|
|
}, [ |
|
|
|
|
setSelectedQuality, |
|
|
|
|
hls, |
|
|
|
|
videoQualities, |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
onQualitySelect(autoQuality.label) |
|
|
|
|
}, [onQualitySelect]) |
|
|
|
|
if (!hls) return |
|
|
|
|
|
|
|
|
|
const quality = find(videoQualities, { label: selectedQuality }) || autoQuality |
|
|
|
|
|
|
|
|
|
if (quality.level === hls.currentLevel) return |
|
|
|
|
|
|
|
|
|
hls.currentLevel = quality.level |
|
|
|
|
setSelectedQuality(quality.label) |
|
|
|
|
}, [ |
|
|
|
|
selectedQuality, |
|
|
|
|
setSelectedQuality, |
|
|
|
|
videoQualities, |
|
|
|
|
hls, |
|
|
|
|
]) |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
onQualitySelect, |
|
|
|
|
|