@trillboards/edge-sdk 0.2.244 → 0.2.246

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.
Files changed (106) hide show
  1. package/deploy/detector/bench/detect_frames.py +88 -0
  2. package/deploy/detector/bench/openvino_parity.py +152 -0
  3. package/deploy/detector/pyproject.toml +10 -1
  4. package/deploy/detector/tests/test_actor_tracking.py +32 -0
  5. package/deploy/detector/tests/test_actor_tracking_wire.py +11 -0
  6. package/deploy/detector/tests/test_openvino_engines.py +251 -0
  7. package/deploy/detector/tests/test_server.py +43 -0
  8. package/deploy/detector/tests/test_version_stamp.py +17 -4
  9. package/deploy/detector/trillboards_detector/_version.py +2 -2
  10. package/deploy/detector/trillboards_detector/actor_tracking.py +51 -0
  11. package/deploy/detector/trillboards_detector/cli.py +5 -0
  12. package/deploy/detector/trillboards_detector/detections.py +5 -0
  13. package/deploy/detector/trillboards_detector/execution.py +386 -0
  14. package/deploy/detector/trillboards_detector/loader.py +12 -1
  15. package/deploy/detector/trillboards_detector/registry.py +6 -0
  16. package/deploy/detector/trillboards_detector/server.py +41 -0
  17. package/deploy/detector/trillboards_detector/session.py +81 -3
  18. package/deploy/detector/trillboards_detector/track_records.py +113 -0
  19. package/deploy/windows/ci/test-alert-window-field.ps1 +28 -6
  20. package/deploy/windows/ci/test-detector-looks-field.ps1 +31 -7
  21. package/deploy/windows/ci/test-detector-reinstall.ps1 +474 -0
  22. package/deploy/windows/setup-detector.ps1 +122 -29
  23. package/dist/ActorTracking.d.ts +38 -0
  24. package/dist/ActorTracking.d.ts.map +1 -1
  25. package/dist/ActorTracking.js +40 -0
  26. package/dist/ActorTracking.js.map +1 -1
  27. package/dist/AlertClips.d.ts +12 -3
  28. package/dist/AlertClips.d.ts.map +1 -1
  29. package/dist/AlertClips.js +52 -11
  30. package/dist/AlertClips.js.map +1 -1
  31. package/dist/ChannelProber.d.ts +15 -0
  32. package/dist/ChannelProber.d.ts.map +1 -1
  33. package/dist/ChannelProber.js +61 -0
  34. package/dist/ChannelProber.js.map +1 -1
  35. package/dist/CloudSensingManager.d.ts.map +1 -1
  36. package/dist/CloudSensingManager.js +6 -1
  37. package/dist/CloudSensingManager.js.map +1 -1
  38. package/dist/ConnectorStatusClient.d.ts +9 -1
  39. package/dist/ConnectorStatusClient.d.ts.map +1 -1
  40. package/dist/ConnectorStatusClient.js +3 -0
  41. package/dist/ConnectorStatusClient.js.map +1 -1
  42. package/dist/DurableVlmCandidateSpool.d.ts +11 -0
  43. package/dist/DurableVlmCandidateSpool.d.ts.map +1 -1
  44. package/dist/DurableVlmCandidateSpool.js +31 -2
  45. package/dist/DurableVlmCandidateSpool.js.map +1 -1
  46. package/dist/EdgeAgent.js +1 -1
  47. package/dist/FrameGroupingShadow.d.ts +57 -0
  48. package/dist/FrameGroupingShadow.d.ts.map +1 -1
  49. package/dist/FrameGroupingShadow.js +123 -5
  50. package/dist/FrameGroupingShadow.js.map +1 -1
  51. package/dist/LocalDetectorClient.d.ts +25 -1
  52. package/dist/LocalDetectorClient.d.ts.map +1 -1
  53. package/dist/LocalDetectorClient.js +60 -0
  54. package/dist/LocalDetectorClient.js.map +1 -1
  55. package/dist/LocalVideoBuffer.d.ts +14 -2
  56. package/dist/LocalVideoBuffer.d.ts.map +1 -1
  57. package/dist/LocalVideoBuffer.js +23 -3
  58. package/dist/LocalVideoBuffer.js.map +1 -1
  59. package/dist/MainStreamRing.d.ts +246 -0
  60. package/dist/MainStreamRing.d.ts.map +1 -0
  61. package/dist/MainStreamRing.js +436 -0
  62. package/dist/MainStreamRing.js.map +1 -0
  63. package/dist/SensingProfileClient.d.ts +20 -0
  64. package/dist/SensingProfileClient.d.ts.map +1 -1
  65. package/dist/SensingProfileClient.js +13 -1
  66. package/dist/SensingProfileClient.js.map +1 -1
  67. package/dist/VideoSegmentUploader.d.ts +59 -8
  68. package/dist/VideoSegmentUploader.d.ts.map +1 -1
  69. package/dist/VideoSegmentUploader.js +143 -54
  70. package/dist/VideoSegmentUploader.js.map +1 -1
  71. package/dist/VlmCandidateBuffer.d.ts +13 -0
  72. package/dist/VlmCandidateBuffer.d.ts.map +1 -1
  73. package/dist/VlmCandidateBuffer.js.map +1 -1
  74. package/dist/autoSense.d.ts +4 -0
  75. package/dist/autoSense.d.ts.map +1 -1
  76. package/dist/autoSense.js +6 -0
  77. package/dist/autoSense.js.map +1 -1
  78. package/dist/cli.d.ts.map +1 -1
  79. package/dist/cli.js +2 -0
  80. package/dist/cli.js.map +1 -1
  81. package/dist/connectorUpdate.d.ts +9 -0
  82. package/dist/connectorUpdate.d.ts.map +1 -1
  83. package/dist/connectorUpdate.js +22 -13
  84. package/dist/connectorUpdate.js.map +1 -1
  85. package/dist/continuousReview.d.ts +6 -2
  86. package/dist/continuousReview.d.ts.map +1 -1
  87. package/dist/continuousReview.js.map +1 -1
  88. package/dist/detectorBackendRelay.d.ts +24 -0
  89. package/dist/detectorBackendRelay.d.ts.map +1 -0
  90. package/dist/detectorBackendRelay.js +70 -0
  91. package/dist/detectorBackendRelay.js.map +1 -0
  92. package/dist/diagnose.d.ts +1 -1
  93. package/dist/diagnose.js +1 -1
  94. package/dist/localSensing.d.ts +3 -0
  95. package/dist/localSensing.d.ts.map +1 -1
  96. package/dist/localSensing.js +72 -3
  97. package/dist/localSensing.js.map +1 -1
  98. package/dist/pipelineSwitches.d.ts +59 -0
  99. package/dist/pipelineSwitches.d.ts.map +1 -0
  100. package/dist/pipelineSwitches.js +240 -0
  101. package/dist/pipelineSwitches.js.map +1 -0
  102. package/package.json +4 -4
  103. package/retained/RetainedCaptureClient.cjs +37 -2
  104. package/retained/WebsiteAdmissionCaptureClient.cjs +57 -2
  105. package/retained/__tests__/look-reuse.test.cjs +167 -0
  106. package/retained/manager.cjs +28 -2
