@trillboards/edge-platform-rtsp 0.2.20 → 0.2.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,12 +23,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.segmentPatternFor = exports.RTSPStreamCameraProvider = exports.volumeFreeBytes = exports.buildSegmentOutputArgs = exports.readVideoStartSec = exports.videoStartSecFromMoov = exports.audioPlanFor = exports.describeAudioSource = exports.aacBitrateFor = exports.InputStreamProbe = exports.parseInputAudioStreamLine = exports.parseInputVideoStreamLine = exports.parseSegmentListCsv = exports.AUDIO_SUSPEND_MAX_MS = exports.AUDIO_SUSPEND_BASE_MS = exports.AUDIO_START_DEADLINE_MS = exports.RETAKE_BACKOFF_MAX_MS = exports.RETAKE_BACKOFF_BASE_MS = exports.RING_RESYNC_MS = exports.RING_FREE_FRACTION = exports.SEGMENT_SUSPEND_MAX_MS = exports.SEGMENT_SUSPEND_BASE_MS = exports.CLOSED_SEGMENTS_DIRNAME = exports.RECORDABLE_CODECS = exports.streamSessionStatsFor = exports.acquirePersistentSessionSlot = exports.__resetStreamAdmissionForTests = exports.reserveLiveViewStreamSession = exports.FrameRing = exports.MjpegFrameSplitter = exports.jpegFrameEnd = exports.JPEG_MALFORMED = exports.JPEG_NEED_MORE = exports.DEFAULT_MAX_STREAM_SESSIONS_PER_SOURCE = exports.RtspStreamStaleError = exports.RtspStreamAdmissionError = void 0;
26
+ exports.segmentPatternFor = exports.RTSPStreamCameraProvider = exports.sortOffLoop = exports.ringIoInFlightForTests = exports.HW_PROBE_REPORT_DELAY_MS = exports.KEYFRAME_JITTER_MS = exports.KEYFRAME_MAX_INTERVAL_MS = exports.volumeFreeBytes = exports.buildSegmentOutputArgs = exports.readVideoStartSec = exports.videoStartSecFromMoov = exports.audioPlanFor = exports.describeAudioSource = exports.aacBitrateFor = exports.InputStreamProbe = exports.parseInputAudioStreamLine = exports.parseInputVideoStreamLine = exports.parseSegmentListCsv = exports.AUDIO_SUSPEND_MAX_MS = exports.AUDIO_SUSPEND_BASE_MS = exports.AUDIO_START_DEADLINE_MS = exports.RETAKE_BACKOFF_MAX_MS = exports.RETAKE_BACKOFF_BASE_MS = exports.RING_IO_IN_FLIGHT_MAX = exports.RING_SLICE_MS = exports.RING_SYNC_ENTRIES_MAX = exports.RING_RESYNC_JITTER = exports.RING_RESYNC_MS = exports.RING_FREE_FRACTION = exports.SEGMENT_SUSPEND_MAX_MS = exports.SEGMENT_SUSPEND_BASE_MS = exports.CLOSED_SEGMENTS_DIRNAME = exports.RECORDABLE_CODECS = exports.streamSessionStatsFor = exports.acquirePersistentSessionSlot = exports.__resetStreamAdmissionForTests = exports.reserveLiveViewStreamSession = exports.FrameRing = exports.MjpegFrameSplitter = exports.jpegFrameEnd = exports.JPEG_MALFORMED = exports.JPEG_NEED_MORE = exports.DEFAULT_MAX_STREAM_SESSIONS_PER_SOURCE = exports.RtspStreamStaleError = exports.RtspStreamAdmissionError = void 0;
27
27
  const child_process_1 = require("child_process");
28
28
  const events_1 = require("events");
29
29
  const fs = __importStar(require("fs"));
30
30
  const os = __importStar(require("os"));
31
31
  const path = __importStar(require("path"));
32
+ const perf_hooks_1 = require("perf_hooks");
32
33
  const RTSPCameraProvider_1 = require("./RTSPCameraProvider");
33
34
  const ffmpegStderr_1 = require("./ffmpegStderr");
34
35
  /**
@@ -1002,11 +1003,42 @@ const SEGMENT_OUTPUT_FAILURE_RE = /(Failed to open segment[^\n]*|No space left o
1002
1003
  /** The ring may hold at most this fraction of the volume's free bytes (unless the caller sets `maxBytes`). */
1003
1004
  exports.RING_FREE_FRACTION = 0.1;
1004
1005
  /**
1005
- * How often the ring's in-memory index is rebuilt from disk. Between rebuilds
1006
+ * How often the ring's in-memory index is checked against disk. Between checks
1006
1007
  * every close updates it, so a ring of thousands of short segments costs one
1007
1008
  * statfs and no directory listing per close.
1008
1009
  */
1009
1010
  exports.RING_RESYNC_MS = 10 * 60000;
1011
+ /**
1012
+ * Each ring checks itself at RING_RESYNC_MS x [1 - this, 1 + this), drawn
1013
+ * afresh every time, so rings opened together drift apart instead of walking
1014
+ * their disks in the same second.
1015
+ */
1016
+ exports.RING_RESYNC_JITTER = 0.25;
1017
+ /**
1018
+ * A ring directory of at most this many entries (segments and their sidecars)
1019
+ * is listed, read and stat'ed in one synchronous pass. A bigger one is walked
1020
+ * off the event loop: an alert camera's week of 10-s segments is 60,480
1021
+ * segments plus their sidecars, and on Texas's store PC one synchronous pass
1022
+ * over such a ring held every camera's looks (plan v7 W8: the looks froze at
1023
+ * every ring check, and at every start).
1024
+ */
1025
+ exports.RING_SYNC_ENTRIES_MAX = 512;
1026
+ /**
1027
+ * The longest one ring step (an eviction pass, a slice of a walk) holds the
1028
+ * event loop before it yields. Small, because the steps of every ring queued
1029
+ * for the same turn of the loop run back to back: three alert rings starting
1030
+ * together hold it for about three slices.
1031
+ */
1032
+ exports.RING_SLICE_MS = 8;
1033
+ /**
1034
+ * At most this many file-system operations of ring reads (a listing, a stat,
1035
+ * a sidecar read, a delete) in flight at once across every ring in the
1036
+ * process. Each ring's read has one in flight at a time; this caps rings read
1037
+ * together (every ring starts its read when the connector starts). libuv's pool
1038
+ * is 4 threads, shared with segment uploads, sidecar writes and every API
1039
+ * call's address lookup, so the reads never take more than half of it.
1040
+ */
1041
+ exports.RING_IO_IN_FLIGHT_MAX = 2;
1010
1042
  /** First wait before a feed the recorder refused retakes a session; doubles to the max. */
1011
1043
  exports.RETAKE_BACKOFF_BASE_MS = 60000;
1012
1044
  exports.RETAKE_BACKOFF_MAX_MS = 3600000;
@@ -1498,6 +1530,42 @@ function normalizeSegmentRecording(cfg) {
1498
1530
  label,
1499
1531
  };
1500
1532
  }
