2023-12-16 00:24:50 +01:00
|
|
|
import WebRtcPlayer from "./WebRTCPlayer";
|
|
|
|
import { CameraConfig } from "@/types/frigateConfig";
|
|
|
|
import AutoUpdatingCameraImage from "../camera/AutoUpdatingCameraImage";
|
|
|
|
import ActivityIndicator from "../ui/activity-indicator";
|
|
|
|
import { Button } from "../ui/button";
|
|
|
|
import { LuSettings } from "react-icons/lu";
|
2024-02-10 13:30:53 +01:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
2023-12-16 00:24:50 +01:00
|
|
|
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
|
|
|
|
import { Switch } from "../ui/switch";
|
|
|
|
import { Label } from "../ui/label";
|
2023-12-16 15:40:00 +01:00
|
|
|
import { usePersistence } from "@/hooks/use-persistence";
|
2024-01-04 00:38:52 +01:00
|
|
|
import MSEPlayer from "./MsePlayer";
|
2023-12-20 15:34:27 +01:00
|
|
|
import JSMpegPlayer from "./JSMpegPlayer";
|
2024-02-10 13:30:53 +01:00
|
|
|
import { MdCircle, MdLeakAdd } from "react-icons/md";
|
|
|
|
import { BsSoundwave } from "react-icons/bs";
|
|
|
|
import Chip from "../Chip";
|
|
|
|
import useCameraActivity from "@/hooks/use-camera-activity";
|
|
|
|
import { useRecordingsState } from "@/api/ws";
|
|
|
|
import { LivePlayerMode } from "@/types/live";
|
|
|
|
import useCameraLiveMode from "@/hooks/use-camera-live-mode";
|
2023-12-16 00:24:50 +01:00
|
|
|
|
|
|
|
const emptyObject = Object.freeze({});
|
|
|
|
|
|
|
|
type LivePlayerProps = {
|
2024-02-10 13:30:53 +01:00
|
|
|
className?: string;
|
2023-12-16 00:24:50 +01:00
|
|
|
cameraConfig: CameraConfig;
|
2024-02-10 13:30:53 +01:00
|
|
|
preferredLiveMode?: LivePlayerMode;
|
|
|
|
showStillWithoutActivity?: boolean;
|
2023-12-16 00:24:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
type Options = { [key: string]: boolean };
|
|
|
|
|
|
|
|
export default function LivePlayer({
|
2024-02-10 13:30:53 +01:00
|
|
|
className,
|
2023-12-16 00:24:50 +01:00
|
|
|
cameraConfig,
|
2024-02-10 13:30:53 +01:00
|
|
|
preferredLiveMode,
|
|
|
|
showStillWithoutActivity = true,
|
2023-12-16 00:24:50 +01:00
|
|
|
}: LivePlayerProps) {
|
2024-02-10 13:30:53 +01:00
|
|
|
// camera activity
|
|
|
|
const { activeMotion, activeAudio, activeTracking } =
|
|
|
|
useCameraActivity(cameraConfig);
|
|
|
|
|
2024-02-11 14:23:45 +01:00
|
|
|
const cameraActive = useMemo(() => activeMotion || activeTracking, [activeMotion, activeTracking])
|
2024-02-10 13:30:53 +01:00
|
|
|
const liveMode = useCameraLiveMode(cameraConfig, preferredLiveMode);
|
|
|
|
|
|
|
|
const [liveReady, setLiveReady] = useState(false);
|
|
|
|
useEffect(() => {
|
|
|
|
if (!liveReady) {
|
2024-02-11 14:23:45 +01:00
|
|
|
if (cameraActive && liveMode == "jsmpeg") {
|
2024-02-10 13:30:53 +01:00
|
|
|
setLiveReady(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-11 14:23:45 +01:00
|
|
|
if (!cameraActive) {
|
2024-02-10 13:30:53 +01:00
|
|
|
setLiveReady(false);
|
|
|
|
}
|
2024-02-11 14:23:45 +01:00
|
|
|
}, [cameraActive, liveReady]);
|
2024-02-10 13:30:53 +01:00
|
|
|
|
|
|
|
const { payload: recording } = useRecordingsState(cameraConfig.name);
|
|
|
|
|
|
|
|
// debug view settings
|
2023-12-16 00:24:50 +01:00
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
const [showSettings, setShowSettings] = useState(false);
|
2023-12-16 00:24:50 +01:00
|
|
|
const [options, setOptions] = usePersistence(
|
2024-02-10 13:30:53 +01:00
|
|
|
`${cameraConfig?.name}-feed`,
|
2023-12-16 00:24:50 +01:00
|
|
|
emptyObject
|
|
|
|
);
|
|
|
|
const handleSetOption = useCallback(
|
|
|
|
(id: string, value: boolean) => {
|
|
|
|
const newOptions = { ...options, [id]: value };
|
|
|
|
setOptions(newOptions);
|
|
|
|
},
|
2024-02-10 13:30:53 +01:00
|
|
|
[options]
|
2023-12-16 00:24:50 +01:00
|
|
|
);
|
|
|
|
const searchParams = useMemo(
|
|
|
|
() =>
|
|
|
|
new URLSearchParams(
|
|
|
|
Object.keys(options).reduce((memo, key) => {
|
|
|
|
//@ts-ignore we know this is correct
|
|
|
|
memo.push([key, options[key] === true ? "1" : "0"]);
|
|
|
|
return memo;
|
|
|
|
}, [])
|
|
|
|
),
|
|
|
|
[options]
|
|
|
|
);
|
|
|
|
const handleToggleSettings = useCallback(() => {
|
|
|
|
setShowSettings(!showSettings);
|
2024-02-10 13:30:53 +01:00
|
|
|
}, [showSettings]);
|
2023-12-16 00:24:50 +01:00
|
|
|
|
2024-02-10 13:30:53 +01:00
|
|
|
if (!cameraConfig) {
|
|
|
|
return <ActivityIndicator />;
|
|
|
|
}
|
|
|
|
|
|
|
|
let player;
|
2023-12-16 00:24:50 +01:00
|
|
|
if (liveMode == "webrtc") {
|
2024-02-10 13:30:53 +01:00
|
|
|
player = (
|
|
|
|
<WebRtcPlayer
|
|
|
|
className={`rounded-2xl h-full ${liveReady ? "" : "hidden"}`}
|
|
|
|
camera={cameraConfig.live.stream_name}
|
|
|
|
onPlaying={() => setLiveReady(true)}
|
|
|
|
/>
|
2023-12-16 00:24:50 +01:00
|
|
|
);
|
|
|
|
} else if (liveMode == "mse") {
|
2023-12-31 14:31:05 +01:00
|
|
|
if ("MediaSource" in window || "ManagedMediaSource" in window) {
|
2024-02-10 13:30:53 +01:00
|
|
|
player = (
|
|
|
|
<MSEPlayer
|
|
|
|
className={`rounded-2xl h-full ${liveReady ? "" : "hidden"}`}
|
|
|
|
camera={cameraConfig.name}
|
|
|
|
onPlaying={() => setLiveReady(true)}
|
|
|
|
/>
|
2023-12-31 14:31:05 +01:00
|
|
|
);
|
|
|
|
} else {
|
2024-02-10 13:30:53 +01:00
|
|
|
player = (
|
2023-12-31 14:31:05 +01:00
|
|
|
<div className="w-5xl text-center text-sm">
|
|
|
|
MSE is only supported on iOS 17.1+. You'll need to update if available
|
|
|
|
or use jsmpeg / webRTC streams. See the docs for more info.
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2023-12-16 00:24:50 +01:00
|
|
|
} else if (liveMode == "jsmpeg") {
|
2024-02-10 13:30:53 +01:00
|
|
|
player = (
|
|
|
|
<JSMpegPlayer
|
|
|
|
className="w-full flex justify-center"
|
|
|
|
camera={cameraConfig.name}
|
|
|
|
width={cameraConfig.detect.width}
|
|
|
|
height={cameraConfig.detect.height}
|
|
|
|
/>
|
2023-12-16 00:24:50 +01:00
|
|
|
);
|
|
|
|
} else if (liveMode == "debug") {
|
2024-02-10 13:30:53 +01:00
|
|
|
player = (
|
2023-12-16 00:24:50 +01:00
|
|
|
<>
|
|
|
|
<AutoUpdatingCameraImage
|
|
|
|
camera={cameraConfig.name}
|
|
|
|
searchParams={searchParams}
|
|
|
|
/>
|
|
|
|
<Button onClick={handleToggleSettings} variant="link" size="sm">
|
|
|
|
<span className="w-5 h-5">
|
|
|
|
<LuSettings />
|
|
|
|
</span>{" "}
|
|
|
|
<span>{showSettings ? "Hide" : "Show"} Options</span>
|
|
|
|
</Button>
|
|
|
|
{showSettings ? (
|
|
|
|
<Card>
|
|
|
|
<CardHeader>
|
|
|
|
<CardTitle>Options</CardTitle>
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent>
|
|
|
|
<DebugSettings
|
|
|
|
handleSetOption={handleSetOption}
|
|
|
|
options={options}
|
|
|
|
/>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
} else {
|
2024-02-10 13:30:53 +01:00
|
|
|
player = <ActivityIndicator />;
|
2023-12-16 00:24:50 +01:00
|
|
|
}
|
2024-02-10 13:30:53 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={`relative flex justify-center w-full outline ${
|
|
|
|
activeTracking
|
|
|
|
? "outline-destructive outline-1 rounded-2xl shadow-[0_0_6px_1px] shadow-destructive"
|
|
|
|
: "outline-0"
|
|
|
|
} transition-all duration-500 ${className}`}
|
|
|
|
>
|
2024-02-11 14:23:45 +01:00
|
|
|
{(showStillWithoutActivity == false || cameraActive) &&
|
2024-02-10 13:30:53 +01:00
|
|
|
player}
|
|
|
|
|
|
|
|
<div
|
|
|
|
className={`absolute left-0 top-0 right-0 bottom-0 w-full ${
|
|
|
|
showStillWithoutActivity && !liveReady ? "visible" : "invisible"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
<AutoUpdatingCameraImage
|
|
|
|
className="w-full h-full"
|
|
|
|
camera={cameraConfig.name}
|
|
|
|
showFps={false}
|
2024-02-11 14:23:45 +01:00
|
|
|
reloadInterval={(cameraActive && !liveReady) ? 200 : 30000}
|
2024-02-10 13:30:53 +01:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="absolute flex left-2 top-2 gap-2">
|
|
|
|
<Chip
|
|
|
|
in={activeMotion}
|
|
|
|
className={`bg-gradient-to-br from-gray-400 to-gray-500 bg-gray-500/90`}
|
|
|
|
>
|
|
|
|
<MdLeakAdd className="w-4 h-4 text-motion" />
|
|
|
|
<div className="ml-1 text-white text-xs">Motion</div>
|
|
|
|
</Chip>
|
|
|
|
|
|
|
|
{cameraConfig.audio.enabled_in_config && (
|
|
|
|
<Chip
|
|
|
|
in={activeAudio}
|
|
|
|
className={`bg-gradient-to-br from-gray-400 to-gray-500 bg-gray-500/90`}
|
|
|
|
>
|
|
|
|
<BsSoundwave className="w-4 h-4 text-audio" />
|
|
|
|
<div className="ml-1 text-white text-xs">Sound</div>
|
|
|
|
</Chip>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Chip className="absolute right-2 top-2 bg-gradient-to-br from-gray-300/50 to-gray-500/90">
|
|
|
|
{recording == "ON" && (
|
|
|
|
<MdCircle className="w-2 h-2 drop-shadow-md shadow-danger text-danger" />
|
|
|
|
)}
|
|
|
|
<div className="ml-1 capitalize text-white text-xs">
|
|
|
|
{cameraConfig.name.replaceAll("_", " ")}
|
|
|
|
</div>
|
|
|
|
</Chip>
|
|
|
|
</div>
|
|
|
|
);
|
2023-12-16 00:24:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type DebugSettingsProps = {
|
|
|
|
handleSetOption: (id: string, value: boolean) => void;
|
|
|
|
options: Options;
|
|
|
|
};
|
|
|
|
|
|
|
|
function DebugSettings({ handleSetOption, options }: DebugSettingsProps) {
|
|
|
|
return (
|
|
|
|
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Switch
|
|
|
|
id="bbox"
|
|
|
|
checked={options["bbox"]}
|
|
|
|
onCheckedChange={(isChecked) => {
|
|
|
|
handleSetOption("bbox", isChecked);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Label htmlFor="bbox">Bounding Box</Label>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Switch
|
|
|
|
id="timestamp"
|
|
|
|
checked={options["timestamp"]}
|
|
|
|
onCheckedChange={(isChecked) => {
|
|
|
|
handleSetOption("timestamp", isChecked);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Label htmlFor="timestamp">Timestamp</Label>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Switch
|
|
|
|
id="zones"
|
|
|
|
checked={options["zones"]}
|
|
|
|
onCheckedChange={(isChecked) => {
|
|
|
|
handleSetOption("zones", isChecked);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Label htmlFor="zones">Zones</Label>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Switch
|
|
|
|
id="mask"
|
|
|
|
checked={options["mask"]}
|
|
|
|
onCheckedChange={(isChecked) => {
|
|
|
|
handleSetOption("mask", isChecked);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Label htmlFor="mask">Mask</Label>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Switch
|
|
|
|
id="motion"
|
|
|
|
checked={options["motion"]}
|
|
|
|
onCheckedChange={(isChecked) => {
|
|
|
|
handleSetOption("motion", isChecked);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Label htmlFor="motion">Motion</Label>
|
|
|
|
</div>
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
<Switch
|
|
|
|
id="regions"
|
|
|
|
checked={options["regions"]}
|
|
|
|
onCheckedChange={(isChecked) => {
|
|
|
|
handleSetOption("regions", isChecked);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Label htmlFor="regions">Regions</Label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|