@@ -44,6 +44,7 @@ exports.attachCctvVideoRecorder = attachCctvVideoRecorder;
44
44
  exports.activeCctvVideoRecorders = activeCctvVideoRecorders;
45
45
  exports.pendingAlertVideoWork = pendingAlertVideoWork;
46
46
  exports.videoBufferReportForToken = videoBufferReportForToken;
47
+ exports.mainStreamReportForToken = mainStreamReportForToken;
47
48
  exports.__resetCctvVideoRecordersForTests = __resetCctvVideoRecordersForTests;
48
49
  const fs = __importStar(require("fs"));
49
50
  const http = __importStar(require("http"));
@@ -56,6 +57,7 @@ const enterpriseNetwork_1 = require("./enterpriseNetwork");
56
57
  const SensingProfileClient_1 = require("./SensingProfileClient");
57
58
  const AlertClips_1 = require("./AlertClips");
58
59
  const LocalVideoBuffer_1 = require("./LocalVideoBuffer");
60
+ const MainStreamRing_1 = require("./MainStreamRing");
59
61
  /** The route's ceiling on one segment: 256 MiB (268,435,456 bytes). */
60
62
  exports.MAX_SEGMENT_BYTES = 268435456;
61
63
  /** `PUT` budget floor, and the throughput it assumes: max(60 s, 2 x bytes / 125,000 B/s). */