1533
+ const LEGACY_CAPTURE = Object.freeze({ selectFirst: false, hwDecode: false, keyframesOnly: false });
1534
+ /**
1535
+ * ffmpeg's words for a GPU (d3d11va) decode that does not work on this feed. The last three
1536
+ * are what ffmpeg 6.0 prints when the device cannot be created at session start, and it then
1537
+ * exits instead of falling back (Windows CI, run 44054959691, 2026-11-17):
1538
+ * Device creation failed: -1313558101.
1539
+ * No device available for decoder: device type d3d11va needed for codec hevc.
1540
+ * Device setup failed for decoder on input stream #0:0 : Unknown error occurred
1541
+ * Without them a GPU session that cannot start restarts on the GPU for ever.
1542
+ */
1543
+ const HW_DECODE_FAILURE_RE = new RegExp('(Failed setup for format d3d11[^\\n]*|hwaccel initialisation returned error[^\\n]*'
1544
+ + '|Failed to (?:create|get) (?:D3D11|decoder)[^\\n]*|Device creation failed[^\\n]*'
1545
+ + '|No device available for decoder[^\\n]*|Device setup failed for decoder[^\\n]*)', 'i');
1546
+ /**
1547
+ * The longest keyframe interval a keyframe-only camera may have. A look reads the
1548
+ * newest decoded frame, and with keyframe-only decode that frame is up to one
1549
+ * interval old, so a longer interval would age every look past the 3 s frame-age
1550
+ * bar. Measured on this camera's own keyframe-only output, never assumed.
1551
+ */
1552
+ exports.KEYFRAME_MAX_INTERVAL_MS = 2000;
1553
+ /**
1554
+ * Timestamp jitter a nominal interval may carry: Texas's 2 s-GOP cameras measure
1555
+ * 1.99-2.09 s between keyframes (recorded segments, 2026-11-17).
1556
+ */
1557
+ exports.KEYFRAME_JITTER_MS = 100;
1558
+ /** Inter-keyframe gaps measured before a camera's keyframe verdict is final. */
1559
+ const KEYFRAME_SAMPLES = 4;
1560
+ /**
1561
+ * When the GPU decode probe runs report-only, after the first open: late enough
1562
+ * to stay clear of the connector's start-up work. The probe encodes and decodes
1563
+ * two 2 s synthetic clips, once per process.
1564
+ */
1565
+ exports.HW_PROBE_REPORT_DELAY_MS = 300000;
1566
+ function sameCapturePlan(a, b) {
1567
+ return a.selectFirst === b.selectFirst && a.hwDecode === b.hwDecode && a.keyframesOnly === b.keyframesOnly;
1568
+ }
1501
1569
  function writeFileAtomic(file, body) {
1502
1570
  const tmp = `${file}.tmp`;
1503
1571
  fs.writeFileSync(tmp, body);
@@ -1505,7 +1573,186 @@ function writeFileAtomic(file, body) {
1505
1573
  }
1506
1574
  function readSidecar(file) {
1507
1575
  try {
1508
- const raw = JSON.parse(fs.readFileSync(file, 'utf8'));
1576
+ return parseSidecar(fs.readFileSync(file, 'utf8'));
1577
+ }
1578
+ catch {
1579
+ return null;
1580
+ }
1581
+ }
1582
+ /** readSidecar, with the read off the event loop. */
1583
+ async function readSidecarOffLoop(file) {
1584
+ try {
1585
+ return parseSidecar(await fs.promises.readFile(file, 'utf8'));
1586
+ }
1587
+ catch {
1588
+ return null;
1589
+ }
1590
+ }
1591
+ /** Binary search: the first position in `items` (sorted by file) whose file is >= `file`. */
1592
+ function ringItemPosition(items, file) {
1593
+ let lo = 0;
1594
+ let hi = items.length;
1595
+ while (lo < hi) {
1596
+ const mid = (lo + hi) >>> 1;
1597
+ if (items[mid].file < file)
1598
+ lo = mid + 1;
1599
+ else
1600
+ hi = mid;
1601
+ }
1602
+ return lo;
1603
+ }
1604
+ /** Insert in name (= capture time) order; a file already present is left as is. */
1605
+ function insertRingItem(items, file, bytes) {
1606
+ if (!items.length || items[items.length - 1].file < file) {
1607
+ items.push({ file, bytes });
1608
+ return;
1609
+ }
1610
+ const at = ringItemPosition(items, file);
1611
+ if (at < items.length && items[at].file === file)
1612
+ return;
1613
+ items.splice(at, 0, { file, bytes });
1614
+ }
1615
+ /** More than `max` entries in `dir` (false when it cannot be read), without listing the rest. */
1616
+ function dirExceeds(dir, max) {
1617
+ let handle = null;
1618
+ try {
1619
+ handle = fs.opendirSync(dir);
1620
+ for (let i = 0; i <= max; i += 1) {
1621
+ if (handle.readSync() === null)
1622
+ return false;
1623
+ }
1624
+ return true;
1625
+ }
1626
+ catch {
1627
+ return false;
1628
+ }
1629
+ finally {
1630
+ try {
1631
+ handle?.closeSync();
1632
+ }
1633
+ catch {
1634
+ /* already closed */
1635
+ }
1636
+ }
1637
+ }
1638
+ /**
1639
+ * Every name in `dir`, read a batch at a time off the event loop, handed to
1640
+ * `onName` as it is read; nothing when the directory cannot be read.
1641
+ */
1642
+ async function listDirOffLoop(dir, onName) {
1643
+ let handle;
1644
+ try {
1645
+ handle = await fs.promises.opendir(dir, { bufferSize: 256 });
1646
+ }
1647
+ catch {
1648
+ return;
1649
+ }
1650
+ try {
1651
+ for await (const entry of handle)
1652
+ onName(entry.name);
1653
+ }
1654
+ catch {
1655
+ /* a listing cut short keeps what it read; the next check reads the rest */
1656
+ }
1657
+ }
1658
+ /** Binary search in names sorted by code unit. */
1659
+ function sortedHas(names, name) {
1660
+ let lo = 0;
1661
+ let hi = names.length;
1662
+ while (lo < hi) {
1663
+ const mid = (lo + hi) >>> 1;
1664
+ if (names[mid] < name)
1665
+ lo = mid + 1;
1666
+ else
1667
+ hi = mid;
1668
+ }
1669
+ return lo < names.length && names[lo] === name;
1670
+ }
1671
+ function yieldToLoop() {
1672
+ return new Promise((resolve) => setImmediate(resolve));
1673
+ }
1674
+ let ringIoInFlight = 0;
1675
+ const ringIoWaiting = [];
1676
+ /**
1677
+ * Run one ring-read file-system operation once fewer than
1678
+ * RING_IO_IN_FLIGHT_MAX are in flight across every ring in the process; the
1679
+ * rings waiting take turns in the order they asked.
1680
+ */
1681
+ async function ringIo(op) {
1682
+ if (ringIoInFlight < exports.RING_IO_IN_FLIGHT_MAX) {
1683
+ ringIoInFlight += 1;
1684
+ }
1685
+ else {
1686
+ // The slot is handed over by the release below, still counted.
1687
+ await new Promise((resolve) => ringIoWaiting.push(resolve));
1688
+ }
1689
+ try {
1690
+ return await op();
1691
+ }
1692
+ finally {
1693
+ const next = ringIoWaiting.shift();
1694
+ if (next)
1695
+ next();
1696
+ else
1697
+ ringIoInFlight -= 1;
1698
+ }
1699
+ }
1700
+ /** Ring-read operations in flight now, and those waiting for a turn (tests). */
1701
+ function ringIoInFlightForTests() {
1702
+ return { now: ringIoInFlight, waiting: ringIoWaiting.length };
1703
+ }
1704
+ exports.ringIoInFlightForTests = ringIoInFlightForTests;
1705
+ /**
1706
+ * Sort names without holding the event loop for longer than a slice. NTFS
1707
+ * lists a directory in name order, so a store PC's listing costs one pass to
1708
+ * confirm; any other order (ext4 hashes it) is sorted in runs that are merged
1709
+ * a slice at a time: 60,480 names in hash order take ~140 ms in one sort().
1710
+ */
1711
+ async function sortOffLoop(names) {
1712
+ let ordered = true;
1713
+ for (let i = 1; i < names.length && ordered; i += 1)
1714
+ ordered = names[i - 1] <= names[i];
1715
+ if (ordered)
1716
+ return names;
1717
+ const RUN = 2048;
1718
+ let runs = [];
1719
+ for (let i = 0; i < names.length; i += RUN) {
1720
+ runs.push(names.slice(i, i + RUN).sort());
1721
+ await yieldToLoop();
1722
+ }
1723
+ while (runs.length > 1) {
1724
+ const merged = [];
1725
+ for (let r = 0; r < runs.length; r += 2) {
1726
+ const a = runs[r];
1727
+ const b = runs[r + 1];
1728
+ if (!b) {
1729
+ merged.push(a);
1730
+ continue;
1731
+ }
1732
+ const out = new Array(a.length + b.length);
1733
+ let i = 0;
1734
+ let j = 0;
1735
+ let k = 0;
1736
+ let sliceStartMs = perf_hooks_1.performance.now();
1737
+ while (i < a.length || j < b.length) {
1738
+ out[k] = j >= b.length || (i < a.length && a[i] <= b[j]) ? a[i++] : b[j++];
1739
+ k += 1;
1740
+ if ((k & 1023) === 0 && perf_hooks_1.performance.now() - sliceStartMs >= exports.RING_SLICE_MS) {
1741
+ await yieldToLoop();
1742
+ sliceStartMs = perf_hooks_1.performance.now();
1743
+ }
1744
+ }
1745
+ merged.push(out);
1746
+ }
1747
+ runs = merged;
1748
+ await yieldToLoop();
1749
+ }
1750
+ return runs[0] ?? [];
1751
+ }
1752
+ exports.sortOffLoop = sortOffLoop;
1753
+ function parseSidecar(text) {
1754
+ try {
1755
+ const raw = JSON.parse(text);
1509
1756
  const nums = [raw.startSec, raw.endSec, raw.captureStartMs, raw.captureEndMs, raw.bytes];
1510
1757
  if (raw.version !== 1 || nums.some((n) => typeof n !== 'number' || !Number.isFinite(n)))
1511
1758
  return null;
@@ -1543,7 +1790,9 @@ function errorText(err) {
1543
1790
  *
1544
1791
  * Events: `'segment'` (RecordedSegment) for every closed recording segment,
1545
1792
  * `'segment_evicted'` (EvictedSegment) when the ring deletes one undelivered,
1546
- * and `'opening'` at the start of every `open()`.
1793
+ * `'ring_indexed'` (RingIndexedEvent) when the ring's figures were just read
1794
+ * from disk (before anything is evicted against them), and `'opening'` at the
1795
+ * start of every `open()`.
1547
1796
  */
1548
1797
  class RTSPStreamCameraProvider extends events_1.EventEmitter {
1549
1798
  constructor(config) {
@@ -1585,13 +1834,25 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1585
1834
  // ── segment recording ────────────────────────────────────────────────────
1586
1835
  this.segmentConfig = null;
1587
1836
  /**
1588
- * The ring's closed segments, oldest first: rebuilt from disk at most every
1837
+ * The ring's closed segments, oldest first: checked against disk about every
1589
1838
  * `RING_RESYNC_MS` and kept current by every move in between, so a ring of
1590
1839
  * thousands of 10-s segments is not re-listed and re-stat'ed at every close.
1591
- * Files a consumer deleted since the last rebuild are dropped when the
1592
- * eviction walk reaches them (ENOENT) and by the next rebuild.
1840
+ * Files a consumer deleted since the last check are dropped when the
1841
+ * eviction walk reaches them (ENOENT) and by the next check.
1593
1842
  */
1594
1843
  this.ringIndex = null;
1844
+ /**
1845
+ * A walk of the ring off the event loop (a big ring's first index, its
1846
+ * periodic check, or its open-time recovery), with what moved in and out of
1847
+ * the ring while it ran; the current index serves until it swaps in.
1848
+ */
1849
+ this.ringWalk = null;
1850
+ /** Bumped by close(): a walk from before it never swaps in. */
1851
+ this.ringGeneration = 0;
1852
+ /** The next slice of an eviction that ran out of its RING_SLICE_MS. */
1853
+ this.ringEvictImmediate = null;
1854
+ /** The directory whose ring figures (`ringSegments`, `ringBytes`) were last read from disk. */
1855
+ this.ringIndexedDir = null;
1595
1856
  /** 0 = an ordinary feed; higher holds its persistent session ahead of lower ones. */
1596
1857
  this.sessionPriority = 0;
1597
1858
  /** No live review needs this feed (setIdle); with no recording it runs per-grab. */
@@ -1670,6 +1931,20 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1670
1931
  this.lastErrorMessage = null;
1671
1932
  /** Resolvers waiting for the next frame. Woken by `admitFrame`. */
1672
1933
  this.frameWaiters = [];
1934
+ // ── capture pipeline (plan v7 W8) ────────────────────────────────────────
1935
+ this.captureMode = 'legacy';
1936
+ /** The host's GPU decode probe, once it has answered. */
1937
+ this.hwProbe = null;
1938
+ this.hwProbeStarted = false;
1939
+ /** The report-only probe's start timer (see `hardwareDecodeProbeDelayMs`). */
1940
+ this.hwProbeTimer = null;
1941
+ /** This feed's GPU decode stopped working after the probe passed: decode in software. */
1942
+ this.hwDecodeFailure = null;
1943
+ /** The keyframe-only verdict, measured on this camera's own keyframe-only output. */
1944
+ this.keyframes = { verdict: 'unmeasured', intervalMs: null, lastAtMs: null, gaps: [], timer: null };
1945
+ this.captureReconfigures = 0;
1946
+ /** When the frame `getFrame` last returned arrived: honest capture time for keyframe-only looks. */
1947
+ this.lastServedFrameAtMs = null;
1673
1948
  if (!config?.rtspUrl)
1674
1949
  throw new Error('rtspUrl is required for RTSPStreamCameraProvider');
1675
1950
  this.rtspUrl = config.rtspUrl;
@@ -1700,6 +1975,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1700
1975
  this.persistentRequested = config.persistent !== false;
1701
1976
  this.spawnFn = config.spawnFn ?? child_process_1.spawn;
1702
1977
  this.now = config.now ?? Date.now;
1978
+ this.random = config.random ?? Math.random;
1703
1979
  this.onLog = config.onLog;
1704
1980
  this.ring = new FrameRing(Math.max(1, Math.trunc(config.ringMaxFrames ?? DEFAULT_RING_MAX_FRAMES)), Math.max(1, Math.trunc(config.ringMaxBytes ?? DEFAULT_RING_MAX_BYTES)));
1705
1981
  this.splitter = new MjpegFrameSplitter(Math.max(1, Math.trunc(config.maxFrameBytes ?? DEFAULT_MAX_FRAME_BYTES)));
@@ -1722,6 +1998,11 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1722
1998
  // No filesystem work here: a provider that is built and never opened (a
1723
1999
  // test, a manager that is configured and then discarded) must leave no trace.
1724
2000
  this.segmentConfig = normalizeSegmentRecording(config.segmentRecording ?? null);
2001
+ this.hardwareDecodeProbe = config.hardwareDecodeProbe ?? RTSPCameraProvider_1.hardwareDecodeProbeFor;
2002
+ this.hwProbeDelayMs = Number.isFinite(config.hardwareDecodeProbeDelayMs)
2003
+ ? Math.trunc(config.hardwareDecodeProbeDelayMs) : exports.HW_PROBE_REPORT_DELAY_MS;
2004
+ if (config.capturePipeline !== undefined)
2005
+ this.setCapturePipeline(config.capturePipeline);
1725
2006
  }
1726
2007
  on(event, listener) {
1727
2008
  return super.on(event, listener);
@@ -1763,6 +2044,57 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1763
2044
  // retakes its session, and one that stops gives it back.
1764
2045
  this.reconcileIdle();
1765
2046
  }
2047
+ /**
2048
+ * How look frames are made (plan v7 W8, `CapturePipelineMode`). A change the
2049
+ * running session's argv does not already satisfy restarts it once, after its
2050
+ * first frame, exactly as a recording change does; the restart is counted in
2051
+ * `capture.reconfigures`, never as a fault. An unknown mode is ignored.
2052
+ */
2053
+ setCapturePipeline(mode) {
2054
+ if (!RTSPCameraProvider_1.CAPTURE_PIPELINE_MODES.includes(mode) || mode === this.captureMode)
2055
+ return;
2056
+ this.captureMode = mode;
2057
+ if (mode === 'select_first_hw' || mode === 'keyframes_hw')
2058
+ this.startHardwareDecodeProbe();
2059
+ this.reconcileCapture();
2060
+ }
2061
+ /**
2062
+ * Run this host's GPU decode probe once (memoized per ffmpeg binary, so one real
2063
+ * probe per process): started by a `_hw` mode, or report-only after open (see
2064
+ * `hardwareDecodeProbeDelayMs`). The probe decodes its own synthetic clips, never
2065
+ * a camera; `planCapture` uses the answer only under a `_hw` mode.
2066
+ */
2067
+ startHardwareDecodeProbe() {
2068
+ if (this.hwProbeTimer) {
2069
+ clearTimeout(this.hwProbeTimer);
2070
+ this.hwProbeTimer = null;
2071
+ }
2072
+ if (this.hwProbeStarted)
2073
+ return;
2074
+ this.hwProbeStarted = true;
2075
+ this.hardwareDecodeProbe(this.ffmpegPath).then((probe) => {
2076
+ this.hwProbe = probe;
2077
+ // Logged where it decides something; a report-only answer is read off capture stats.
2078
+ if (this.captureMode === 'select_first_hw' || this.captureMode === 'keyframes_hw') {
2079
+ this.log(probe.state === 'passed' ? 'info' : 'warn', `GPU decode probe: ${probe.state} (h264 ${probe.h264}, hevc ${probe.hevc}, ${probe.durationMs} ms)` +
2080
+ (probe.detail ? `: ${probe.detail}` : ''));
2081
+ }
2082
+ this.reconcileCapture();
2083
+ }, () => undefined);
2084
+ }
2085
+ getCapturePipeline() {
2086
+ return this.captureMode;
2087
+ }
2088
+ /**
2089
+ * When the frame the last `getFrame()` returned arrived from ffmpeg, for a
2090
+ * keyframe-only session only (null otherwise). A keyframe-only look reads a
2091
+ * frame up to one keyframe interval old, and stamping it with the time it was
2092
+ * read would hide that age; every other mode serves a frame at most one
2093
+ * look interval old and keeps the stamp it always had.
2094
+ */
2095
+ servedFrameCapturedAtMs() {
2096
+ return this.session?.capture.keyframesOnly ? this.lastServedFrameAtMs : null;
2097
+ }
1766
2098
  /**
1767
2099
  * Rank this feed for its recorder's persistent sessions. 0 is an ordinary
1768
2100
  * feed; the connector raises a camera with an enabled alert to 1, because
@@ -1784,6 +2116,14 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1784
2116
  getSessionPriority() {
1785
2117
  return this.sessionPriority;
1786
2118
  }
2119
+ /**
2120
+ * The ring's closed segments, oldest first, from its in-memory index: no
2121
+ * listing, no stat. Null until the ring's first read lands.
2122
+ */
2123
+ ringFiles() {
2124
+ const index = this.currentRingIndex();
2125
+ return index ? index.items.map((item) => item.file) : null;
2126
+ }
1787
2127
  /**
1788
2128
  * Take a segment another recorder of this camera closed into THIS feed's ring
1789
2129
  * — the camera's main-stream recorder (`MainStreamSession`), which records
@@ -1899,6 +2239,14 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
1899
2239
  if (this.opened)
1900
2240
  return;
1901
2241
  const generation = ++this.openGeneration;
2242
+ if (!this.hwProbeStarted && this.hwProbeTimer === null && this.hwProbeDelayMs >= 0) {
2243
+ this.hwProbeTimer = setTimeout(() => {
2244
+ this.hwProbeTimer = null;
2245
+ this.startHardwareDecodeProbe();
2246
+ }, this.hwProbeDelayMs);
2247
+ if (typeof this.hwProbeTimer.unref === 'function')
2248
+ this.hwProbeTimer.unref();
2249
+ }
1902
2250
  // A controller attached at construction starts here, so a provider that is
1903
2251
  // built and never opened costs nothing. Its listener may set the recording
1904
2252
  // config synchronously, which the first session below then carries.
@@ -2100,7 +2448,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2100
2448
  const fresh = this.takeFreshFrame();
2101
2449
  if (fresh)
2102
2450
  return fresh;
2103
- const arrived = await this.waitForFrame(this.frameWaitMs);
2451
+ const arrived = await this.waitForFrame(this.frameWaitForCapture());
2104
2452
  // The session was handed back (the feed went idle) while this call waited.
2105
2453
  if (this.fallback)
2106
2454
  return this.fallbackFrame();
@@ -2155,9 +2503,24 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2155
2503
  this.openGeneration += 1;
2156
2504
  this.startingUp = false;
2157
2505
  this.opened = false;
2506
+ if (this.hwProbeTimer) {
2507
+ clearTimeout(this.hwProbeTimer);
2508
+ this.hwProbeTimer = null;
2509
+ }
2158
2510
  this.leaveSessionQueue();
2159
- // A re-open rebuilds the ring index from disk once.
2160
- this.ringIndex = null;
2511
+ // A re-open checks the ring against disk once, at its next eviction pass;
2512
+ // the index serves until then. A walk under way is dropped, and a recovery
2513
+ // it had not finished runs again at the next open.
2514
+ this.ringGeneration += 1;
2515
+ if (this.ringWalk?.recover)
2516
+ this.segmentsRecoveredFor = null;
2517
+ this.ringWalk = null;
2518
+ if (this.ringEvictImmediate) {
2519
+ clearImmediate(this.ringEvictImmediate);
2520
+ this.ringEvictImmediate = null;
2521
+ }
2522
+ if (this.ringIndex)
2523
+ this.ringIndex.resyncAtMs = this.now();
2161
2524
  if (this.restartTimer) {
2162
2525
  clearTimeout(this.restartTimer);
2163
2526
  this.restartTimer = null;
@@ -2213,6 +2576,14 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2213
2576
  frameAgeMs: newest ? this.now() - newest.atMs : null,
2214
2577
  lastErrorMessage: this.lastErrorMessage,
2215
2578
  segments: this.segmentStats(),
2579
+ capture: {
2580
+ mode: this.captureMode,
2581
+ session: this.session ? { ...this.session.capture } : null,
2582
+ hardwareDecode: this.hwProbe ? { ...this.hwProbe } : null,
2583
+ hardwareDecodeFailure: this.hwDecodeFailure,
2584
+ keyframes: { verdict: this.keyframes.verdict, intervalMs: this.keyframes.intervalMs },
2585
+ reconfigures: this.captureReconfigures,
2586
+ },
2216
2587
  };
2217
2588
  }
2218
2589
  // ── internals ────────────────────────────────────────────────────────────
@@ -2420,6 +2791,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2420
2791
  return null;
2421
2792
  this.lastServedSeq = newest.seq;
2422
2793
  this.framesServed += 1;
2794
+ this.lastServedFrameAtMs = newest.atMs;
2423
2795
  return newest.bytes;
2424
2796
  }
2425
2797
  waitForFrame(timeoutMs) {
@@ -2459,6 +2831,8 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2459
2831
  return; // counted as rejected inside the ring
2460
2832
  this.framesDecoded += 1;
2461
2833
  this.lastFrameAtMs = at;
2834
+ if (this.session)
2835
+ this.noteKeyframe(this.session, at);
2462
2836
  this.wakeWaiters();
2463
2837
  }
2464
2838
  /**
@@ -2514,11 +2888,12 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2514
2888
  };
2515
2889
  }
2516
2890
  /**
2517
- * The session's argv. With no `segmentOutput` it is byte-identical to the
2518
- * look-only session this provider has always run; with one, the second
2519
- * output (`buildSegmentOutputArgs`) follows `pipe:1`.
2891
+ * The session's argv. With no `segmentOutput` and the `legacy` capture plan
2892
+ * it is byte-identical to the look-only session this provider has always run;
2893
+ * with a segment output, the second output (`buildSegmentOutputArgs`) follows
2894
+ * `pipe:1`. `capture` defaults to what the current mode plans.
2520
2895
  */
2521
- buildArgs(segmentOutput) {
2896
+ buildArgs(segmentOutput, capture = this.planCapture()) {
2522
2897
  const input = this.buildInputArgs();
2523
2898
  return {
2524
2899
  args: [
@@ -2527,18 +2902,26 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2527
2902
  // DEFAULT_DECODER_THREADS: one thread is both smaller and faster here.
2528
2903
  '-threads',
2529
2904
  String(this.decoderThreads),
2905
+ // Plan v7 W8 (`CapturePipelineMode`). Decoder options, before the input,
2906
+ // so they bind to the decoder only: the segment output copies the
2907
+ // input's packets and never decodes, so its bytes do not move.
2908
+ ...(capture.hwDecode ? ['-hwaccel', 'd3d11va'] : []),
2909
+ ...(capture.keyframesOnly ? ['-skip_frame', 'nokey'] : []),
2530
2910
  ...input.args,
2531
2911
  '-an',
2532
- // crop (mosaic tile) FIRST, then the aspect-preserving scale+pad — the
2533
- // SAME carve math as the one-shot path, imported rather than restated,
2534
- // so a persistent feed and a per-grab feed can never disagree about
2535
- // what a frame of this camera looks like.
2912
+ // crop (mosaic tile), then the aspect-preserving scale+pad — the SAME
2913
+ // carve math as the one-shot path, imported rather than restated, so a
2914
+ // persistent feed and a per-grab feed can never disagree about what a
2915
+ // frame of this camera looks like.
2536
2916
  //
2537
- // `fps` LAST and deliberately not an output `-r`: the filter is driven
2538
- // by arriving input frames, so a frozen source emits nothing instead of
2539
- // duplicating the last frame into a lie about freshness.
2917
+ // The look rate is a `select`, deliberately not an output `-r`: the
2918
+ // filter is driven by arriving input frames, so a frozen source emits
2919
+ // nothing instead of duplicating the last frame into a lie about
2920
+ // freshness. Legacy runs it LAST; select_first runs it FIRST, so only
2921
+ // the frames a look can read are carved (same frames: `select` reads
2922
+ // only timestamps, which crop/scale/pad pass through).
2540
2923
  '-vf',
2541
- `${(0, RTSPCameraProvider_1.buildVideoFilter)(this.width, this.height, this.crop)},select='isnan(prev_selected_t)+gte(t-prev_selected_t,${1 / this.streamFps}-0.000001)'`,
2924
+ (0, RTSPCameraProvider_1.buildLookFilter)(this.width, this.height, this.crop, 1 / this.streamFps, capture.selectFirst),
2542
2925
  ...(this.ffmpegMajorVersion >= 5 ? ['-fps_mode', 'passthrough'] : ['-vsync', '0']),
2543
2926
  '-f',
2544
2927
  'mjpeg',
@@ -2564,6 +2947,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2564
2947
  startSession() {
2565
2948
  return new Promise((resolve, reject) => {
2566
2949
  const recording = this.prepareSessionRecording();
2950
+ const capture = this.planCapture();
2567
2951
  const { args, cleanup } = this.buildArgs(recording
2568
2952
  ? {
2569
2953
  csvPath: recording.csvPath,
@@ -2573,9 +2957,14 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2573
2957
  minSegDuration: recording.plan.minSegDuration,
2574
2958
  audio: recording.plan.audio,
2575
2959
  }
2576
- : null);
2960
+ : null, capture);
2577
2961
  this.sessionStarts += 1;
2578
2962
  this.splitter.reset();
2963
+ // A keyframe gap is measured within one session, never across a restart.
2964
+ this.keyframes.lastAtMs = null;
2965
+ if (this.keyframes.timer)
2966
+ clearTimeout(this.keyframes.timer);
2967
+ this.keyframes.timer = null;
2579
2968
  let proc;
2580
2969
  try {
2581
2970
  proc = this.spawnFn(this.ffmpegPath, args, { stdio: ['ignore', 'pipe', 'pipe'] });
@@ -2595,6 +2984,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2595
2984
  probe: new InputStreamProbe(),
2596
2985
  streamInfo: null,
2597
2986
  recording,
2987
+ capture,
2598
2988
  superseded: false,
2599
2989
  detached: false,
2600
2990
  ended: false,
@@ -2639,6 +3029,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2639
3029
  proc.stderr?.on('data', (chunk) => {
2640
3030
  const text = chunk.toString();
2641
3031
  stderrBuffer.append(text);
3032
+ this.noteCaptureStderr(session, text);
2642
3033
  if (!session.probe.done) {
2643
3034
  const info = session.probe.push(text);
2644
3035
  if (info)
@@ -2658,6 +3049,10 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2658
3049
  // in 26 ms and freezing it exits 0 after ~10 s with "Failed reading
2659
3050
  // RTSP data: End of file". Both are restarts, never successes.
2660
3051
  const stderr = stderrBuffer.text();
3052
+ // The whole session's stderr, not one pipe chunk: a failure line split across two
3053
+ // chunks matched neither. Recorded before the restart below plans its capture, so
3054
+ // the next session decodes in software.
3055
+ this.noteCaptureStderr(session, stderr, false);
2661
3056
  const summary = (0, RTSPCameraProvider_1.summarizeFfmpegStderr)(stderr);
2662
3057
  const message = summary || `ffmpeg stream session exited with code ${code}`;
2663
3058
  // A session stopped on purpose to change its outputs ended because we
@@ -2861,8 +3256,11 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2861
3256
  return;
2862
3257
  const want = this.planRecording();
2863
3258
  const have = session.recording ? session.recording.plan : null;
2864
- if (this.sessionSatisfies(have, want))
3259
+ if (this.sessionSatisfies(have, want)) {
3260
+ // Recording is in line; the look output may still need a change (plan v7 W8).
3261
+ this.reconcileCapture();
2865
3262
  return;
3263
+ }
2866
3264
  this.segCounters.reconfigures += 1;
2867
3265
  this.log('info', want
2868
3266
  ? `RTSP stream session restarting to record ${want.segmentSeconds}s segments` +
@@ -2873,6 +3271,130 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
2873
3271
  this.session = null;
2874
3272
  this.killSession(session);
2875
3273
  }
3274
+ /** What the next session's look output should be (`CapturePipelineMode`). */
3275
+ planCapture() {
3276
+ const mode = this.captureMode;
3277
+ if (mode === 'legacy')
3278
+ return LEGACY_CAPTURE;
3279
+ const codec = this.streamInfo?.codec ?? null;
3280
+ const probe = this.hwProbe;
3281
+ // The GPU decodes only a codec this host's probe decoded there, and only once
3282
+ // ffmpeg has named this camera's codec: a first session decodes in software.
3283
+ const hwDecode = (mode === 'select_first_hw' || mode === 'keyframes_hw') && this.hwDecodeFailure === null
3284
+ && probe?.state === 'passed' && (codec === 'h264' ? probe.h264 : codec === 'hevc' ? probe.hevc : false);
3285
+ const keyframesOnly = (mode === 'keyframes' || mode === 'keyframes_hw') && this.keyframes.verdict !== 'too_long';
3286
+ return { selectFirst: true, hwDecode, keyframesOnly };
3287
+ }
3288
+ /**
3289
+ * Bring the live session's look output in line with `planCapture` — by
3290
+ * restarting it once, after its first frame, never with another reconfigure
3291
+ * in flight: the same rules as a recording change, counted apart.
3292
+ */
3293
+ reconcileCapture() {
3294
+ const session = this.session;
3295
+ if (!session || !this.opened || this.fallback || this.startingUp)
3296
+ return;
3297
+ if (session.firstFrameAtMs === null || this.reconfigurePendingFor)
3298
+ return;
3299
+ const want = this.planCapture();
3300
+ if (sameCapturePlan(session.capture, want))
3301
+ return;
3302
+ this.captureReconfigures += 1;
3303
+ this.log('info', `RTSP stream session restarting to capture with ${want.selectFirst ? 'select first' : 'legacy filters'}`
3304
+ + `${want.keyframesOnly ? ', keyframes only' : ''}${want.hwDecode ? ', GPU decode (d3d11va)' : ''}`);
3305
+ session.superseded = true;
3306
+ this.reconfigurePendingFor = session;
3307
+ this.session = null;
3308
+ this.killSession(session);
3309
+ }
3310
+ /** How long `getFrame` waits for a new frame: one keyframe interval more on a keyframe-only session. */
3311
+ frameWaitForCapture() {
3312
+ return this.session?.capture.keyframesOnly
3313
+ ? Math.max(this.frameWaitMs, exports.KEYFRAME_MAX_INTERVAL_MS + this.frameWaitMs)
3314
+ : this.frameWaitMs;
3315
+ }
3316
+ /**
3317
+ * Measure this camera's keyframe interval on its own keyframe-only output: every
3318
+ * one of KEYFRAME_SAMPLES gaps at or under KEYFRAME_MAX_INTERVAL_MS (plus
3319
+ * KEYFRAME_JITTER_MS) keeps it. A gap past 1.5x the bar at any time, or three
3320
+ * gaps in a row past the bar once kept, puts the camera back on full decode
3321
+ * (select first) for the rest of this provider's life: a "smart" encoder that
3322
+ * stretches its GOP in a still scene (Texas cams 3/5/7/8/10/11: 12-28 s gaps
3323
+ * between 2-4 s ones) would otherwise age every look past the 3 s bar while it
3324
+ * waits. Called for every frame of the live session.
3325
+ */
3326
+ noteKeyframe(session, atMs) {
3327
+ if (!session.capture.keyframesOnly)
3328
+ return;
3329
+ const k = this.keyframes;
3330
+ if (k.verdict === 'unmeasured')
3331
+ k.verdict = 'measuring';
3332
+ if (k.lastAtMs !== null && atMs >= k.lastAtMs) {
3333
+ const gap = atMs - k.lastAtMs;
3334
+ if (k.verdict === 'measuring') {
3335
+ k.gaps.push(gap);
3336
+ if (k.gaps.length >= KEYFRAME_SAMPLES) {
3337
+ k.intervalMs = Math.max(...k.gaps);
3338
+ this.keyframeVerdict(k.intervalMs <= exports.KEYFRAME_MAX_INTERVAL_MS + exports.KEYFRAME_JITTER_MS ? 'kept' : 'too_long');
3339
+ }
3340
+ }
3341
+ else if (k.verdict === 'kept') {
3342
+ if (gap > exports.KEYFRAME_MAX_INTERVAL_MS * 1.5) {
3343
+ k.intervalMs = gap;
3344
+ this.keyframeVerdict('too_long');
3345
+ }
3346
+ else {
3347
+ k.gaps = gap > exports.KEYFRAME_MAX_INTERVAL_MS + exports.KEYFRAME_JITTER_MS ? [...k.gaps, gap].slice(-3) : [];
3348
+ if (k.gaps.length >= 3)
3349
+ this.keyframeVerdict('too_long');
3350
+ }
3351
+ }
3352
+ }
3353
+ k.lastAtMs = atMs;
3354
+ if (k.timer)
3355
+ clearTimeout(k.timer);
3356
+ k.timer = null;
3357
+ if (k.verdict === 'measuring') {
3358
+ k.timer = setTimeout(() => {
3359
+ k.timer = null;
3360
+ if (k.verdict === 'measuring' && this.session === session)
3361
+ this.keyframeVerdict('too_long');
3362
+ }, Math.round(exports.KEYFRAME_MAX_INTERVAL_MS * 1.5));
3363
+ if (typeof k.timer.unref === 'function')
3364
+ k.timer.unref();
3365
+ }
3366
+ }
3367
+ keyframeVerdict(verdict) {
3368
+ const k = this.keyframes;
3369
+ if (k.timer)
3370
+ clearTimeout(k.timer);
3371
+ k.timer = null;
3372
+ k.verdict = verdict;
3373
+ k.gaps = [];
3374
+ this.log(verdict === 'kept' ? 'info' : 'warn', verdict === 'kept'
3375
+ ? `keyframe-only decode kept: keyframes every ${k.intervalMs} ms`
3376
+ : `keyframe-only decode off for this camera: keyframes further apart than ${exports.KEYFRAME_MAX_INTERVAL_MS} ms`
3377
+ + (k.intervalMs !== null ? ` (${k.intervalMs} ms)` : ''));
3378
+ if (verdict === 'too_long')
3379
+ this.reconcileCapture();
3380
+ }
3381
+ /**
3382
+ * A GPU decode that stopped working on this feed after the probe passed —
3383
+ * ffmpeg says so on stderr and falls back to software, or errors. The feed
3384
+ * decodes in software for the rest of this provider's life.
3385
+ */
3386
+ noteCaptureStderr(session, text, reconcile = true) {
3387
+ if (!session.capture.hwDecode || this.hwDecodeFailure !== null)
3388
+ return;
3389
+ const m = HW_DECODE_FAILURE_RE.exec(text);
3390
+ if (!m)
3391
+ return;
3392
+ this.hwDecodeFailure = (0, RTSPCameraProvider_1.summarizeFfmpegStderr)(m[1], 200);
3393
+ this.log('warn', `GPU decode failed on this feed; decoding in software: ${this.hwDecodeFailure}`);
3394
+ // At the session's end its restart plans the capture; a reconcile there would restart twice.
3395
+ if (reconcile && this.session === session)
3396
+ this.reconcileCapture();
3397
+ }
2876
3398
  onSessionFirstFrame(session) {
2877
3399
  session.firstFrameAtMs = this.now();
2878
3400
  this.reconcileRecording();
@@ -3391,8 +3913,20 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
3391
3913
  catch {
3392
3914
  return; // nothing recorded here yet
3393
3915
  }
3394
- // closed/ FIRST: recovering a work directory below moves files INTO it.
3395
3916
  const closedDir = path.join(cfg.dir, exports.CLOSED_SEGMENTS_DIRNAME);
3917
+ const running = this.ringWalk && this.ringWalk.dir === cfg.dir ? this.ringWalk : null;
3918
+ if (running || this.ringIsBig(cfg.dir)) {
3919
+ // A big ring (an alert camera's week of 10-s segments): its closed
3920
+ // segments are handed out by a walk off the event loop. The walk is
3921
+ // registered first, so what the work directories move in below is
3922
+ // already handed out and the walk skips it; they are recovered now,
3923
+ // before this process's first session writes a work directory of its own.
3924
+ const walk = running ?? this.startRingWalk(cfg.dir, true);
3925
+ walk.recover = true;
3926
+ this.recoverWorkDirs(cfg.dir, names);
3927
+ return;
3928
+ }
3929
+ // closed/ FIRST: recovering a work directory below moves files INTO it.
3396
3930
  let closed = [];
3397
3931
  try {
3398
3932
  closed = fs.readdirSync(closedDir).sort();
@@ -3465,21 +3999,25 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
3465
3999
  this.countSegment(segment);
3466
4000
  this.emitSegment(segment);
3467
4001
  }
4002
+ this.recoverWorkDirs(cfg.dir, names);
4003
+ this.enforceRing();
4004
+ }
4005
+ /** Every session work directory an earlier process left under `dir` (never the live one's). */
4006
+ recoverWorkDirs(dir, names) {
3468
4007
  const live = this.session?.recording?.workDir ?? null;
3469
- for (const name of names.sort()) {
4008
+ for (const name of [...names].sort()) {
3470
4009
  if (!name.startsWith(SESSION_WORKDIR_PREFIX))
3471
4010
  continue;
3472
- const workDir = path.join(cfg.dir, name);
4011
+ const workDir = path.join(dir, name);
3473
4012
  if (workDir === live)
3474
4013
  continue;
3475
4014
  try {
3476
- this.recoverWorkDir(cfg.dir, workDir);
4015
+ this.recoverWorkDir(dir, workDir);
3477
4016
  }
3478
4017
  catch (err) {
3479
4018
  this.segCounters.lastError = `segment recovery: ${errorText(err)}`;
3480
4019
  }
3481
4020
  }
3482
- this.enforceRing();
3483
4021
  }
3484
4022
  recoverWorkDir(dir, workDir) {
3485
4023
  let text = '';
@@ -3539,7 +4077,21 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
3539
4077
  if (!leftBehind)
3540
4078
  this.removeWorkDir(workDir, true);
3541
4079
  }
3542
- /** List and stat the ring once; every close until the next rebuild updates the result in memory. */
4080
+ /** The ring index of the configured directory, when one is in. */
4081
+ currentRingIndex() {
4082
+ const cfg = this.segmentConfig;
4083
+ const index = this.ringIndex;
4084
+ return cfg && index && index.dir === cfg.dir ? index : null;
4085
+ }
4086
+ /** A ring too big to list and stat in one synchronous pass (RING_SYNC_ENTRIES_MAX). */
4087
+ ringIsBig(dir) {
4088
+ return dirExceeds(path.join(dir, exports.CLOSED_SEGMENTS_DIRNAME), exports.RING_SYNC_ENTRIES_MAX);
4089
+ }
4090
+ /**
4091
+ * List and stat a small ring in one synchronous pass and swap it in; every
4092
+ * close until the next check updates it in memory. A big ring is read by
4093
+ * `startRingWalk` instead.
4094
+ */
3543
4095
  rebuildRingIndex(dir) {
3544
4096
  const closedDir = path.join(dir, exports.CLOSED_SEGMENTS_DIRNAME);
3545
4097
  let names;
@@ -3559,23 +4111,274 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
3559
4111
  /* delivered and deleted under us */
3560
4112
  }
3561
4113
  }
3562
- this.ringIndex = { dir, items, rebuiltAtMs: this.now() };
4114
+ this.installRingIndex(dir, items);
3563
4115
  return items;
3564
4116
  }
4117
+ /**
4118
+ * Swap in a ring index read from disk, due its next check RING_RESYNC_MS
4119
+ * (jittered by RING_RESYNC_JITTER) from now, and announce the ring's figures.
4120
+ * `'ring_indexed'` fires BEFORE anything is evicted against them, so a caller
4121
+ * that sizes the ring from the bytes it holds (the box buffer's share)
4122
+ * re-sizes it first.
4123
+ */
4124
+ installRingIndex(dir, items) {
4125
+ const nowMs = this.now();
4126
+ const raw = this.random();
4127
+ const draw = Number.isFinite(raw) ? Math.min(1, Math.max(0, raw)) : 0.5;
4128
+ const spread = 1 - exports.RING_RESYNC_JITTER + 2 * exports.RING_RESYNC_JITTER * draw;
4129
+ this.ringIndex = { dir, items, rebuiltAtMs: nowMs, resyncAtMs: nowMs + Math.floor(exports.RING_RESYNC_MS * spread) };
4130
+ this.ringIndexedDir = dir;
4131
+ let bytes = 0;
4132
+ for (const item of items)
4133
+ bytes += item.bytes;
4134
+ this.segCounters.ringSegments = items.length;
4135
+ this.segCounters.ringBytes = bytes;
4136
+ try {
4137
+ this.emit('ring_indexed', { dir, segments: items.length, bytes });
4138
+ }
4139
+ catch (err) {
4140
+ this.log('warn', `segment recording: a 'ring_indexed' listener threw: ${errorText(err)}`);
4141
+ }
4142
+ }
4143
+ /**
4144
+ * Read a big ring off the event loop: list it a batch at a time, stat only
4145
+ * the files the current index does not know, keep serving that index
4146
+ * meanwhile, and swap at the end, with whatever closed or was evicted while
4147
+ * it ran reconciled in. With `recover`, then hand out what an earlier
4148
+ * process left, its sidecars read off the loop too. One walk at a time.
4149
+ */
4150
+ startRingWalk(dir, recover) {
4151
+ const walk = {
4152
+ dir,
4153
+ generation: this.ringGeneration,
4154
+ recover,
4155
+ added: new Map(),
4156
+ removed: new Set(),
4157
+ done: Promise.resolve(),
4158
+ };
4159
+ this.ringWalk = walk;
4160
+ walk.done = this.runRingWalk(walk).catch((err) => {
4161
+ if (this.ringWalk === walk)
4162
+ this.ringWalk = null;
4163
+ this.segCounters.lastError = `ring read: ${errorText(err)}`;
4164
+ });
4165
+ return walk;
4166
+ }
4167
+ async runRingWalk(walk) {
4168
+ const current = () => this.ringWalk === walk && walk.generation === this.ringGeneration;
4169
+ const closedDir = path.join(walk.dir, exports.CLOSED_SEGMENTS_DIRNAME);
4170
+ const segments = [];
4171
+ const sidecars = [];
4172
+ const partials = [];
4173
+ // One turn for the whole listing: its batch reads run one at a time.
4174
+ await ringIo(() => listDirOffLoop(closedDir, (name) => {
4175
+ const lower = name.toLowerCase();
4176
+ if (lower.endsWith('.mp4'))
4177
+ segments.push(name);
4178
+ else if (lower.endsWith('.json'))
4179
+ sidecars.push(name);
4180
+ else if (lower.endsWith('.tmp'))
4181
+ partials.push(name);
4182
+ }));
4183
+ if (!current())
4184
+ return;
4185
+ const ordered = await sortOffLoop(segments);
4186
+ if (!current())
4187
+ return;
4188
+ // What the index already knows keeps its bytes: only files new to it are stat'ed.
4189
+ const known = this.ringIndex && this.ringIndex.dir === walk.dir ? this.ringIndex.items.slice() : [];
4190
+ const items = [];
4191
+ let k = 0;
4192
+ let sliceStartMs = perf_hooks_1.performance.now();
4193
+ for (const name of ordered) {
4194
+ const file = path.join(closedDir, name);
4195
+ while (k < known.length && known[k].file < file)
4196
+ k += 1;
4197
+ if (k < known.length && known[k].file === file) {
4198
+ items.push(known[k]);
4199
+ }
4200
+ else {
4201
+ try {
4202
+ items.push({ file, bytes: (await ringIo(() => fs.promises.stat(file))).size });
4203
+ }
4204
+ catch {
4205
+ /* delivered and deleted under us */
4206
+ }
4207
+ if (!current())
4208
+ return;
4209
+ }
4210
+ if (perf_hooks_1.performance.now() - sliceStartMs >= exports.RING_SLICE_MS) {
4211
+ await yieldToLoop();
4212
+ if (!current())
4213
+ return;
4214
+ sliceStartMs = perf_hooks_1.performance.now();
4215
+ }
4216
+ }
4217
+ if (this.segmentConfig?.dir !== walk.dir) {
4218
+ this.ringWalk = null; // the ring moved while it was read
4219
+ return;
4220
+ }
4221
+ // What moved while the walk ran wins over what it read.
4222
+ const merged = walk.removed.size ? items.filter((item) => !walk.removed.has(item.file)) : items;
4223
+ for (const [file, bytes] of walk.added) {
4224
+ if (!walk.removed.has(file))
4225
+ insertRingItem(merged, file, bytes);
4226
+ }
4227
+ const recover = walk.recover;
4228
+ if (!recover)
4229
+ this.ringWalk = null;
4230
+ this.installRingIndex(walk.dir, merged);
4231
+ if (this.segmentsRecoveredFor === walk.dir)
4232
+ this.enforceRing();
4233
+ if (!recover || !current())
4234
+ return;
4235
+ await this.recoverRingOffLoop(walk, closedDir, ordered, sidecars, partials, current);
4236
+ }
4237
+ /**
4238
+ * `recoverSegments` for a big ring, after its index is in: hand out every
4239
+ * closed segment an earlier process left, oldest first, its sidecar read off
4240
+ * the loop; delete what can never be indexed (no sidecar), leftover `.tmp`
4241
+ * files and sidecars whose segment is gone. A segment this process moved in
4242
+ * while the walk ran was handed out as it moved, and is skipped.
4243
+ */
4244
+ async recoverRingOffLoop(walk, closedDir, segments, sidecars, partials, current) {
4245
+ let sliceStartMs = perf_hooks_1.performance.now();
4246
+ const yieldIfDue = async () => {
4247
+ if (perf_hooks_1.performance.now() - sliceStartMs < exports.RING_SLICE_MS)
4248
+ return current();
4249
+ await yieldToLoop();
4250
+ sliceStartMs = perf_hooks_1.performance.now();
4251
+ return current();
4252
+ };
4253
+ for (const name of segments) {
4254
+ const file = path.join(closedDir, name);
4255
+ const index = this.ringIndex && this.ringIndex.dir === walk.dir ? this.ringIndex.items : [];
4256
+ const at = ringItemPosition(index, file);
4257
+ // Not in the index: gone before the walk could stat it, or already evicted.
4258
+ if (at >= index.length || index[at].file !== file || walk.added.has(file)) {
4259
+ if (!(await yieldIfDue()))
4260
+ return;
4261
+ continue;
4262
+ }
4263
+ const bytes = index[at].bytes;
4264
+ const metaFile = file.replace(/\.mp4$/i, '.json');
4265
+ const sidecar = await ringIo(() => readSidecarOffLoop(metaFile));
4266
+ if (!current())
4267
+ return;
4268
+ if (!sidecar) {
4269
+ // No capture time, so it can never be indexed.
4270
+ try {
4271
+ await ringIo(() => fs.promises.unlink(file));
4272
+ this.segCounters.partialsDeleted += 1;
4273
+ this.dropRingFile(walk.dir, file);
4274
+ try {
4275
+ await ringIo(() => fs.promises.unlink(metaFile));
4276
+ }
4277
+ catch {
4278
+ /* absent or inaccessible */
4279
+ }
4280
+ }
4281
+ catch {
4282
+ /* evicted meanwhile, or busy: best-effort */
4283
+ }
4284
+ if (!current())
4285
+ return;
4286
+ continue;
4287
+ }
4288
+ const segment = {
4289
+ file,
4290
+ metaFile,
4291
+ startSec: sidecar.startSec,
4292
+ endSec: sidecar.endSec,
4293
+ captureStartMs: sidecar.captureStartMs,
4294
+ captureEndMs: sidecar.captureEndMs,
4295
+ bytes,
4296
+ stream: sidecar.stream,
4297
+ recovered: true,
4298
+ label: sidecar.label ?? null,
4299
+ };
4300
+ this.countSegment(segment);
4301
+ this.emitSegment(segment);
4302
+ if (!(await yieldIfDue()))
4303
+ return;
4304
+ }
4305
+ for (const name of partials) {
4306
+ try {
4307
+ await ringIo(() => fs.promises.unlink(path.join(closedDir, name)));
4308
+ }
4309
+ catch {
4310
+ /* best-effort */
4311
+ }
4312
+ if (!current())
4313
+ return;
4314
+ }
4315
+ for (const name of sidecars) {
4316
+ if (sortedHas(segments, name.replace(/\.json$/i, '.mp4'))) {
4317
+ if (!(await yieldIfDue()))
4318
+ return;
4319
+ continue;
4320
+ }
4321
+ const metaFile = path.join(closedDir, name);
4322
+ const file = metaFile.replace(/\.json$/i, '.mp4');
4323
+ // Listed mid-move: its segment landed after the listing.
4324
+ if (walk.added.has(file))
4325
+ continue;
4326
+ try {
4327
+ await ringIo(() => fs.promises.access(file));
4328
+ continue;
4329
+ }
4330
+ catch {
4331
+ /* its segment is gone: the sidecar is an orphan */
4332
+ }
4333
+ try {
4334
+ await ringIo(() => fs.promises.unlink(metaFile));
4335
+ }
4336
+ catch {
4337
+ /* best-effort */
4338
+ }
4339
+ if (!current())
4340
+ return;
4341
+ }
4342
+ this.ringWalk = null;
4343
+ if (this.segmentsRecoveredFor === walk.dir)
4344
+ this.enforceRing();
4345
+ }
3565
4346
  /** A file just moved into `closed/`: keep the index in name (= capture time) order. */
3566
4347
  noteRingFile(dir, file, bytes) {
4348
+ const walk = this.ringWalk;
4349
+ if (walk && walk.dir === dir)
4350
+ walk.added.set(file, bytes);
3567
4351
  const index = this.ringIndex;
3568
4352
  if (!index || index.dir !== dir)
3569
4353
  return;
3570
- const items = index.items;
3571
- if (!items.length || items[items.length - 1].file < file) {
3572
- items.push({ file, bytes });
4354
+ insertRingItem(index.items, file, bytes);
4355
+ }
4356
+ /** A file left `closed/` (evicted, or found gone): a walk under way must not swap it back in. */
4357
+ noteRingRemoval(dir, file) {
4358
+ const walk = this.ringWalk;
4359
+ if (walk && walk.dir === dir)
4360
+ walk.removed.add(file);
4361
+ }
4362
+ /** A file deleted outside an eviction pass: out of the index and of any walk under way. */
4363
+ dropRingFile(dir, file) {
4364
+ this.noteRingRemoval(dir, file);
4365
+ const index = this.ringIndex;
4366
+ if (!index || index.dir !== dir)
3573
4367
  return;
3574
- }
3575
- if (items.some((item) => item.file === file))
4368
+ const at = ringItemPosition(index.items, file);
4369
+ if (at < index.items.length && index.items[at].file === file)
4370
+ index.items.splice(at, 1);
4371
+ }
4372
+ /** The rest of an eviction pass that ran out of its RING_SLICE_MS, from where it stopped. */
4373
+ scheduleRingEviction(from) {
4374
+ if (this.ringEvictImmediate)
3576
4375
  return;
3577
- const at = items.findIndex((item) => item.file > file);
3578
- items.splice(at < 0 ? items.length : at, 0, { file, bytes });
4376
+ const generation = this.ringGeneration;
4377
+ this.ringEvictImmediate = setImmediate(() => {
4378
+ this.ringEvictImmediate = null;
4379
+ if (generation === this.ringGeneration)
4380
+ this.enforceRing(from);
4381
+ });
3579
4382
  }
3580
4383
  /**
3581
4384
  * Hold the closed segments to min(ringSeconds x measured bytes/s, the cap),
@@ -3584,16 +4387,39 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
3584
4387
  * the volume's free bytes. A file the OS will not let us delete (Windows,
3585
4388
  * mid-upload) is skipped, never waited on; a file already gone (its consumer
3586
4389
  * delivered and deleted it) leaves the index without counting as evicted.
4390
+ *
4391
+ * Never holds the event loop for a big ring: until its first read lands
4392
+ * nothing is evicted (the figures would leave its own files out), its check
4393
+ * against disk runs in the background, and a pass longer than RING_SLICE_MS
4394
+ * finishes in later slices, resuming past the files it could not delete.
3587
4395
  */
3588
- enforceRing() {
4396
+ enforceRing(from = 0) {
3589
4397
  const cfg = this.segmentConfig;
3590
4398
  if (!cfg)
3591
4399
  return;
3592
- const index = this.ringIndex;
3593
- const items = !index || index.dir !== cfg.dir || this.now() - index.rebuiltAtMs >= exports.RING_RESYNC_MS
3594
- ? this.rebuildRingIndex(cfg.dir)
3595
- : index.items;
3596
- let total = items.reduce((n, item) => n + item.bytes, 0);
4400
+ const index = this.currentRingIndex();
4401
+ const walking = this.ringWalk !== null && this.ringWalk.dir === cfg.dir;
4402
+ if (!index || (this.now() >= index.resyncAtMs && !walking)) {
4403
+ if (walking)
4404
+ return; // the walk enforces when it lands
4405
+ if (this.ringIsBig(cfg.dir)) {
4406
+ this.startRingWalk(cfg.dir, false);
4407
+ if (!index)
4408
+ return;
4409
+ }
4410
+ else {
4411
+ // A small ring: read in one pass. Its listeners have seen the figures
4412
+ // (and may have re-sized the ring) before anything below runs.
4413
+ this.rebuildRingIndex(cfg.dir);
4414
+ if (this.currentRingIndex())
4415
+ this.enforceRing();
4416
+ return;
4417
+ }
4418
+ }
4419
+ const items = this.currentRingIndex().items;
4420
+ let total = 0;
4421
+ for (const item of items)
4422
+ total += item.bytes;
3597
4423
  const freeBytes = this.freeBytesFn(cfg.dir);
3598
4424
  const bytesPerSec = this.measuredBytesPerSec();
3599
4425
  const byRing = bytesPerSec === null ? null : Math.floor(cfg.ringSeconds * bytesPerSec);
@@ -3602,43 +4428,54 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
3602
4428
  : freeBytes === null ? null : Math.floor(exports.RING_FREE_FRACTION * freeBytes);
3603
4429
  const limit = byRing === null ? byFree : byFree === null ? byRing : Math.min(byRing, byFree);
3604
4430
  if (limit !== null) {
3605
- let i = 0;
4431
+ const sliceStartMs = perf_hooks_1.performance.now();
4432
+ let i = Math.min(Math.max(0, from), items.length);
3606
4433
  while (total > limit && i < items.length) {
3607
4434
  const item = items[i];
3608
4435
  const metaFile = item.file.replace(/\.mp4$/i, '.json');
4436
+ let evicted = false;
3609
4437
  try {
3610
4438
  fs.unlinkSync(item.file);
4439
+ evicted = true;
3611
4440
  }
3612
4441
  catch (err) {
3613
4442
  if (err?.code === 'ENOENT') {
3614
4443
  total -= item.bytes;
3615
4444
  items.splice(i, 1);
4445
+ this.noteRingRemoval(cfg.dir, item.file);
3616
4446
  try {
3617
4447
  fs.unlinkSync(metaFile);
3618
4448
  }
3619
4449
  catch {
3620
4450
  /* its consumer took both */
3621
4451
  }
3622
- continue;
3623
4452
  }
3624
- i += 1;
3625
- continue;
3626
- }
3627
- try {
3628
- fs.unlinkSync(metaFile);
3629
- }
3630
- catch {
3631
- /* best-effort */
4453
+ else {
4454
+ i += 1;
4455
+ }
3632
4456
  }
3633
- items.splice(i, 1);
3634
- total -= item.bytes;
3635
- this.segCounters.evicted += 1;
3636
- this.segCounters.evictedBytes += item.bytes;
3637
- try {
3638
- this.emit('segment_evicted', { file: item.file, metaFile, bytes: item.bytes });
4457
+ if (evicted) {
4458
+ try {
4459
+ fs.unlinkSync(metaFile);
4460
+ }
4461
+ catch {
4462
+ /* best-effort */
4463
+ }
4464
+ items.splice(i, 1);
4465
+ total -= item.bytes;
4466
+ this.noteRingRemoval(cfg.dir, item.file);
4467
+ this.segCounters.evicted += 1;
4468
+ this.segCounters.evictedBytes += item.bytes;
4469
+ try {
4470
+ this.emit('segment_evicted', { file: item.file, metaFile, bytes: item.bytes });
4471
+ }
4472
+ catch (err) {
4473
+ this.log('warn', `segment recording: a 'segment_evicted' listener threw: ${errorText(err)}`);
4474
+ }
3639
4475
  }
3640
- catch (err) {
3641
- this.log('warn', `segment recording: a 'segment_evicted' listener threw: ${errorText(err)}`);
4476
+ if (total > limit && i < items.length && perf_hooks_1.performance.now() - sliceStartMs >= exports.RING_SLICE_MS) {
4477
+ this.scheduleRingEviction(i);
4478
+ break;
3642
4479
  }
3643
4480
  }
3644
4481
  }
@@ -3668,6 +4505,7 @@ class RTSPStreamCameraProvider extends events_1.EventEmitter {
3668
4505
  partialsDeleted: c.partialsDeleted,
3669
4506
  ringSegments: c.ringSegments,
3670
4507
  ringBytes: c.ringBytes,
4508
+ ringIndexed: cfg !== null && this.ringIndexedDir === cfg.dir,
3671
4509
  ringLimitBytes: c.ringLimitBytes,
3672
4510
  measuredBytesPerSec: bytesPerSec === null ? null : Math.round(bytesPerSec),
3673
4511
  freeBytes: c.freeBytes,