@camstack/addon-pipeline 1.2.243 → 1.2.245

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 (33) hide show
  1. package/dist/audio-analyzer/index.js +2 -2
  2. package/dist/audio-analyzer/index.mjs +2 -2
  3. package/dist/{default-detection-model-Bq_xdwWw.js → default-detection-model-DLFtoi4W.js} +1 -1
  4. package/dist/{default-detection-model-IKNS25_s.mjs → default-detection-model-UXQCoRj_.mjs} +1 -1
  5. package/dist/detection-pipeline/index.js +4 -4
  6. package/dist/detection-pipeline/index.mjs +4 -4
  7. package/dist/{dist-Zw3eICiX.js → dist-CWWQf3ci.js} +117 -2
  8. package/dist/{dist-CswFZdi6.mjs → dist-CoZwNgzu.mjs} +117 -2
  9. package/dist/motion-wasm/index.js +1 -1
  10. package/dist/motion-wasm/index.mjs +1 -1
  11. package/dist/{node-BAzE1jef.js → node-DLZWItaN.js} +1 -1
  12. package/dist/{node-0m0RkbvB.mjs → node-Uh59Eo57.mjs} +1 -1
  13. package/dist/pipeline-runner/index.js +63 -3
  14. package/dist/pipeline-runner/index.mjs +63 -3
  15. package/dist/{process-memory-BcqRS5L_.mjs → process-memory-BnFnR_nP.mjs} +1 -1
  16. package/dist/{process-memory-BkGgkl4T.js → process-memory-Bv9SiLn-.js} +1 -1
  17. package/dist/recorder/index.js +2 -2
  18. package/dist/recorder/index.mjs +2 -2
  19. package/dist/{segment-demux-js-AQvsdVSO.mjs → segment-demux-js-7DukFIT6.mjs} +1 -1
  20. package/dist/{segment-demux-js-mwoA6eC7.js → segment-demux-js-C6B53KjT.js} +1 -1
  21. package/dist/stream-broker/_stub.js +2 -2
  22. package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-Chu9oUjF.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-B20FCUk4.mjs} +3 -3
  23. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-DVqDp5VD.mjs +26 -0
  24. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-CJunbWSo.mjs +26 -0
  25. package/dist/stream-broker/demux-worker-child.js +1 -1
  26. package/dist/stream-broker/demux-worker-child.mjs +1 -1
  27. package/dist/stream-broker/{hostInit-BVmX2y-u.mjs → hostInit-DCTAdB0n.mjs} +3 -3
  28. package/dist/stream-broker/index.js +501 -47
  29. package/dist/stream-broker/index.mjs +501 -47
  30. package/dist/stream-broker/remoteEntry.js +1 -1
  31. package/package.json +2 -2
  32. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-aawWFyGL.mjs +0 -26
  33. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_ui_mf_2_library__loadShare__.js-C3P2fV1x.mjs +0 -26
