blakeblackshear.frigate/web/src/routes/Recording.jsx

101 lines
3.1 KiB
React
Raw Normal View History

2021-05-28 19:13:48 +02:00
import { h } from 'preact';
2021-06-02 08:41:26 +02:00
import { closestTo, format, parseISO } from 'date-fns';
2021-05-28 19:13:48 +02:00
import ActivityIndicator from '../components/ActivityIndicator';
import Heading from '../components/Heading';
2021-06-02 08:41:26 +02:00
import RecordingPlaylist from '../components/RecordingPlaylist';
2021-05-28 19:13:48 +02:00
import VideoPlayer from '../components/VideoPlayer';
import { FetchStatus, useApiHost, useRecording } from '../api';
2021-06-02 10:27:07 +02:00
export default function Recording({ camera, date, hour, seconds }) {
2021-05-28 19:13:48 +02:00
const apiHost = useApiHost();
const { data, status } = useRecording(camera);
if (status !== FetchStatus.LOADED) {
return <ActivityIndicator />;
}
if (data.length === 0) {
return (
<div className="space-y-4">
<Heading>{camera} Recordings</Heading>
<div class="bg-yellow-100 border-l-4 border-yellow-500 text-yellow-700 p-4" role="alert">
<p class="font-bold">No Recordings Found</p>
<p>Make sure you have enabled the record role in your configuration for the {camera} camera.</p>
</div>
</div>
);
}
2021-05-28 19:13:48 +02:00
const recordingDates = data.map((item) => item.date);
const selectedDate = closestTo(
date ? parseISO(date) : new Date(),
recordingDates.map((i) => parseISO(i))
);
const selectedKey = format(selectedDate, 'yyyy-MM-dd');
const [year, month, day] = selectedKey.split('-');
const playlist = [];
const hours = [];
for (const item of data) {
2021-05-28 19:25:49 +02:00
if (item.date === selectedKey) {
2021-05-28 19:13:48 +02:00
for (const recording of item.recordings) {
playlist.push({
name: `${selectedKey} ${recording.hour}:00`,
description: `${camera} recording @ ${recording.hour}:00.`,
sources: [
{
src: `${apiHost}/vod/${year}-${month}/${day}/${recording.hour}/${camera}/index.m3u8`,
type: 'application/vnd.apple.mpegurl',
},
],
});
hours.push(recording.hour);
}
}
}
const selectedHour = hours.indexOf(hour);
if (this.player) {
2021-05-28 19:13:48 +02:00
this.player.playlist([]);
this.player.playlist(playlist);
this.player.playlist.autoadvance(0);
if (selectedHour !== -1) {
this.player.playlist.currentItem(selectedHour);
2021-06-02 10:27:07 +02:00
if (seconds !== undefined) {
this.player.currentTime(seconds);
}
2021-05-28 19:13:48 +02:00
}
// Force playback rate to be correct
const playbackRate = this.player.playbackRate();
this.player.defaultPlaybackRate(playbackRate);
2021-05-28 19:13:48 +02:00
}
return (
2022-02-27 15:04:12 +01:00
<div className="space-y-4 p-2 px-4">
2021-05-28 19:13:48 +02:00
<Heading>{camera} Recordings</Heading>
<VideoPlayer
onReady={(player) => {
if (player.playlist) {
player.playlist(playlist);
player.playlist.autoadvance(0);
if (selectedHour !== -1) {
player.playlist.currentItem(selectedHour);
2021-06-02 10:27:07 +02:00
if (seconds !== undefined) {
player.currentTime(seconds);
}
2021-05-28 19:13:48 +02:00
}
this.player = player;
}
}}
onDispose={() => {
this.player = null;
}}
2021-06-02 08:41:26 +02:00
>
<RecordingPlaylist camera={camera} recordings={data} selectedDate={selectedKey} selectedHour={hour} />
2021-06-02 08:41:26 +02:00
</VideoPlayer>
2021-05-28 19:13:48 +02:00
</div>
);
}