frigate/web/src/components/JSMpegPlayer.jsx

38 lines
977 B
React
Raw Normal View History

2021-06-12 17:55:40 +03:00
import { h } from 'preact';
import { baseUrl } from '../api/baseUrl';
import { useRef, useEffect } from 'preact/hooks';
import JSMpeg from '@cycjimmy/jsmpeg-player';
2021-10-17 15:39:23 +03:00
export default function JSMpegPlayer({ camera, width, height }) {
2021-06-12 17:55:40 +03:00
const playerRef = useRef();
2022-02-26 22:11:00 +03:00
const url = `${baseUrl.replace(/^http/, 'ws')}/live/${camera}`;
2021-06-12 17:55:40 +03:00
useEffect(() => {
const video = new JSMpeg.VideoElement(
playerRef.current,
url,
2021-06-13 22:21:20 +03:00
{},
2021-07-21 16:11:16 +03:00
{protocols: [], audio: false, videoBufferSize: 1024*1024*4}
2021-06-12 17:55:40 +03:00
);
2021-06-19 17:16:00 +03:00
const fullscreen = () => {
2022-02-26 22:11:00 +03:00
if (video.els.canvas.webkitRequestFullScreen) {
2021-06-19 17:16:00 +03:00
video.els.canvas.webkitRequestFullScreen();
}
else {
video.els.canvas.mozRequestFullScreen();
}
2022-02-26 22:11:00 +03:00
};
2021-06-19 17:16:00 +03:00
2022-02-26 22:11:00 +03:00
video.els.canvas.addEventListener('click',fullscreen);
2021-06-19 17:16:00 +03:00
2021-06-12 17:55:40 +03:00
return () => {
video.destroy();
};
}, [url]);
return (
2021-10-17 15:39:23 +03:00
<div ref={playerRef} class="jsmpeg" style={`max-height: ${height}px; max-width: ${width}px`} />
2021-06-12 17:55:40 +03:00
);
}