@@ -3,12 +3,12 @@ Object.defineProperties(exports, {
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
5
  const require_chunk = require("../chunk-emK7D4bc.js");
6
- const require_dist = require("../dist-Zw3eICiX.js");
7
- const require_node = require("../node-BAzE1jef.js");
6
+ const require_dist = require("../dist-CWWQf3ci.js");
7
+ const require_node = require("../node-DLZWItaN.js");
8
8
  const require_restream_intent = require("../restream-intent-C4adO0P8.js");
9
9
  const require_remote_restream = require("../remote-restream-CO36Sr30.js");
10
10
  const require_retire_root_keys = require("../retire-root-keys-Csb8z2f3.js");
11
- const require_segment_demux_js = require("../segment-demux-js-mwoA6eC7.js");
11
+ const require_segment_demux_js = require("../segment-demux-js-C6B53KjT.js");
12
12
  let node_os = require("node:os");
13
13
  node_os = require_chunk.__toESM(node_os);
14
14
  let node_crypto = require("node:crypto");
@@ -10710,8 +10710,23 @@ var AudioCodecSession = class {
10710
10710
  var EncodedRingBuffer = class {
10711
10711
  packets = [];
10712
10712
  maxDurationMs;
10713
- constructor(maxDurationSec = 10) {
10713
+ maxBytes;
10714
+ bytes = 0;
10715
+ clampedByBytes = false;
10716
+ /**
10717
+ * @param maxDurationSec how much WALL TIME to keep.
10718
+ * @param maxBytes how much MEMORY that is allowed to cost. `0` = unbounded,
10719
+ * which is what this buffer was before and is why it needed a bound: the
10720
+ * retention is set in SECONDS, but what a second costs depends entirely on
10721
+ * which profile got armed. On the operator's fleet the cheap profile runs
10722
+ * at 34-118 kbps while the main stream runs at up to 11 Mbps — a 25:1
10723
+ * ratio — so one rule arming a clip on the main stream turns a 60-second
10724
+ * retention into 82 MB on a single camera, silently, in native memory.
10725
+ * A time limit cannot express that; only a byte limit can.
10726
+ */
10727
+ constructor(maxDurationSec = 10, maxBytes = 0) {
10714
10728
  this.maxDurationMs = maxDurationSec * 1e3;
10729
+ this.maxBytes = Math.max(0, maxBytes);
10715
10730
  }
10716
10731
  /** Update the buffer duration (in seconds). Evicts old packets immediately. */
10717
10732
  setDuration(seconds) {
@@ -10725,8 +10740,23 @@ var EncodedRingBuffer = class {
10725
10740
  /** Push a new packet into the buffer and evict expired ones. */
10726
10741
  push(packet) {
10727
10742
  this.packets.push(packet);
10743
+ this.bytes += packet.data.length;
10728
10744
  this.evict();
10729
10745
  }
10746
+ /** What the retained window currently costs, in bytes. */
10747
+ getBufferedBytes() {
10748
+ return this.bytes;
10749
+ }
10750
+ /**
10751
+ * True when the byte budget — not the clock — is what bounds this window.
10752
+ *
10753
+ * Worth asking rather than inferring from a short buffer: a window cut by
10754
+ * bytes holds LESS time than it was configured for, and a consumer that
10755
+ * believes the configured number will look for footage that was never kept.
10756
+ */
10757
+ isClampedByBytes() {
10758
+ return this.clampedByBytes;
10759
+ }
10730
10760
  /**
10731
10761
  * Get buffered packets, starting from the most recent keyframe.
10732
10762
  * Returns packets in chronological order (oldest first).
@@ -10768,6 +10798,8 @@ var EncodedRingBuffer = class {
10768
10798
  /** Clear all buffered packets. */
10769
10799
  clear() {
10770
10800
  this.packets.length = 0;
10801
+ this.bytes = 0;
10802
+ this.clampedByBytes = false;
10771
10803
  }
10772
10804
  /**
10773
10805
  * Remove packets older than maxDurationMs based on PTS.
@@ -10788,7 +10820,19 @@ var EncodedRingBuffer = class {
10788
10820
  const cutoff = newest - this.maxDurationMs;
10789
10821
  let removeCount = 0;
10790
10822
  while (removeCount < this.packets.length && (this.packets[removeCount].pts < cutoff || this.packets[removeCount].pts > newest)) removeCount++;
10791
- if (removeCount > 0) this.packets.splice(0, removeCount);
10823
+ if (removeCount > 0) {
10824
+ for (let i = 0; i < removeCount; i++) this.bytes -= this.packets[i].data.length;
10825
+ this.packets.splice(0, removeCount);
10826
+ }
10827
+ if (this.maxBytes > 0 && this.bytes > this.maxBytes) {
10828
+ this.clampedByBytes = true;
10829
+ let dropped = 0;
10830
+ while (dropped < this.packets.length && this.bytes > this.maxBytes) {
10831
+ this.bytes -= this.packets[dropped].data.length;
10832
+ dropped++;
10833
+ }
10834
+ if (dropped > 0) this.packets.splice(0, dropped);
10835
+ } else if (this.maxBytes > 0) this.clampedByBytes = false;
10792
10836
  }
10793
10837
  };
10794
10838
  //#endregion
@@ -12807,6 +12851,27 @@ var StreamBroker = class StreamBroker {
12807
12851
  /** Maximum allowed pre-buffer duration in seconds */
12808
12852
  static MAX_PRE_BUFFER_SEC = 30;
12809
12853
  /**
12854
+ * Ceiling for the notification clip ring, in seconds.
12855
+ *
12856
+ * Separate from `MAX_PRE_BUFFER_SEC` because the two rings live on different
12857
+ * cost axes: the pre-buffer holds the MAIN stream (the fleet's 22 warm
12858
+ * streams sum 107 Mbps), the clip ring holds the cheapest profile (the same
12859
+ * 22 sum 4.3 Mbps — a 25:1 ratio). A minute of the cheap one costs ~32 MB
12860
+ * across the whole fleet; a minute of the main one would cost ~800 MB.
12861
+ */
12862
+ static MAX_CLIP_RETENTION_SEC = 120;
12863
+ /**
12864
+ * What ONE camera's clip ring may cost, whatever profile ends up armed.
12865
+ *
12866
+ * The same 16 MiB the RTP pre-roll ring already uses, deliberately — one
12867
+ * number for "how much native memory one stream's history may hold". It
12868
+ * holds a full minute of any realistic cheap stream (up to ~2.2 Mbps) and
12869
+ * clamps a rule that arms the MAIN stream to about twelve seconds instead of
12870
+ * letting it take 82 MB. A window cut by this says so
12871
+ * (`isClampedByBytes`).
12872
+ */
12873
+ static CLIP_RING_MAX_BYTES = 16 * 1024 * 1024;
12874
+ /**
12810
12875
  * Retention for the decoder-seed GOP ring. Only ever needs to hold the
12811
12876
  * current GOP (a fresh keyframe evicts the previous GOP on read), so a few
12812
12877
  * seconds covers every realistic keyframe interval; cameras with a GOP longer
@@ -14584,15 +14649,31 @@ var StreamBroker = class StreamBroker {
14584
14649
  }
14585
14650
  /** Retain `seconds` of AnnexB video for notification clips (0 = stop, frees the ring). */
14586
14651
  setClipRetention(seconds) {
14587
- const clamped = Math.max(0, Math.min(seconds, StreamBroker.MAX_PRE_BUFFER_SEC));
14652
+ const clamped = Math.max(0, Math.min(seconds, StreamBroker.MAX_CLIP_RETENTION_SEC));
14588
14653
  if (clamped === this.clipRetentionSec) return;
14654
+ const previous = this.clipRetentionSec;
14589
14655
  this.clipRetentionSec = clamped;
14656
+ this.logger?.info(clamped === 0 ? "Clip retention off" : "Clip retention set", { meta: {
14657
+ seconds: clamped,
14658
+ requested: seconds,
14659
+ previousSeconds: previous
14660
+ } });
14590
14661
  if (clamped === 0) {
14591
14662
  this.clipBuffer = null;
14592
14663
  return;
14593
14664
  }
14594
14665
  if (this.clipBuffer) this.clipBuffer.setDuration(clamped);
14595
- else this.clipBuffer = new EncodedRingBuffer(clamped);
14666
+ else this.clipBuffer = new EncodedRingBuffer(clamped, StreamBroker.CLIP_RING_MAX_BYTES);
14667
+ }
14668
+ /**
14669
+ * Seconds this stream's clip ring is currently holding. `0` = no ring.
14670
+ *
14671
+ * A READ of what was applied, for the manager's own bound (how long a cut may
14672
+ * wait for its post-roll) and for the tests that pin the retention a camera's
14673
+ * notification rules ask for.
14674
+ */
14675
+ getClipRetentionSec() {
14676
+ return this.clipRetentionSec;
14596
14677
  }
14597
14678
  /**
14598
14679
  * The retained clip window, oldest first. Empty when retention is off.
@@ -16225,13 +16306,57 @@ function formatClockHHMM(at) {
16225
16306
  * reach `streaming` with audio detected, and the stats poll cadence. */
16226
16307
  var PROBE_STREAM_TIMEOUT_MS = 6e3;
16227
16308
  var PROBE_STREAM_POLL_MS = 250;
16228
- var CLIP_RETENTION_SEC = 25;
16309
+ /**
16310
+ * How much history a notification clip may be cut from on a camera the
16311
+ * notification centre has **not spoken about yet**.
16312
+ *
16313
+ * Retention is no longer flat: every camera holds what its own notification
16314
+ * rules ask for ({@link refreshClipRetentionPlan}), which is nothing at all on
16315
+ * a camera whose rules never cut footage. This number is what a camera holds
16316
+ * while that ask is UNKNOWN — before the first successful pull, and after a
16317
+ * pull that failed or answered with a rule set that has not loaded. Absence
16318
+ * must not read as zero: dropping a ring on boot, before the rules arrive,
16319
+ * loses the footage for exactly the first events after every restart, which is
16320
+ * when an operator is most likely to be watching. So an unknown camera keeps
16321
+ * what the whole fleet used to keep, and only an ANSWER can take it away.
16322
+ *
16323
+ * Sixty seconds, raised from 25 on 2026-09-15 after measuring what the
16324
+ * alternative sources can actually serve. The recordings' availability edge is
16325
+ * not a number but a PHASE: asking for the same 10-second window at different
16326
+ * anchors, full coverage arrived at −18 s on one camera and −26 s on two
16327
+ * others, depending on where the request falls in the 10-second segment cycle.
16328
+ * A notification window is `[anchor−4, anchor+6]`, so at 25 s the ring served
16329
+ * an anchor roughly 6–19 s old while the recordings began at 14–26 s — and on
16330
+ * an unlucky phase those bands did not meet, which is the silent "no clip" the
16331
+ * operator kept receiving. No fixed threshold closes a gap that opens and
16332
+ * closes with the segment cycle; margin over the WORST phase does.
16333
+ *
16334
+ * It is affordable only because this ring runs on the cheap profile: the
16335
+ * fleet's 22 cheap streams sum 4.3 Mbps, so a minute costs ~32 MB across every
16336
+ * camera — against ~800 MB for the same minute of the main stream. And it is
16337
+ * SAFE only because the ring is now bounded by bytes
16338
+ * (`StreamBroker.CLIP_RING_MAX_BYTES`): a rule that arms a clip on the main
16339
+ * stream is clamped to what 16 MiB holds rather than taking 82 MB.
16340
+ */
16341
+ var CLIP_RETENTION_UNKNOWN_SEC = 60;
16229
16342
  /** Cheapest-first — the profile a clip is cut from when the rule says nothing. */
16230
16343
  var CLIP_PROFILE_BY_COST = [
16231
16344
  "low",
16232
16345
  "mid",
16233
16346
  "high"
16234
16347
  ];
16348
+ /**
16349
+ * Head-room added to an OBSERVED ask (`preRoll + postRoll + this`).
16350
+ *
16351
+ * The notification centre derives the same shape from its own rules
16352
+ * (`NC_CLIP_DISPATCH_MARGIN_SEC`) and for the same reason: a cut is taken
16353
+ * seconds after the moment it is about — the row is queued, drained, and its
16354
+ * still is waited for — so a ring sized to the window alone drops the pre-roll
16355
+ * a heartbeat before the renderer asks for it. Deliberately NOT imported from
16356
+ * the notification addon: addons never import each other, and this is the
16357
+ * broker's own floor for a consumer that is not the notification centre.
16358
+ */
16359
+ var CLIP_OBSERVED_ASK_MARGIN_SEC = 10;
16235
16360
  /** How long `renderPreBufferClip` will wait for the post-roll to accumulate. */
16236
16361
  var CLIP_POST_ROLL_POLL_MS = 250;
16237
16362
  /**
@@ -18105,6 +18230,7 @@ var StreamBrokerManager = class StreamBrokerManager {
18105
18230
  } else this.disabledDevices.delete(d.id);
18106
18231
  const live = cameras.filter((d) => !d.disabled);
18107
18232
  await Promise.allSettled(live.map((d) => this.reconcileDeviceCatalog(d.id, "poll")));
18233
+ await this.refreshClipRetentionPlan();
18108
18234
  await Promise.allSettled(live.map((d) => this.reconcileDeviceAudioPlane(d.id)));
18109
18235
  const liveIds = new Set(live.map((d) => d.id));
18110
18236
  for (const deviceId of [...this.audioPlaneState.keys()]) if (!liveIds.has(deviceId)) this.audioPlaneState.delete(deviceId);
@@ -18643,8 +18769,8 @@ var StreamBrokerManager = class StreamBrokerManager {
18643
18769
  const assigned = CLIP_PROFILE_BY_COST.filter((p) => map[p] !== void 0);
18644
18770
  const wanted = pickClipProfile(assigned, input.profile);
18645
18771
  if (!wanted) throw new Error(`No profile assigned for device ${input.deviceId}`);
18646
- this.armClipRetention(input.deviceId, wanted);
18647
- const until = Math.min(input.aroundMs + input.postRollSec * 1e3, Date.now() + CLIP_RETENTION_SEC * 1e3);
18772
+ this.noteClipAsk(input.deviceId, wanted, input.preRollSec + input.postRollSec + CLIP_OBSERVED_ASK_MARGIN_SEC);
18773
+ const until = Math.min(input.aroundMs + input.postRollSec * 1e3, Date.now() + this.clipRetentionSecFor(input.deviceId) * 1e3);
18648
18774
  while (Date.now() < until) await new Promise((resolve) => setTimeout(resolve, CLIP_POST_ROLL_POLL_MS));
18649
18775
  const candidates = [wanted, ...assigned.filter((p) => p !== wanted)];
18650
18776
  let profile = null;
@@ -20366,8 +20492,18 @@ var StreamBrokerManager = class StreamBrokerManager {
20366
20492
  this.applyClipRetention(deviceId);
20367
20493
  }
20368
20494
  /**
20369
- * Keep a clip window retained on the ONE profile clips are cut from — the
20370
- * cheapest assigned, which is what `pickClipProfile` defaults to.
20495
+ * Apply the retention THIS camera's notification rules actually ask for.
20496
+ *
20497
+ * Three inputs, one number:
20498
+ *
20499
+ * 1. the PLAN — `notificationRules.getClipRetentionAsks`, the rules' own
20500
+ * demand per camera ({@link refreshClipRetentionPlan});
20501
+ * 2. what a cut has been OBSERVED asking for on this camera
20502
+ * ({@link noteClipAsk}) — the old reactive arming, folded into the same
20503
+ * ask so there is one mechanism rather than two;
20504
+ * 3. nothing at all, when the plan has never arrived — see
20505
+ * {@link CLIP_RETENTION_UNKNOWN_SEC}. An unknown camera is not a camera
20506
+ * that needs nothing.
20371
20507
  *
20372
20508
  * Retention is opportunistic: it never dials a camera, it only fills while
20373
20509
  * something else already holds the stream up (usually the detection pipeline
@@ -20376,32 +20512,159 @@ var StreamBrokerManager = class StreamBrokerManager {
20376
20512
  */
20377
20513
  applyClipRetention(deviceId) {
20378
20514
  const map = this.assignments.get(deviceId)?.map ?? {};
20379
- const clipProfile = CLIP_PROFILE_BY_COST.find((p) => map[p] !== void 0);
20380
- const armed = this.clipRetentionArmed.get(deviceId);
20515
+ const resolved = this.resolveClipRetention(deviceId, map);
20381
20516
  for (const profile of require_dist.CAM_PROFILE_ORDER) {
20382
20517
  const camStreamId = map[profile];
20383
20518
  if (!camStreamId) continue;
20384
- const wanted = profile === clipProfile || armed?.has(profile) === true;
20385
- this.brokers.get(brokerIdFor(deviceId, camStreamId))?.setClipRetention(wanted ? CLIP_RETENTION_SEC : 0);
20519
+ const wanted = resolved.profiles.has(profile) ? resolved.seconds : 0;
20520
+ this.brokers.get(brokerIdFor(deviceId, camStreamId))?.setClipRetention(wanted);
20521
+ }
20522
+ this.reportClipRetention(deviceId, resolved);
20523
+ }
20524
+ /**
20525
+ * The seconds and the profiles this camera's ring should hold, and WHY.
20526
+ *
20527
+ * The MAX over every ask that reaches the camera — the asks describe the
20528
+ * rules, the bounding belongs here (and, below this, to the ring's own byte
20529
+ * budget, which is never raised for an ask).
20530
+ */
20531
+ resolveClipRetention(deviceId, map) {
20532
+ const cheapest = CLIP_PROFILE_BY_COST.find((p) => map[p] !== void 0);
20533
+ const observed = this.clipRetentionObserved.get(deviceId);
20534
+ const profiles = /* @__PURE__ */ new Set();
20535
+ let seconds = 0;
20536
+ let source;
20537
+ if (this.clipRetentionAsks === null) {
20538
+ seconds = CLIP_RETENTION_UNKNOWN_SEC;
20539
+ source = "unknown";
20540
+ } else {
20541
+ source = "rules";
20542
+ for (const ask of this.clipRetentionAsks) {
20543
+ if (ask.deviceId !== void 0 && ask.deviceId !== deviceId) continue;
20544
+ if (ask.seconds > seconds) seconds = ask.seconds;
20545
+ if (ask.profile !== void 0) profiles.add(ask.profile);
20546
+ }
20547
+ }
20548
+ if (observed !== void 0) for (const [profile, asked] of observed) {
20549
+ if (asked > seconds) {
20550
+ seconds = asked;
20551
+ source = source === "unknown" ? "unknown" : "observed";
20552
+ }
20553
+ profiles.add(profile);
20386
20554
  }
20555
+ if (seconds <= 0) return {
20556
+ seconds: 0,
20557
+ profiles: /* @__PURE__ */ new Set(),
20558
+ source
20559
+ };
20560
+ if (cheapest !== void 0) profiles.add(cheapest);
20561
+ return {
20562
+ seconds,
20563
+ profiles,
20564
+ source
20565
+ };
20566
+ }
20567
+ /**
20568
+ * Say it, once per transition, per camera.
20569
+ *
20570
+ * This mechanism MOVES — a rule created, disabled or deleted changes what a
20571
+ * camera holds within one reconcile tick — and a retention that changes
20572
+ * silently is a retention nobody can correlate with a missing clip. The
20573
+ * broker's own `setClipRetention` logs the buffer it allocated; this line
20574
+ * says who asked for it.
20575
+ */
20576
+ reportClipRetention(deviceId, resolved) {
20577
+ const signature = `${resolved.seconds}:${resolved.source}:${[...resolved.profiles].sort().join(",")}`;
20578
+ const previous = this.clipRetentionApplied.get(deviceId);
20579
+ if (previous === signature) return;
20580
+ this.clipRetentionApplied.set(deviceId, signature);
20581
+ this.logger.info(resolved.seconds === 0 ? "clip retention: none — no notification rule on this camera cuts footage" : "clip retention set from the notification rules", {
20582
+ tags: { deviceId },
20583
+ meta: {
20584
+ seconds: resolved.seconds,
20585
+ profiles: [...resolved.profiles],
20586
+ source: resolved.source,
20587
+ previous: previous ?? "none"
20588
+ }
20589
+ });
20590
+ }
20591
+ /** Seconds this camera's ring is currently asked to hold (0 when none). */
20592
+ clipRetentionSecFor(deviceId) {
20593
+ return this.resolveClipRetention(deviceId, this.assignments.get(deviceId)?.map ?? {}).seconds;
20387
20594
  }
20388
20595
  /**
20389
- * Profiles a rule has actually asked a clip from, beyond the cheapest one
20390
- * retained by default.
20596
+ * PULL the notification centre's per-camera asks and apply them.
20597
+ *
20598
+ * A pull, not a push: a camera's ring lives on the node that ingests it while
20599
+ * `notification-rules` is a singleton, so a push would have to enumerate the
20600
+ * cluster. It also makes the failure mode right — a pull that fails, or one
20601
+ * answered by a centre whose rules have not loaded, changes NOTHING (D49: a
20602
+ * read that fails must never be able to destroy work, and dropping a ring is
20603
+ * destroying work). Only an answer about a rule set that was actually read
20604
+ * can take retention away.
20391
20605
  *
20392
- * This must be REMEMBERED, not just applied: catalog reconcile re-runs
20393
- * `applyClipRetention` on a timer, and without this the arming was wiped
20394
- * within the minute — a rule asking for `high` fell back to `low` forever
20395
- * and looked like the choice did nothing.
20606
+ * Rides the catalog reconcile, which is also the D8 backstop that re-derives
20607
+ * after a rule is created, edited, enabled, disabled or deleted — bounded
20608
+ * staleness (one tick), never a mirror that only follows the rules at boot.
20396
20609
  */
20610
+ async refreshClipRetentionPlan() {
20611
+ const api = this.api;
20612
+ if (!api) return;
20613
+ let plan;
20614
+ try {
20615
+ plan = await api.notificationRules.getClipRetentionAsks.query({});
20616
+ } catch (err) {
20617
+ this.logger.debug("clip retention plan unavailable — keeping the one in force", { meta: {
20618
+ error: require_dist.errMsg(err),
20619
+ known: this.clipRetentionAsks !== null
20620
+ } });
20621
+ return;
20622
+ }
20623
+ if (!plan.rulesLoaded) {
20624
+ this.logger.debug("clip retention plan: the notification centre has not read its rules yet", { meta: { known: this.clipRetentionAsks !== null } });
20625
+ return;
20626
+ }
20627
+ this.clipRetentionAsks = plan.asks;
20628
+ for (const deviceId of this.assignments.keys()) this.applyClipRetention(deviceId);
20629
+ }
20397
20630
  /** Produced event artifacts waiting to be fetched by handle (D9/D18). */
20398
20631
  eventMedia = new EventMediaStore();
20399
- clipRetentionArmed = /* @__PURE__ */ new Map();
20400
- armClipRetention(deviceId, profile) {
20401
- const armed = this.clipRetentionArmed.get(deviceId) ?? /* @__PURE__ */ new Set();
20402
- if (armed.has(profile)) return;
20403
- armed.add(profile);
20404
- this.clipRetentionArmed.set(deviceId, armed);
20632
+ /**
20633
+ * The notification rules' own asks, per camera. `null` = never answered.
20634
+ *
20635
+ * `null` and `[]` are DIFFERENT and the difference is a camera's footage:
20636
+ * `[]` means the centre read its rules and none of them cut footage here,
20637
+ * `null` means nobody has said. Only the first one is allowed to zero a ring.
20638
+ */
20639
+ clipRetentionAsks = null;
20640
+ /**
20641
+ * What a cut was OBSERVED asking for, per camera and profile.
20642
+ *
20643
+ * The old `armClipRetention`, folded into the ask above so retention has ONE
20644
+ * input shape. It still exists because the plan describes the notification
20645
+ * rules and a cut can come from elsewhere (a clips API, an export, a rule
20646
+ * created a second ago on another node): a consumer that asked for a window
20647
+ * this ring did not hold gets it next time. It is REMEMBERED because the
20648
+ * reconcile re-applies retention on a timer, and without that the arming was
20649
+ * wiped within the minute — a rule asking for `high` fell back to `low`
20650
+ * forever and looked like the choice did nothing.
20651
+ */
20652
+ clipRetentionObserved = /* @__PURE__ */ new Map();
20653
+ /** Last reported (seconds, source, profiles) per camera — transitions only. */
20654
+ clipRetentionApplied = /* @__PURE__ */ new Map();
20655
+ /**
20656
+ * A cut asked this camera for `seconds` on `profile` — remember it.
20657
+ *
20658
+ * ONE ask, with both halves: the reactive arming used to carry the profile
20659
+ * and inherit the flat duration, so a consumer that needed a longer window
20660
+ * had no way to say so and a consumer that needed none still paid.
20661
+ */
20662
+ noteClipAsk(deviceId, profile, seconds) {
20663
+ const observed = this.clipRetentionObserved.get(deviceId) ?? /* @__PURE__ */ new Map();
20664
+ const bounded = Math.max(0, Math.min(seconds, CLIP_RETENTION_UNKNOWN_SEC));
20665
+ if ((observed.get(profile) ?? 0) >= bounded && this.clipRetentionObserved.has(deviceId)) return;
20666
+ observed.set(profile, Math.max(observed.get(profile) ?? 0, bounded));
20667
+ this.clipRetentionObserved.set(deviceId, observed);
20405
20668
  this.applyClipRetention(deviceId);
20406
20669
  }
20407
20670
  countPushConsumers(map, deviceId) {
@@ -20701,6 +20964,7 @@ var StreamBrokerManager = class StreamBrokerManager {
20701
20964
  return;
20702
20965
  }
20703
20966
  this.brokers.set(brokerId, broker);
20967
+ this.applyClipRetention(deviceId);
20704
20968
  const existingToken = this.persistedTokens.get(brokerId);
20705
20969
  const warmUp = () => broker.onEncodedData(() => {});
20706
20970
  this.rtspServer.registerRestreamer(brokerId, broker.getRtspRestreamer(), warmUp, existingToken);
@@ -24277,6 +24541,18 @@ var AdaptiveSession = class AdaptiveSession {
24277
24541
  }
24278
24542
  static MAX_RTP_PAYLOAD = 1100;
24279
24543
  rtpPacketsSent = 0;
24544
+ /**
24545
+ * Cumulative RTP packets this session has put on the wire.
24546
+ *
24547
+ * Read by the adaptive ladder's stall detector: "the client received
24548
+ * nothing" only means the link is broken if the broker was SENDING at the
24549
+ * time. Without this half, a paused camera, a battery source between wakes
24550
+ * or any legitimately idle feed reads as a stall and walks the ladder to
24551
+ * the floor for nothing.
24552
+ */
24553
+ get rtpPacketsSentTotal() {
24554
+ return this.rtpPacketsSent;
24555
+ }
24280
24556
  rtpLogCounter = 0;
24281
24557
  /** One-shot guards for the "first video/audio RTP" debug markers. */
24282
24558
  firstVideoRtpLogged = false;
@@ -25064,6 +25340,105 @@ var LossTracker = class {
25064
25340
  }
25065
25341
  };
25066
25342
  //#endregion
25343
+ //#region src/stream-broker/webrtc/stall-tracker.ts
25344
+ /**
25345
+ * Windowed "the media stopped arriving" detector. Pure state machine: no
25346
+ * clock of its own, no side effects — `observe` is the only mutator and every
25347
+ * decision is a function of the samples it was given.
25348
+ */
25349
+ var StallTracker = class {
25350
+ config;
25351
+ /** When the current uninterrupted stall began; null when not stalling. */
25352
+ stallStartedAt = null;
25353
+ blackoutUntil = 0;
25354
+ lastEvidence = {
25355
+ clientKbps: null,
25356
+ stalledForMs: 0,
25357
+ serverSending: false
25358
+ };
25359
+ constructor(config) {
25360
+ this.config = config;
25361
+ }
25362
+ /** A tier change just happened — ignore the re-offer's own dead air. */
25363
+ markChanged(now) {
25364
+ this.stallStartedAt = null;
25365
+ this.blackoutUntil = now + this.config.waitAfterChangeMs;
25366
+ }
25367
+ /**
25368
+ * Fold one evaluation into the window. Returns nothing; ask `isStalled`.
25369
+ *
25370
+ * Three outcomes, and the middle one is the reason this is not a simple
25371
+ * boolean: a rate at or above `recoverKbpsFloor` CLEARS the stall, a rate
25372
+ * below `stallKbpsFloor` starts or extends it, and anything between the two
25373
+ * (or unknown, or with the server idle) HOLDS whatever the window already
25374
+ * believed. Holding is what stops a borderline link from oscillating.
25375
+ */
25376
+ observe(sample, now) {
25377
+ if (now < this.blackoutUntil) {
25378
+ this.stallStartedAt = null;
25379
+ this.lastEvidence = {
25380
+ clientKbps: sample.clientKbps,
25381
+ stalledForMs: 0,
25382
+ serverSending: sample.serverSending
25383
+ };
25384
+ return;
25385
+ }
25386
+ const kbps = sample.clientKbps;
25387
+ const age = sample.clientSampleAgeMs;
25388
+ if (!(kbps !== null && Number.isFinite(kbps) && age !== null && age <= this.config.maxClientSampleAgeMs) || kbps === null) {
25389
+ this.lastEvidence = {
25390
+ clientKbps: null,
25391
+ stalledForMs: this.stalledForMs(now),
25392
+ serverSending: sample.serverSending
25393
+ };
25394
+ return;
25395
+ }
25396
+ if (kbps >= this.config.recoverKbpsFloor) this.stallStartedAt = null;
25397
+ else if (kbps < this.config.stallKbpsFloor && sample.serverSending) this.stallStartedAt ??= now;
25398
+ this.lastEvidence = {
25399
+ clientKbps: kbps,
25400
+ stalledForMs: this.stalledForMs(now),
25401
+ serverSending: sample.serverSending
25402
+ };
25403
+ }
25404
+ stalledForMs(now) {
25405
+ return this.stallStartedAt === null ? 0 : now - this.stallStartedAt;
25406
+ }
25407
+ /** True once the stall has persisted for `stallConfirmMs`. */
25408
+ isStalled(now) {
25409
+ if (now < this.blackoutUntil) return false;
25410
+ return this.stalledForMs(now) >= this.config.stallConfirmMs;
25411
+ }
25412
+ /**
25413
+ * May the ladder PROMOTE on this evaluation?
25414
+ *
25415
+ * False when the last fresh measurement was below `recoverKbpsFloor` —
25416
+ * either a stall building, or a link crawling along in the dead band.
25417
+ * Promoting there spends bandwidth the session has just been shown not to
25418
+ * have, and the loss path cannot object: a stream delivering almost nothing
25419
+ * reports almost no loss. Same doctrine as `LossTracker.hasEvidence` — a
25420
+ * climb needs POSITIVE evidence, not the absence of a complaint.
25421
+ *
25422
+ * Unknown does not block. A deployment with no client telemetry at all must
25423
+ * behave exactly as it did before this tracker existed; the loss path's own
25424
+ * `hasEvidence` is the gate there.
25425
+ */
25426
+ isClimbSafe(now) {
25427
+ if (now < this.blackoutUntil) return true;
25428
+ if (this.stallStartedAt !== null) return false;
25429
+ const last = this.lastEvidence.clientKbps;
25430
+ if (last === null) return true;
25431
+ return last >= this.config.recoverKbpsFloor;
25432
+ }
25433
+ /** What the last `observe` measured — for the switch's log line. */
25434
+ evidence(now) {
25435
+ return {
25436
+ ...this.lastEvidence,
25437
+ stalledForMs: this.stalledForMs(now)
25438
+ };
25439
+ }
25440
+ };
25441
+ //#endregion
25067
25442
  //#region src/stream-broker/webrtc/adaptive-controller.ts
25068
25443
  var TIER_ORDER = require_dist.CAM_PROFILE_ORDER;
25069
25444
  /**
@@ -25085,6 +25460,7 @@ var TIER_ORDER = require_dist.CAM_PROFILE_ORDER;
25085
25460
  var AdaptiveController = class {
25086
25461
  opts;
25087
25462
  tracker;
25463
+ stalls;
25088
25464
  tier;
25089
25465
  mtuReduced = false;
25090
25466
  transcodeEngaged = false;
@@ -25103,6 +25479,7 @@ var AdaptiveController = class {
25103
25479
  constructor(opts) {
25104
25480
  this.opts = opts;
25105
25481
  this.tracker = new LossTracker(opts.config);
25482
+ this.stalls = new StallTracker(opts.config);
25106
25483
  this.tier = opts.initialTier;
25107
25484
  this.failures = new Map(opts.priorFailures ?? []);
25108
25485
  this.upgradeCount = Math.max(0, opts.priorUpgradeCount ?? 0);
@@ -25125,6 +25502,19 @@ var AdaptiveController = class {
25125
25502
  this.tracker.track(sample, now);
25126
25503
  }
25127
25504
  /**
25505
+ * Fold one TRANSPORT measurement into the ladder — what the client actually
25506
+ * received, and whether the broker was sending at the time.
25507
+ *
25508
+ * This is the half the ladder was missing. Packet loss cannot see a freeze
25509
+ * (a stream delivering nothing reports 0 % loss, because there are no gaps
25510
+ * in a sequence that received no packets), and the client's downlink hint
25511
+ * cannot see one either — it is a one-shot HTTP burst measured at connect
25512
+ * time, on a path the media does not take. See `stall-tracker.ts`.
25513
+ */
25514
+ onThroughput(sample, now) {
25515
+ this.stalls.observe(sample, now);
25516
+ }
25517
+ /**
25128
25518
  * Freeze the ladder for this evaluation instead of ticking it.
25129
25519
  *
25130
25520
  * Called while the session's track is NOT carrying the live source — D42
@@ -25145,6 +25535,7 @@ var AdaptiveController = class {
25145
25535
  this.lastChangeAt = now;
25146
25536
  }
25147
25537
  this.healthySince = now;
25538
+ this.stalls.markChanged(now);
25148
25539
  }
25149
25540
  tick(now) {
25150
25541
  if (!this.armed) {
@@ -25152,9 +25543,18 @@ var AdaptiveController = class {
25152
25543
  this.healthySince = now;
25153
25544
  this.lastChangeAt = now;
25154
25545
  }
25546
+ if (this.stalls.isStalled(now)) {
25547
+ this.healthySince = now;
25548
+ this.handleFailing(now, "stall");
25549
+ return;
25550
+ }
25155
25551
  if (this.tracker.isFailing(now)) {
25156
25552
  this.healthySince = now;
25157
- this.handleFailing(now);
25553
+ this.handleFailing(now, "loss");
25554
+ return;
25555
+ }
25556
+ if (!this.stalls.isClimbSafe(now)) {
25557
+ this.healthySince = now;
25158
25558
  return;
25159
25559
  }
25160
25560
  if (!this.tracker.hasEvidence(now)) {
@@ -25180,26 +25580,36 @@ var AdaptiveController = class {
25180
25580
  commitChange(now) {
25181
25581
  this.lastChangeAt = now;
25182
25582
  this.tracker.markChanged(now);
25583
+ this.stalls.markChanged(now);
25584
+ }
25585
+ /** The measurement this decision rests on, snapshotted for the log line. */
25586
+ decisionAt(now, cause) {
25587
+ return {
25588
+ cause,
25589
+ lossRatio: this.tracker.lossRatio(),
25590
+ stall: this.stalls.evidence(now)
25591
+ };
25183
25592
  }
25184
- handleFailing(now) {
25593
+ handleFailing(now, cause) {
25594
+ const decision = this.decisionAt(now, cause);
25185
25595
  const idx = TIER_ORDER.indexOf(this.tier);
25186
25596
  if (idx < TIER_ORDER.length - 1) {
25187
25597
  const failed = this.tier;
25188
25598
  this.failures.set(failed, (this.failures.get(failed) ?? 0) + 1);
25189
25599
  this.tier = TIER_ORDER[idx + 1];
25190
- this.opts.onSwitchTier(this.tier);
25600
+ this.opts.onSwitchTier(this.tier, decision);
25191
25601
  this.commitChange(now);
25192
25602
  return;
25193
25603
  }
25194
25604
  if (!this.mtuReduced) {
25195
25605
  this.mtuReduced = true;
25196
- this.opts.onReduceMtu();
25606
+ this.opts.onReduceMtu(decision);
25197
25607
  this.commitChange(now);
25198
25608
  return;
25199
25609
  }
25200
25610
  if (!this.transcodeEngaged) {
25201
25611
  this.transcodeEngaged = true;
25202
- this.opts.onTranscodeDown();
25612
+ this.opts.onTranscodeDown(decision);
25203
25613
  this.commitChange(now);
25204
25614
  }
25205
25615
  }
@@ -25213,7 +25623,7 @@ var AdaptiveController = class {
25213
25623
  this.mtuReduced = false;
25214
25624
  this.transcodeEngaged = false;
25215
25625
  this.upgradeCount += 1;
25216
- this.opts.onSwitchTier(next);
25626
+ this.opts.onSwitchTier(next, this.decisionAt(now, "healthy"));
25217
25627
  this.commitChange(now);
25218
25628
  this.healthySince = now;
25219
25629
  }
@@ -29354,7 +29764,12 @@ var ADAPTIVE_CONFIG = {
29354
29764
  upgradeHealthyGrowthFactor: 3,
29355
29765
  maxProgressiveUpgradeHealthyMs: 3e4,
29356
29766
  minTimeBetweenChangesMs: 4e3,
29357
- maxUpgradeBackoffMs: 9e5
29767
+ maxUpgradeBackoffMs: 9e5,
29768
+ stallKbpsFloor: 64,
29769
+ stallConfirmMs: 6e3,
29770
+ recoverKbpsFloor: 128,
29771
+ waitAfterChangeMs: 8e3,
29772
+ maxClientSampleAgeMs: 1e4
29358
29773
  };
29359
29774
  /**
29360
29775
  * True when `broker` is an in-process StreamBroker exposing the Opus egress
@@ -29498,7 +29913,7 @@ var BrokerWebrtcServer = class {
29498
29913
  staticIceServers;
29499
29914
  icePortRange;
29500
29915
  iceAdditionalHostAddresses;
29501
- clientLossPct;
29916
+ clientNetworkStats;
29502
29917
  locateRecordedSegment;
29503
29918
  readRecordedSegmentBytes;
29504
29919
  readRecordedGopBytes;
@@ -29565,7 +29980,7 @@ var BrokerWebrtcServer = class {
29565
29980
  this.staticIceServers = options.iceServers;
29566
29981
  this.icePortRange = options.icePortRange;
29567
29982
  this.iceAdditionalHostAddresses = options.iceAdditionalHostAddresses;
29568
- this.clientLossPct = options.clientLossPct;
29983
+ this.clientNetworkStats = options.clientNetworkStats;
29569
29984
  this.locateRecordedSegment = options.locateRecordedSegment;
29570
29985
  this.readRecordedSegmentBytes = options.readRecordedSegmentBytes;
29571
29986
  this.readRecordedGopBytes = options.readRecordedGopBytes;
@@ -30503,12 +30918,27 @@ var BrokerWebrtcServer = class {
30503
30918
  priorFailures,
30504
30919
  priorUpgradeCount,
30505
30920
  config: ADAPTIVE_CONFIG,
30506
- onSwitchTier: (tier) => {
30507
- this.switchSessionTier(entry, deviceId, tier, sessionId);
30921
+ onSwitchTier: (tier, decision) => {
30922
+ this.switchSessionTier(entry, deviceId, tier, sessionId, false, decision);
30508
30923
  },
30509
- onReduceMtu: () => entry.session.setRepacketizerMtu(AdaptiveSession.ADAPTIVE_LOW_MTU),
30510
- onTranscodeDown: () => {
30511
- this.switchSessionTier(entry, deviceId, "low", sessionId, true);
30924
+ onReduceMtu: (decision) => {
30925
+ entry.session.setRepacketizerMtu(AdaptiveSession.ADAPTIVE_LOW_MTU);
30926
+ this.logger.info("adaptive: reducing repacketizer MTU", {
30927
+ tags: { deviceId },
30928
+ meta: {
30929
+ sessionId,
30930
+ deviceId,
30931
+ mtu: AdaptiveSession.ADAPTIVE_LOW_MTU,
30932
+ reason: decision.cause,
30933
+ lossRatio: Number(decision.lossRatio.toFixed(4)),
30934
+ clientKbps: decision.stall.clientKbps,
30935
+ stalledForMs: decision.stall.stalledForMs,
30936
+ serverSending: decision.stall.serverSending
30937
+ }
30938
+ });
30939
+ },
30940
+ onTranscodeDown: (decision) => {
30941
+ this.switchSessionTier(entry, deviceId, "low", sessionId, true, decision);
30512
30942
  }
30513
30943
  });
30514
30944
  entry.session.onReceiverReport((s) => controller.onLoss(s, Date.now()));
@@ -30516,6 +30946,9 @@ var BrokerWebrtcServer = class {
30516
30946
  let clientHighestSequence = 0;
30517
30947
  let clientPacketsLost = 0;
30518
30948
  let tickInFlight = false;
30949
+ /** RTP packets sent as of the previous evaluation — the "was the broker
30950
+ * sending?" half of the stall verdict. */
30951
+ let lastRtpSent = entry.session.rtpPacketsSentTotal;
30519
30952
  const CLIENT_LOSS_SPAN = 1e3;
30520
30953
  entry.adaptiveTimer = setInterval(() => {
30521
30954
  if (tickInFlight) return;
@@ -30524,10 +30957,20 @@ var BrokerWebrtcServer = class {
30524
30957
  try {
30525
30958
  if (entry.timeline?.currentMode === "recorded") {
30526
30959
  controller.suspend(Date.now());
30960
+ lastRtpSent = entry.session.rtpPacketsSentTotal;
30527
30961
  return;
30528
30962
  }
30529
- const pct = this.clientLossPct ? await this.clientLossPct(deviceId) : null;
30963
+ const client = this.clientNetworkStats ? await this.clientNetworkStats(deviceId) : null;
30530
30964
  const now = Date.now();
30965
+ const rtpSent = entry.session.rtpPacketsSentTotal;
30966
+ const serverSending = rtpSent > lastRtpSent;
30967
+ lastRtpSent = rtpSent;
30968
+ controller.onThroughput({
30969
+ clientKbps: client ? client.estimatedBandwidthKbps : null,
30970
+ clientSampleAgeMs: client ? Math.max(0, now - client.lastUpdated) : null,
30971
+ serverSending
30972
+ }, now);
30973
+ const pct = client?.packetLossPercent ?? null;
30531
30974
  if (pct !== null && pct !== void 0) {
30532
30975
  clientHighestSequence += CLIENT_LOSS_SPAN;
30533
30976
  clientPacketsLost += Math.round(Math.max(0, Math.min(100, pct)) / 100 * CLIENT_LOSS_SPAN);
@@ -30556,7 +30999,7 @@ var BrokerWebrtcServer = class {
30556
30999
  * consumes the recorded intent to pin the recreated session to `tier` and
30557
31000
  * apply the reduced transcode profile when `forceTranscodeDown` is set.
30558
31001
  */
30559
- switchSessionTier(entry, deviceId, tier, sessionId, forceTranscodeDown = false) {
31002
+ switchSessionTier(entry, deviceId, tier, sessionId, forceTranscodeDown = false, decision) {
30560
31003
  const prev = this.adaptiveIntent.get(sessionId);
30561
31004
  const failures = new Map(entry.adaptiveController?.failureCounts ?? prev?.failures);
30562
31005
  const upgradeCount = entry.adaptiveController?.successfulUpgradeCount ?? prev?.upgradeCount ?? 0;
@@ -30580,7 +31023,12 @@ var BrokerWebrtcServer = class {
30580
31023
  forceTranscodeDown,
30581
31024
  epoch,
30582
31025
  failures: Object.fromEntries(failures),
30583
- upgradeCount
31026
+ upgradeCount,
31027
+ reason: decision?.cause ?? "unknown",
31028
+ lossRatio: decision ? Number(decision.lossRatio.toFixed(4)) : null,
31029
+ clientKbps: decision?.stall.clientKbps ?? null,
31030
+ stalledForMs: decision?.stall.stalledForMs ?? null,
31031
+ serverSending: decision?.stall.serverSending ?? null
30584
31032
  }
30585
31033
  });
30586
31034
  }
@@ -31652,11 +32100,17 @@ var StreamBrokerAddon = class extends require_dist.BaseAddon {
31652
32100
  return [];
31653
32101
  }
31654
32102
  },
31655
- clientLossPct: async (deviceId) => {
32103
+ clientNetworkStats: async (deviceId) => {
31656
32104
  const ctrl = new AbortController();
31657
32105
  const timer = setTimeout(() => ctrl.abort(), NETWORK_QUALITY_STATS_TIMEOUT_MS);
31658
32106
  try {
31659
- return (await this.ctx.api.networkQuality.getDeviceStats.query({ deviceId }, { signal: ctrl.signal }))?.client?.packetLossPercent ?? null;
32107
+ const client = (await this.ctx.api.networkQuality.getDeviceStats.query({ deviceId }, { signal: ctrl.signal }))?.client;
32108
+ if (!client) return null;
32109
+ return {
32110
+ packetLossPercent: client.packetLossPercent,
32111
+ estimatedBandwidthKbps: client.estimatedBandwidthKbps,
32112
+ lastUpdated: client.lastUpdated
32113
+ };
31660
32114
  } catch {
31661
32115
  return null;
31662
32116
  } finally {