@@ -1037,18 +1039,24 @@ function writeJsonAtomic(file, value) {
1037
1039
  function sameRecording(a, b) {
1038
1040
  return !!a && a.dailySeconds === b.dailySeconds && a.segmentSeconds === b.segmentSeconds && a.ringSeconds === b.ringSeconds
1039
1041
  && (0, SensingProfileClient_1.effectiveVideoMode)(a) === (0, SensingProfileClient_1.effectiveVideoMode)(b) && (a.alerting === true) === (b.alerting === true)
1040
- && a.alertWindow?.beforeSeconds === b.alertWindow?.beforeSeconds && a.alertWindow?.afterSeconds === b.alertWindow?.afterSeconds;
1042
+ && a.alertWindow?.beforeSeconds === b.alertWindow?.beforeSeconds && a.alertWindow?.afterSeconds === b.alertWindow?.afterSeconds
1043
+ && (a.ringStream ?? 'sub') === (b.ringStream ?? 'sub');
1041
1044
  }
1042
- /** The provider's ring figures, read structurally (edge-platform-rtsp is optional). */
1045
+ /**
1046
+ * The provider's ring figures, read structurally (edge-platform-rtsp is
1047
+ * optional). `indexed` is whether `ringBytes` was read from the ring's disk
1048
+ * (edge-platform-rtsp 0.2.21+); null from a provider that does not say.
1049
+ */
1043
1050
  function ringFigures(provider) {
1044
1051
  try {
1045
1052
  const segments = provider.getStats?.()?.segments;
1046
1053
  const ringBytes = typeof segments?.ringBytes === 'number' && Number.isFinite(segments.ringBytes) ? segments.ringBytes : 0;
1047
1054
  const rate = segments?.measuredBytesPerSec;
1048
- return { ringBytes, bytesPerSec: typeof rate === 'number' && Number.isFinite(rate) && rate > 0 ? rate : null };
1055
+ const indexed = typeof segments?.ringIndexed === 'boolean' ? segments.ringIndexed : null;
1056
+ return { ringBytes, bytesPerSec: typeof rate === 'number' && Number.isFinite(rate) && rate > 0 ? rate : null, indexed };
1049
1057
  }
1050
1058
  catch {
1051
- return { ringBytes: 0, bytesPerSec: null };
1059
+ return { ringBytes: 0, bytesPerSec: null, indexed: null };
1052
1060
  }
1053
1061
  }
1054
1062
  /**
@@ -1073,6 +1081,11 @@ function ringFigures(provider) {
1073
1081
  class CctvVideoRecorder {
1074
1082
  constructor(options) {
1075
1083
  this.knobs = null;
1084
+ /**
1085
+ * The ring is a local buffer: `alert_windows`, or an `off` camera whose main
1086
+ * stream records (the cloud keeps nothing; the store keeps the video).
1087
+ */
1088
+ this.buffering = false;
1076
1089
  this.daily = { day: '', recordedSeconds: 0 };
1077
1090
  this.recording = false;
1078
1091
  this.started = false;
@@ -1091,8 +1104,6 @@ class CctvVideoRecorder {
1091
1104
  /** The window being cut right now: a request arriving meanwhile is not queued a second time. */
1092
1105
  this.cuttingTrigger = null;
1093
1106
  this.cutting = false;
1094
- /** The ring's bytes read off disk before the provider has enforced it once (see heldBytes). */
1095
- this.diskRingBytes = null;
1096
1107
  this.pendingTimer = null;
1097
1108
  this.clipCounts = { requested: 0, captured: 0, uploaded: 0, unavailable: 0 };
1098
1109
  this.provider = options.provider;
@@ -1107,8 +1118,17 @@ class CctvVideoRecorder {
1107
1118
  this.buffer = options.buffer ?? (0, LocalVideoBuffer_1.sharedLocalVideoBuffer)(path.dirname(path.resolve(options.dir)));
1108
1119
  this.receiptPoller = options.receiptPoller ?? null;
1109
1120
  this.cutWindow = options.cutWindow ?? AlertClips_1.cutAlertWindow;
1121
+ this.main = options.mainStream ?? null;
1122
+ // The main stream started or stopped recording the ring: hand the ring over.
1123
+ this.main?.on('change', () => this.apply());
1110
1124
  this.provider.on('segment', (segment) => this.onSegment(segment));
1111
1125
  this.provider.on('segment_evicted', (evicted) => this.uploader.forget(evicted.file));
1126
+ // The ring's figures were just read from disk, before the provider evicts
1127
+ // against them: size every share with them first (see heldRingBytes).
1128
+ this.provider.on('ring_indexed', () => {
1129
+ if (this.buffering && this.buffer.has(this.dir))
1130
+ this.buffer.rebalance();
1131
+ });
1112
1132
  this.provider.on('opening', () => this.start());
1113
1133
  }
1114
1134
  /** Idempotent. Runs synchronously inside the provider's `open()`. */
@@ -1139,6 +1159,7 @@ class CctvVideoRecorder {
1139
1159
  this.buffer.remove(this.dir);
1140
1160
  this.receiptPoller?.stop();
1141
1161
  this.uploader.stop();
1162
+ void this.main?.stop();
1142
1163
  }
1143
1164
  getStats() {
1144
1165
  return {
@@ -1153,7 +1174,8 @@ class CctvVideoRecorder {
1153
1174
  keptOnMalformed: this.counts.keptOnMalformed,
1154
1175
  bufferSegmentsKept: this.counts.bufferSegmentsKept,
1155
1176
  clips: { ...this.clipCounts, pending: this.pendingClips.size },
1156
- bufferShareBytes: this.appliedMode === 'alert_windows' ? this.shareBytes : null,
1177
+ bufferShareBytes: this.buffering ? this.shareBytes : null,
1178
+ mainStream: this.main?.report() ?? null,
1157
1179
  };
1158
1180
  }
1159
1181
  /**
@@ -1163,11 +1185,36 @@ class CctvVideoRecorder {
1163
1185
  videoBufferReport() {
1164
1186
  const snapshot = this.buffer.snapshot();
1165
1187
  const { ringBytes, bytesPerSec } = ringFigures(this.provider);
1188
+ const ringStream = this.main?.ringActive() ? 'main' : 'sub';
1166
1189
  let oldestCaptureAt = null;
1190
+ let ringStreamSince = null;
1191
+ let newestCaptureAt = null;
1167
1192
  try {
1168
- const first = fs.readdirSync(path.join(this.dir, 'closed')).filter((n) => /^\d{13}-.+\.mp4$/i.test(n)).sort()[0];
1169
- if (first)
1170
- oldestCaptureAt = new Date(Number(first.slice(0, 13))).toISOString();
1193
+ // The provider's in-memory index, never a listing: an alert camera's ring
1194
+ // is a week of 10-s segments, and this report runs on every status beat.
1195
+ // Unknown (null) until the ring's first read lands after a start.
1196
+ const files = this.provider.ringFiles?.() ?? null;
1197
+ const names = files ? files.map((f) => path.basename(f)).filter((n) => /^\d{13}-.+\.mp4$/i.test(n)) : [];
1198
+ if (names.length) {
1199
+ oldestCaptureAt = new Date(Number(names[0].slice(0, 13))).toISOString();
1200
+ // The latest unbroken stretch of the stream the ring fills now: what a request of that stream can name.
1201
+ let since = null;
1202
+ for (let i = names.length - 1; i >= 0 && (0, AlertClips_1.ringSegmentStream)(names[i]) === ringStream; i -= 1)
1203
+ since = names[i];
1204
+ if (since)
1205
+ ringStreamSince = new Date(Number(since.slice(0, 13))).toISOString();
1206
+ const newest = names[names.length - 1];
1207
+ let endMs = Number(newest.slice(0, 13));
1208
+ try {
1209
+ const meta = JSON.parse(fs.readFileSync(path.join(this.dir, 'closed', newest.replace(/\.mp4$/i, '.json')), 'utf8'));
1210
+ if (Number.isFinite(meta?.captureEndMs) && meta.captureEndMs > endMs)
1211
+ endMs = meta.captureEndMs;
1212
+ }
1213
+ catch {
1214
+ /* no sidecar: its start is what is known */
1215
+ }
1216
+ newestCaptureAt = new Date(endMs).toISOString();
1217
+ }
1171
1218
  }
