-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(gallery): HLS -> HLS.Light #1510
base: master
Are you sure you want to change the base?
Changes from all commits
d674052
cedb184
e875c31
521900b
2e682fb
722b4b3
d884f1b
6f35815
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@alfalab/core-components-gallery': patch | ||
--- | ||
|
||
- Импорт HLS заменён на light версию, только энкодер без дополнительных обвязок. | ||
- Подгрузка модуля перенесена на время после загрузки основного js, для лучшей клиентской доступности основного приложения. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
import React, { MouseEvent, ReactEventHandler, useContext, useEffect, useRef } from 'react'; | ||
import React, { | ||
MouseEvent, | ||
ReactEventHandler, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import cn from 'classnames'; | ||
import Hls from 'hls.js'; | ||
import type HlsType from 'hls.js/dist/hls.light.mjs'; | ||
import type { ErrorData, Events } from 'hls.js/dist/hls.light.mjs'; | ||
|
||
import { Circle } from '@alfalab/core-components/icon-view/circle'; | ||
import PlayCompactMIcon from '@alfalab/icons-glyph/PlayCompactMIcon'; | ||
|
@@ -20,6 +28,7 @@ type Props = { | |
export const Video = ({ url, index, className, isActive }: Props) => { | ||
const playerRef = useRef<HTMLVideoElement>(null); | ||
const timer = useRef<ReturnType<typeof setTimeout>>(); | ||
const [HLSSupported, setHLSSupported] = useState<boolean>(true); | ||
|
||
const { setImageMeta, mutedVideo, view, playingVideo, setPlayingVideo, setHideNavigation } = | ||
useContext(GalleryContext); | ||
|
@@ -32,10 +41,22 @@ export const Video = ({ url, index, className, isActive }: Props) => { | |
}, [index]); | ||
|
||
useEffect(() => { | ||
const hls = new Hls(); | ||
let hls: HlsType; | ||
|
||
if (Hls.isSupported()) { | ||
hls.on(Hls.Events.ERROR, (_, data) => { | ||
async function initHls() { | ||
const { default: Hls } = await import( | ||
/* webpackChunkName: "hls-js-video" */ 'hls.js/dist/hls.light.mjs' | ||
); | ||
|
||
hls = new Hls(); | ||
|
||
if (!Hls.isSupported()) { | ||
setHLSSupported(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. проверка до загрузки |
||
|
||
return; | ||
} | ||
|
||
hls.on(Hls.Events.ERROR, (_: Events.ERROR, data: ErrorData) => { | ||
if (data.fatal) { | ||
switch (data.type) { | ||
case Hls.ErrorTypes.MEDIA_ERROR: | ||
|
@@ -52,11 +73,14 @@ export const Video = ({ url, index, className, isActive }: Props) => { | |
}); | ||
|
||
hls.loadSource(url); | ||
|
||
if (playerRef.current) { | ||
hls.attachMedia(playerRef.current); | ||
} | ||
} | ||
|
||
initHls().catch(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. может добавим количество попыток загрузки? |
||
return () => { | ||
if (hls) { | ||
hls.destroy(); | ||
|
@@ -65,6 +89,7 @@ export const Video = ({ url, index, className, isActive }: Props) => { | |
clearTimeout(timer.current); | ||
} | ||
}; | ||
|
||
/* eslint-disable-next-line react-hooks/exhaustive-deps */ | ||
}, [url, index]); | ||
|
||
|
@@ -142,7 +167,7 @@ export const Video = ({ url, index, className, isActive }: Props) => { | |
playsInline={true} | ||
muted={mutedVideo} | ||
loop={true} | ||
src={Hls.isSupported() ? undefined : url} | ||
src={HLSSupported ? undefined : url} | ||
className={cn(styles.video, { [styles.mobile]: view === 'mobile' }, className)} | ||
> | ||
<track kind='captions' /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. перенести в |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare module 'hls.js/dist/hls.light.mjs' { | ||
import Hls, { ErrorData, Events } from 'hls'; | ||
|
||
export default Hls; | ||
export { ErrorData, Events }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
300 Kb в отдельный чанк