mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-12-06 13:34:13 +03:00
Some checks failed
CI / AMD64 Build (push) Has been cancelled
CI / ARM Build (push) Has been cancelled
CI / Jetson Jetpack 6 (push) Has been cancelled
CI / AMD64 Extra Build (push) Has been cancelled
CI / ARM Extra Build (push) Has been cancelled
CI / Synaptics Build (push) Has been cancelled
CI / Assemble and push default build (push) Has been cancelled
* Implement renaming in model editing dialog * add transcription faq * remove incorrect constraint for viewer as username should be able to change anyone's role other than admin * Don't save redundant state changes * prevent crash when a camera doesn't support onvif imaging service required for focus support * Fine tune behavior * Stop redundant go2rtc stream metadata requests and defer audio information to allow bandwidth for image requests * Improve cleanup logic for capture process --------- Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
91 lines
2.7 KiB
TypeScript
91 lines
2.7 KiB
TypeScript
import { baseUrl } from "@/api/baseUrl";
|
|
import { useCallback, useEffect, useState, useMemo } from "react";
|
|
import useSWR from "swr";
|
|
import { LiveStreamMetadata } from "@/types/live";
|
|
|
|
const FETCH_TIMEOUT_MS = 10000;
|
|
const DEFER_DELAY_MS = 2000;
|
|
|
|
/**
|
|
* Hook that fetches go2rtc stream metadata with deferred loading.
|
|
*
|
|
* Metadata fetching is delayed to prevent blocking initial page load
|
|
* and camera image requests.
|
|
*
|
|
* @param streamNames - Array of stream names to fetch metadata for
|
|
* @returns Object containing stream metadata keyed by stream name
|
|
*/
|
|
export default function useDeferredStreamMetadata(streamNames: string[]) {
|
|
const [fetchEnabled, setFetchEnabled] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const timeoutId = setTimeout(() => {
|
|
setFetchEnabled(true);
|
|
}, DEFER_DELAY_MS);
|
|
|
|
return () => clearTimeout(timeoutId);
|
|
}, []);
|
|
|
|
const swrKey = useMemo(() => {
|
|
if (!fetchEnabled || streamNames.length === 0) return null;
|
|
// Use spread to avoid mutating the original array
|
|
return `deferred-streams:${[...streamNames].sort().join(",")}`;
|
|
}, [fetchEnabled, streamNames]);
|
|
|
|
const fetcher = useCallback(async (key: string) => {
|
|
// Extract stream names from key (remove prefix)
|
|
const names = key.replace("deferred-streams:", "").split(",");
|
|
|
|
const promises = names.map(async (streamName) => {
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
|
|
try {
|
|
const response = await fetch(
|
|
`${baseUrl}api/go2rtc/streams/${streamName}`,
|
|
{
|
|
priority: "low",
|
|
signal: controller.signal,
|
|
},
|
|
);
|
|
clearTimeout(timeoutId);
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
return { streamName, data };
|
|
}
|
|
return { streamName, data: null };
|
|
} catch (error) {
|
|
clearTimeout(timeoutId);
|
|
if ((error as Error).name !== "AbortError") {
|
|
// eslint-disable-next-line no-console
|
|
console.error(`Failed to fetch metadata for ${streamName}:`, error);
|
|
}
|
|
return { streamName, data: null };
|
|
}
|
|
});
|
|
|
|
const results = await Promise.allSettled(promises);
|
|
|
|
const metadata: { [key: string]: LiveStreamMetadata } = {};
|
|
results.forEach((result) => {
|
|
if (result.status === "fulfilled" && result.value.data) {
|
|
metadata[result.value.streamName] = result.value.data;
|
|
}
|
|
});
|
|
|
|
return metadata;
|
|
}, []);
|
|
|
|
const { data: metadata = {} } = useSWR<{
|
|
[key: string]: LiveStreamMetadata;
|
|
}>(swrKey, fetcher, {
|
|
revalidateOnFocus: false,
|
|
revalidateOnReconnect: false,
|
|
revalidateIfStale: false,
|
|
dedupingInterval: 60000,
|
|
});
|
|
|
|
return metadata;
|
|
}
|