diff --git a/frigate/app.py b/frigate/app.py index ba4f2ad4e0..9c49cabb1b 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -483,7 +483,7 @@ class FrigateApp: def start_audio_processor(self) -> None: self.audio_process = AudioProcessor( - self.config, self.camera_metrics, self.stop_event + self.config, self.camera_metrics, self.embeddings_metrics, self.stop_event ) self.audio_process.start() self.processes["audio_detector"] = self.audio_process.pid or 0 @@ -556,10 +556,20 @@ class FrigateApp: "output", lambda: OutputProcess(self.config, self.stop_event), ), + ( + "audio_process", + "audio_detector", + lambda: AudioProcessor( + self.config, + self.camera_metrics, + self.embeddings_metrics, + self.stop_event, + ), + ), ] for attr, key, factory in specs: - if not hasattr(self, attr): + if getattr(self, attr, None) is None: continue def on_restart( diff --git a/frigate/events/audio.py b/frigate/events/audio.py index 457b1d967d..3d1bfbee06 100644 --- a/frigate/events/audio.py +++ b/frigate/events/audio.py @@ -11,6 +11,7 @@ from typing import Any import numpy as np +from frigate.camera import CameraMetrics from frigate.comms.detections_updater import DetectionPublisher, DetectionTypeEnum from frigate.comms.inter_process import InterProcessRequestor from frigate.config import CameraConfig, CameraInput, FrigateConfig @@ -36,6 +37,7 @@ from frigate.data_processing.common.audio_transcription.model import ( from frigate.data_processing.real_time.audio_transcription import ( AudioTranscriptionRealTimeProcessor, ) +from frigate.data_processing.types import DataProcessorMetrics from frigate.ffmpeg_presets import parse_preset_input from frigate.log import LogPipe, suppress_stderr_during from frigate.util.builtin import get_ffmpeg_arg_list, load_labels @@ -86,6 +88,7 @@ class AudioProcessor(FrigateProcess): self, config: FrigateConfig, camera_metrics: DictProxy, + embeddings_metrics: DataProcessorMetrics, stop_event: MpEvent, ): super().__init__( @@ -93,8 +96,38 @@ class AudioProcessor(FrigateProcess): ) self.camera_metrics = camera_metrics + self.embeddings_metrics = embeddings_metrics self.config = config + def spawn_if_needed(self, camera: CameraConfig) -> None: + """Start an audio maintainer for the camera once everything it needs + has arrived. Returning early leaves the camera for the next poll.""" + name = camera.name + if name is None or name in self.audio_threads: + return + if not camera.enabled or not camera.audio.enabled: + return + # ffmpeg update may not have arrived yet + if not any("audio" in i.roles for i in camera.ffmpeg.inputs): + return + # the camera maintainer creates metrics on its own poll of the same + # add update and may not have gotten there yet + metrics = self.camera_metrics.get(name) + if metrics is None: + return + thread = AudioEventMaintainer( + camera, + self.config, + metrics, + self.embeddings_metrics, + self.transcription_model_runner, + self.stop_event, # type: ignore[arg-type] + self.genai_manager, + ) + self.audio_threads[name] = thread + thread.start() + self.logger.info(f"Audio maintainer started for {name}") + def __stop_audio_thread(self, camera: str) -> None: thread = self.audio_threads.pop(camera, None) if thread is None: @@ -150,29 +183,8 @@ class AudioProcessor(FrigateProcess): ], ) - def spawn_if_needed(camera: CameraConfig) -> None: - name = camera.name - if name is None or name in self.audio_threads: - return - if not camera.enabled or not camera.audio.enabled: - return - # ffmpeg update may not have arrived yet; wait for next poll - if not any("audio" in i.roles for i in camera.ffmpeg.inputs): - return - thread = AudioEventMaintainer( - camera, - self.config, - self.camera_metrics, - self.transcription_model_runner, - self.stop_event, # type: ignore[arg-type] - self.genai_manager, - ) - self.audio_threads[name] = thread - thread.start() - self.logger.info(f"Audio maintainer started for {name}") - for camera in self.config.cameras.values(): - spawn_if_needed(camera) + self.spawn_if_needed(camera) self.logger.info(f"Audio processor started (pid: {self.pid})") @@ -182,15 +194,14 @@ class AudioProcessor(FrigateProcess): updated_topics = config_subscriber.check_for_updates() # stop maintainers for removed cameras so their ffmpeg process is - # torn down and they stop touching camera_metrics (which the camera - # maintainer has already popped for the removed camera) + # torn down for removed_camera in updated_topics.get( CameraConfigUpdateEnum.remove.name, [] ): self.__stop_audio_thread(removed_camera) for camera in self.config.cameras.values(): - spawn_if_needed(camera) + self.spawn_if_needed(camera) config_subscriber.stop() @@ -212,7 +223,8 @@ class AudioEventMaintainer(threading.Thread): self, camera: CameraConfig, config: FrigateConfig, - camera_metrics: DictProxy, + metrics: CameraMetrics, + embeddings_metrics: DataProcessorMetrics, audio_transcription_model_runner: AudioTranscriptionModelRunner | None, stop_event: threading.Event, genai_manager: Any = None, @@ -221,7 +233,11 @@ class AudioEventMaintainer(threading.Thread): self.config = config self.camera_config = camera - self.camera_metrics = camera_metrics + # hold the metrics object rather than indexing the manager dict per + # chunk, which costs an IPC round trip and breaks once the camera + # maintainer pops the entry on removal + self.metrics = metrics + self.embeddings_metrics = embeddings_metrics self.stop_event = stop_event # per-camera stop signal so a single maintainer can be torn down at # runtime (e.g. on camera removal) without stopping the whole process @@ -265,7 +281,7 @@ class AudioEventMaintainer(threading.Thread): camera_config=self.camera_config, requestor=self.requestor, model_runner=self.audio_transcription_model_runner, - metrics=self.camera_metrics[self.camera_config.name], + metrics=self.embeddings_metrics, stop_event=self.stop_event, genai_manager=self.genai_manager, ) @@ -291,8 +307,8 @@ class AudioEventMaintainer(threading.Thread): audio_as_float: np.ndarray = audio.astype(np.float32) rms, dBFS = self.calculate_audio_levels(audio_as_float) - self.camera_metrics[self.camera_config.name].audio_rms.value = rms - self.camera_metrics[self.camera_config.name].audio_dBFS.value = dBFS + self.metrics.audio_rms.value = rms + self.metrics.audio_dBFS.value = dBFS audio_detections: list[tuple[str, float]] = [] @@ -377,7 +393,6 @@ class AudioEventMaintainer(threading.Thread): return time.sleep(self.camera_config.ffmpeg.retry_interval) - self.logpipe.dump() self.start_or_restart_ffmpeg() if self.audio_listener is None or self.audio_listener.stdout is None: diff --git a/frigate/log.py b/frigate/log.py index 693d70e8c9..632db56738 100644 --- a/frigate/log.py +++ b/frigate/log.py @@ -144,6 +144,14 @@ class LogPipe(threading.Thread): self.pipeReader.close() def dump(self) -> None: + if not self.deque: + return + + self.logger.log( + self.level, + "The following ffmpeg logs include the last 100 lines prior to exit.", + ) + while len(self.deque) > 0: self.logger.log(self.level, self.deque.popleft()) diff --git a/frigate/motion/improved_motion.py b/frigate/motion/improved_motion.py index 598eeacd1d..bbcaee23af 100644 --- a/frigate/motion/improved_motion.py +++ b/frigate/motion/improved_motion.py @@ -188,8 +188,12 @@ class ImprovedMotionDetector(MotionDetector): self.config.skip_motion_threshold is not None and pct_motion > self.config.skip_motion_threshold ): - # force a recalibration so we transition to the new background + # recalibrate so we transition to the new background. the frame + # still has to be blended in here, otherwise the background stays + # frozen and every subsequent frame skips as well self.calibrating = True + cv2.accumulateWeighted(resized_frame, self.avg_frame, 0.2) + self.motion_frame_count = 0 return [] # once the motion is less than 5% and the number of contours is < 4, assume its calibrated diff --git a/frigate/test/test_audio_spawn.py b/frigate/test/test_audio_spawn.py new file mode 100644 index 0000000000..6c1c31a6a5 --- /dev/null +++ b/frigate/test/test_audio_spawn.py @@ -0,0 +1,90 @@ +"""Tests for audio maintainer spawning on runtime camera adds.""" + +import threading +import unittest +from unittest.mock import MagicMock, patch + +from frigate.config import FrigateConfig +from frigate.events.audio import AudioProcessor + + +def _build_config() -> FrigateConfig: + return FrigateConfig( + **{ + "mqtt": {"host": "mqtt"}, + "cameras": { + "front_door": { + "ffmpeg": { + "inputs": [ + { + "path": "rtsp://10.0.0.1:554/video", + "roles": ["detect", "audio"], + } + ] + }, + "audio": {"enabled": True}, + } + }, + } + ) + + +class TestAudioSpawnIfNeeded(unittest.TestCase): + def _processor(self, config: FrigateConfig, camera_metrics: dict) -> AudioProcessor: + processor = AudioProcessor.__new__(AudioProcessor) + processor.config = config + processor.camera_metrics = camera_metrics + processor.embeddings_metrics = MagicMock() + processor.audio_threads = {} + processor.transcription_model_runner = None + processor.genai_manager = None + processor.stop_event = threading.Event() + processor.logger = MagicMock() + return processor + + def test_skips_camera_whose_metrics_have_not_been_created_yet(self): + """The camera maintainer creates metrics on its own poll of the add + update, so the audio processor can see the camera first.""" + config = _build_config() + processor = self._processor(config, {}) + + with patch("frigate.events.audio.AudioEventMaintainer") as maintainer: + processor.spawn_if_needed(config.cameras["front_door"]) + + maintainer.assert_not_called() + assert processor.audio_threads == {} + + def test_spawns_once_metrics_exist_and_passes_the_metrics_object(self): + config = _build_config() + metrics = object() + processor = self._processor(config, {"front_door": metrics}) + + with patch("frigate.events.audio.AudioEventMaintainer") as maintainer: + processor.spawn_if_needed(config.cameras["front_door"]) + + assert maintainer.call_args.args[2] is metrics + assert "front_door" in processor.audio_threads + maintainer.return_value.start.assert_called_once() + + def test_does_not_respawn_for_a_camera_already_running(self): + config = _build_config() + processor = self._processor(config, {"front_door": object()}) + processor.audio_threads["front_door"] = MagicMock() + + with patch("frigate.events.audio.AudioEventMaintainer") as maintainer: + processor.spawn_if_needed(config.cameras["front_door"]) + + maintainer.assert_not_called() + + def test_skips_camera_whose_ffmpeg_update_has_not_arrived_yet(self): + """The add update carries the camera before its ffmpeg inputs are + applied, so the audio role can be briefly missing.""" + config = _build_config() + camera = config.cameras["front_door"] + camera.ffmpeg.inputs[0].roles = ["detect"] + processor = self._processor(config, {"front_door": object()}) + + with patch("frigate.events.audio.AudioEventMaintainer") as maintainer: + processor.spawn_if_needed(camera) + + maintainer.assert_not_called() diff --git a/frigate/test/test_motion_detector.py b/frigate/test/test_motion_detector.py index cdf4210a51..6fb7a68efc 100644 --- a/frigate/test/test_motion_detector.py +++ b/frigate/test/test_motion_detector.py @@ -73,6 +73,35 @@ class TestImprovedMotionDetector(unittest.TestCase): "Motion boxes should be empty when scene change exceeds skip threshold", ) + def _bright_frame(self, offset: int) -> np.ndarray: + """Produce a bright frame with a small dark object that moves.""" + frame = np.full((self.frame_shape[0], self.frame_shape[1]), 200, dtype=np.uint8) + x = 10 + (offset * 5) % 60 + frame[40:60, x : x + 15] = 20 + return frame + + def test_skip_motion_threshold_recovers_after_skip(self): + """Skipped frames must still be blended into the background. + + A bright scene differs from the zeroed background across the whole + frame, so the first frames are skipped. The background has to catch up + anyway, otherwise motion detection never returns. + """ + self.config.skip_motion_threshold = 0.5 + self.config.improve_contrast = False + self.detector.config = self.config + self.detector.update_mask() + + boxes = [len(self.detector.detect(self._bright_frame(i))) for i in range(40)] + + self.assertEqual(boxes[0], 0, "First frame should exceed the skip threshold") + self.assertGreater( + self.detector.avg_frame.max(), + 0, + "Background was never updated while frames were skipped", + ) + self.assertTrue(any(boxes), "Motion detection never recovered after a skip") + def test_skip_motion_threshold_does_not_affect_calibration(self): """Even when skipping, the detector should go into calibrating state.""" self.config.skip_motion_threshold = 0.4 diff --git a/frigate/track/norfair_tracker.py b/frigate/track/norfair_tracker.py index 6b39885c0d..b2e8221f27 100644 --- a/frigate/track/norfair_tracker.py +++ b/frigate/track/norfair_tracker.py @@ -324,9 +324,7 @@ class NorfairTracker(ObjectTracker): ): tracker = self.get_tracker(obj["label"]) tracker.tracked_objects = [ - o - for o in tracker.tracked_objects - if str(o.global_id) != track_id and o.hit_counter < 0 + o for o in tracker.tracked_objects if str(o.global_id) != track_id ] del self.track_id_map[track_id] diff --git a/frigate/util/ffmpeg.py b/frigate/util/ffmpeg.py index fed2fd2c3f..e0e9879cde 100644 --- a/frigate/util/ffmpeg.py +++ b/frigate/util/ffmpeg.py @@ -49,6 +49,9 @@ def start_or_restart_ffmpeg( if ffmpeg_process is not None: stop_ffmpeg(ffmpeg_process, logger) + # flush after the stop so the logs cover ffmpeg's output up to exit + logpipe.dump() + if frame_size is None: process = sp.Popen( ffmpeg_cmd, diff --git a/frigate/video/ffmpeg.py b/frigate/video/ffmpeg.py index c55c127498..a676cfc670 100644 --- a/frigate/video/ffmpeg.py +++ b/frigate/video/ffmpeg.py @@ -319,9 +319,6 @@ class CameraWatchdog(threading.Thread): f"Capture thread for {self.config.name} did not exit in time" ) - self.logger.error( - "The following ffmpeg logs include the last 100 lines prior to exit." - ) self.logpipe.dump() self.logger.info("Restarting ffmpeg...") self.start_ffmpeg_detect() @@ -543,7 +540,6 @@ class CameraWatchdog(threading.Thread): f"{self.config.name}/status/{role.value}", "offline" ) - p["logpipe"].dump() p["process"] = start_or_restart_ffmpeg( p["cmd"], self.logger, p["logpipe"], ffmpeg_process=p["process"] ) diff --git a/web/e2e/helpers/monaco.ts b/web/e2e/helpers/monaco.ts index 6e9bcd871a..f9a551a3d7 100644 --- a/web/e2e/helpers/monaco.ts +++ b/web/e2e/helpers/monaco.ts @@ -56,3 +56,14 @@ export async function waitForErrorMarker( .poll(() => hasErrorMarkers(page), { timeout: timeoutMs }) .toBe(true); } + +/** + * Append a trailing comment so the editor content differs from the + * loaded config. The save buttons are disabled until then. + */ +export async function makeMonacoEdit(page: Page): Promise { + const editor = page.locator(".monaco-editor").first(); + await editor.click(); + await page.keyboard.press("ControlOrMeta+End"); + await page.keyboard.type("# edited by e2e", { delay: 0 }); +} diff --git a/web/e2e/specs/config-editor.spec.ts b/web/e2e/specs/config-editor.spec.ts index 2b51a93636..10f6fb1993 100644 --- a/web/e2e/specs/config-editor.spec.ts +++ b/web/e2e/specs/config-editor.spec.ts @@ -11,6 +11,7 @@ import { installWsFrameCapture, waitForWsFrame } from "../helpers/ws-frames"; import { grantClipboardPermissions, readClipboard } from "../helpers/clipboard"; import { getMonacoVisibleText, + makeMonacoEdit, replaceMonacoValue, waitForErrorMarker, } from "../helpers/monaco"; @@ -71,6 +72,7 @@ test.describe("Config Editor — Save @medium", () => { await expect(frigateApp.page.locator(".monaco-editor").first()).toBeVisible( { timeout: 15_000 }, ); + await makeMonacoEdit(frigateApp.page); await frigateApp.page.getByLabel("Save Only").click(); await expect .poll(() => capture.capturedUrl(), { timeout: 5_000 }) @@ -92,6 +94,7 @@ test.describe("Config Editor — Save @medium", () => { await expect(frigateApp.page.locator(".monaco-editor").first()).toBeVisible( { timeout: 15_000 }, ); + await makeMonacoEdit(frigateApp.page); await frigateApp.page.getByLabel("Save Only").click(); await expect(frigateApp.page.getByText(/Invalid field/i)).toBeVisible({ timeout: 5_000, @@ -117,6 +120,7 @@ test.describe("Config Editor — Save and Restart @medium", () => { { timeout: 15_000 }, ); + await makeMonacoEdit(frigateApp.page); await frigateApp.page.getByLabel("Save & Restart").click(); const dialog = frigateApp.page.getByRole("alertdialog"); await expect(dialog).toBeVisible({ timeout: 5_000 }); @@ -140,6 +144,7 @@ test.describe("Config Editor — Save and Restart @medium", () => { { timeout: 15_000 }, ); + await makeMonacoEdit(frigateApp.page); await frigateApp.page.getByLabel("Save & Restart").click(); const dialog = frigateApp.page.getByRole("alertdialog"); await expect(dialog).toBeVisible({ timeout: 5_000 }); diff --git a/web/public/locales/en/views/settings.json b/web/public/locales/en/views/settings.json index dbf8f1874a..1df503441f 100644 --- a/web/public/locales/en/views/settings.json +++ b/web/public/locales/en/views/settings.json @@ -1208,9 +1208,8 @@ "desc": "Frigate can natively send push notifications to your device when it is running in the browser or installed as a PWA." }, "notificationUnavailable": { - "title": "Notifications Unavailable", - "desc": "Web push notifications require a secure context (https://…). This is a browser limitation. Access Frigate securely to use notifications.", - "descPwa": "On iOS, web push notifications are only available when Frigate is installed to your Home Screen. Open the Share menu, choose Add to Home Screen, then open Frigate from the new icon to register this device for notifications." + "desc": "Web push notifications require a secure context (https://…). This is a browser limitation. Access Frigate securely to use notifications.", + "descPwa": "On iOS, web push notifications are only available when Frigate is installed to your Home Screen. Open the Share menu, choose Add to Home Screen, then open Frigate from the new icon to register this device for notifications." }, "globalSettings": { "title": "Global Settings", @@ -1316,7 +1315,6 @@ "triggers": { "documentTitle": "Triggers", "semanticSearch": { - "title": "Semantic Search is disabled", "desc": "Semantic Search must be enabled to use Triggers." }, "management": { diff --git a/web/src/components/config-form/section-configs/audio_transcription.ts b/web/src/components/config-form/section-configs/audio_transcription.ts index c359611b86..f1cc6680d1 100644 --- a/web/src/components/config-form/section-configs/audio_transcription.ts +++ b/web/src/components/config-form/section-configs/audio_transcription.ts @@ -45,7 +45,6 @@ const audioTranscription: SectionConfigOverrides = { fieldMessages: [ { key: "genai-provider-ignores-local-settings", - health: (ctx) => ctx.fullConfig.audio_transcription?.enabled === true, field: "device", messageKey: "configMessages.audioTranscription.genaiProviderSelected", severity: "info", diff --git a/web/src/components/config-form/sectionExtras/NotificationsSettingsExtras.tsx b/web/src/components/config-form/sectionExtras/NotificationsSettingsExtras.tsx index de61340735..2644ff89a5 100644 --- a/web/src/components/config-form/sectionExtras/NotificationsSettingsExtras.tsx +++ b/web/src/components/config-form/sectionExtras/NotificationsSettingsExtras.tsx @@ -24,7 +24,7 @@ import { } from "react"; import { useForm } from "react-hook-form"; import { LuCheck, LuExternalLink, LuX } from "react-icons/lu"; -import { CiCircleAlert } from "react-icons/ci"; +import { ConfigFieldMessage } from "../ConfigFieldMessage"; import { Link } from "react-router-dom"; import { toast } from "sonner"; import useSWR from "swr"; @@ -44,8 +44,7 @@ import { import { formatUnixTimestampToDateTime } from "@/utils/dateUtil"; import { use24HourTime } from "@/hooks/use-date-utils"; import FilterSwitch from "@/components/filter/FilterSwitch"; -import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; -import { Trans, useTranslation } from "react-i18next"; +import { useTranslation } from "react-i18next"; import { useDateLocale } from "@/hooks/use-date-locale"; import { useDocDomain } from "@/hooks/use-doc-domain"; import { isPWA } from "@/utils/isPWA"; @@ -467,37 +466,19 @@ export default function NotificationsSettingsExtras({ - - - - {t("notification.notificationUnavailable.title")} - - - -
- - {t("readTheDocumentation", { ns: "common" })}{" "} - - -
-
-
+ diff --git a/web/src/pages/ConfigEditor.tsx b/web/src/pages/ConfigEditor.tsx index d932f5c031..01eefd201e 100644 --- a/web/src/pages/ConfigEditor.tsx +++ b/web/src/pages/ConfigEditor.tsx @@ -287,6 +287,7 @@ function ConfigEditor() {