2021-06-13 21:21:20 +02:00
|
|
|
import { h, Fragment } from 'preact';
|
2021-02-07 22:46:05 +01:00
|
|
|
import AutoUpdatingCameraImage from '../components/AutoUpdatingCameraImage';
|
2022-03-06 05:16:31 +01:00
|
|
|
import ActivityIndicator from '../components/ActivityIndicator';
|
2021-06-13 21:21:20 +02:00
|
|
|
import JSMpegPlayer from '../components/JSMpegPlayer';
|
2021-02-07 22:46:05 +01:00
|
|
|
import Button from '../components/Button';
|
|
|
|
import Card from '../components/Card';
|
|
|
|
import Heading from '../components/Heading';
|
|
|
|
import Link from '../components/Link';
|
|
|
|
import SettingsIcon from '../icons/Settings';
|
|
|
|
import Switch from '../components/Switch';
|
2021-07-14 15:32:19 +02:00
|
|
|
import ButtonsTabbed from '../components/ButtonsTabbed';
|
2021-02-07 22:46:05 +01:00
|
|
|
import { usePersistence } from '../context';
|
2021-02-09 20:35:33 +01:00
|
|
|
import { useCallback, useMemo, useState } from 'preact/hooks';
|
2022-02-26 20:11:00 +01:00
|
|
|
import { useApiHost } from '../api';
|
|
|
|
import useSWR from 'swr';
|
2022-11-02 12:36:09 +01:00
|
|
|
import WebRtcPlayer from '../components/WebRtcPlayer';
|
2023-05-21 14:53:25 +02:00
|
|
|
import '../components/MsePlayer';
|
2023-04-26 13:08:53 +02:00
|
|
|
import CameraControlPanel from '../components/CameraControlPanel';
|
2023-05-21 14:53:25 +02:00
|
|
|
import { baseUrl } from '../api/baseUrl';
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-02-09 20:35:33 +01:00
|
|
|
const emptyObject = Object.freeze({});
|
|
|
|
|
2021-02-05 00:19:47 +01:00
|
|
|
export default function Camera({ camera }) {
|
2022-02-26 20:11:00 +01:00
|
|
|
const { data: config } = useSWR('config');
|
2021-01-26 16:04:03 +01:00
|
|
|
const apiHost = useApiHost();
|
2021-02-05 00:19:47 +01:00
|
|
|
const [showSettings, setShowSettings] = useState(false);
|
2021-06-13 21:21:20 +02:00
|
|
|
const [viewMode, setViewMode] = useState('live');
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-02-09 20:35:33 +01:00
|
|
|
const cameraConfig = config?.cameras[camera];
|
2023-01-28 15:15:52 +01:00
|
|
|
const restreamEnabled =
|
|
|
|
cameraConfig && Object.keys(config.go2rtc.streams || {}).includes(cameraConfig.live.stream_name);
|
2022-11-02 12:36:09 +01:00
|
|
|
const jsmpegWidth = cameraConfig
|
2023-01-17 00:50:35 +01:00
|
|
|
? Math.round(cameraConfig.live.height * (cameraConfig.detect.width / cameraConfig.detect.height))
|
2022-03-06 05:16:31 +01:00
|
|
|
: 0;
|
2023-01-14 00:27:16 +01:00
|
|
|
const [viewSource, setViewSource, sourceIsLoaded] = usePersistence(
|
|
|
|
`${camera}-source`,
|
2023-02-12 14:36:36 +01:00
|
|
|
getDefaultLiveMode(config, cameraConfig, restreamEnabled)
|
2023-01-14 00:27:16 +01:00
|
|
|
);
|
2023-01-17 00:50:35 +01:00
|
|
|
const sourceValues = restreamEnabled ? ['mse', 'webrtc', 'jsmpeg'] : ['jsmpeg'];
|
2021-02-09 20:35:33 +01:00
|
|
|
const [options, setOptions] = usePersistence(`${camera}-feed`, emptyObject);
|
2021-01-09 18:26:46 +01:00
|
|
|
|
|
|
|
const handleSetOption = useCallback(
|
|
|
|
(id, value) => {
|
2021-02-05 00:19:47 +01:00
|
|
|
const newOptions = { ...options, [id]: value };
|
|
|
|
setOptions(newOptions);
|
2021-01-09 18:26:46 +01:00
|
|
|
},
|
2021-02-09 20:35:33 +01:00
|
|
|
[options, setOptions]
|
2021-01-09 18:26:46 +01:00
|
|
|
);
|
|
|
|
|
2021-02-05 00:19:47 +01:00
|
|
|
const searchParams = useMemo(
|
|
|
|
() =>
|
|
|
|
new URLSearchParams(
|
|
|
|
Object.keys(options).reduce((memo, key) => {
|
|
|
|
memo.push([key, options[key] === true ? '1' : '0']);
|
|
|
|
return memo;
|
|
|
|
}, [])
|
|
|
|
),
|
2021-02-09 20:35:33 +01:00
|
|
|
[options]
|
2021-02-05 00:19:47 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleToggleSettings = useCallback(() => {
|
|
|
|
setShowSettings(!showSettings);
|
|
|
|
}, [showSettings, setShowSettings]);
|
|
|
|
|
2022-11-02 12:36:09 +01:00
|
|
|
if (!cameraConfig || !sourceIsLoaded) {
|
2022-03-06 05:16:31 +01:00
|
|
|
return <ActivityIndicator />;
|
|
|
|
}
|
|
|
|
|
2023-01-28 15:15:52 +01:00
|
|
|
if (!restreamEnabled) {
|
|
|
|
setViewSource('jsmpeg');
|
|
|
|
}
|
|
|
|
|
2021-02-05 00:19:47 +01:00
|
|
|
const optionContent = showSettings ? (
|
|
|
|
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
|
2021-02-13 01:06:51 +01:00
|
|
|
<Switch
|
|
|
|
checked={options['bbox']}
|
|
|
|
id="bbox"
|
|
|
|
onChange={handleSetOption}
|
|
|
|
label="Bounding box"
|
|
|
|
labelPosition="after"
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
checked={options['timestamp']}
|
|
|
|
id="timestamp"
|
|
|
|
onChange={handleSetOption}
|
|
|
|
label="Timestamp"
|
|
|
|
labelPosition="after"
|
|
|
|
/>
|
|
|
|
<Switch checked={options['zones']} id="zones" onChange={handleSetOption} label="Zones" labelPosition="after" />
|
2023-01-14 00:27:16 +01:00
|
|
|
<Switch
|
|
|
|
checked={options['mask']}
|
|
|
|
id="mask"
|
|
|
|
onChange={handleSetOption}
|
|
|
|
label="Motion Masks"
|
|
|
|
labelPosition="after"
|
|
|
|
/>
|
2021-02-13 01:06:51 +01:00
|
|
|
<Switch
|
|
|
|
checked={options['motion']}
|
|
|
|
id="motion"
|
|
|
|
onChange={handleSetOption}
|
|
|
|
label="Motion boxes"
|
|
|
|
labelPosition="after"
|
|
|
|
/>
|
|
|
|
<Switch
|
|
|
|
checked={options['regions']}
|
|
|
|
id="regions"
|
|
|
|
onChange={handleSetOption}
|
|
|
|
label="Regions"
|
|
|
|
labelPosition="after"
|
|
|
|
/>
|
2021-02-05 00:19:47 +01:00
|
|
|
<Link href={`/cameras/${camera}/editor`}>Mask & Zone creator</Link>
|
|
|
|
</div>
|
|
|
|
) : null;
|
2021-01-09 18:26:46 +01:00
|
|
|
|
2021-06-13 21:21:20 +02:00
|
|
|
let player;
|
2021-06-13 21:24:34 +02:00
|
|
|
if (viewMode === 'live') {
|
2023-01-17 00:50:35 +01:00
|
|
|
if (viewSource == 'mse' && restreamEnabled) {
|
2023-01-14 00:27:16 +01:00
|
|
|
if ('MediaSource' in window) {
|
2022-12-18 00:56:26 +01:00
|
|
|
player = (
|
|
|
|
<Fragment>
|
2023-01-14 00:27:16 +01:00
|
|
|
<div className="max-w-5xl">
|
2023-05-21 14:53:25 +02:00
|
|
|
<video-stream
|
|
|
|
mode="mse"
|
|
|
|
src={new URL(`${baseUrl.replace(/^http/, 'ws')}live/webrtc/api/ws?src=${camera}`)}
|
|
|
|
/>
|
2022-12-18 00:56:26 +01:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
player = (
|
|
|
|
<Fragment>
|
2023-01-14 00:27:16 +01:00
|
|
|
<div className="w-5xl text-center text-sm">
|
|
|
|
MSE is not supported on iOS devices. You'll need to use jsmpeg or webRTC. See the docs for more info.
|
2022-12-18 00:56:26 +01:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2023-01-17 00:50:35 +01:00
|
|
|
} else if (viewSource == 'webrtc' && restreamEnabled) {
|
2022-11-02 12:36:09 +01:00
|
|
|
player = (
|
|
|
|
<Fragment>
|
|
|
|
<div className="max-w-5xl">
|
2023-01-18 05:36:52 +01:00
|
|
|
<WebRtcPlayer camera={cameraConfig.live.stream_name} />
|
2022-11-02 12:36:09 +01:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
player = (
|
|
|
|
<Fragment>
|
|
|
|
<div>
|
2023-01-17 00:50:35 +01:00
|
|
|
<JSMpegPlayer camera={camera} width={jsmpegWidth} height={cameraConfig.live.height} />
|
2022-11-02 12:36:09 +01:00
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2022-03-06 05:16:31 +01:00
|
|
|
} else if (viewMode === 'debug') {
|
2021-06-13 21:49:13 +02:00
|
|
|
player = (
|
|
|
|
<Fragment>
|
|
|
|
<div>
|
|
|
|
<AutoUpdatingCameraImage camera={camera} searchParams={searchParams} />
|
|
|
|
</div>
|
2021-02-05 00:19:47 +01:00
|
|
|
|
2021-06-13 21:49:13 +02:00
|
|
|
<Button onClick={handleToggleSettings} type="text">
|
|
|
|
<span className="w-5 h-5">
|
|
|
|
<SettingsIcon />
|
|
|
|
</span>{' '}
|
|
|
|
<span>{showSettings ? 'Hide' : 'Show'} Options</span>
|
|
|
|
</Button>
|
|
|
|
{showSettings ? <Card header="Options" elevated={false} content={optionContent} /> : null}
|
|
|
|
</Fragment>
|
|
|
|
);
|
2021-06-13 21:21:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-02-27 15:04:12 +01:00
|
|
|
<div className="space-y-4 p-2 px-4">
|
2022-11-02 12:36:09 +01:00
|
|
|
<div className="flex justify-between">
|
|
|
|
<Heading className="p-2" size="2xl">
|
|
|
|
{camera.replaceAll('_', ' ')}
|
|
|
|
</Heading>
|
|
|
|
<select
|
|
|
|
className="basis-1/8 cursor-pointer rounded dark:bg-slate-800"
|
|
|
|
value={viewSource}
|
|
|
|
onChange={(e) => setViewSource(e.target.value)}
|
|
|
|
>
|
|
|
|
{sourceValues.map((item) => (
|
|
|
|
<option key={item} value={item}>
|
|
|
|
{item}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
2023-01-11 13:09:58 +01:00
|
|
|
<ButtonsTabbed viewModes={['live', 'debug']} currentViewMode={viewMode} setViewMode={setViewMode} />
|
2021-06-13 21:21:20 +02:00
|
|
|
|
|
|
|
{player}
|
2021-01-19 17:44:18 +01:00
|
|
|
|
2023-04-26 13:08:53 +02:00
|
|
|
{cameraConfig?.onvif?.host && (
|
|
|
|
<div className="dark:bg-gray-800 shadow-md hover:shadow-lg rounded-lg transition-shadow p-4 w-full sm:w-min">
|
|
|
|
<Heading size="sm">Control Panel</Heading>
|
|
|
|
<CameraControlPanel camera={camera} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2021-01-19 17:44:18 +01:00
|
|
|
<div className="space-y-4">
|
2021-01-09 18:26:46 +01:00
|
|
|
<Heading size="sm">Tracked objects</Heading>
|
2021-02-02 05:28:25 +01:00
|
|
|
<div className="flex flex-wrap justify-start">
|
|
|
|
{cameraConfig.objects.track.map((objectType) => (
|
|
|
|
<Card
|
|
|
|
className="mb-4 mr-4"
|
|
|
|
key={objectType}
|
|
|
|
header={objectType}
|
2022-12-12 13:30:34 +01:00
|
|
|
href={`/events?cameras=${camera}&labels=${encodeURIComponent(objectType)}`}
|
2022-08-25 14:32:30 +02:00
|
|
|
media={<img src={`${apiHost}/api/${camera}/${encodeURIComponent(objectType)}/thumbnail.jpg`} />}
|
2021-02-02 05:28:25 +01:00
|
|
|
/>
|
|
|
|
))}
|
2021-01-19 17:44:18 +01:00
|
|
|
</div>
|
2021-01-09 18:26:46 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2023-01-14 00:27:16 +01:00
|
|
|
|
2023-01-17 00:50:35 +01:00
|
|
|
function getDefaultLiveMode(config, cameraConfig, restreamEnabled) {
|
2023-01-14 00:27:16 +01:00
|
|
|
if (cameraConfig) {
|
2023-01-17 00:50:35 +01:00
|
|
|
if (restreamEnabled) {
|
2023-01-14 00:27:16 +01:00
|
|
|
return config.ui.live_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'jsmpeg';
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|