Files
frigate/web/src/utils/streamIssues.ts
T
05a6233b5a Miscellaneous Fixes (#25007)
* don't crash review maintainer when a manual event ends with no active segment

The review maintainer read the label of every manual event message before checking its state, so an end message, which carries no label, raised a KeyError and killed the review process when the camera had no active segment. This happened when a recordings or enabled toggle, a completed manual event, or audio closed the segment of a still-open indefinite event, and on every plate expiry on dedicated LPR cameras with detections disabled. With the process dead, no review items were created and alert/detection-only retention deleted all new recordings until restart. End messages with no active segment now drop the event from the indefinite events and are otherwise ignored.

* Update the default timeout for detection subscriber to avoid blocking indefinitely

* run embeddings requests off the api event loop

set_description, regenerate_review_description, and the chat semantic search tools called the embeddings requestor directly from async handlers, so the API event loop waited on the embeddings process with no timeout. In normal operation the wait is short (about 80 to 140 ms of text embedding with Jina V1 on CPU, and only queue time for review regeneration), so this is mostly defensive. The cases it guards against are a request sent while the embeddings process is still downloading or loading models, which froze the whole UI until it finished, and the embeddings process dying with a request in flight, which left the API unresponsive until Frigate restarted. The requestor calls now run in asyncio.to_thread, matching the other blocking calls in the API.

* Handle other fields for context size from api providers

* fix stream checks missing non-aac audio

The ffprobe API never asked ffprobe for codec_type, so the stream checks could only spot audio and video by codec name. G.711, Opus, and other non-AAC audio showed as no audio, and H.265 detect streams showed an unknown resolution. ffprobe_stream now requests codec_type, and the stream check matches tracks on it alone.

* fix timezone-dependent http tests

The devcontainer mounts the host's /etc/localtime over zoneinfo/Etc/UTC, so the API's ZoneInfo("UTC") used the host's local zone and the recordings summary and event time_range tests failed at any hour. The review summary tests also built their expected day key from the local date, so they failed whenever the local day differed from the UTC day. BaseTestHttp now writes a minimal UTC zone file to a temp dir and puts it first on the zoneinfo search path, which works without the tzdata package, and the review tests convert their expected day keys to UTC.

---------

Co-authored-by: Nicolas Mowen <nickmowen213@gmail.com>
2026-11-19 06:38:44 -07:00

290 lines
7.8 KiB
TypeScript

import type { TFunction } from "i18next";
import { parseRestreamStreamName } from "@/components/config-form/theme/fields/streamSource";
import type { CameraBrand, StreamRole, TestResult } from "@/types/cameraWizard";
export type StreamIssue = {
type: "good" | "warning" | "error";
message: string;
/** stable key for the rule that fired, for filtering and tests */
rule: string;
};
export type StreamIssueInput = {
url: string;
roles: StreamRole[];
brand?: CameraBrand;
reolinkProtocol?: "http-flv" | "rtsp" | null;
useFfmpeg?: boolean;
restream?: boolean;
testResult?: TestResult;
};
type ProbeStream = {
codec_type?: string;
codec_name?: string;
width?: number;
height?: number;
avg_frame_rate?: string;
};
export type FfprobeEntry = {
return_code?: number;
stdout?: { streams?: ProbeStream[] } | string;
/** the backend sends a list of non-empty lines on failure */
stderr?: string | string[];
};
function errorText(stderr: string | string[] | undefined): string {
const text = Array.isArray(stderr) ? stderr.join("\n") : stderr;
return text?.trim() || "Unknown error";
}
/** The human-readable end of an ffprobe error; the first lines are plumbing. */
export function lastErrorLine(error: string | undefined): string {
const lines = (error ?? "")
.split("\n")
.map((line) => line.trim())
.filter(Boolean);
return lines[lines.length - 1] ?? "";
}
/** Parse one entry of the ffprobe API response the way the wizard does. */
export function ffprobeToTestResult(
entry: FfprobeEntry | undefined,
): TestResult {
if (!entry || entry.return_code !== 0 || typeof entry.stdout !== "object") {
return { success: false, error: errorText(entry?.stderr) };
}
const streams = entry.stdout?.streams ?? [];
const videoStream = streams.find((s) => s.codec_type === "video");
const audioStream = streams.find((s) => s.codec_type === "audio");
const resolution = videoStream
? `${videoStream.width}x${videoStream.height}`
: undefined;
const fps = videoStream?.avg_frame_rate
? parseFloat(videoStream.avg_frame_rate.split("/")[0]) /
parseFloat(videoStream.avg_frame_rate.split("/")[1])
: undefined;
return {
success: true,
resolution,
videoCodec: videoStream?.codec_name,
audioCodec: audioStream?.codec_name,
fps: fps && !isNaN(fps) ? fps : undefined,
};
}
/** The wizard's Stream Validation rules, unchanged, over plain input. */
export function getStreamIssues(
input: StreamIssueInput,
t: TFunction,
): StreamIssue[] {
const result: StreamIssue[] = [];
const { roles, testResult } = input;
if (input.brand === "reolink") {
const streamUrl = input.url.toLowerCase();
if (streamUrl.startsWith("rtsp://") && input.reolinkProtocol !== "rtsp") {
result.push({
type: "warning",
rule: "reolink-rtsp",
message: t("cameraWizard.step4.issues.brands.reolink-rtsp", {
ns: "views/settings",
}),
});
}
if (streamUrl.startsWith("http://") && !input.useFfmpeg) {
result.push({
type: "warning",
rule: "reolink-http",
message: t("cameraWizard.step4.issues.brands.reolink-http", {
ns: "views/settings",
}),
});
}
}
if (testResult?.videoCodec) {
const videoCodec = testResult.videoCodec.toLowerCase();
if (["h264", "h265", "hevc"].includes(videoCodec)) {
result.push({
type: "good",
rule: "video-codec",
message: t("cameraWizard.step4.issues.videoCodecGood", {
ns: "views/settings",
codec: testResult.videoCodec,
}),
});
}
}
if (roles.includes("record")) {
if (testResult?.audioCodec) {
const audioCodec = testResult.audioCodec.toLowerCase();
if (audioCodec === "aac") {
result.push({
type: "good",
rule: "audio-codec",
message: t("cameraWizard.step4.issues.audioCodecGood", {
ns: "views/settings",
codec: testResult.audioCodec,
}),
});
} else {
result.push({
type: "error",
rule: "audio-codec-record",
message: t("cameraWizard.step4.issues.audioCodecRecordError", {
ns: "views/settings",
}),
});
}
} else {
result.push({
type: "warning",
rule: "no-audio",
message: t("cameraWizard.step4.issues.noAudioWarning", {
ns: "views/settings",
}),
});
}
}
if (roles.includes("audio") && !testResult?.audioCodec) {
result.push({
type: "error",
rule: "audio-required",
message: t("cameraWizard.step4.issues.audioCodecRequired", {
ns: "views/settings",
}),
});
}
if (roles.includes("record") && input.restream) {
result.push({
type: "warning",
rule: "restream",
message: t("cameraWizard.step4.issues.restreamingWarning", {
ns: "views/settings",
}),
});
}
if (roles.includes("detect") && testResult) {
const probedResolution = testResult.resolution;
let probedWidth = 0;
let probedHeight = 0;
if (probedResolution) {
const [w, h] = probedResolution.split("x").map(Number);
if (!isNaN(w) && !isNaN(h)) {
probedWidth = w;
probedHeight = h;
}
}
if (probedWidth <= 0 || probedHeight <= 0) {
result.push({
type: "error",
rule: "resolution-unknown",
message: t("cameraWizard.step4.issues.resolutionUnknown", {
ns: "views/settings",
}),
});
} else {
const minDimension = Math.min(probedWidth, probedHeight);
const maxDimension = Math.max(probedWidth, probedHeight);
if (minDimension > 1080) {
result.push({
type: "warning",
rule: "resolution-high",
message: t("cameraWizard.step4.issues.resolutionHigh", {
ns: "views/settings",
resolution: probedResolution,
}),
});
} else if (maxDimension < 640) {
result.push({
type: "error",
rule: "resolution-low",
message: t("cameraWizard.step4.issues.resolutionLow", {
ns: "views/settings",
resolution: probedResolution,
}),
});
}
}
}
if (
input.brand === "dahua" &&
roles.includes("detect") &&
input.url.includes("subtype=1")
) {
result.push({
type: "warning",
rule: "dahua-substream",
message: t("cameraWizard.step4.issues.dahua.substreamWarning", {
ns: "views/settings",
}),
});
}
if (
input.brand === "hikvision" &&
roles.includes("detect") &&
input.url.includes("/102")
) {
result.push({
type: "warning",
rule: "hikvision-substream",
message: t("cameraWizard.step4.issues.hikvision.substreamWarning", {
ns: "views/settings",
}),
});
}
return result;
}
/**
* For an input that points at a go2rtc restream, find the camera URL behind
* it. Returns undefined when the path is not a restream or the stream is not
* in the go2rtc config. The /config response redacts credentials in these
* sources, so the URL is only good for pattern matching.
*/
export function resolveRestreamSource(
path: string,
streams: Record<string, string | string[]> | undefined,
): { url: string; useFfmpeg: boolean } | undefined {
const name = parseRestreamStreamName(path);
if (!name || !streams) {
return undefined;
}
const configured = streams[name];
const sources = Array.isArray(configured)
? configured
: configured
? [configured]
: [];
const source = sources.find((s) => !s.startsWith(`ffmpeg:${name}`));
if (!source) {
return undefined;
}
if (source.startsWith("ffmpeg:")) {
return {
url: source.slice("ffmpeg:".length).split("#")[0],
useFfmpeg: true,
};
}
return { url: source, useFfmpeg: false };
}