2025-06-12 22:34:45 +03:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Switch } from "@/components/ui/switch";
|
2025-10-13 19:52:08 +03:00
|
|
|
import { Card, CardContent } from "@/components/ui/card";
|
2025-06-12 22:34:45 +03:00
|
|
|
import Heading from "@/components/ui/heading";
|
|
|
|
|
import { Separator } from "@/components/ui/separator";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import { useForm, useFieldArray } from "react-hook-form";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
import { toast, Toaster } from "sonner";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { useState, useMemo } from "react";
|
|
|
|
|
import { LuTrash2, LuPlus } from "react-icons/lu";
|
|
|
|
|
import ActivityIndicator from "@/components/indicators/activity-indicator";
|
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
|
import useSWR from "swr";
|
2025-10-13 19:52:08 +03:00
|
|
|
import { processCameraName } from "@/utils/cameraUtil";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import { ConfigSetBody } from "@/types/cameraWizard";
|
2025-06-12 22:34:45 +03:00
|
|
|
|
|
|
|
|
const RoleEnum = z.enum(["audio", "detect", "record"]);
|
|
|
|
|
type Role = z.infer<typeof RoleEnum>;
|
|
|
|
|
|
|
|
|
|
type CameraEditFormProps = {
|
|
|
|
|
cameraName?: string;
|
|
|
|
|
onSave?: () => void;
|
|
|
|
|
onCancel?: () => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function CameraEditForm({
|
|
|
|
|
cameraName,
|
|
|
|
|
onSave,
|
|
|
|
|
onCancel,
|
|
|
|
|
}: CameraEditFormProps) {
|
|
|
|
|
const { t } = useTranslation(["views/settings"]);
|
|
|
|
|
const { data: config } = useSWR<FrigateConfig>("config");
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
const formSchema = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
z.object({
|
|
|
|
|
cameraName: z
|
|
|
|
|
.string()
|
2025-10-13 19:52:08 +03:00
|
|
|
.min(1, { message: t("cameraManagement.cameraConfig.nameRequired") }),
|
2025-06-12 22:34:45 +03:00
|
|
|
enabled: z.boolean(),
|
|
|
|
|
ffmpeg: z.object({
|
|
|
|
|
inputs: z
|
|
|
|
|
.array(
|
|
|
|
|
z.object({
|
|
|
|
|
path: z.string().min(1, {
|
2025-10-13 19:52:08 +03:00
|
|
|
message: t(
|
|
|
|
|
"cameraManagement.cameraConfig.ffmpeg.pathRequired",
|
|
|
|
|
),
|
2025-06-12 22:34:45 +03:00
|
|
|
}),
|
|
|
|
|
roles: z.array(RoleEnum).min(1, {
|
2025-10-13 19:52:08 +03:00
|
|
|
message: t(
|
|
|
|
|
"cameraManagement.cameraConfig.ffmpeg.rolesRequired",
|
|
|
|
|
),
|
2025-06-12 22:34:45 +03:00
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.min(1, {
|
2025-10-13 19:52:08 +03:00
|
|
|
message: t("cameraManagement.cameraConfig.ffmpeg.inputsRequired"),
|
2025-06-12 22:34:45 +03:00
|
|
|
})
|
|
|
|
|
.refine(
|
|
|
|
|
(inputs) => {
|
|
|
|
|
const roleOccurrences = new Map<Role, number>();
|
|
|
|
|
inputs.forEach((input) => {
|
|
|
|
|
input.roles.forEach((role) => {
|
|
|
|
|
roleOccurrences.set(
|
|
|
|
|
role,
|
|
|
|
|
(roleOccurrences.get(role) || 0) + 1,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return Array.from(roleOccurrences.values()).every(
|
|
|
|
|
(count) => count <= 1,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-10-13 19:52:08 +03:00
|
|
|
message: t("cameraManagement.cameraConfig.ffmpeg.rolesUnique"),
|
2025-06-12 22:34:45 +03:00
|
|
|
path: ["inputs"],
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
}),
|
2025-10-13 19:52:08 +03:00
|
|
|
go2rtcStreams: z.record(z.string(), z.array(z.string())).optional(),
|
2025-06-12 22:34:45 +03:00
|
|
|
}),
|
|
|
|
|
[t],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
type FormValues = z.infer<typeof formSchema>;
|
|
|
|
|
|
2025-08-26 20:15:01 +03:00
|
|
|
const cameraInfo = useMemo(() => {
|
|
|
|
|
if (!cameraName || !config?.cameras[cameraName]) {
|
|
|
|
|
return {
|
2025-08-26 23:29:52 +03:00
|
|
|
friendly_name: undefined,
|
2025-08-26 20:15:01 +03:00
|
|
|
name: cameraName || "",
|
|
|
|
|
roles: new Set<Role>(),
|
2025-10-13 19:52:08 +03:00
|
|
|
go2rtcStreams: {},
|
2025-08-26 20:15:01 +03:00
|
|
|
};
|
2025-06-12 22:34:45 +03:00
|
|
|
}
|
2025-08-26 20:15:01 +03:00
|
|
|
|
|
|
|
|
const camera = config.cameras[cameraName];
|
|
|
|
|
const roles = new Set<Role>();
|
|
|
|
|
|
|
|
|
|
camera.ffmpeg?.inputs?.forEach((input) => {
|
|
|
|
|
input.roles.forEach((role) => roles.add(role as Role));
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-13 19:52:08 +03:00
|
|
|
// Load existing go2rtc streams
|
|
|
|
|
const go2rtcStreams = config.go2rtc?.streams || {};
|
|
|
|
|
|
2025-08-26 20:15:01 +03:00
|
|
|
return {
|
2025-08-26 23:29:52 +03:00
|
|
|
friendly_name: camera?.friendly_name || cameraName,
|
2025-08-26 20:15:01 +03:00
|
|
|
name: cameraName,
|
|
|
|
|
roles,
|
2025-10-13 19:52:08 +03:00
|
|
|
go2rtcStreams,
|
2025-08-26 20:15:01 +03:00
|
|
|
};
|
2025-06-12 22:34:45 +03:00
|
|
|
}, [cameraName, config]);
|
|
|
|
|
|
|
|
|
|
const defaultValues: FormValues = {
|
2025-08-26 23:29:52 +03:00
|
|
|
cameraName: cameraInfo?.friendly_name || cameraName || "",
|
2025-06-12 22:34:45 +03:00
|
|
|
enabled: true,
|
|
|
|
|
ffmpeg: {
|
|
|
|
|
inputs: [
|
|
|
|
|
{
|
|
|
|
|
path: "",
|
2025-08-26 20:15:01 +03:00
|
|
|
roles: cameraInfo.roles.has("detect") ? [] : ["detect"],
|
2025-06-12 22:34:45 +03:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
2025-10-13 19:52:08 +03:00
|
|
|
go2rtcStreams: {},
|
2025-06-12 22:34:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Load existing camera config if editing
|
|
|
|
|
if (cameraName && config?.cameras[cameraName]) {
|
|
|
|
|
const camera = config.cameras[cameraName];
|
|
|
|
|
defaultValues.enabled = camera.enabled ?? true;
|
|
|
|
|
defaultValues.ffmpeg.inputs = camera.ffmpeg?.inputs?.length
|
|
|
|
|
? camera.ffmpeg.inputs.map((input) => ({
|
|
|
|
|
path: input.path,
|
|
|
|
|
roles: input.roles as Role[],
|
|
|
|
|
}))
|
|
|
|
|
: defaultValues.ffmpeg.inputs;
|
2025-10-13 19:52:08 +03:00
|
|
|
|
|
|
|
|
const go2rtcStreams = config.go2rtc?.streams || {};
|
|
|
|
|
const cameraStreams: Record<string, string[]> = {};
|
|
|
|
|
|
2025-10-20 18:33:02 +03:00
|
|
|
// get candidate stream names for this camera. could be the camera's own name,
|
|
|
|
|
// any restream names referenced by this camera, or any keys under live --> streams
|
|
|
|
|
const validNames = new Set<string>();
|
|
|
|
|
validNames.add(cameraName);
|
2025-10-13 19:52:08 +03:00
|
|
|
|
2025-10-20 18:33:02 +03:00
|
|
|
// deduce go2rtc stream names from rtsp restream inputs
|
|
|
|
|
camera.ffmpeg?.inputs?.forEach((input) => {
|
|
|
|
|
// exclude any query strings or trailing slashes from the stream name
|
2025-10-13 19:52:08 +03:00
|
|
|
const restreamMatch = input.path.match(
|
2025-10-20 18:33:02 +03:00
|
|
|
/^rtsp:\/\/127\.0\.0\.1:8554\/([^?#/]+)(?:[?#].*)?$/,
|
2025-10-13 19:52:08 +03:00
|
|
|
);
|
|
|
|
|
if (restreamMatch) {
|
|
|
|
|
const streamName = restreamMatch[1];
|
2025-10-20 18:33:02 +03:00
|
|
|
validNames.add(streamName);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Include live --> streams keys
|
|
|
|
|
const liveStreams = camera?.live?.streams;
|
|
|
|
|
if (liveStreams) {
|
|
|
|
|
Object.keys(liveStreams).forEach((key) => {
|
|
|
|
|
validNames.add(key);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Map only go2rtc entries that match the collected names
|
|
|
|
|
Object.entries(go2rtcStreams).forEach(([name, urls]) => {
|
|
|
|
|
if (validNames.has(name)) {
|
|
|
|
|
cameraStreams[name] = Array.isArray(urls) ? urls : [urls];
|
2025-10-13 19:52:08 +03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
defaultValues.go2rtcStreams = cameraStreams;
|
2025-06-12 22:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const form = useForm<FormValues>({
|
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
|
defaultValues,
|
|
|
|
|
mode: "onChange",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const { fields, append, remove } = useFieldArray({
|
|
|
|
|
control: form.control,
|
|
|
|
|
name: "ffmpeg.inputs",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Watch ffmpeg.inputs to track used roles
|
|
|
|
|
const watchedInputs = form.watch("ffmpeg.inputs");
|
|
|
|
|
|
2025-10-13 19:52:08 +03:00
|
|
|
// Watch go2rtc streams
|
|
|
|
|
const watchedGo2rtcStreams = form.watch("go2rtcStreams") || {};
|
|
|
|
|
|
2025-06-12 22:34:45 +03:00
|
|
|
const saveCameraConfig = (values: FormValues) => {
|
|
|
|
|
setIsLoading(true);
|
2025-10-13 19:52:08 +03:00
|
|
|
const { finalCameraName, friendlyName } = processCameraName(
|
|
|
|
|
values.cameraName,
|
|
|
|
|
);
|
2025-08-26 20:15:01 +03:00
|
|
|
|
2025-06-12 22:34:45 +03:00
|
|
|
const configData: ConfigSetBody["config_data"] = {
|
|
|
|
|
cameras: {
|
2025-08-26 20:15:01 +03:00
|
|
|
[finalCameraName]: {
|
2025-06-12 22:34:45 +03:00
|
|
|
enabled: values.enabled,
|
2025-10-13 19:52:08 +03:00
|
|
|
...(friendlyName && { friendly_name: friendlyName }),
|
2025-06-12 22:34:45 +03:00
|
|
|
ffmpeg: {
|
|
|
|
|
inputs: values.ffmpeg.inputs.map((input) => ({
|
|
|
|
|
path: input.path,
|
|
|
|
|
roles: input.roles,
|
|
|
|
|
})),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-13 19:52:08 +03:00
|
|
|
// Add go2rtc streams if provided
|
|
|
|
|
if (values.go2rtcStreams && Object.keys(values.go2rtcStreams).length > 0) {
|
|
|
|
|
configData.go2rtc = {
|
|
|
|
|
streams: values.go2rtcStreams,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 22:34:45 +03:00
|
|
|
const requestBody: ConfigSetBody = {
|
|
|
|
|
requires_restart: 1,
|
|
|
|
|
config_data: configData,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Add update_topic for new cameras
|
|
|
|
|
if (!cameraName) {
|
2025-08-26 20:15:01 +03:00
|
|
|
requestBody.update_topic = `config/cameras/${finalCameraName}/add`;
|
2025-06-12 22:34:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
axios
|
|
|
|
|
.put("config/set", requestBody)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (res.status === 200) {
|
2025-10-13 19:52:08 +03:00
|
|
|
// Update running go2rtc instance if streams were configured
|
|
|
|
|
if (
|
|
|
|
|
values.go2rtcStreams &&
|
|
|
|
|
Object.keys(values.go2rtcStreams).length > 0
|
|
|
|
|
) {
|
|
|
|
|
const updatePromises = Object.entries(values.go2rtcStreams).map(
|
|
|
|
|
([streamName, urls]) =>
|
|
|
|
|
axios.put(
|
|
|
|
|
`go2rtc/streams/${streamName}?src=${encodeURIComponent(urls[0])}`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Promise.allSettled(updatePromises).then(() => {
|
|
|
|
|
toast.success(
|
|
|
|
|
t("cameraManagement.cameraConfig.toast.success", {
|
|
|
|
|
cameraName: values.cameraName,
|
|
|
|
|
}),
|
|
|
|
|
{ position: "top-center" },
|
|
|
|
|
);
|
|
|
|
|
if (onSave) onSave();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
toast.success(
|
|
|
|
|
t("cameraManagement.cameraConfig.toast.success", {
|
|
|
|
|
cameraName: values.cameraName,
|
|
|
|
|
}),
|
|
|
|
|
{ position: "top-center" },
|
|
|
|
|
);
|
|
|
|
|
if (onSave) onSave();
|
|
|
|
|
}
|
2025-06-12 22:34:45 +03:00
|
|
|
} else {
|
|
|
|
|
throw new Error(res.statusText);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
const errorMessage =
|
|
|
|
|
error.response?.data?.message ||
|
|
|
|
|
error.response?.data?.detail ||
|
|
|
|
|
"Unknown error";
|
|
|
|
|
toast.error(
|
|
|
|
|
t("toast.save.error.title", { errorMessage, ns: "common" }),
|
|
|
|
|
{ position: "top-center" },
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmit = (values: FormValues) => {
|
2025-08-26 20:15:01 +03:00
|
|
|
if (
|
|
|
|
|
cameraName &&
|
|
|
|
|
values.cameraName !== cameraName &&
|
2025-08-26 23:29:52 +03:00
|
|
|
values.cameraName !== cameraInfo?.friendly_name
|
2025-08-26 20:15:01 +03:00
|
|
|
) {
|
2025-06-12 22:34:45 +03:00
|
|
|
// If camera name changed, delete old camera config
|
2025-10-13 19:52:08 +03:00
|
|
|
const deleteRequestBody = {
|
2025-06-12 22:34:45 +03:00
|
|
|
requires_restart: 1,
|
|
|
|
|
config_data: {
|
|
|
|
|
cameras: {
|
2025-10-13 19:52:08 +03:00
|
|
|
[cameraName]: null,
|
2025-06-12 22:34:45 +03:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
update_topic: `config/cameras/${cameraName}/remove`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
axios
|
|
|
|
|
.put("config/set", deleteRequestBody)
|
|
|
|
|
.then(() => saveCameraConfig(values))
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
const errorMessage =
|
|
|
|
|
error.response?.data?.message ||
|
|
|
|
|
error.response?.data?.detail ||
|
|
|
|
|
"Unknown error";
|
|
|
|
|
toast.error(
|
|
|
|
|
t("toast.save.error.title", { errorMessage, ns: "common" }),
|
|
|
|
|
{ position: "top-center" },
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
saveCameraConfig(values);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Determine available roles for new streams
|
|
|
|
|
const getAvailableRoles = (): Role[] => {
|
|
|
|
|
const used = new Set<Role>();
|
|
|
|
|
watchedInputs.forEach((input) => {
|
|
|
|
|
input.roles.forEach((role) => used.add(role));
|
|
|
|
|
});
|
|
|
|
|
return used.has("detect") ? [] : ["detect"];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getUsedRolesExcludingIndex = (excludeIndex: number) => {
|
|
|
|
|
const roles = new Set<Role>();
|
|
|
|
|
watchedInputs.forEach((input, idx) => {
|
|
|
|
|
if (idx !== excludeIndex) {
|
|
|
|
|
input.roles.forEach((role) => roles.add(role));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return roles;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-10-13 19:52:08 +03:00
|
|
|
<div className="scrollbar-container max-w-4xl overflow-y-auto md:mb-24">
|
2025-06-12 22:34:45 +03:00
|
|
|
<Toaster position="top-center" closeButton />
|
|
|
|
|
<Heading as="h3" className="my-2">
|
|
|
|
|
{cameraName
|
2025-10-13 19:52:08 +03:00
|
|
|
? t("cameraManagement.cameraConfig.edit")
|
|
|
|
|
: t("cameraManagement.cameraConfig.add")}
|
2025-06-12 22:34:45 +03:00
|
|
|
</Heading>
|
|
|
|
|
<div className="my-3 text-sm text-muted-foreground">
|
2025-10-13 19:52:08 +03:00
|
|
|
{t("cameraManagement.cameraConfig.description")}
|
2025-06-12 22:34:45 +03:00
|
|
|
</div>
|
|
|
|
|
<Separator className="my-3 bg-secondary" />
|
|
|
|
|
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="cameraName"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
2025-10-13 19:52:08 +03:00
|
|
|
<FormLabel>{t("cameraManagement.cameraConfig.name")}</FormLabel>
|
2025-06-12 22:34:45 +03:00
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
2025-10-13 19:52:08 +03:00
|
|
|
placeholder={t(
|
|
|
|
|
"cameraManagement.cameraConfig.namePlaceholder",
|
|
|
|
|
)}
|
2025-06-12 22:34:45 +03:00
|
|
|
{...field}
|
|
|
|
|
disabled={!!cameraName} // Prevent editing name for existing cameras
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="enabled"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem className="flex items-center space-x-2">
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Switch
|
|
|
|
|
checked={field.value}
|
|
|
|
|
onCheckedChange={field.onChange}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
2025-10-13 19:52:08 +03:00
|
|
|
<FormLabel>
|
|
|
|
|
{t("cameraManagement.cameraConfig.enabled")}
|
|
|
|
|
</FormLabel>
|
2025-06-12 22:34:45 +03:00
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
|
2025-10-13 19:52:08 +03:00
|
|
|
<div className="space-y-4">
|
|
|
|
|
<Label className="text-sm font-medium">
|
|
|
|
|
{t("cameraManagement.cameraConfig.ffmpeg.inputs")}
|
|
|
|
|
</Label>
|
2025-06-12 22:34:45 +03:00
|
|
|
{fields.map((field, index) => (
|
2025-10-13 19:52:08 +03:00
|
|
|
<Card key={field.id} className="bg-secondary text-primary">
|
|
|
|
|
<CardContent className="space-y-4 p-4">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<h4 className="font-medium">
|
|
|
|
|
{t("cameraWizard.step2.streamTitle", {
|
|
|
|
|
number: index + 1,
|
|
|
|
|
})}
|
|
|
|
|
</h4>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => remove(index)}
|
|
|
|
|
disabled={fields.length === 1}
|
|
|
|
|
className="text-secondary-foreground hover:text-secondary-foreground"
|
|
|
|
|
>
|
|
|
|
|
<LuTrash2 className="size-5" />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name={`ffmpeg.inputs.${index}.path`}
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel className="text-sm font-medium">
|
|
|
|
|
{t("cameraManagement.cameraConfig.ffmpeg.path")}
|
|
|
|
|
</FormLabel>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
|
|
|
|
className="h-8"
|
|
|
|
|
placeholder={t(
|
|
|
|
|
"cameraManagement.cameraConfig.ffmpeg.pathPlaceholder",
|
|
|
|
|
)}
|
|
|
|
|
{...field}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label className="text-sm font-medium">
|
|
|
|
|
{t("cameraManagement.cameraConfig.ffmpeg.roles")}
|
|
|
|
|
</Label>
|
|
|
|
|
<div className="rounded-lg bg-background p-3">
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
{(["detect", "record", "audio"] as const).map(
|
|
|
|
|
(role) => {
|
|
|
|
|
const isUsedElsewhere =
|
|
|
|
|
getUsedRolesExcludingIndex(index).has(role);
|
|
|
|
|
const isChecked =
|
|
|
|
|
watchedInputs[index]?.roles?.includes(role) ||
|
|
|
|
|
false;
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2025-06-12 22:34:45 +03:00
|
|
|
key={role}
|
2025-10-13 19:52:08 +03:00
|
|
|
className="flex w-full items-center justify-between"
|
2025-06-12 22:34:45 +03:00
|
|
|
>
|
2025-10-13 19:52:08 +03:00
|
|
|
<span className="text-sm capitalize">
|
|
|
|
|
{role}
|
|
|
|
|
</span>
|
|
|
|
|
<Switch
|
|
|
|
|
checked={isChecked}
|
|
|
|
|
onCheckedChange={(checked) => {
|
|
|
|
|
const currentRoles =
|
|
|
|
|
watchedInputs[index]?.roles || [];
|
|
|
|
|
const updatedRoles = checked
|
|
|
|
|
? [...currentRoles, role]
|
|
|
|
|
: currentRoles.filter((r) => r !== role);
|
|
|
|
|
form.setValue(
|
|
|
|
|
`ffmpeg.inputs.${index}.roles`,
|
|
|
|
|
updatedRoles,
|
|
|
|
|
);
|
2025-06-12 22:34:45 +03:00
|
|
|
}}
|
2025-10-13 19:52:08 +03:00
|
|
|
disabled={!isChecked && isUsedElsewhere}
|
2025-06-12 22:34:45 +03:00
|
|
|
/>
|
2025-10-13 19:52:08 +03:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
2025-06-12 22:34:45 +03:00
|
|
|
))}
|
|
|
|
|
<FormMessage>
|
|
|
|
|
{form.formState.errors.ffmpeg?.inputs?.root &&
|
|
|
|
|
form.formState.errors.ffmpeg.inputs.root.message}
|
|
|
|
|
</FormMessage>
|
|
|
|
|
<Button
|
2025-10-13 19:52:08 +03:00
|
|
|
type="button"
|
2025-06-12 22:34:45 +03:00
|
|
|
onClick={() => append({ path: "", roles: getAvailableRoles() })}
|
2025-10-13 19:52:08 +03:00
|
|
|
variant="outline"
|
|
|
|
|
className=""
|
2025-06-12 22:34:45 +03:00
|
|
|
>
|
2025-10-13 19:52:08 +03:00
|
|
|
<LuPlus className="mr-2 size-4" />
|
|
|
|
|
{t("cameraManagement.cameraConfig.ffmpeg.addInput")}
|
2025-06-12 22:34:45 +03:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-10-13 19:52:08 +03:00
|
|
|
{/* go2rtc Streams Section */}
|
|
|
|
|
{Object.keys(watchedGo2rtcStreams).length > 0 && (
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<Label className="text-sm font-medium">
|
|
|
|
|
{t("cameraManagement.cameraConfig.go2rtcStreams")}
|
|
|
|
|
</Label>
|
|
|
|
|
{Object.entries(watchedGo2rtcStreams).map(
|
|
|
|
|
([streamName, urls]) => (
|
|
|
|
|
<Card key={streamName} className="bg-secondary text-primary">
|
|
|
|
|
<CardContent className="space-y-4 p-4">
|
|
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<h4 className="font-medium">{streamName}</h4>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const updatedStreams = { ...watchedGo2rtcStreams };
|
|
|
|
|
delete updatedStreams[streamName];
|
|
|
|
|
form.setValue("go2rtcStreams", updatedStreams);
|
|
|
|
|
}}
|
|
|
|
|
className="text-secondary-foreground hover:text-secondary-foreground"
|
|
|
|
|
>
|
|
|
|
|
<LuTrash2 className="size-5" />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label className="text-sm font-medium">
|
|
|
|
|
{t("cameraManagement.cameraConfig.streamUrls")}
|
|
|
|
|
</Label>
|
|
|
|
|
{(Array.isArray(urls) ? urls : [urls]).map(
|
|
|
|
|
(url, urlIndex) => (
|
|
|
|
|
<div
|
|
|
|
|
key={urlIndex}
|
|
|
|
|
className="flex items-center gap-2"
|
|
|
|
|
>
|
|
|
|
|
<Input
|
|
|
|
|
className="h-8 flex-1"
|
|
|
|
|
value={url}
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
const updatedStreams = {
|
|
|
|
|
...watchedGo2rtcStreams,
|
|
|
|
|
};
|
|
|
|
|
const currentUrls = Array.isArray(
|
|
|
|
|
updatedStreams[streamName],
|
|
|
|
|
)
|
|
|
|
|
? updatedStreams[streamName]
|
|
|
|
|
: [updatedStreams[streamName]];
|
|
|
|
|
currentUrls[urlIndex] = e.target.value;
|
|
|
|
|
updatedStreams[streamName] = currentUrls;
|
|
|
|
|
form.setValue(
|
|
|
|
|
"go2rtcStreams",
|
|
|
|
|
updatedStreams,
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
placeholder="rtsp://username:password@host:port/path"
|
|
|
|
|
/>
|
|
|
|
|
{(Array.isArray(urls) ? urls : [urls]).length >
|
|
|
|
|
1 && (
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const updatedStreams = {
|
|
|
|
|
...watchedGo2rtcStreams,
|
|
|
|
|
};
|
|
|
|
|
const currentUrls = Array.isArray(
|
|
|
|
|
updatedStreams[streamName],
|
|
|
|
|
)
|
|
|
|
|
? updatedStreams[streamName]
|
|
|
|
|
: [updatedStreams[streamName]];
|
|
|
|
|
currentUrls.splice(urlIndex, 1);
|
|
|
|
|
updatedStreams[streamName] = currentUrls;
|
|
|
|
|
form.setValue(
|
|
|
|
|
"go2rtcStreams",
|
|
|
|
|
updatedStreams,
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
className="text-secondary-foreground hover:text-secondary-foreground"
|
|
|
|
|
>
|
|
|
|
|
<LuTrash2 className="size-4" />
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
)}
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const updatedStreams = { ...watchedGo2rtcStreams };
|
|
|
|
|
const currentUrls = Array.isArray(
|
|
|
|
|
updatedStreams[streamName],
|
|
|
|
|
)
|
|
|
|
|
? updatedStreams[streamName]
|
|
|
|
|
: [updatedStreams[streamName]];
|
|
|
|
|
currentUrls.push("");
|
|
|
|
|
updatedStreams[streamName] = currentUrls;
|
|
|
|
|
form.setValue("go2rtcStreams", updatedStreams);
|
|
|
|
|
}}
|
|
|
|
|
className="w-fit"
|
|
|
|
|
>
|
|
|
|
|
<LuPlus className="mr-2 size-4" />
|
|
|
|
|
{t("cameraManagement.cameraConfig.addUrl")}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
),
|
|
|
|
|
)}
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const streamName = `${cameraName}_stream_${Object.keys(watchedGo2rtcStreams).length + 1}`;
|
|
|
|
|
const updatedStreams = {
|
|
|
|
|
...watchedGo2rtcStreams,
|
|
|
|
|
[streamName]: [""],
|
|
|
|
|
};
|
|
|
|
|
form.setValue("go2rtcStreams", updatedStreams);
|
|
|
|
|
}}
|
|
|
|
|
variant="outline"
|
|
|
|
|
className=""
|
|
|
|
|
>
|
|
|
|
|
<LuPlus className="mr-2 size-4" />
|
|
|
|
|
{t("cameraManagement.cameraConfig.addGo2rtcStream")}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-06-12 22:34:45 +03:00
|
|
|
<div className="flex w-full flex-row items-center gap-2 pt-2 md:w-[50%]">
|
|
|
|
|
<Button
|
|
|
|
|
className="flex flex-1"
|
|
|
|
|
aria-label={t("button.cancel", { ns: "common" })}
|
|
|
|
|
onClick={onCancel}
|
|
|
|
|
type="button"
|
|
|
|
|
>
|
|
|
|
|
{t("button.cancel", { ns: "common" })}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="select"
|
|
|
|
|
disabled={isLoading}
|
|
|
|
|
className="flex flex-1"
|
|
|
|
|
aria-label={t("button.save", { ns: "common" })}
|
|
|
|
|
type="submit"
|
|
|
|
|
>
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<div className="flex flex-row items-center gap-2">
|
|
|
|
|
<ActivityIndicator />
|
|
|
|
|
<span>{t("button.saving", { ns: "common" })}</span>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
t("button.save", { ns: "common" })
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
2025-10-13 19:52:08 +03:00
|
|
|
</div>
|
2025-06-12 22:34:45 +03:00
|
|
|
);
|
|
|
|
|
}
|