diff --git a/web/e2e/specs/settings/hwaccel-args.spec.ts b/web/e2e/specs/settings/hwaccel-args.spec.ts index 0c54ae354f..e9e7f43507 100644 --- a/web/e2e/specs/settings/hwaccel-args.spec.ts +++ b/web/e2e/specs/settings/hwaccel-args.spec.ts @@ -1,8 +1,10 @@ /** - * Hardware acceleration args widget -- MEDIUM tier. + * FFmpeg args widget -- MEDIUM tier. * * Global hwaccel offers Auto (writes "auto") and Disabled (writes []). * Cameras offer Disabled next to "Inherit from global setting". + * Global input and record args offer Default, and record_sub offers + * "Same as record" (writes []). */ import { readFileSync } from "node:fs"; @@ -49,7 +51,10 @@ async function openSettings( json: { hwaccel_args: ["preset-vaapi", "preset-nvidia"], input_args: ["preset-rtsp-generic"], - output_args: { record: ["preset-record-generic"], detect: [] }, + output_args: { + record: ["preset-record-generic", "preset-record-generic-audio-aac"], + detect: [], + }, }, }), ); @@ -576,3 +581,200 @@ test.describe("hwaccel args widget @medium", () => { ).toBeChecked(); }); }); + +const DEFAULT_MODE = "default"; +const DEFAULTED_FIELDS = ["root_input_args", "root_output_args_record"]; +const RECORD_SUB = "root_output_args_record_sub"; + +const showOutputArgs = (app: FrigateApp) => + app.page.getByText("Output arguments", { exact: true }).first().click(); + +const showGlobalArgs = async (app: FrigateApp) => { + await app.page + .getByRole("button", { name: /Advanced/ }) + .first() + .click(); + await showOutputArgs(app); +}; + +const switchSection = (app: FrigateApp, name: string) => + app.page + .locator('[data-sidebar="menu-sub-button"]', { + hasText: new RegExp(`^${name}$`), + }) + .click(); + +test.describe("ffmpeg default args @medium", () => { + test("global defaults show Default", async ({ frigateApp }) => { + await openSettings(frigateApp, "/settings?page=globalFfmpeg"); + await showGlobalArgs(frigateApp); + + for (const field of DEFAULTED_FIELDS) { + await expect(radio(frigateApp, field, DEFAULT_MODE)).toBeChecked(); + } + }); + + test("global Default on defaults sends no delete", async ({ frigateApp }) => { + const capture = await openSettings( + frigateApp, + "/settings?page=globalFfmpeg", + ); + await showGlobalArgs(frigateApp); + + for (const field of DEFAULTED_FIELDS) { + const unset = radio(frigateApp, field, DEFAULT_MODE); + await radio(frigateApp, field, "manual").click(); + await manualInput(frigateApp, field).fill("-x 1"); + await unset.click(); + await expect(unset).toBeChecked(); + } + + await frigateApp.page + .getByRole("switch", { name: "Apple compatibility" }) + .click(); + await save(frigateApp).click(); + await expect.poll(capture.saved, { timeout: 5_000 }).toMatchObject({ + config_data: { ffmpeg: { apple_compatibility: true } }, + }); + const { ffmpeg } = ( + capture.saved() as { config_data: { ffmpeg: Record } } + ).config_data; + expect(ffmpeg).not.toHaveProperty("input_args"); + expect(ffmpeg).not.toHaveProperty("output_args"); + }); + + test("Default then Manual starts with an empty box", async ({ + frigateApp, + }) => { + await openSettings(frigateApp, "/settings?page=globalFfmpeg"); + await showGlobalArgs(frigateApp); + + for (const field of DEFAULTED_FIELDS) { + const manual = radio(frigateApp, field, "manual"); + await manual.click(); + + await expect(manual).toBeChecked(); + await expect(manualInput(frigateApp, field)).toHaveValue(""); + } + await expect(save(frigateApp)).toBeDisabled(); + }); + + test("global Default on defaults stays across a section switch", async ({ + frigateApp, + }) => { + test.skip(frigateApp.isMobile, "the section sidebar is desktop only"); + await openSettings(frigateApp, "/settings?page=globalFfmpeg"); + await showGlobalArgs(frigateApp); + + for (const field of DEFAULTED_FIELDS) { + await radio(frigateApp, field, "manual").click(); + await manualInput(frigateApp, field).fill("-x 1"); + await radio(frigateApp, field, DEFAULT_MODE).click(); + } + await switchSection(frigateApp, "Recording"); + await switchSection(frigateApp, "FFmpeg"); + await showGlobalArgs(frigateApp); + + for (const field of DEFAULTED_FIELDS) { + await expect(radio(frigateApp, field, DEFAULT_MODE)).toBeChecked(); + } + await expect(save(frigateApp)).toBeDisabled(); + }); + + test("global Default on saved args deletes them", async ({ frigateApp }) => { + test.skip(frigateApp.isMobile, "the section sidebar is desktop only"); + const capture = await openSettings( + frigateApp, + "/settings?page=globalFfmpeg", + { ffmpeg: { input_args: "-in 1", output_args: { record: "-out 1" } } }, + ); + await showGlobalArgs(frigateApp); + + for (const field of DEFAULTED_FIELDS) { + await radio(frigateApp, field, DEFAULT_MODE).click(); + } + await switchSection(frigateApp, "Recording"); + await switchSection(frigateApp, "FFmpeg"); + await showGlobalArgs(frigateApp); + for (const field of DEFAULTED_FIELDS) { + await expect(radio(frigateApp, field, DEFAULT_MODE)).toBeChecked(); + } + + await save(frigateApp).click(); + await expect.poll(capture.saved, { timeout: 5_000 }).toMatchObject({ + config_data: { + ffmpeg: { input_args: "", output_args: { record: "" } }, + }, + }); + }); + + test("Same as record on defaults leaves nothing to save", async ({ + frigateApp, + }) => { + await openSettings(frigateApp, "/settings?page=globalFfmpeg"); + await showGlobalArgs(frigateApp); + + const sameAsRecord = radio(frigateApp, RECORD_SUB, "inherit"); + await expect(sameAsRecord).toBeChecked(); + await radio(frigateApp, RECORD_SUB, "preset").click(); + await expect(save(frigateApp)).toBeEnabled(); + await sameAsRecord.click(); + + await expect(sameAsRecord).toBeChecked(); + await expect(save(frigateApp)).toBeDisabled(); + }); + + test("global Same as record on saved args saves an empty list", async ({ + frigateApp, + }) => { + const capture = await openSettings( + frigateApp, + "/settings?page=globalFfmpeg", + { ffmpeg: { output_args: { record_sub: "preset-record-generic" } } }, + ); + await showGlobalArgs(frigateApp); + + await radio(frigateApp, RECORD_SUB, "inherit").click(); + await save(frigateApp).click(); + await expect.poll(capture.saved, { timeout: 5_000 }).toMatchObject({ + config_data: { ffmpeg: { output_args: { record_sub: [] } } }, + }); + }); + + test("camera Same as record over a global value saves an empty list", async ({ + frigateApp, + }) => { + const recordSub = { output_args: { record_sub: "preset-record-generic" } }; + const capture = await openSettings( + frigateApp, + "/settings?page=cameraFfmpeg&camera=front_door", + { ffmpeg: recordSub, cameras: { front_door: { ffmpeg: recordSub } } }, + ); + + const sameAsRecord = radio(frigateApp, RECORD_SUB, "inherit"); + await showOutputArgs(frigateApp); + await sameAsRecord.click(); + await frigateApp.page + .getByRole("button", { name: "Select a camera" }) + .click(); + await frigateApp.page.getByRole("switch", { name: "Backyard" }).click(); + await frigateApp.page + .getByRole("button", { name: "Select a camera" }) + .click(); + await frigateApp.page.getByRole("switch", { name: "Front Door" }).click(); + await expect(radio(frigateApp, "root_input_args", "inherit")).toBeVisible(); + if (!(await sameAsRecord.isVisible())) { + await showOutputArgs(frigateApp); + } + await expect(sameAsRecord).toBeChecked(); + + await save(frigateApp).click(); + await expect.poll(capture.saved, { timeout: 5_000 }).toMatchObject({ + config_data: { + cameras: { + front_door: { ffmpeg: { output_args: { record_sub: [] } } }, + }, + }, + }); + }); +}); diff --git a/web/public/locales/en/views/settings.json b/web/public/locales/en/views/settings.json index cf9bc25e83..a761df07f2 100644 --- a/web/public/locales/en/views/settings.json +++ b/web/public/locales/en/views/settings.json @@ -1607,7 +1607,7 @@ "preset": "Preset", "manual": "Manual arguments", "inherit": "Inherit from camera setting", - "none": "None", + "default": "Default", "auto": "Auto", "autoDescription": "Frigate detects Nvidia or VAAPI hardware. After saving, this field shows the detected preset, or Disabled if none is found. Other hardware needs a preset.", "disabled": "Disabled", diff --git a/web/src/components/config-form/theme/widgets/FfmpegArgsWidget.tsx b/web/src/components/config-form/theme/widgets/FfmpegArgsWidget.tsx index c171271f89..ce7311e9a6 100644 --- a/web/src/components/config-form/theme/widgets/FfmpegArgsWidget.tsx +++ b/web/src/components/config-form/theme/widgets/FfmpegArgsWidget.tsx @@ -1,4 +1,4 @@ -import type { WidgetProps } from "@rjsf/utils"; +import type { RJSFSchema, WidgetProps } from "@rjsf/utils"; import useSWR from "swr"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; @@ -25,7 +25,7 @@ type FfmpegPresetResponse = { }; type FfmpegArgsMode = - "preset" | "manual" | "inherit" | "none" | "auto" | "disabled"; + "preset" | "manual" | "inherit" | "default" | "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 @@ -68,6 +68,20 @@ const getPresetOptions = ( return []; }; +// AnyOfField hands the widget a branch without the default, so read it +// from the section schema, which arrives with its $refs resolved. +const getSchemaDefault = ( + rootSchema: RJSFSchema | undefined, + fieldPath: string | undefined, +): unknown => { + if (!rootSchema || !fieldPath) { + return undefined; + } + + const schemaPath = fieldPath.split(".").flatMap((key) => ["properties", key]); + return get(rootSchema, [...schemaPath, "default"]); +}; + const resolveMode = ( value: unknown, presets: string[], @@ -77,7 +91,7 @@ const resolveMode = ( ): FfmpegArgsMode => { if (value === null || value === undefined) { if (allowInherit) return "inherit"; - return hwaccelModes ? "auto" : "none"; + return hwaccelModes ? "auto" : "default"; } if (allowInherit && Array.isArray(value) && value.length === 0) { @@ -157,15 +171,27 @@ export function FfmpegArgsWidget(props: WidgetProps) { const useSplitLayout = options?.splitLayout !== false; // Detect camera-level top-level fields (not inside inputs array). - // These should show "Use global setting" instead of "None". + // These should show "Use global setting" instead of "Default". const isInputScoped = id.includes("_inputs_"); const showUseGlobalSetting = isCameraLevel && !isInputScoped && !allowInherit; - // Hardware acceleration offers Auto and Disabled in place of None. Auto + // Hardware acceleration offers Auto and Disabled in place of Default. 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; + const unsetMode: FfmpegArgsMode = + showUseGlobalSetting || allowInherit + ? "inherit" + : hwaccelModes + ? "auto" + : "default"; + + const schemaDefault = + unsetMode === "default" + ? getSchemaDefault(props.registry?.rootSchema, globalFieldPath) + : undefined; + // Extract the global value for this specific field to detect inheritance const globalFieldValue = useMemo(() => { if ( @@ -233,6 +259,9 @@ export function FfmpegArgsWidget(props: WidgetProps) { if (hwaccelModes && value === "" && !isEqual(savedValue, "")) { return "manual" as FfmpegArgsMode; } + if (schemaDefault !== undefined && isEqual(value, schemaDefault)) { + return "default" as FfmpegArgsMode; + } return resolveMode( value, presetOptions, @@ -249,6 +278,7 @@ export function FfmpegArgsWidget(props: WidgetProps) { allowInherit, hwaccelModes, savedValue, + schemaDefault, ]); const modeKey = [ @@ -324,15 +354,27 @@ export function FfmpegArgsWidget(props: WidgetProps) { 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. + // An empty record_sub is the backend's "same as record" at both levels. + // Deleting it could target a key absent from the YAML, and a camera + // would fall back to the global value instead. + if (nextMode === "inherit" && presetField === "output_args.record_sub") { + choose(nextMode, []); + return; + } + + // Restoring an inherited or default saved value leaves the form clean. + // Sending undefined would delete a key that may be absent from the + // YAML. A global value that differs from the default must be in it. if (nextMode === "inherit") { choose(nextMode, isInherited(savedValue) ? savedValue : undefined); return; } - if (nextMode === "none") { - choose(nextMode, undefined); + if (nextMode === "default") { + choose( + nextMode, + isEqual(savedValue, schemaDefault) ? savedValue : undefined, + ); return; } @@ -362,7 +404,12 @@ export function FfmpegArgsWidget(props: WidgetProps) { // 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") { + if ( + mode === "preset" || + mode === "default" || + mode === "auto" || + mode === "disabled" + ) { const savedIsManual = resolveMode( savedValue, @@ -384,8 +431,10 @@ export function FfmpegArgsWidget(props: WidgetProps) { hwaccelModes, isInherited, mode, + presetField, presetOptions, savedValue, + schemaDefault, value, ], ); @@ -454,12 +503,6 @@ 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 @@ -470,7 +513,7 @@ export function FfmpegArgsWidget(props: WidgetProps) { ? t(unsetLabelKey ?? "configForm.ffmpegArgs.auto", { ns: "views/settings", }) - : t(unsetLabelKey ?? "configForm.ffmpegArgs.none", { + : t(unsetLabelKey ?? "configForm.ffmpegArgs.default", { ns: "views/settings", }); @@ -528,7 +571,7 @@ export function FfmpegArgsWidget(props: WidgetProps) {

) : mode === "auto" || mode === "inherit" || - mode === "none" || + mode === "default" || mode === "disabled" ? null : mode === "preset" && canUsePresets ? (