๐บ HLS(Http Live Streaming)
HLS(HTTP ๋ผ์ด๋ธ ์คํธ๋ฆฌ๋ฐ)์ ๊ฐ์ฅ ๋๋ฆฌ ์ฌ์ฉ๋๋ ๋น๋์ค ์คํธ๋ฆฌ๋ฐ ํ๋กํ ์ฝ์ ๋๋ค. HTTP "๋ผ์ด๋ธ" ์คํธ๋ฆฌ๋ฐ์ด๋ผ ๋ถ๋ฆฌ์ง๋ง ์ฃผ๋ฌธํ ์คํธ๋ฆฌ๋ฐ์ด์ ๋์์ ๋ผ์ด๋ธ ์คํธ๋ฆฌ๋ฐ์ ๋๋ค. HLS๋ ๋น๋์ค ํ์ผ์ ๋ค์ด๋ก๋ํ ์ ์๋ HTTP ํ์ผ ์กฐ๊ฐ์ผ๋ก ๋๋๊ณ HTTP ํ๋กํ ์ฝ์ ์ด์ฉํ์ฌ ์ ์กํฉ๋๋ค. ํด๋ผ์ด์ธํธ ์ฅ์น๋ ์ด๋ฌํ HTTP ํ์ผ์ ๋ก๋ํ ํ ๋น๋์ค๋ก ์ฌ์ํฉ๋๋ค.
๐ Hls.js
Hls.js๋ http live streaming์ ๊ตฌํํ๋ javascript ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
ios/safari ๊ฐ์ ๊ฒฝ์ฐ ๋นํธ์ธ์ด ๋์ด์๊ธฐ๋๋ฌธ์ ์ง์ํ์ง๋ ์๊ณ , ๋ค๋ฅธ ๋ธ๋ผ์ฐ์ ์์๋ Hls.js๋ฅผ ํตํด์ ์คํธ๋ฆฌ๋ฐ์ด ๊ฐ๋ฅํฉ๋๋ค.
๐ฐ Hls.js ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
cdn ๋ฐฉ์์ผ๋ก hls๋ฅผ ์ถ๊ฐํฉ๋๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ######### hls ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ cdn ๋ฐฉ์์ผ๋ก ์ถ๊ฐํด์ค๋ค.######### -->
<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script src="https://vjs.zencdn.net/8.3.0/video.min.js"></script>
<title>Hls Practice</title>
<style>
video {
max-width: 500px;
width: 100%;
max-height: 500px;
height: 100%;
object-fit: fill;
}
</style>
</head>
<body>
<!--
controls : ์ฌ์ฉ์๊ฐ ๋น๋์ค ์ปจํธ๋กค์ด ๊ฐ๋ฅํ๊ฒ ํ ์ ์๋ค
playsinline : ์ ์ฒด ํ๋ฉด ๋ฐฉ์ง์ด๋ค
autoplay : ์๋์ฌ์์ด๋ฉฐ , ์ ์ฑ
์ ์์๊ฑฐ ์ํ์์ ๊ฐ๋ฅํ๋ค.
-->
<video id="video" controls playsinline autoplay></video>
</body>
</html>
๋ค์์ผ๋ก ์ํ m3u8 url๋ฅผ ์ค๋นํ๊ณ , video๋ฅผ ์ด๊ธฐํ ์ํต๋๋ค.
<script>
/* video Element */
let video = null;
/* ์ํ m3u8 url */
let videoSrc = 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8'
/* initVideo */
const initVideo = () => {
video = document.querySelector('#video')
}
/* DOMContentLoaded ๋ก๋๋ฅผ ํตํด video Element ๋ด๊ธฐ*/
window.addEventListener("DOMContentLoaded", () => {
initVideo();
})
</script>
์ธ์ hls.js๋ฅผ ์ฌ์ฉํ์ฌ ์ด๊ธฐํ ์ํต๋๋ค.
"initHls" ํจ์๋ฅผ ํตํด hls๋ฅผ ์ด๊ธฐํ ์์ผ์ค๋๋ค.
๋ค๋ง ์ฌ๊ธฐ์ Hls๋ฅผ ์ง์ํ์ง ์๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ ๊ทธ๊ฑด ios/safari ์ด๋ค. ios/safari๋ hls๊ฐ built-in์ด ๋์ด์๊ธฐ ๋๋ฌธ์ด๋ค.
<script>
/* video Element */
let video = null;
/* ์ํ m3u8 url */
let videoSrc = 'https://demo.unified-streaming.com/k8s/features/stable/video/tears-of-steel/tears-of-steel.ism/.m3u8'
/* initVideo */
const initVideo = () => {
video = document.querySelector('#video')
}
/* initHls : hls๋ฅผ ์ด๊ธฐํ ํ๋ ํจ์ */
const initHls = () => {
if (Hls.isSupported()) {
hls = new Hls({
autoStartLoad: false,
});
hls.loadSource(videoSrc);
hls.attachMedia(video);
hls.startLoad();
}
/* ios/safari ๊ฐ์ ๊ฒฝ์ฐ์ hls๊ฐ built-in ๋์ด์๋ค.*/
else {
video.src = videoSrc;
}
}
/* DOMContentLoaded ๋ก๋๋ฅผ ํตํด video Element ๋ด๊ธฐ*/
window.addEventListener("DOMContentLoaded", () => {
initVideo();
initHls();
})
</script>
์ฌ๊ธฐ๊น์ง ํ๋ฉด ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ผ๋ก ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค.
๋ค์์๋ video "์๋์ฌ์" ์ํค๊ธฐ๋ฅผ ํด๋ณผ์์ ์ด๋ค.
๊ทธ๋ฆฌ๊ณ hls.js ๋ํ config์ https://github.com/video-dev/hls.js/blob/master/docs/API.md ์์ ํ์ธ์ด ๊ฐ๋ฅํ๋ค.
'Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
navigator.mediaSession (0) | 2023.07.27 |
---|---|
๐ฆ๋น๋์ค ์๋์ฌ์ ์คํ์ํค๊ธฐ (0) | 2023.07.02 |
๐ธ ๋ธ๋ผ์ฐ์ ๋ ๋๋ง ๊ณผ์ (0) | 2023.04.12 |
๐ ๋ ์์ปฌ ์ค์ฝํ & ํด๋ก์ (0) | 2023.03.07 |
๊ฐ์์(Observer) ํจํด (0) | 2022.01.23 |
๋๊ธ