1172
1219
  catch {
1173
1220
  /* nothing buffered yet */
@@ -1175,18 +1222,25 @@ class CctvVideoRecorder {
1175
1222
  return {
1176
1223
  mode: this.appliedMode,
1177
1224
  freeBytes: snapshot.freeBytes ?? (0, LocalVideoBuffer_1.volumeFreeBytesOf)(this.dir),
1178
- budgetBytes: this.appliedMode === 'alert_windows' ? snapshot.budgetBytes : null,
1225
+ budgetBytes: this.buffering ? snapshot.budgetBytes : null,
1179
1226
  bufferBytes: ringBytes,
1180
1227
  bufferSeconds: bytesPerSec ? Math.round(ringBytes / bytesPerSec) : null,
1181
1228
  oldestCaptureAt,
1229
+ ringStream,
1230
+ ringStreamSince,
1231
+ newestCaptureAt,
1182
1232
  };
1183
1233
  }
1234
+ /** The camera's main stream for the status beacon (`mainStream`), or null when its knob never asked. */
1235
+ mainStreamReport() {
1236
+ return this.stopped ? null : this.main?.report() ?? null;
1237
+ }
1184
1238
  /**
1185
1239
  * A review that supported an alert was accepted: follow its receipt for the
1186
1240
  * clip the server will ask for. Only a buffering feed has anything to cut.
1187
1241
  */
1188
1242
  watchReceipt(evidenceId, opts) {
1189
- if (this.stopped || this.appliedMode !== 'alert_windows')
1243
+ if (this.stopped || !this.buffering)
1190
1244
  return;
1191
1245
  this.receiptPoller?.watch(evidenceId, opts);
1192
1246
  }
@@ -1214,7 +1268,7 @@ class CctvVideoRecorder {
1214
1268
  }
1215
1269
  /** Clip requests from the receipt or the knobs. Each window is cut once. */
1216
1270
  requestClips(requests) {
1217
- if (this.stopped || this.appliedMode !== 'alert_windows')
1271
+ if (this.stopped || !this.buffering)
1218
1272
  return;
1219
1273
  let added = 0;
1220
1274
  for (const request of requests) {
@@ -1236,6 +1290,8 @@ class CctvVideoRecorder {
1236
1290
  if (this.stopped)
1237
1291
  return;
1238
1292
  this.counts.polls += 1;
1293
+ // A daily main-stream probe that came due rides the knob poll.
1294
+ this.main?.check();
1239
1295
  let profile;
1240
1296
  try {
1241
1297
  profile = await this.profileClient.fetchProfile();
@@ -1289,14 +1345,19 @@ class CctvVideoRecorder {
1289
1345
  catch {
1290
1346
  /* an older provider: no ranking */
1291
1347
  }
1292
- if (mode === 'alert_windows') {
1348
+ // The camera's main stream: the knob first (it may start a probe), then
1349
+ // whether it records the ring.
1350
+ this.main?.setKnob(k?.ringStream);
1351
+ const mainWanted = this.main?.wantsRing() === true;
1352
+ this.buffering = mode === 'alert_windows' || (mode === 'off' && mainWanted);
1353
+ if (this.buffering) {
1293
1354
  this.recording = true;
1294
1355
  if (previousMode === 'continuous') {
1295
1356
  const dropped = this.uploader.dropContinuous();
1296
1357
  if (dropped)
1297
1358
  this.log('info', `[video] ${this.feedName}: continuous upload off; ${dropped} queued segment(s) stay in the local buffer`);
1298
1359
  }
1299
- // Joining computes this feed's share, which reaches the provider through applyShare.
1360
+ // Joining computes this feed's share, which reaches the ring through applyShare.
1300
1361
  if (!this.buffer.has(this.dir)) {
1301
1362
  this.buffer.add({
1302
1363
  key: this.dir,
@@ -1304,12 +1365,12 @@ class CctvVideoRecorder {
1304
1365
  heldBytes: () => this.heldRingBytes(),
1305
1366
  applyShare: (maxBytes) => {
1306
1367
  this.shareBytes = maxBytes;
1307
- if (this.appliedMode === 'alert_windows')
1308
- this.provider.setSegmentRecording(this.bufferConfig());
1368
+ if (this.buffering)
1369
+ this.toRing(this.bufferConfig(), this.appliedMode);
1309
1370
  },
1310
1371
  });
1311
1372
  }
1312
- this.provider.setSegmentRecording(this.bufferConfig());
1373
+ this.toRing(this.bufferConfig(), mode);
1313
1374
  return;
1314
1375
  }
1315
1376
  if (this.buffer.has(this.dir)) {
@@ -1321,59 +1382,60 @@ class CctvVideoRecorder {
1321
1382
  this.recording = false;
1322
1383
  // No `maxBytes`: the provider reads its absence as "no caller cap", so a
1323
1384
  // share from an earlier alert_windows run never outlives the mode.
1324
- this.provider.setSegmentRecording({
1385
+ this.toRing({
1325
1386
  dir: this.dir,
1326
1387
  segmentSeconds: UNANSWERED_SEGMENT_SECONDS,
1327
1388
  ringSeconds: UNANSWERED_RING_SECONDS,
1328
1389
  enabled: false,
1329
- });
1390
+ }, mode);
1330
1391
  return;
1331
1392
  }
1332
1393
  const continuous = mode === 'continuous';
1333
1394
  const budgetLeft = k.dailySeconds >= SECONDS_PER_DAY || this.daily.recordedSeconds < k.dailySeconds;
1334
1395
  this.recording = continuous && k.dailySeconds > 0 && budgetLeft;
1335
- this.provider.setSegmentRecording({
1396
+ this.toRing({
1336
1397
  dir: this.dir,
1337
1398
  segmentSeconds: k.segmentSeconds,
1338
1399
  ringSeconds: k.ringSeconds,
1339
1400
  enabled: this.recording,
1340
- });
1401
+ }, mode);
1341
1402
  if (continuous && k.dailySeconds > 0 && !budgetLeft)
1342
1403
  this.scheduleNextDay();
1343
1404
  }
1344
1405
  /**
1345
- * The bytes this feed's ring holds. The provider reports 0 until it has
1346
- * enforced the ring once, which after a restart is AFTER the share is first
1347
- * computed; counting 0 then would hand the ring a share that leaves out its
1348
- * own week of video and trim the oldest of it at every start. Until the
1349
- * provider reports, the closed segments are summed off disk (once a minute).
1406
+ * ONE RING PER CAMERA. The main stream records it while it is wanted (the
1407
+ * `ringStream` knob says main and the probe passed), into the same
1408
+ * `<dir>/closed/` through the provider. The feed's own session records it, in
1409
+ * exactly the modes it always has, whenever the main stream is not RUNNING —
1410
+ * so a main stream that is starting, backing off or shed never costs the
1411
+ * camera its ring — and records nothing once it is. The provider keeps the
1412
+ * config either way: its `dir`, cap and label are the ring's.
1413
+ */
1414
+ toRing(config, mode) {
1415
+ const main = this.main;
1416
+ // Always handed over: the main stream uses it only while it is wanted, and a
1417
+ // probe that passes later finds it without waiting for the next knob answer.
1418
+ main?.record(config);
1419
+ const ownSessionRecords = mode !== 'off' && main?.ringActive() !== true;
1420
+ this.provider.setSegmentRecording(ownSessionRecords ? config : { ...config, enabled: false });
1421
+ }
1422
+ /**
1423
+ * The bytes this feed's ring holds, or null while the provider is still
1424
+ * reading it. After a restart the share is first computed BEFORE the ring's
1425
+ * read lands (a week of segments is read off the event loop); counting 0
1426
+ * then would hand the ring a share that leaves out its own week of video.
1427
+ * Null instead keeps every share from falling below what its ring holds
1428
+ * (LocalVideoBuffer.rebalance), the provider evicts nothing until its read
1429
+ * lands, and the read's 'ring_indexed' re-sizes the shares before it evicts.
1430
+ * Never a listing of the ring here: on the store PC that sum was a second
1431
+ * ~1-s synchronous walk of each alert camera's week at every start.
1350
1432
  */
1351
1433
  heldRingBytes() {
1352
- const reported = ringFigures(this.provider).ringBytes;
1353
- if (reported > 0)
1354
- return reported;
1355
- const nowMs = this.now();
1356
- if (this.diskRingBytes && nowMs - this.diskRingBytes.atMs < 60000)
1357
- return this.diskRingBytes.bytes;
1358
- let bytes = 0;
1359
- try {
1360
- const closed = path.join(this.dir, 'closed');
1361
- for (const name of fs.readdirSync(closed)) {
1362
- if (!name.toLowerCase().endsWith('.mp4'))
1363
- continue;
1364
- try {
1365
- bytes += fs.statSync(path.join(closed, name)).size;
1366
- }
1367
- catch {
1368
- /* gone meanwhile */
1369
- }
1370
- }
1371
- }
1372
- catch {
1373
- /* nothing buffered yet */
1374
- }
1375
- this.diskRingBytes = { bytes, atMs: nowMs };
1376
- return bytes;
1434
+ const { ringBytes, indexed } = ringFigures(this.provider);
1435
+ if (indexed !== null)
1436
+ return indexed ? ringBytes : null;
1437
+ // A provider that does not say: 0 is "not enforced yet" as often as "empty".
1438
+ return ringBytes > 0 ? ringBytes : null;
1377
1439
  }
1378
1440
  /** The provider config in alert_windows mode: short segments, the byte share as the cap. */
1379
1441
  bufferConfig() {
@@ -1387,7 +1449,7 @@ class CctvVideoRecorder {
1387
1449
  };
1388
1450
  }
1389
1451
  onSegment(segment) {
1390
- if (this.appliedMode === 'alert_windows') {
1452
+ if (this.buffering) {
1391
1453
  // The local buffer: nothing leaves the box but an alert's window.
1392
1454
  if (Number.isFinite(segment.captureEndMs))
1393
1455
  this.lastClosedEndMs = Math.max(this.lastClosedEndMs, segment.captureEndMs);
@@ -1430,7 +1492,7 @@ class CctvVideoRecorder {
1430
1492
  this.cutting = true;
1431
1493
  try {
1432
1494
  for (const [triggerId, request] of [...this.pendingClips]) {
1433
- if (this.stopped || this.appliedMode !== 'alert_windows')
1495
+ if (this.stopped || !this.buffering)
1434
1496
  break;
1435
1497
  const windowEndMs = request.capturedAtMs + request.afterSeconds * 1000;
1436
1498
  const covered = this.lastClosedEndMs >= windowEndMs;
@@ -1553,6 +1615,7 @@ class CctvVideoRecorder {
1553
1615
  ringSeconds: raw.ringSeconds,
1554
1616
  ...(raw.mode !== undefined ? { mode: raw.mode } : {}),
1555
1617
  ...(raw.alerting !== undefined ? { alerting: raw.alerting } : {}),
1618
+ ...(raw.ringStream !== undefined ? { ringStream: raw.ringStream } : {}),
1556
1619
  ...(raw.alertWindow !== undefined ? { alertWindow: raw.alertWindow } : {}),
1557
1620
  });
1558
1621
  this.knobs = knobs ?? { dailySeconds: raw.dailySeconds, segmentSeconds: raw.segmentSeconds, ringSeconds: raw.ringSeconds };
@@ -1626,6 +1689,17 @@ function attachCctvVideoRecorder(args) {
1626
1689
  ...(args.httpPoster ? { poster: args.httpPoster } : {}),
1627
1690
  })
1628
1691
  : null;
1692
+ // The camera's main stream for its ring: idle until its knob asks (no I/O before that).
1693
+ const mainStream = new MainStreamRing_1.MainStreamRing({
1694
+ dir: args.dir,
1695
+ feedName: args.feedName,
1696
+ rtspUrl: args.rtspUrl ?? null,
1697
+ ...(args.transport ? { transport: args.transport } : {}),
1698
+ ...(args.ffmpegPath ? { ffmpegPath: args.ffmpegPath } : {}),
1699
+ sink: typeof provider.adoptSegment === 'function'
1700
+ ? provider
1701
+ : null,
1702
+ });
1629
1703
  const recorder = new CctvVideoRecorder({
1630
1704
  provider: provider,
1631
1705
  dir: args.dir,
@@ -1633,6 +1707,7 @@ function attachCctvVideoRecorder(args) {
1633
1707
  profileClient,
1634
1708
  uploader,
1635
1709
  receiptPoller,
1710
+ mainStream,
1636
1711
  ...(args.ffmpegPath ? { ffmpegPath: args.ffmpegPath } : {}),
1637
1712
  ...(args.pollMs !== undefined ? { pollMs: args.pollMs } : {}),
1638
1713
  });
@@ -1678,6 +1753,20 @@ function videoBufferReportForToken(deviceToken) {
1678
1753
  }
1679
1754
  return null;
1680
1755
  }
1756
+ /** The main-stream report of the feed that uses this device token, or null (its knob never asked, or no recording feed). */
1757
+ function mainStreamReportForToken(deviceToken) {
1758
+ for (const entry of recordersByDir.values()) {
1759
+ if (entry.deviceToken !== deviceToken)
1760
+ continue;
1761
+ try {
1762
+ return entry.recorder.mainStreamReport();
1763
+ }
1764
+ catch {
1765
+ return null;
1766
+ }
1767
+ }
1768
+ return null;
1769
+ }
1681
1770
  /** Test seam. */
1682
1771
  function __resetCctvVideoRecordersForTests() {
1683
1772
  for (const entry of recordersByDir.values())