2023-12-16 00:24:50 +01:00
|
|
|
import LivePlayer from "@/components/player/LivePlayer";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import {
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuLabel,
|
|
|
|
DropdownMenuRadioGroup,
|
|
|
|
DropdownMenuRadioItem,
|
|
|
|
DropdownMenuSeparator,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
} from "@/components/ui/dropdown-menu";
|
2023-12-08 14:33:22 +01:00
|
|
|
import Heading from "@/components/ui/heading";
|
2023-12-16 00:24:50 +01:00
|
|
|
import { usePersistence } from "@/hooks/use-persistence";
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
import { useMemo, useState } from "react";
|
2023-12-16 15:40:00 +01:00
|
|
|
import { useParams } from "react-router-dom";
|
2023-12-16 00:24:50 +01:00
|
|
|
import useSWR from "swr";
|
2023-12-08 14:33:22 +01:00
|
|
|
|
|
|
|
function Live() {
|
2023-12-16 00:24:50 +01:00
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
2023-12-16 15:40:00 +01:00
|
|
|
const { camera: openedCamera } = useParams();
|
2023-12-16 00:24:50 +01:00
|
|
|
|
2023-12-16 15:40:00 +01:00
|
|
|
const [camera, setCamera] = useState<string>(
|
|
|
|
openedCamera ?? "Select A Camera"
|
|
|
|
);
|
2023-12-16 00:24:50 +01:00
|
|
|
const cameraConfig = useMemo(() => {
|
|
|
|
return config?.cameras[camera];
|
|
|
|
}, [camera, config]);
|
2023-12-16 15:40:00 +01:00
|
|
|
const sortedCameras = useMemo(() => {
|
|
|
|
if (!config) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.values(config.cameras)
|
|
|
|
.sort((aConf, bConf) => aConf.ui.order - bConf.ui.order);
|
|
|
|
}, [config]);
|
2023-12-16 00:24:50 +01:00
|
|
|
const restreamEnabled = useMemo(() => {
|
|
|
|
return (
|
|
|
|
config &&
|
|
|
|
cameraConfig &&
|
|
|
|
Object.keys(config.go2rtc.streams || {}).includes(
|
|
|
|
cameraConfig.live.stream_name
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}, [config, cameraConfig]);
|
|
|
|
const defaultLiveMode = useMemo(() => {
|
|
|
|
if (cameraConfig) {
|
|
|
|
if (restreamEnabled) {
|
|
|
|
return cameraConfig.ui.live_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "jsmpeg";
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}, [cameraConfig, restreamEnabled]);
|
|
|
|
const [viewSource, setViewSource, sourceIsLoaded] = usePersistence(
|
|
|
|
`${camera}-source`,
|
|
|
|
defaultLiveMode
|
|
|
|
);
|
|
|
|
|
2023-12-08 14:33:22 +01:00
|
|
|
return (
|
2023-12-16 00:24:50 +01:00
|
|
|
<div className=" w-full">
|
|
|
|
<div className="flex justify-between">
|
|
|
|
<Heading as="h2">Live</Heading>
|
|
|
|
<div>
|
|
|
|
<DropdownMenu>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button className="capitalize" variant="outline">
|
|
|
|
{camera?.replaceAll("_", " ") || "Select A Camera"}
|
|
|
|
</Button>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuLabel>Select A Camera</DropdownMenuLabel>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<DropdownMenuRadioGroup value={camera} onValueChange={setCamera}>
|
2023-12-16 15:40:00 +01:00
|
|
|
{(sortedCameras).map((item) => (
|
2023-12-16 00:24:50 +01:00
|
|
|
<DropdownMenuRadioItem
|
|
|
|
className="capitalize"
|
2023-12-16 15:40:00 +01:00
|
|
|
key={item.name}
|
|
|
|
value={item.name}
|
2023-12-16 00:24:50 +01:00
|
|
|
>
|
2023-12-16 15:40:00 +01:00
|
|
|
{item.name.replaceAll("_", " ")}
|
2023-12-16 00:24:50 +01:00
|
|
|
</DropdownMenuRadioItem>
|
|
|
|
))}
|
|
|
|
</DropdownMenuRadioGroup>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
<DropdownMenu>
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
<Button className="capitalize" variant="outline">
|
|
|
|
{viewSource || defaultLiveMode || "Select A Live Mode"}
|
|
|
|
</Button>
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuLabel>Select A Live Mode</DropdownMenuLabel>
|
|
|
|
<DropdownMenuSeparator />
|
|
|
|
<DropdownMenuRadioGroup
|
|
|
|
value={`${viewSource}`}
|
|
|
|
onValueChange={setViewSource}
|
|
|
|
>
|
|
|
|
<DropdownMenuRadioItem value="webrtc">
|
|
|
|
Webrtc
|
|
|
|
</DropdownMenuRadioItem>
|
|
|
|
<DropdownMenuRadioItem value="mse">MSE</DropdownMenuRadioItem>
|
|
|
|
<DropdownMenuRadioItem value="jsmpeg">
|
|
|
|
Jsmpeg
|
|
|
|
</DropdownMenuRadioItem>
|
|
|
|
<DropdownMenuRadioItem value="debug">
|
|
|
|
Debug
|
|
|
|
</DropdownMenuRadioItem>
|
|
|
|
</DropdownMenuRadioGroup>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{cameraConfig && sourceIsLoaded && (
|
|
|
|
<LivePlayer
|
|
|
|
liveMode={`${viewSource ?? defaultLiveMode}`}
|
|
|
|
cameraConfig={cameraConfig}
|
|
|
|
/>
|
|
|
|
)}
|
2023-12-08 14:33:22 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Live;
|