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';
|
|
|
|
|
|
|
|
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 21:31:20 +02:00
|
|
|
const videoRef = useRef<HTMLVideoElement | null>(null);
|
|
|
|
|
|
|
|
const [posterLoaded, setPosterLoaded] = useState(false);
|
|
|
|
const [videoHeight, setVideoHeight] = useState<number | undefined>(undefined);
|
|
|
|
|
2022-03-06 05:16:31 +01:00
|
|
|
const [videoProperties, setVideoProperties] = useState<VideoProperties>({
|
|
|
|
posterUrl: '',
|
|
|
|
videoUrl: '',
|
|
|
|
});
|
2022-02-27 15:04:12 +01:00
|
|
|
|
2022-06-02 21:39:53 +02:00
|
|
|
const videoCallback = useCallback(
|
|
|
|
(domNode: any) => {
|
|
|
|
videoRef.current = domNode;
|
|
|
|
|
|
|
|
if (posterLoaded) {
|
|
|
|
setVideoHeight(videoRef.current?.offsetHeight);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[posterLoaded]
|
|
|
|
);
|
2022-02-27 15:04:12 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const idExists = !isNullOrUndefined(id);
|
|
|
|
if (idExists) {
|
|
|
|
if (videoRef.current && !videoRef.current.paused) {
|
2022-03-06 05:16:31 +01:00
|
|
|
videoRef.current = null;
|
2022-02-27 15:04:12 +01:00
|
|
|
}
|
|
|
|
|
2022-06-02 21:31:20 +02:00
|
|
|
const posterUrl = `${apiHost}/api/events/${id}/snapshot.jpg`;
|
|
|
|
const poster = new Image();
|
|
|
|
poster.src = posterUrl;
|
|
|
|
poster.onload = () => {
|
|
|
|
setPosterLoaded(true);
|
|
|
|
};
|
2022-02-27 15:04:12 +01:00
|
|
|
setVideoProperties({
|
2022-06-02 21:31:20 +02:00
|
|
|
posterUrl,
|
2022-02-27 15:04:12 +01:00
|
|
|
videoUrl: `${apiHost}/vod/event/${id}/index.m3u8`,
|
|
|
|
});
|
|
|
|
} else {
|
2022-03-06 05:16:31 +01:00
|
|
|
setVideoProperties({
|
|
|
|
posterUrl: '',
|
|
|
|
videoUrl: '',
|
|
|
|
});
|
2022-02-27 15:04:12 +01:00
|
|
|
}
|
|
|
|
}, [id, videoHeight, videoRef, apiHost]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const playVideo = (video: HTMLMediaElement) => video.play();
|
|
|
|
|
|
|
|
const attemptPlayVideo = (video: HTMLMediaElement) => {
|
|
|
|
const videoHasNotLoaded = video.readyState <= 1;
|
|
|
|
if (videoHasNotLoaded) {
|
|
|
|
video.oncanplay = () => {
|
|
|
|
playVideo(video);
|
|
|
|
};
|
|
|
|
video.load();
|
|
|
|
} else {
|
|
|
|
playVideo(video);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const video = videoRef.current;
|
|
|
|
const videoExists = !isNullOrUndefined(video);
|
2022-03-06 05:16:31 +01:00
|
|
|
if (video && videoExists) {
|
2022-02-27 15:04:12 +01:00
|
|
|
if (videoIsPlaying) {
|
|
|
|
attemptPlayVideo(video);
|
|
|
|
} else {
|
|
|
|
video.pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [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 (
|
|
|
|
<video
|
2022-06-02 21:39:53 +02:00
|
|
|
ref={videoCallback}
|
2022-02-27 15:04:12 +01:00
|
|
|
key={posterUrl}
|
|
|
|
onTimeUpdate={onTimeUpdateHandler}
|
|
|
|
onPause={onPause}
|
|
|
|
onPlay={onPlay}
|
|
|
|
poster={posterUrl}
|
2022-06-02 21:31:20 +02:00
|
|
|
preload="metadata"
|
2022-02-27 15:04:12 +01:00
|
|
|
controls
|
2022-06-02 21:31:20 +02:00
|
|
|
style={videoHeight ? { minHeight: `${videoHeight}px` } : {}}
|
2022-02-27 15:04:12 +01:00
|
|
|
playsInline
|
|
|
|
>
|
2022-06-02 21:31:20 +02:00
|
|
|
<source type="application/vnd.apple.mpegurl" src={videoUrl} />
|
2022-02-27 15:04:12 +01:00
|
|
|
</video>
|
|
|
|
);
|
|
|
|
};
|