Improve hwaccel widget in the Settings UI (#25143)
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.
This commit is contained in:
Josh Hawkins
2026-12-02 12:59:51 -07:00
committed by GitHub
parent fda5950d50
commit 9b386acaf1
6 changed files with 847 additions and 138 deletions
@@ -15,6 +15,8 @@ const ffmpegArgsWidget = (
"ui:widget": "FfmpegArgsWidget",
"ui:options": {
suppressMultiSchema: true,
// A list value renders through a different anyOf branch and layout
forceSplitLayout: true,
ffmpegPresetField: presetField,
...extraOptions,
},
@@ -23,7 +25,6 @@ const ffmpegArgsWidget = (
const recordSubArgsWidget = () =>
ffmpegArgsWidget("output_args.record_sub", {
allowInherit: true,
forceSplitLayout: true,
unsetLabelKey: "configForm.ffmpegArgs.sameAsRecord",
});
@@ -45,7 +46,9 @@ const ffmpeg: SectionConfigOverrides = {
return value.length > 0;
}
if (typeof value === "string") {
return !value.startsWith("preset-");
return (
value !== "" && value !== "auto" && !value.startsWith("preset-")
);
}
return false;
},
@@ -146,13 +149,11 @@ const ffmpeg: SectionConfigOverrides = {
hwaccel_args: ffmpegArgsWidget("hwaccel_args", {
allowInherit: true,
hideDescription: true,
forceSplitLayout: true,
showArrayItemDescription: true,
}),
input_args: ffmpegArgsWidget("input_args", {
allowInherit: true,
hideDescription: true,
forceSplitLayout: true,
showArrayItemDescription: true,
}),
output_args: {
@@ -87,8 +87,11 @@ const record: SectionConfigOverrides = {
"ui:widget": "FfmpegArgsWidget",
"ui:options": {
suppressMultiSchema: true,
forceSplitLayout: true,
ffmpegPresetField: "hwaccel_args",
ffmpegGlobalFieldPath: "export.hwaccel_args",
unsetLabelKey: "configForm.ffmpegArgs.sameAsFfmpeg",
cameraFallbackPath: "ffmpeg.hwaccel_args",
},
},
},
@@ -1,6 +1,6 @@
import type { WidgetProps } from "@rjsf/utils";
import useSWR from "swr";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import get from "lodash/get";
import isEqual from "lodash/isEqual";
@@ -24,7 +24,15 @@ type FfmpegPresetResponse = {
};
};
type FfmpegArgsMode = "preset" | "manual" | "inherit" | "none";
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"
@@ -65,15 +73,26 @@ const resolveMode = (
presets: string[],
defaultMode: FfmpegArgsMode,
allowInherit: boolean,
hwaccelModes: boolean,
): FfmpegArgsMode => {
if (value === null || value === undefined) {
return allowInherit ? "inherit" : "none";
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";
}
@@ -129,6 +148,9 @@ export function FfmpegArgsWidget(props: WidgetProps) {
// 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;
@@ -139,6 +161,11 @@ export function FfmpegArgsWidget(props: WidgetProps) {
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 (
@@ -164,21 +191,55 @@ export function FfmpegArgsWidget(props: WidgetProps) {
const canUsePresets = presetOptions.length > 0;
const defaultMode: FfmpegArgsMode = canUsePresets ? "preset" : "manual";
// Detect if this field's value is effectively inherited from the global
// config (i.e. the camera does not override it).
const isInheritedFromGlobal = useMemo(() => {
if (!showUseGlobalSetting) return false;
if (value === undefined || value === null) return true;
if (globalFieldValue === undefined || globalFieldValue === null)
return false;
return isEqual(value, globalFieldValue);
}, [showUseGlobalSetting, value, globalFieldValue]);
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;
}
return resolveMode(value, presetOptions, defaultMode, allowInherit);
// 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,
@@ -186,25 +247,63 @@ export function FfmpegArgsWidget(props: WidgetProps) {
presetOptions,
defaultMode,
allowInherit,
hwaccelModes,
savedValue,
]);
const [mode, setMode] = useState<FfmpegArgsMode>(detectedMode);
const modeKey = [
formContext?.level,
formContext?.cameraName,
formContext?.sectionPath,
formContext?.isProfile,
id,
].join(":");
// Track whether the user has explicitly changed mode to prevent the
// detected-mode sync from snapping back (e.g. when a user-selected
// preset happens to match the global value).
const userSetModeRef = useRef(false);
// 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],
);
const formIsClean = !formContext?.hasChanges;
// Reset tracking when the widget identity changes (camera switch)
// or when the form returns to a clean state (after a successful save).
// A remount mounts the replacement before the timer fires, so only leaving
// the page clears the choice.
useEffect(() => {
userSetModeRef.current = false;
}, [id, formIsClean]);
clearTimeout(chosenModeClears.get(modeKey));
return () => {
chosenModeClears.set(
modeKey,
setTimeout(() => chosenModes.delete(modeKey)),
);
};
}, [modeKey]);
const [mode, setMode] = useState<FfmpegArgsMode>(
() => chosenMode(value) ?? detectedMode,
);
useEffect(() => {
if (userSetModeRef.current) return;
const chosen = chosenMode(value);
if (chosen) {
setMode(chosen);
return;
}
if (!canUsePresets && detectedMode === "preset") {
setMode("manual");
@@ -212,20 +311,38 @@ export function FfmpegArgsWidget(props: WidgetProps) {
}
setMode(detectedMode);
}, [canUsePresets, 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) => {
userSetModeRef.current = true;
setMode(nextMode);
// 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") {
onChange(undefined);
choose(nextMode, isInherited(savedValue) ? savedValue : undefined);
return;
}
if (nextMode === "none") {
onChange(undefined);
choose(nextMode, undefined);
return;
}
if (nextMode === "auto") {
choose(nextMode, "auto");
return;
}
if (nextMode === "disabled") {
choose(nextMode, []);
return;
}
@@ -236,38 +353,58 @@ export function FfmpegArgsWidget(props: WidgetProps) {
? currentValue
: presetOptions[0];
if (presetValue) {
onChange(presetValue);
choose(nextMode, presetValue);
} else {
setMode(nextMode);
}
return;
}
if (mode === "preset") {
onChange("");
// 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;
}
const manualText = normalizeManualText(value);
onChange(manualText);
choose(nextMode, normalizeManualText(value));
},
[mode, onChange, presetOptions, value],
[
allowInherit,
choose,
defaultMode,
hwaccelModes,
isInherited,
mode,
presetOptions,
savedValue,
value,
],
);
const handlePresetChange = useCallback(
(preset: string) => {
onChange(preset);
},
[onChange],
(preset: string) => choose("preset", preset),
[choose],
);
const handleManualChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
const newText = event.target.value;
onChange(newText);
},
[onChange],
(event: React.ChangeEvent<HTMLInputElement>) =>
choose("manual", event.target.value),
[choose],
);
const manualValue = normalizeManualText(value);
const manualValue =
chosenMode(value) === "manual" && chosenModes.get(modeKey)?.value === ""
? ""
: normalizeManualText(value);
const presetValue =
typeof value === "string" && presetOptions.includes(value) ? value : "";
const fallbackDescriptionKey = useMemo(() => {
@@ -317,6 +454,48 @@ export function FfmpegArgsWidget(props: WidgetProps) {
? 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
@@ -324,95 +503,33 @@ export function FfmpegArgsWidget(props: WidgetProps) {
onValueChange={(next) => handleModeChange(next as FfmpegArgsMode)}
className="gap-3"
>
{showUseGlobalSetting ? (
<div className="flex items-center space-x-2">
<RadioGroupItem
value="inherit"
id={`${id}-inherit`}
disabled={disabled || readonly}
className={
mode === "inherit"
? "bg-selected from-selected/50 to-selected/90 text-selected"
: "bg-secondary from-secondary/50 to-secondary/90 text-secondary"
}
/>
<label htmlFor={`${id}-inherit`} className="cursor-pointer text-sm">
{t("configForm.ffmpegArgs.useGlobalSetting", {
ns: "views/settings",
})}
</label>
</div>
) : allowInherit ? (
<div className="flex items-center space-x-2">
<RadioGroupItem
value="inherit"
id={`${id}-inherit`}
disabled={disabled || readonly}
className={
mode === "inherit"
? "bg-selected from-selected/50 to-selected/90 text-selected"
: "bg-secondary from-secondary/50 to-secondary/90 text-secondary"
}
/>
<label htmlFor={`${id}-inherit`} className="cursor-pointer text-sm">
{t(unsetLabelKey ?? "configForm.ffmpegArgs.inherit", {
ns: "views/settings",
})}
</label>
</div>
) : (
<div className="flex items-center space-x-2">
<RadioGroupItem
value="none"
id={`${id}-none`}
disabled={disabled || readonly}
className={
mode === "none"
? "bg-selected from-selected/50 to-selected/90 text-selected"
: "bg-secondary from-secondary/50 to-secondary/90 text-secondary"
}
/>
<label htmlFor={`${id}-none`} className="cursor-pointer text-sm">
{t(unsetLabelKey ?? "configForm.ffmpegArgs.none", {
ns: "views/settings",
})}
</label>
</div>
{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" }),
)}
<div className="flex items-center space-x-2">
<RadioGroupItem
value="preset"
id={`${id}-preset`}
disabled={disabled || readonly || !canUsePresets}
className={
mode === "preset"
? "bg-selected from-selected/50 to-selected/90 text-selected"
: "bg-secondary from-secondary/50 to-secondary/90 text-secondary"
}
/>
<label htmlFor={`${id}-preset`} className="cursor-pointer text-sm">
{t("configForm.ffmpegArgs.preset", { ns: "views/settings" })}
</label>
</div>
<div className="flex items-center space-x-2">
<RadioGroupItem
value="manual"
id={`${id}-manual`}
disabled={disabled || readonly}
className={
mode === "manual"
? "bg-selected from-selected/50 to-selected/90 text-selected"
: "bg-secondary from-secondary/50 to-secondary/90 text-secondary"
}
/>
<label htmlFor={`${id}-manual`} className="cursor-pointer text-sm">
{t("configForm.ffmpegArgs.manual", { ns: "views/settings" })}
</label>
</div>
</RadioGroup>
{mode === "inherit" || mode === "none" ? null : mode === "preset" &&
canUsePresets ? (
{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}