mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-12-03 10:47:06 +03:00
CI / AMD64 Build (push) Canceled after 0s
CI / AMD64 Smoke Test (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
* replace hwaccel None with Auto and Disabled Choosing None for the global hardware acceleration args sent undefined, which deleted the key, so the backend default `auto` turned detection back on (VAAPI on Intel hosts). On a config without the key the save returned a 500 from the update_yaml KeyError. Global hwaccel now offers Auto, which writes `auto`, and Disabled, which writes `[]`. Cameras get Disabled next to Inherit from global setting, and `""` and `[]` both display as Disabled. Camera-level Inherit restores the saved value instead of sending a delete for a key that may not be in the YAML, and camera export also counts as inherited when it matches the camera's own hwaccel value, since that's what the backend falls back to. The mode choice survives the remount RJSF does when the value flips between a string and a list, and every ffmpeg args field forces the split layout so a list value no longer stacks. The docs now say what Auto detects and how to turn decoding off. * keep Manual selected when switching to it from Disabled or Auto Choosing Manual after an unsaved Disabled or Auto sent `""`, which counts as no change, so BaseSection reset the form to the saved value and the field snapped back to the saved option. Manual now stays selected when the form falls back to the saved value, and it starts from the saved manual args when there are any, otherwise from an empty box. * don't copy camera args into an input's Manual box Switching an input from Preset to Manual read the saved value from the camera-level field, so the camera's manual args replaced the input's own. Input-scoped fields now have no saved value to fall back to. * keep saved manual lists intact and scope the empty Manual box to its value Switching back to Manual turned a saved YAML list into a string, so the round trip dirtied the form and saving rewrote the list. The cleared Manual box also followed its input index, so deleting an input showed it on the next one. Manual now restores the raw saved value, and the empty box only applies while the remembered choice still matches the current value. * keep a cleared input's Manual box empty Clearing an input's saved manual args after an edit reset the form to the saved value, and with no saved value for inputs the box refilled with the old text. Inputs now read their own saved value from `inputs.N`, but only while the input at that index still has the same path, so a delete or reorder doesn't pick up another input's value. * match dev for input args and fix Inherit on list values The per-input saved value lookup could match the wrong input (same path and roles, empty roles, or roles edited or re-added after a delete), so Preset to Manual restored another input's args. Inputs no longer read a saved value and behave like dev, which already refills a cleared box with the saved args. A cleared Manual hwaccel box also showed Disabled after a section or camera switch, and Inherit on a list-valued camera field showed Disabled or an empty Manual box because RJSF hands the unset list back as `[]`. A pending `""` now shows as Manual unless `""` is the saved value, and an Inherit choice also matches an empty list.
581 lines
17 KiB
TypeScript
581 lines
17 KiB
TypeScript
import type { WidgetProps } from "@rjsf/utils";
|
|
import useSWR from "swr";
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import get from "lodash/get";
|
|
import isEqual from "lodash/isEqual";
|
|
import { Input } from "@/components/ui/input";
|
|
import { ConfigFormContext } from "@/types/configForm";
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/components/ui/select";
|
|
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
|
|
|
|
type FfmpegPresetResponse = {
|
|
hwaccel_args: string[];
|
|
input_args: string[];
|
|
output_args: {
|
|
record: string[];
|
|
detect: string[];
|
|
};
|
|
};
|
|
|
|
type FfmpegArgsMode =
|
|
"preset" | "manual" | "inherit" | "none" | "auto" | "disabled";
|
|
|
|
// RJSF remounts the widget when the value flips between a string and a list,
|
|
// so the user's mode choice lives outside the component. It applies only
|
|
// while the value is still the one it was made for, so a save or undo that
|
|
// changes the value hands the mode back to detection.
|
|
const chosenModes = new Map<string, { mode: FfmpegArgsMode; value: unknown }>();
|
|
const chosenModeClears = new Map<string, ReturnType<typeof setTimeout>>();
|
|
|
|
type PresetField =
|
|
| "hwaccel_args"
|
|
| "input_args"
|
|
| "output_args.record"
|
|
| "output_args.record_sub"
|
|
| "output_args.detect";
|
|
|
|
const getPresetOptions = (
|
|
data: FfmpegPresetResponse | undefined,
|
|
field: PresetField | undefined,
|
|
): string[] => {
|
|
if (!data || !field) {
|
|
return [];
|
|
}
|
|
|
|
if (field === "hwaccel_args") {
|
|
return data.hwaccel_args;
|
|
}
|
|
|
|
if (field === "input_args") {
|
|
return data.input_args;
|
|
}
|
|
|
|
if (field.startsWith("output_args.")) {
|
|
const key =
|
|
field === "output_args.record_sub"
|
|
? "record"
|
|
: (field.split(".")[1] as "record" | "detect");
|
|
return data.output_args?.[key] ?? [];
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
const resolveMode = (
|
|
value: unknown,
|
|
presets: string[],
|
|
defaultMode: FfmpegArgsMode,
|
|
allowInherit: boolean,
|
|
hwaccelModes: boolean,
|
|
): FfmpegArgsMode => {
|
|
if (value === null || value === undefined) {
|
|
if (allowInherit) return "inherit";
|
|
return hwaccelModes ? "auto" : "none";
|
|
}
|
|
|
|
if (allowInherit && Array.isArray(value) && value.length === 0) {
|
|
return "inherit";
|
|
}
|
|
|
|
if (hwaccelModes) {
|
|
// An empty string is how auto-detection reports no hardware, and the
|
|
// backend runs no hwaccel for it either way.
|
|
if (value === "" || (Array.isArray(value) && value.length === 0)) {
|
|
return "disabled";
|
|
}
|
|
if (value === "auto") return "auto";
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
return "manual";
|
|
}
|
|
|
|
if (typeof value === "string") {
|
|
if (presets.length === 0) {
|
|
return defaultMode;
|
|
}
|
|
|
|
return presets.includes(value) ? "preset" : "manual";
|
|
}
|
|
|
|
return defaultMode;
|
|
};
|
|
|
|
const normalizeManualText = (value: unknown): string => {
|
|
if (Array.isArray(value)) {
|
|
return value.join(" ");
|
|
}
|
|
|
|
if (typeof value === "string") {
|
|
return value;
|
|
}
|
|
|
|
return "";
|
|
};
|
|
|
|
export function FfmpegArgsWidget(props: WidgetProps) {
|
|
const formContext = props.registry?.formContext as
|
|
ConfigFormContext | undefined;
|
|
const i18nNamespace = formContext?.i18nNamespace as string | undefined;
|
|
const isCameraLevel = formContext?.level === "camera";
|
|
const effectiveNamespace = isCameraLevel ? "config/cameras" : i18nNamespace;
|
|
const { t, i18n } = useTranslation([
|
|
effectiveNamespace || i18nNamespace || "common",
|
|
i18nNamespace || "common",
|
|
"views/settings",
|
|
]);
|
|
const {
|
|
value,
|
|
onChange,
|
|
disabled,
|
|
readonly,
|
|
options,
|
|
placeholder,
|
|
schema,
|
|
id,
|
|
} = props;
|
|
const presetField = options?.ffmpegPresetField as PresetField | undefined;
|
|
// Path to this field within its config section. This is usually the same as
|
|
// the preset field, but the two diverge when the field sits below the
|
|
// section root: record.export.hwaccel_args uses the hwaccel_args preset list
|
|
// while living at export.hwaccel_args inside the record section.
|
|
const globalFieldPath =
|
|
(options?.ffmpegGlobalFieldPath as string | undefined) ?? presetField;
|
|
// Camera-config path the backend falls back to when this camera field is
|
|
// unset, used alongside the global value to detect inheritance.
|
|
const cameraFallbackPath = options?.cameraFallbackPath as string | undefined;
|
|
const allowInherit = options?.allowInherit === true;
|
|
const unsetLabelKey = options?.unsetLabelKey as string | undefined;
|
|
const hideDescription = options?.hideDescription === true;
|
|
const useSplitLayout = options?.splitLayout !== false;
|
|
|
|
// Detect camera-level top-level fields (not inside inputs array).
|
|
// These should show "Use global setting" instead of "None".
|
|
const isInputScoped = id.includes("_inputs_");
|
|
const showUseGlobalSetting = isCameraLevel && !isInputScoped && !allowInherit;
|
|
|
|
// Hardware acceleration offers Auto and Disabled in place of None. Auto
|
|
// sends the literal "auto" because the form holds the resolved value, so a
|
|
// delete could target a key that is absent from the YAML.
|
|
const hwaccelModes = presetField === "hwaccel_args" && !allowInherit;
|
|
|
|
// Extract the global value for this specific field to detect inheritance
|
|
const globalFieldValue = useMemo(() => {
|
|
if (
|
|
!showUseGlobalSetting ||
|
|
!formContext?.globalValue ||
|
|
!globalFieldPath
|
|
) {
|
|
return undefined;
|
|
}
|
|
return get(
|
|
formContext.globalValue as Record<string, unknown>,
|
|
globalFieldPath,
|
|
);
|
|
}, [showUseGlobalSetting, formContext?.globalValue, globalFieldPath]);
|
|
|
|
const { data } = useSWR<FfmpegPresetResponse>("ffmpeg/presets");
|
|
|
|
const presetOptions = useMemo(
|
|
() => getPresetOptions(data, presetField),
|
|
[data, presetField],
|
|
);
|
|
|
|
const canUsePresets = presetOptions.length > 0;
|
|
const defaultMode: FfmpegArgsMode = canUsePresets ? "preset" : "manual";
|
|
|
|
const cameraFallbackValue = useMemo(
|
|
() =>
|
|
cameraFallbackPath && formContext?.fullCameraConfig
|
|
? get(formContext.fullCameraConfig, cameraFallbackPath)
|
|
: undefined,
|
|
[cameraFallbackPath, formContext?.fullCameraConfig],
|
|
);
|
|
|
|
// A camera value equal to what it would resolve to when unset counts as
|
|
// inherited (i.e. the camera does not override it).
|
|
const isInherited = useCallback(
|
|
(candidate: unknown) => {
|
|
if (!showUseGlobalSetting) return false;
|
|
if (candidate === undefined || candidate === null) return true;
|
|
return [globalFieldValue, cameraFallbackValue].some(
|
|
(inherited) =>
|
|
inherited !== undefined &&
|
|
inherited !== null &&
|
|
isEqual(candidate, inherited),
|
|
);
|
|
},
|
|
[showUseGlobalSetting, globalFieldValue, cameraFallbackValue],
|
|
);
|
|
|
|
const isInheritedFromGlobal = isInherited(value);
|
|
|
|
// globalFieldPath points at the camera-level field, not an input's own
|
|
// value, so input-scoped fields have no saved value to fall back to.
|
|
const savedValue =
|
|
!isInputScoped && globalFieldPath && formContext?.baselineFormData
|
|
? get(formContext.baselineFormData, globalFieldPath)
|
|
: undefined;
|
|
|
|
const detectedMode = useMemo(() => {
|
|
if (showUseGlobalSetting && isInheritedFromGlobal) {
|
|
return "inherit" as FfmpegArgsMode;
|
|
}
|
|
// A pending "" is a cleared Manual box. It means Disabled only when it
|
|
// is the saved value.
|
|
if (hwaccelModes && value === "" && !isEqual(savedValue, "")) {
|
|
return "manual" as FfmpegArgsMode;
|
|
}
|
|
return resolveMode(
|
|
value,
|
|
presetOptions,
|
|
defaultMode,
|
|
allowInherit,
|
|
hwaccelModes,
|
|
);
|
|
}, [
|
|
showUseGlobalSetting,
|
|
isInheritedFromGlobal,
|
|
value,
|
|
presetOptions,
|
|
defaultMode,
|
|
allowInherit,
|
|
hwaccelModes,
|
|
savedValue,
|
|
]);
|
|
|
|
const modeKey = [
|
|
formContext?.level,
|
|
formContext?.cameraName,
|
|
formContext?.sectionPath,
|
|
formContext?.isProfile,
|
|
id,
|
|
].join(":");
|
|
|
|
// An empty manual box is no change, so the form falls back to the saved
|
|
// value while Manual stays chosen.
|
|
const chosenMode = useCallback(
|
|
(current: unknown) => {
|
|
const chosen = chosenModes.get(modeKey);
|
|
if (!chosen) return undefined;
|
|
const emptyManual = chosen.mode === "manual" && chosen.value === "";
|
|
// RJSF hands an unset list-valued field back as []
|
|
const unsetList =
|
|
chosen.value === undefined &&
|
|
Array.isArray(current) &&
|
|
current.length === 0;
|
|
return isEqual(chosen.value, current) ||
|
|
unsetList ||
|
|
(emptyManual &&
|
|
savedValue !== undefined &&
|
|
isEqual(current, savedValue))
|
|
? chosen.mode
|
|
: undefined;
|
|
},
|
|
[modeKey, savedValue],
|
|
);
|
|
|
|
// A remount mounts the replacement before the timer fires, so only leaving
|
|
// the page clears the choice.
|
|
useEffect(() => {
|
|
clearTimeout(chosenModeClears.get(modeKey));
|
|
return () => {
|
|
chosenModeClears.set(
|
|
modeKey,
|
|
setTimeout(() => chosenModes.delete(modeKey)),
|
|
);
|
|
};
|
|
}, [modeKey]);
|
|
|
|
const [mode, setMode] = useState<FfmpegArgsMode>(
|
|
() => chosenMode(value) ?? detectedMode,
|
|
);
|
|
|
|
useEffect(() => {
|
|
const chosen = chosenMode(value);
|
|
if (chosen) {
|
|
setMode(chosen);
|
|
return;
|
|
}
|
|
|
|
if (!canUsePresets && detectedMode === "preset") {
|
|
setMode("manual");
|
|
return;
|
|
}
|
|
|
|
setMode(detectedMode);
|
|
}, [canUsePresets, chosenMode, detectedMode, value]);
|
|
|
|
const choose = useCallback(
|
|
(nextMode: FfmpegArgsMode, nextValue: unknown) => {
|
|
chosenModes.set(modeKey, { mode: nextMode, value: nextValue });
|
|
setMode(nextMode);
|
|
onChange(nextValue);
|
|
},
|
|
[modeKey, onChange],
|
|
);
|
|
|
|
const handleModeChange = useCallback(
|
|
(nextMode: FfmpegArgsMode) => {
|
|
// Restoring an inherited saved value leaves the form clean. Sending
|
|
// undefined would delete a key that may be absent from the YAML.
|
|
if (nextMode === "inherit") {
|
|
choose(nextMode, isInherited(savedValue) ? savedValue : undefined);
|
|
return;
|
|
}
|
|
|
|
if (nextMode === "none") {
|
|
choose(nextMode, undefined);
|
|
return;
|
|
}
|
|
|
|
if (nextMode === "auto") {
|
|
choose(nextMode, "auto");
|
|
return;
|
|
}
|
|
|
|
if (nextMode === "disabled") {
|
|
choose(nextMode, []);
|
|
return;
|
|
}
|
|
|
|
if (nextMode === "preset") {
|
|
const currentValue = typeof value === "string" ? value : undefined;
|
|
const presetValue =
|
|
currentValue && presetOptions.includes(currentValue)
|
|
? currentValue
|
|
: presetOptions[0];
|
|
if (presetValue) {
|
|
choose(nextMode, presetValue);
|
|
} else {
|
|
setMode(nextMode);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Start from the saved manual args, if any, so a switch back to Manual
|
|
// shows them instead of an empty box.
|
|
if (mode === "preset" || mode === "auto" || mode === "disabled") {
|
|
const savedIsManual =
|
|
resolveMode(
|
|
savedValue,
|
|
presetOptions,
|
|
defaultMode,
|
|
allowInherit,
|
|
hwaccelModes,
|
|
) === "manual";
|
|
choose(nextMode, savedIsManual ? savedValue : "");
|
|
return;
|
|
}
|
|
|
|
choose(nextMode, normalizeManualText(value));
|
|
},
|
|
[
|
|
allowInherit,
|
|
choose,
|
|
defaultMode,
|
|
hwaccelModes,
|
|
isInherited,
|
|
mode,
|
|
presetOptions,
|
|
savedValue,
|
|
value,
|
|
],
|
|
);
|
|
|
|
const handlePresetChange = useCallback(
|
|
(preset: string) => choose("preset", preset),
|
|
[choose],
|
|
);
|
|
|
|
const handleManualChange = useCallback(
|
|
(event: React.ChangeEvent<HTMLInputElement>) =>
|
|
choose("manual", event.target.value),
|
|
[choose],
|
|
);
|
|
|
|
const manualValue =
|
|
chosenMode(value) === "manual" && chosenModes.get(modeKey)?.value === ""
|
|
? ""
|
|
: normalizeManualText(value);
|
|
const presetValue =
|
|
typeof value === "string" && presetOptions.includes(value) ? value : "";
|
|
const fallbackDescriptionKey = useMemo(() => {
|
|
if (!presetField) {
|
|
return undefined;
|
|
}
|
|
|
|
const prefix = isInputScoped ? "ffmpeg.inputs" : "ffmpeg";
|
|
|
|
if (presetField === "hwaccel_args") {
|
|
return `${prefix}.hwaccel_args.description`;
|
|
}
|
|
|
|
if (presetField === "input_args") {
|
|
return `${prefix}.input_args.description`;
|
|
}
|
|
|
|
if (presetField === "output_args.record") {
|
|
return isInputScoped
|
|
? "ffmpeg.inputs.output_args.record.description"
|
|
: "ffmpeg.output_args.record.description";
|
|
}
|
|
|
|
if (presetField === "output_args.record_sub") {
|
|
return isInputScoped
|
|
? "ffmpeg.inputs.output_args.record_sub.description"
|
|
: "ffmpeg.output_args.record_sub.description";
|
|
}
|
|
|
|
if (presetField === "output_args.detect") {
|
|
return isInputScoped
|
|
? "ffmpeg.inputs.output_args.detect.description"
|
|
: "ffmpeg.output_args.detect.description";
|
|
}
|
|
|
|
return undefined;
|
|
}, [isInputScoped, presetField]);
|
|
|
|
const translatedDescription =
|
|
fallbackDescriptionKey &&
|
|
effectiveNamespace &&
|
|
i18n.exists(fallbackDescriptionKey, { ns: effectiveNamespace })
|
|
? t(fallbackDescriptionKey, { ns: effectiveNamespace })
|
|
: "";
|
|
const fieldDescription =
|
|
typeof schema.description === "string" && schema.description.length > 0
|
|
? schema.description
|
|
: translatedDescription;
|
|
|
|
const unsetMode: FfmpegArgsMode =
|
|
showUseGlobalSetting || allowInherit
|
|
? "inherit"
|
|
: hwaccelModes
|
|
? "auto"
|
|
: "none";
|
|
const unsetLabel = showUseGlobalSetting
|
|
? t("configForm.ffmpegArgs.useGlobalSetting", { ns: "views/settings" })
|
|
: allowInherit
|
|
? t(unsetLabelKey ?? "configForm.ffmpegArgs.inherit", {
|
|
ns: "views/settings",
|
|
})
|
|
: hwaccelModes
|
|
? t(unsetLabelKey ?? "configForm.ffmpegArgs.auto", {
|
|
ns: "views/settings",
|
|
})
|
|
: t(unsetLabelKey ?? "configForm.ffmpegArgs.none", {
|
|
ns: "views/settings",
|
|
});
|
|
|
|
const renderOption = (
|
|
optionMode: FfmpegArgsMode,
|
|
label: string,
|
|
optionDisabled = false,
|
|
) => (
|
|
<div className="flex items-center space-x-2">
|
|
<RadioGroupItem
|
|
value={optionMode}
|
|
id={`${id}-${optionMode}`}
|
|
disabled={disabled || readonly || optionDisabled}
|
|
className={
|
|
mode === optionMode
|
|
? "bg-selected from-selected/50 to-selected/90 text-selected"
|
|
: "bg-secondary from-secondary/50 to-secondary/90 text-secondary"
|
|
}
|
|
/>
|
|
<label htmlFor={`${id}-${optionMode}`} className="cursor-pointer text-sm">
|
|
{label}
|
|
</label>
|
|
</div>
|
|
);
|
|
|
|
return (
|
|
<div className="space-y-2">
|
|
<RadioGroup
|
|
value={mode}
|
|
onValueChange={(next) => handleModeChange(next as FfmpegArgsMode)}
|
|
className="gap-3"
|
|
>
|
|
{renderOption(unsetMode, unsetLabel)}
|
|
{hwaccelModes &&
|
|
renderOption(
|
|
"disabled",
|
|
t("configForm.ffmpegArgs.disabled", { ns: "views/settings" }),
|
|
)}
|
|
{renderOption(
|
|
"preset",
|
|
t("configForm.ffmpegArgs.preset", { ns: "views/settings" }),
|
|
!canUsePresets,
|
|
)}
|
|
{renderOption(
|
|
"manual",
|
|
t("configForm.ffmpegArgs.manual", { ns: "views/settings" }),
|
|
)}
|
|
</RadioGroup>
|
|
|
|
{mode === "auto" && !unsetLabelKey ? (
|
|
<p className="text-xs text-muted-foreground">
|
|
{t("configForm.ffmpegArgs.autoDescription", {
|
|
ns: "views/settings",
|
|
})}
|
|
</p>
|
|
) : mode === "auto" ||
|
|
mode === "inherit" ||
|
|
mode === "none" ||
|
|
mode === "disabled" ? null : mode === "preset" && canUsePresets ? (
|
|
<Select
|
|
value={presetValue}
|
|
onValueChange={handlePresetChange}
|
|
disabled={disabled || readonly}
|
|
>
|
|
<SelectTrigger id={id} className="w-full md:max-w-md">
|
|
<SelectValue
|
|
placeholder={
|
|
placeholder ||
|
|
schema.title ||
|
|
t("configForm.ffmpegArgs.selectPreset", {
|
|
ns: "views/settings",
|
|
})
|
|
}
|
|
/>
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{presetOptions.map((preset) => (
|
|
<SelectItem key={preset} value={preset}>
|
|
{t(`configForm.ffmpegArgs.presetLabels.${preset}`, {
|
|
ns: "views/settings",
|
|
defaultValue: preset,
|
|
})}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
) : (
|
|
<Input
|
|
id={id}
|
|
value={manualValue}
|
|
onChange={handleManualChange}
|
|
disabled={disabled || readonly}
|
|
placeholder={
|
|
placeholder ||
|
|
t("configForm.ffmpegArgs.manualPlaceholder", {
|
|
ns: "views/settings",
|
|
})
|
|
}
|
|
/>
|
|
)}
|
|
|
|
{!hideDescription && !useSplitLayout && fieldDescription ? (
|
|
<p className="text-xs text-muted-foreground">{fieldDescription}</p>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|