2022-02-27 15:04:12 +01:00
|
|
|
import { h } from 'preact';
|
|
|
|
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
|
|
|
|
import { useApiHost } from '../../api';
|
|
|
|
import { isNullOrUndefined } from '../../utils/objectUtils';
|
|
|
|
|
2022-06-02 23:05:56 +02:00
|
|
|
import 'videojs-seek-buttons';
|
|
|
|
import 'video.js/dist/video-js.css';
|
|
|
|
import 'videojs-seek-buttons/dist/videojs-seek-buttons.css';
|
|
|
|
|
|
|
|
import videojs from 'video.js';
|
|
|
|
|
2022-02-27 15:04:12 +01:00
|
|
|
interface OnTimeUpdateEvent {
|
|
|
|
timestamp: number;
|
|
|
|
isPlaying: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface VideoProperties {
|
|
|
|
posterUrl: string;
|
|
|
|
videoUrl: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface HistoryVideoProps {
|
2022-03-06 05:16:31 +01:00
|
|
|
id?: string;
|
2022-02-27 15:04:12 +01:00
|
|
|
isPlaying: boolean;
|
|
|
|
currentTime: number;
|
|
|
|
onTimeUpdate?: (event: OnTimeUpdateEvent) => void;
|
|
|
|
onPause: () => void;
|
|
|
|
onPlay: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const HistoryVideo = ({
|
|
|
|
id,
|
|
|
|
isPlaying: videoIsPlaying,
|
|
|
|
currentTime,
|
|
|
|
onTimeUpdate,
|
|
|
|
onPause,
|
|
|
|
onPlay,
|
|
|
|
}: HistoryVideoProps) => {
|
|
|
|
const apiHost = useApiHost();
|
2022-06-02 23:05:56 +02:00
|
|
|
const videoRef = useRef<HTMLVideoElement>();
|
2022-06-02 21:31:20 +02:00
|
|
|
|
2022-03-06 05:16:31 +01:00
|
|
|
const [videoProperties, setVideoProperties] = useState<VideoProperties>({
|
|
|
|
posterUrl: '',
|
|
|
|
videoUrl: '',
|
|
|
|
});
|
2022-02-27 15:04:12 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-02 23:05:56 +02:00
|
|
|
let video: any;
|
|
|
|
if (videoRef.current && id) {
|
|
|
|
video = videojs(videoRef.current, {});
|
2022-02-27 15:04:12 +01:00
|
|
|
}
|
2022-06-02 23:05:56 +02:00
|
|
|
}, [videoRef]);
|
2022-02-27 15:04:12 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-02 23:05:56 +02:00
|
|
|
if (!id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const video = videojs(videoRef.current);
|
|
|
|
video.src({
|
|
|
|
src: `${apiHost}/vod/event/${id}/index.m3u8`,
|
|
|
|
type: 'application/vnd.apple.mpegurl',
|
|
|
|
});
|
|
|
|
video.poster(`${apiHost}/api/events/${id}/snapshot.jpg`);
|
|
|
|
if (videoIsPlaying) {
|
|
|
|
video.play();
|
|
|
|
}
|
|
|
|
}, [id]);
|
2022-02-27 15:04:12 +01:00
|
|
|
|
2022-06-02 23:05:56 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (!videoRef) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-27 15:04:12 +01:00
|
|
|
|
2022-06-02 23:05:56 +02:00
|
|
|
const video = videojs(videoRef.current);
|
|
|
|
if (video.paused() && videoIsPlaying) {
|
|
|
|
video.play();
|
|
|
|
} else if (!video.paused() && !videoIsPlaying) {
|
|
|
|
video.pause();
|
2022-02-27 15:04:12 +01:00
|
|
|
}
|
|
|
|
}, [videoIsPlaying, videoRef]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const video = videoRef.current;
|
|
|
|
const videoExists = !isNullOrUndefined(video);
|
|
|
|
const hasSeeked = currentTime >= 0;
|
2022-03-06 05:16:31 +01:00
|
|
|
if (video && videoExists && hasSeeked) {
|
2022-02-27 15:04:12 +01:00
|
|
|
video.currentTime = currentTime;
|
|
|
|
}
|
|
|
|
}, [currentTime, videoRef]);
|
|
|
|
|
|
|
|
const onTimeUpdateHandler = useCallback(
|
|
|
|
(event: Event) => {
|
|
|
|
const target = event.target as HTMLMediaElement;
|
|
|
|
const timeUpdateEvent = {
|
|
|
|
isPlaying: videoIsPlaying,
|
|
|
|
timestamp: target.currentTime,
|
|
|
|
};
|
|
|
|
onTimeUpdate && onTimeUpdate(timeUpdateEvent);
|
|
|
|
},
|
|
|
|
[videoIsPlaying, onTimeUpdate]
|
|
|
|
);
|
|
|
|
|
2022-06-02 21:31:20 +02:00
|
|
|
const { posterUrl, videoUrl } = videoProperties;
|
2022-02-27 15:04:12 +01:00
|
|
|
return (
|
2022-06-02 23:05:56 +02:00
|
|
|
<div data-vjs-player>
|
|
|
|
<video
|
|
|
|
ref={videoRef}
|
|
|
|
onTimeUpdate={onTimeUpdateHandler}
|
|
|
|
onPause={onPause}
|
|
|
|
onPlay={onPlay}
|
|
|
|
poster={posterUrl}
|
|
|
|
src={videoUrl}
|
|
|
|
className="video-js vjs-fluid"
|
|
|
|
data-setup="{}"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-02-27 15:04:12 +01:00
|
|
|
);
|
|
|
|
};
|