@camstack/addon-pipeline 1.2.284 → 1.2.286

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-XmUJCdXn.js → default-detection-model-B9sgmjNE.js} +1 -1
  4. package/dist/{default-detection-model-DuK-Xu10.mjs → default-detection-model-BbaeqJWd.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-CWJ_t5jq.mjs → dist-L6fspiMs.mjs} +1479 -3
  8. package/dist/{dist-BS_GvN2R.js → dist-bAVvh6zw.js} +1496 -2
  9. package/dist/motion-wasm/index.js +1 -1
  10. package/dist/motion-wasm/index.mjs +1 -1
  11. package/dist/{node-CqOIo6CL.js → node-BAHXRXps.js} +1 -1
  12. package/dist/{node-Cz6ByejK.mjs → node-BKSzm60G.mjs} +1 -1
  13. package/dist/pipeline-runner/index.js +3 -3
  14. package/dist/pipeline-runner/index.mjs +3 -3
  15. package/dist/{process-memory-BFycx9BF.js → process-memory-B5Enn1QL.js} +1 -1
  16. package/dist/{process-memory-DAwCuIeI.mjs → process-memory-gIGkgoj7.mjs} +1 -1
  17. package/dist/recorder/index.js +2 -2
  18. package/dist/recorder/index.mjs +2 -2
  19. package/dist/{segment-demux-js-r4t4zgdr.js → segment-demux-js-BCxiqxMJ.js} +166 -148
  20. package/dist/{segment-demux-js-gLuKs5Mg.mjs → segment-demux-js-DIMPKsLl.mjs} +149 -149
  21. package/dist/stream-broker/_stub.js +2 -2
  22. package/dist/stream-broker/{_virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-CROj7Fwq.mjs → _virtual_mf-localSharedImportMap___mfe_internal__addon_stream_broker_widgets-BQppCOBh.mjs} +2 -2
  23. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-BxqJbWC8.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-DWcIgV47.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-DvNcV7kj.mjs → hostInit-BMoj1Wh2.mjs} +2 -2
  28. package/dist/stream-broker/index.js +2193 -51
  29. package/dist/stream-broker/index.mjs +2193 -51
  30. package/dist/stream-broker/remoteEntry.js +1 -1
  31. package/package.json +1 -1
  32. package/dist/stream-broker/_virtual_mf___mfe_internal__addon_stream_broker_widgets__loadShare___mf_0_camstack_mf_1_types__loadShare__.js-DByJbFCy.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-BnLVuoDg.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-BS_GvN2R.js");
7
- const require_node = require("../node-CqOIo6CL.js");
6
+ const require_dist = require("../dist-bAVvh6zw.js");
7
+ const require_node = require("../node-BAHXRXps.js");
8
8
  const require_restream_intent = require("../restream-intent-7uDNqMKs.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-r4t4zgdr.js");
11
+ const require_segment_demux_js = require("../segment-demux-js-BCxiqxMJ.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");
@@ -28,6 +28,7 @@ let node_dgram = require("node:dgram");
28
28
  let events = require("events");
29
29
  let net = require("net");
30
30
  let node_dns_promises = require("node:dns/promises");
31
+ let node_http = require("node:http");
31
32
  let node_worker_threads = require("node:worker_threads");
32
33
  let node_events = require("node:events");
33
34
  //#region src/stream-broker/client-log-ingest.ts
@@ -26690,6 +26691,29 @@ var ServerMessageSchema = require_dist.discriminatedUnion("t", [
26690
26691
  t: require_dist.literal("clipDuration"),
26691
26692
  clipId: require_dist.string().min(1),
26692
26693
  durationMs: require_dist.number().positive()
26694
+ }),
26695
+ require_dist.object({
26696
+ t: require_dist.literal("clipTransport"),
26697
+ clipId: require_dist.string().min(1),
26698
+ /** `forward` = only ahead of the playhead. `free` = anywhere. */
26699
+ seek: require_dist._enum(["forward", "free"]),
26700
+ /** Frame-step BACKWARD is meaningful. Forward always is. */
26701
+ stepBack: require_dist.boolean()
26702
+ }),
26703
+ require_dist.object({
26704
+ t: require_dist.literal("clipAudio"),
26705
+ clipId: require_dist.string().min(1),
26706
+ carried: require_dist.boolean(),
26707
+ codec: require_dist._enum(["opus", "pcmu"]).optional(),
26708
+ /** Why there is no sound. Present iff `carried` is false. */
26709
+ reason: require_dist._enum([
26710
+ "not-negotiated",
26711
+ "not-offered",
26712
+ "no-audio-track",
26713
+ "unsupported-codec",
26714
+ "codec-mismatch",
26715
+ "stream-failed"
26716
+ ]).optional()
26693
26717
  })
26694
26718
  ]);
26695
26719
  function parseControlMessage(raw) {
@@ -26706,11 +26730,49 @@ function serializeServerMessage(msg) {
26706
26730
  //#endregion
26707
26731
  //#region src/stream-broker/recorded/feed-clock.ts
26708
26732
  /**
26733
+ * The ORIGIN two tracks are measured from — the base and the source anchor,
26734
+ * shared, so the offset between video and audio survives a discontinuity.
26735
+ *
26736
+ * ## Why this exists
26737
+ *
26738
+ * `markDiscontinuity` re-bases on the last output. With one instance per
26739
+ * track, that last output is the TRACK'S own, and the two differ by however
26740
+ * far apart the interleave left them — so every seek replaced the real A/V
26741
+ * offset with that gap, in the same direction each time. Measured on the
26742
+ * clock alone it grows by a fixed step each time and never comes back; how
26743
+ * big that step is, is the interleave's business — 10–23 ms a seek on a
26744
+ * synthetic fixture, **900 ms over three seeks on a real clip**.
26745
+ *
26746
+ * Calling `markSeek()` on both instances "together" did not help and could
26747
+ * not: it is not the CALL that has to be shared, it is the ORIGIN. With one,
26748
+ * `out_video − out_audio` is `(vPts − aPts) / rate` — exactly the source
26749
+ * offset, which is the entire content of lip sync — and there is nothing left
26750
+ * to keep in step.
26751
+ *
26752
+ * `lastOutMs` stays per-track: monotonicity is a promise to ONE receiver
26753
+ * track, and clamping video against audio's last stamp would move video.
26754
+ */
26755
+ var FeedOrigin = class {
26756
+ /** Output ms the current run is measured from. */
26757
+ outBaseMs = 0;
26758
+ /** Source ms the current run is measured from; `null` until the next stamp. */
26759
+ srcAnchorMs = null;
26760
+ /**
26761
+ * The furthest EITHER track has been stamped. What a discontinuity clears,
26762
+ * so the gap is opened past both — and so two clocks marking the same
26763
+ * discontinuity in a row produce one gap, not two.
26764
+ */
26765
+ highWaterMs = 0;
26766
+ };
26767
+ /**
26709
26768
  * Maps arbitrary, possibly-resetting source PTS (ms) onto a strictly increasing
26710
26769
  * output clock at `rateHz` (default 90 kHz video; pass 8000 for PCMU audio). On
26711
26770
  * seek the output keeps advancing while the source anchor re-bases, so
26712
- * per-segment PTS resets never rewind the track. Video and audio use SEPARATE
26713
- * instances that `markSeek()` together to stay A/V aligned across boundaries.
26771
+ * per-segment PTS resets never rewind the track.
26772
+ *
26773
+ * Video and audio are separate instances SHARING one {@link FeedOrigin} — see
26774
+ * there for what happens when they do not. An instance constructed without one
26775
+ * gets its own, so a lone clock behaves exactly as it always did.
26714
26776
  *
26715
26777
  * Playback rate: `setRate(r)` divides SOURCE elapsed time by `r` on the output
26716
26778
  * timeline (2× → half the output spacing → the receiver plays frames twice as
@@ -26720,20 +26782,21 @@ function serializeServerMessage(msg) {
26720
26782
  */
26721
26783
  var MonotonicClock = class {
26722
26784
  rateHz;
26723
- srcAnchorMs = null;
26724
26785
  lastSrcMs = null;
26725
- outBaseMs = 0;
26726
26786
  lastOutMs = 0;
26727
26787
  rate = 1;
26728
- constructor(rateHz = 9e4) {
26788
+ origin;
26789
+ constructor(rateHz = 9e4, origin) {
26729
26790
  this.rateHz = rateHz;
26791
+ this.origin = origin ?? new FeedOrigin();
26730
26792
  }
26731
26793
  stamp(srcPtsMs) {
26732
- if (this.srcAnchorMs === null) this.srcAnchorMs = srcPtsMs;
26794
+ if (this.origin.srcAnchorMs === null) this.origin.srcAnchorMs = srcPtsMs;
26733
26795
  this.lastSrcMs = srcPtsMs;
26734
- const elapsed = Math.max(0, (srcPtsMs - this.srcAnchorMs) / this.rate);
26735
- const outMs = this.outBaseMs + elapsed;
26796
+ const elapsed = Math.max(0, (srcPtsMs - this.origin.srcAnchorMs) / this.rate);
26797
+ const outMs = this.origin.outBaseMs + elapsed;
26736
26798
  this.lastOutMs = Math.max(this.lastOutMs, outMs);
26799
+ this.origin.highWaterMs = Math.max(this.origin.highWaterMs, this.lastOutMs);
26737
26800
  return Math.round(this.lastOutMs * this.rateHz / 1e3);
26738
26801
  }
26739
26802
  /**
@@ -26757,8 +26820,8 @@ var MonotonicClock = class {
26757
26820
  * rate-scaled.
26758
26821
  */
26759
26822
  markDiscontinuity(gapMs) {
26760
- this.outBaseMs = this.lastOutMs + gapMs;
26761
- this.srcAnchorMs = null;
26823
+ this.origin.outBaseMs = this.origin.highWaterMs + gapMs;
26824
+ this.origin.srcAnchorMs = null;
26762
26825
  this.lastSrcMs = null;
26763
26826
  }
26764
26827
  /**
@@ -26770,9 +26833,9 @@ var MonotonicClock = class {
26770
26833
  */
26771
26834
  setRate(rate) {
26772
26835
  if (!(rate > 0) || rate === this.rate) return;
26773
- if (this.srcAnchorMs !== null && this.lastSrcMs !== null) {
26774
- this.outBaseMs = this.lastOutMs;
26775
- this.srcAnchorMs = this.lastSrcMs;
26836
+ if (this.origin.srcAnchorMs !== null && this.lastSrcMs !== null) {
26837
+ this.origin.outBaseMs = this.lastOutMs;
26838
+ this.origin.srcAnchorMs = this.lastSrcMs;
26776
26839
  }
26777
26840
  this.rate = rate;
26778
26841
  }
@@ -27267,7 +27330,7 @@ var SEGMENT_TIMEOUT_MS = 8e3;
27267
27330
  * 2 ms, p95 202 ms, max 285 ms), so a segment that CAN yield audio still does.
27268
27331
  */
27269
27332
  var AUDIO_DEMUX_TIMEOUT_MS = 1500;
27270
- var POSITION_INTERVAL_MS = 1e3;
27333
+ var POSITION_INTERVAL_MS$1 = 1e3;
27271
27334
  /** How often `recorded:play-census` fires while the advance loop is alive. */
27272
27335
  var PLAY_CENSUS_INTERVAL_TICKS = 5;
27273
27336
  /**
@@ -27289,7 +27352,7 @@ var PTS_REPAIR_LOG_MEMORY = 64;
27289
27352
  * PTS gap (or an empty/tail wait) still re-checks the abort guards promptly,
27290
27353
  * keeping seeks responsive without per-frame drift.
27291
27354
  */
27292
- var WAIT_TICK_MS = 250;
27355
+ var WAIT_TICK_MS$1 = 250;
27293
27356
  /**
27294
27357
  * Output-clock gap stamped at every seek/scrub commit (markDiscontinuity on
27295
27358
  * both A/V clocks). Root cause of the "smeared frame on scrub": the aborted
@@ -27302,7 +27365,7 @@ var WAIT_TICK_MS = 250;
27302
27365
  * can't look contiguous with the new GOP, and the receiver resets timing
27303
27366
  * instead of merging the two feeds.
27304
27367
  */
27305
- var SEEK_DISCONTINUITY_MS = 250;
27368
+ var SEEK_DISCONTINUITY_MS$1 = 250;
27306
27369
  /**
27307
27370
  * Coalescing window for feeder-scrub `scrubTo` (experimental). Pointer-move
27308
27371
  * targets arrive far faster than a segment demux completes; the feeder collapses
@@ -27611,7 +27674,14 @@ function emptyPlayCensus() {
27611
27674
  }
27612
27675
  var RecordedFeeder = class {
27613
27676
  deps;
27614
- clock = new MonotonicClock();
27677
+ /**
27678
+ * ONE origin for both tracks. Two independent ones replaced the real A/V
27679
+ * offset with the interleave gap at every seek, in the same direction each
27680
+ * time — measured at 23 ms per seek on the clock alone and 900 ms over
27681
+ * three seeks on a real clip. See `FeedOrigin`.
27682
+ */
27683
+ feedOrigin = new FeedOrigin();
27684
+ clock = new MonotonicClock(9e4, this.feedOrigin);
27615
27685
  /** Audio clock (PCMU 8 kHz / Opus 48 kHz); re-anchored together with video. */
27616
27686
  audioClock;
27617
27687
  clockHasAdvanced = false;
@@ -27794,7 +27864,7 @@ var RecordedFeeder = class {
27794
27864
  lastAnnouncedState = null;
27795
27865
  constructor(deps) {
27796
27866
  this.deps = deps;
27797
- this.audioClock = new MonotonicClock(deps.audioClockRateHz ?? 8e3);
27867
+ this.audioClock = new MonotonicClock(deps.audioClockRateHz ?? 8e3, this.feedOrigin);
27798
27868
  this.scrubCache = deps.fragmentCache ?? new ScrubFragmentCache();
27799
27869
  }
27800
27870
  /**
@@ -27888,8 +27958,8 @@ var RecordedFeeder = class {
27888
27958
  try {
27889
27959
  this.emitState("loading", "seek-start", token, dragId);
27890
27960
  if (this.clockHasAdvanced) {
27891
- this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS);
27892
- this.audioClock.markDiscontinuity(SEEK_DISCONTINUITY_MS);
27961
+ this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS$1);
27962
+ this.audioClock.markDiscontinuity(SEEK_DISCONTINUITY_MS$1);
27893
27963
  }
27894
27964
  const locateStartedAt = performance.now();
27895
27965
  let loc = await this.withTimeout(this.deps.locate({
@@ -28764,12 +28834,12 @@ var RecordedFeeder = class {
28764
28834
  return;
28765
28835
  }
28766
28836
  for (const s of jumpTravel) {
28767
- if (this.clockHasAdvanced) this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS);
28837
+ if (this.clockHasAdvanced) this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS$1);
28768
28838
  this.deps.sink.pushAccessUnit(s.au.data, this.clock.stamp(s.au.ptsMs), s.au.codec);
28769
28839
  this.clockHasAdvanced = true;
28770
28840
  this.deps.onPosition(this.clampIntoSegment(s.seg, s.tMs));
28771
28841
  }
28772
- if ((mode === "jump" || rewind) && this.clockHasAdvanced) this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS);
28842
+ if ((mode === "jump" || rewind) && this.clockHasAdvanced) this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS$1);
28773
28843
  else if (crossesFragment && this.clockHasAdvanced) this.clock.markSeek();
28774
28844
  for (const au of aus) this.deps.sink.pushAccessUnit(au.data, this.clock.stamp(au.ptsMs), au.codec);
28775
28845
  const pushedAtMs = performance.now();
@@ -29193,7 +29263,7 @@ var RecordedFeeder = class {
29193
29263
  if (still === "deadline") break;
29194
29264
  if (still !== null && still.seg.startMs !== lastFragStartMs && !fragmentCovers(still.seg, toMs) && !fragmentCovers(still.seg, fromMs)) {
29195
29265
  lastFragStartMs = still.seg.startMs;
29196
- if (this.clockHasAdvanced) this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS);
29266
+ if (this.clockHasAdvanced) this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS$1);
29197
29267
  this.deps.sink.pushAccessUnit(still.au.data, this.clock.stamp(still.au.ptsMs), still.au.codec);
29198
29268
  this.clockHasAdvanced = true;
29199
29269
  this.deps.onPosition(this.clampIntoSegment(still.seg, tMs));
@@ -29640,7 +29710,7 @@ var RecordedFeeder = class {
29640
29710
  if (this.disposed || gen !== this.advanceToken || this.rate === 0) return true;
29641
29711
  const remaining = targetWall - performance.now();
29642
29712
  if (remaining <= 0) return false;
29643
- const tick = Math.min(remaining, WAIT_TICK_MS);
29713
+ const tick = Math.min(remaining, WAIT_TICK_MS$1);
29644
29714
  await new Promise((resolve) => setTimeout(resolve, tick));
29645
29715
  }
29646
29716
  }
@@ -29765,7 +29835,7 @@ var RecordedFeeder = class {
29765
29835
  if (this.rate <= 0) return;
29766
29836
  this.positionTicks += 1;
29767
29837
  if (this.positionTicks % PLAY_CENSUS_INTERVAL_TICKS === 0) this.emitPlayCensus("tick");
29768
- }, POSITION_INTERVAL_MS);
29838
+ }, POSITION_INTERVAL_MS$1);
29769
29839
  }
29770
29840
  /** Tear down the ~1 Hz position interval so it stops reporting a stale cursor. */
29771
29841
  stopPositionReports() {
@@ -30061,7 +30131,97 @@ var TimelineSession = class {
30061
30131
  this.lastPositionMs = null;
30062
30132
  const feeder = this.enterRecorded();
30063
30133
  await feeder.seek(offsetMs);
30064
- await this.reportClipDuration(clipId, feeder);
30134
+ this.reportClipTransport(clipId, feeder);
30135
+ await Promise.all([this.reportClipDuration(clipId, feeder), this.reportClipAudio(clipId, feeder)]);
30136
+ }
30137
+ /**
30138
+ * Tell the viewer what this clip can be driven to do.
30139
+ *
30140
+ * A clip served from the provider's stream is forward-only; one fetched as
30141
+ * a file is not. Without the answer the viewer draws the same progress bar
30142
+ * for both, and on the stream a backward drag is refused and the position
30143
+ * re-sent — a control that visibly undoes the operator's gesture, which
30144
+ * reads as broken rather than as unavailable (D62).
30145
+ *
30146
+ * A feeder that does not say sends nothing: silence is "the freedom you
30147
+ * always had", and claiming `free` for a feeder that never answered would
30148
+ * be this surface inventing a capability.
30149
+ */
30150
+ reportClipTransport(clipId, feeder) {
30151
+ const report = feeder.transport;
30152
+ if (report === void 0) {
30153
+ this.deps.onLog?.("clip playback: transport not reported — this feeder cannot say", {
30154
+ deviceId: this.deps.deviceId,
30155
+ clipId
30156
+ });
30157
+ return;
30158
+ }
30159
+ if (this.clip?.clipId !== clipId) return;
30160
+ this.deps.onLog?.("clip playback: transport reported", {
30161
+ deviceId: this.deps.deviceId,
30162
+ clipId,
30163
+ seek: report.seek,
30164
+ stepBack: report.stepBack
30165
+ });
30166
+ this.deps.session.sendControl(serializeServerMessage({
30167
+ t: "clipTransport",
30168
+ clipId,
30169
+ seek: report.seek,
30170
+ stepBack: report.stepBack
30171
+ }));
30172
+ }
30173
+ /**
30174
+ * Tell the viewer whether this clip has sound.
30175
+ *
30176
+ * Silence has three causes the operator cannot tell apart by listening —
30177
+ * a camera with no microphone, a transport that carried no audio, and a
30178
+ * session that negotiated none — so the viewer is told which. Every branch
30179
+ * that sends nothing says so (D391): a feeder that cannot answer, an answer
30180
+ * that rejected, and an answer that landed after the pointer had left the
30181
+ * clip, which must not be pinned on whatever came next.
30182
+ */
30183
+ async reportClipAudio(clipId, feeder) {
30184
+ if (feeder.audio === void 0) {
30185
+ this.deps.onLog?.("clip playback: audio not reported — this feeder cannot say", {
30186
+ deviceId: this.deps.deviceId,
30187
+ clipId
30188
+ });
30189
+ return;
30190
+ }
30191
+ let report;
30192
+ try {
30193
+ report = await feeder.audio();
30194
+ } catch (err) {
30195
+ this.deps.onLog?.("clip playback: audio not reported — the answer failed", {
30196
+ deviceId: this.deps.deviceId,
30197
+ clipId,
30198
+ error: err instanceof Error ? err.message : String(err)
30199
+ });
30200
+ return;
30201
+ }
30202
+ if (this.clip?.clipId !== clipId) {
30203
+ this.deps.onLog?.("clip playback: audio dropped — the pointer has left this clip", {
30204
+ deviceId: this.deps.deviceId,
30205
+ clipId,
30206
+ carried: report.carried
30207
+ });
30208
+ return;
30209
+ }
30210
+ this.deps.onLog?.("clip playback: audio reported", {
30211
+ deviceId: this.deps.deviceId,
30212
+ clipId,
30213
+ carried: report.carried,
30214
+ codec: report.codec,
30215
+ reason: report.reason,
30216
+ detail: report.detail
30217
+ });
30218
+ this.deps.session.sendControl(serializeServerMessage({
30219
+ t: "clipAudio",
30220
+ clipId,
30221
+ carried: report.carried,
30222
+ ...report.codec !== null ? { codec: report.codec } : {},
30223
+ ...report.reason !== null ? { reason: report.reason } : {}
30224
+ }));
30065
30225
  }
30066
30226
  /**
30067
30227
  * Tell the viewer how long the file on the pointer REALLY is.
@@ -30104,6 +30264,13 @@ var TimelineSession = class {
30104
30264
  });
30105
30265
  return;
30106
30266
  }
30267
+ if (durationMs === null) {
30268
+ this.deps.onLog?.("clip playback: duration unknown — the source ended without a length", {
30269
+ deviceId: this.deps.deviceId,
30270
+ clipId
30271
+ });
30272
+ return;
30273
+ }
30107
30274
  if (!(durationMs > 0)) {
30108
30275
  this.deps.onLog?.("clip playback: duration not reported — the measure is not positive", {
30109
30276
  deviceId: this.deps.deviceId,
@@ -30302,7 +30469,7 @@ var LARGE_SIZE_HEADER_LENGTH = 16;
30302
30469
  * structurally impossible (a size that does not fit), which the caller turns
30303
30470
  * into `malformed` — never a throw, because these bytes come off a camera.
30304
30471
  */
30305
- function childBoxes(bytes, start, end) {
30472
+ function childBoxes$1(bytes, start, end) {
30306
30473
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
30307
30474
  const out = [];
30308
30475
  let off = start;
@@ -30332,7 +30499,7 @@ function childBoxes(bytes, start, end) {
30332
30499
  }
30333
30500
  return out;
30334
30501
  }
30335
- function findBox(boxes, type) {
30502
+ function findBox$1(boxes, type) {
30336
30503
  for (const b of boxes) if (b.type === type) return b;
30337
30504
  return null;
30338
30505
  }
@@ -30362,7 +30529,7 @@ function readSyncSampleNumbers(bytes, box) {
30362
30529
  for (let i = 0; i < entries; i++) out.add(view.getUint32(base + 4 + i * 4));
30363
30530
  return out;
30364
30531
  }
30365
- function mediaTimescale(bytes, mdhd) {
30532
+ function mediaTimescale$1(bytes, mdhd) {
30366
30533
  const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
30367
30534
  const version = bytes[mdhd.contentStart] ?? 0;
30368
30535
  const at = mdhd.contentStart + 4 + (version === 1 ? 16 : 8);
@@ -30370,7 +30537,7 @@ function mediaTimescale(bytes, mdhd) {
30370
30537
  const ts = view.getUint32(at);
30371
30538
  return ts > 0 ? ts : null;
30372
30539
  }
30373
- function handlerType(bytes, hdlr) {
30540
+ function handlerType$1(bytes, hdlr) {
30374
30541
  const at = hdlr.contentStart + 8;
30375
30542
  if (at + FOURCC_LENGTH > hdlr.end) return "";
30376
30543
  let out = "";
@@ -30379,16 +30546,16 @@ function handlerType(bytes, hdlr) {
30379
30546
  }
30380
30547
  /** The video `trak` of a `moov`, or `null` when it has none. */
30381
30548
  function videoTrack(bytes, moov) {
30382
- const children = childBoxes(bytes, moov.contentStart, moov.end);
30549
+ const children = childBoxes$1(bytes, moov.contentStart, moov.end);
30383
30550
  if (!children) return null;
30384
30551
  for (const trak of children) {
30385
30552
  if (trak.type !== "trak") continue;
30386
- const trakKids = childBoxes(bytes, trak.contentStart, trak.end);
30387
- const mdia = trakKids ? findBox(trakKids, "mdia") : null;
30553
+ const trakKids = childBoxes$1(bytes, trak.contentStart, trak.end);
30554
+ const mdia = trakKids ? findBox$1(trakKids, "mdia") : null;
30388
30555
  if (!mdia) continue;
30389
- const mdiaKids = childBoxes(bytes, mdia.contentStart, mdia.end);
30390
- const hdlr = mdiaKids ? findBox(mdiaKids, "hdlr") : null;
30391
- if (hdlr && handlerType(bytes, hdlr) === "vide") return mdia;
30556
+ const mdiaKids = childBoxes$1(bytes, mdia.contentStart, mdia.end);
30557
+ const hdlr = mdiaKids ? findBox$1(mdiaKids, "hdlr") : null;
30558
+ if (hdlr && handlerType$1(bytes, hdlr) === "vide") return mdia;
30392
30559
  }
30393
30560
  return null;
30394
30561
  }
@@ -30397,12 +30564,12 @@ function videoTrack(bytes, moov) {
30397
30564
  * comes back as a named refusal so the caller can say WHY a clip will not play.
30398
30565
  */
30399
30566
  function indexClipMp4(bytes) {
30400
- const top = childBoxes(bytes, 0, bytes.length);
30567
+ const top = childBoxes$1(bytes, 0, bytes.length);
30401
30568
  if (!top) return {
30402
30569
  kind: "refused",
30403
30570
  reason: "malformed"
30404
30571
  };
30405
- const moov = findBox(top, "moov");
30572
+ const moov = findBox$1(top, "moov");
30406
30573
  if (!moov) return {
30407
30574
  kind: "refused",
30408
30575
  reason: "no-moov"
@@ -30412,34 +30579,34 @@ function indexClipMp4(bytes) {
30412
30579
  kind: "refused",
30413
30580
  reason: "no-video-track"
30414
30581
  };
30415
- const mdiaKids = childBoxes(bytes, mdia.contentStart, mdia.end);
30582
+ const mdiaKids = childBoxes$1(bytes, mdia.contentStart, mdia.end);
30416
30583
  if (!mdiaKids) return {
30417
30584
  kind: "refused",
30418
30585
  reason: "malformed"
30419
30586
  };
30420
- const mdhd = findBox(mdiaKids, "mdhd");
30421
- const minf = findBox(mdiaKids, "minf");
30587
+ const mdhd = findBox$1(mdiaKids, "mdhd");
30588
+ const minf = findBox$1(mdiaKids, "minf");
30422
30589
  if (!mdhd || !minf) return {
30423
30590
  kind: "refused",
30424
30591
  reason: "malformed"
30425
30592
  };
30426
- const timescale = mediaTimescale(bytes, mdhd);
30593
+ const timescale = mediaTimescale$1(bytes, mdhd);
30427
30594
  if (timescale === null) return {
30428
30595
  kind: "refused",
30429
30596
  reason: "malformed"
30430
30597
  };
30431
- const minfKids = childBoxes(bytes, minf.contentStart, minf.end);
30432
- const stbl = minfKids ? findBox(minfKids, "stbl") : null;
30598
+ const minfKids = childBoxes$1(bytes, minf.contentStart, minf.end);
30599
+ const stbl = minfKids ? findBox$1(minfKids, "stbl") : null;
30433
30600
  if (!stbl) return {
30434
30601
  kind: "refused",
30435
30602
  reason: "no-samples"
30436
30603
  };
30437
- const stblKids = childBoxes(bytes, stbl.contentStart, stbl.end);
30604
+ const stblKids = childBoxes$1(bytes, stbl.contentStart, stbl.end);
30438
30605
  if (!stblKids) return {
30439
30606
  kind: "refused",
30440
30607
  reason: "malformed"
30441
30608
  };
30442
- const sttsBox = findBox(stblKids, "stts");
30609
+ const sttsBox = findBox$1(stblKids, "stts");
30443
30610
  if (!sttsBox) return {
30444
30611
  kind: "refused",
30445
30612
  reason: "no-samples"
@@ -30449,7 +30616,7 @@ function indexClipMp4(bytes) {
30449
30616
  kind: "refused",
30450
30617
  reason: "malformed"
30451
30618
  };
30452
- const stssBox = findBox(stblKids, "stss");
30619
+ const stssBox = findBox$1(stblKids, "stss");
30453
30620
  const syncNumbers = stssBox === null ? null : readSyncSampleNumbers(bytes, stssBox);
30454
30621
  if (stssBox !== null && syncNumbers === null) return {
30455
30622
  kind: "refused",
@@ -30497,6 +30664,13 @@ var ClipSourceError = class extends Error {
30497
30664
  this.name = "ClipSourceError";
30498
30665
  }
30499
30666
  };
30667
+ /**
30668
+ * 64 MB. Above `VIDEOCLIPS_MAX_READ_BYTES` (50 MB) on purpose: the cap's own
30669
+ * bound refuses first and by name, and this one exists so a provider that ever
30670
+ * answers larger cannot pin a broker runner's heap. A `high` twin measured
30671
+ * 20–35 MB (D570 § 9.6).
30672
+ */
30673
+ var CLIP_SOURCE_MAX_BYTES = 64 * 1024 * 1024;
30500
30674
  function createClipFileSource(deps) {
30501
30675
  const maxBytes = deps.maxBytes ?? 67108864;
30502
30676
  const { deviceId, clipId, provider, profile } = deps.handle;
@@ -30590,6 +30764,1827 @@ function createClipFileSource(deps) {
30590
30764
  };
30591
30765
  }
30592
30766
  //#endregion
30767
+ //#region src/stream-broker/recorded/clip-path-choice.ts
30768
+ function chooseClipPath(input) {
30769
+ if (input.streamDialWired) return {
30770
+ path: "stream",
30771
+ reason: "stream-serves-every-profile"
30772
+ };
30773
+ if (input.fileReadWired) return {
30774
+ path: "file",
30775
+ reason: "no-stream-dial-wired"
30776
+ };
30777
+ return {
30778
+ path: "none",
30779
+ reason: "nothing-wired"
30780
+ };
30781
+ }
30782
+ /**
30783
+ * The feeder a `none` choice gets: every verb is a named refusal and the
30784
+ * first seek reports `error`, so a clip nothing can serve is never a frozen
30785
+ * frame with no line behind it (D391).
30786
+ */
30787
+ function createRefusedClipFeeder(deps) {
30788
+ const data = {
30789
+ deviceId: deps.handle.deviceId,
30790
+ clipId: deps.handle.clipId,
30791
+ reason: deps.reason
30792
+ };
30793
+ let announced = false;
30794
+ const refuse = (verb) => {
30795
+ deps.onLog?.("clip playback: refused — no path can serve this clip", {
30796
+ ...data,
30797
+ verb
30798
+ });
30799
+ if (announced) return;
30800
+ announced = true;
30801
+ deps.onState({
30802
+ from: null,
30803
+ to: "error",
30804
+ reason: `refused:${deps.reason}`,
30805
+ seekToken: 0
30806
+ });
30807
+ };
30808
+ return {
30809
+ seek: async () => refuse("seek"),
30810
+ setRate: () => {},
30811
+ stepFrame: () => refuse("stepFrame"),
30812
+ beginScrub: () => refuse("beginScrub"),
30813
+ scrubTo: () => {},
30814
+ scrubAck: () => {},
30815
+ durationMs: () => Promise.resolve(null),
30816
+ dispose: () => {}
30817
+ };
30818
+ }
30819
+ var PCMU_RATE_HZ = 8e3;
30820
+ function createPassthroughFramer() {
30821
+ return {
30822
+ push: (unit) => unit.data.byteLength === 0 ? [] : [{
30823
+ data: unit.data,
30824
+ ptsMs: unit.ptsMs,
30825
+ durationMs: unit.durationMs
30826
+ }],
30827
+ carriedBytes: 0,
30828
+ reanchors: 0
30829
+ };
30830
+ }
30831
+ var PcmuFramer = class {
30832
+ carry = new Uint8Array(0);
30833
+ /** `ptsMs` of the first byte currently held in {@link carry}. */
30834
+ anchorMs = null;
30835
+ /** Bytes emitted plus carried since {@link anchorMs} — the exact clock. */
30836
+ bytesSinceAnchor = 0;
30837
+ reanchorCount = 0;
30838
+ get carriedBytes() {
30839
+ return this.carry.byteLength;
30840
+ }
30841
+ get reanchors() {
30842
+ return this.reanchorCount;
30843
+ }
30844
+ push(unit) {
30845
+ if (unit.data.byteLength === 0) return [];
30846
+ const expectedMs = this.anchorMs === null ? null : this.anchorMs + this.bytesSinceAnchor * 1e3 / PCMU_RATE_HZ;
30847
+ if (expectedMs === null || Math.abs(expectedMs - unit.ptsMs) > 20) {
30848
+ if (expectedMs !== null) this.reanchorCount += 1;
30849
+ this.carry = new Uint8Array(0);
30850
+ this.anchorMs = unit.ptsMs;
30851
+ this.bytesSinceAnchor = 0;
30852
+ }
30853
+ const anchorMs = this.anchorMs;
30854
+ if (anchorMs === null) return [];
30855
+ const joined = new Uint8Array(this.carry.byteLength + unit.data.byteLength);
30856
+ joined.set(this.carry, 0);
30857
+ joined.set(unit.data, this.carry.byteLength);
30858
+ const out = [];
30859
+ let at = 0;
30860
+ while (at + 160 <= joined.byteLength) {
30861
+ out.push({
30862
+ data: joined.slice(at, at + 160),
30863
+ ptsMs: anchorMs + this.bytesSinceAnchor * 1e3 / PCMU_RATE_HZ,
30864
+ durationMs: 20
30865
+ });
30866
+ this.bytesSinceAnchor += 160;
30867
+ at += 160;
30868
+ }
30869
+ this.carry = joined.slice(at);
30870
+ return out;
30871
+ }
30872
+ };
30873
+ /** The framer for a codec: a pass-through for Opus, a re-cut for µ-law. */
30874
+ function createClipAudioFramer(codec) {
30875
+ return codec === "opus" ? createPassthroughFramer() : new PcmuFramer();
30876
+ }
30877
+ //#endregion
30878
+ //#region src/stream-broker/recorded/fmp4-stream-demux.ts
30879
+ /**
30880
+ * fmp4-stream-demux — an INCREMENTAL demux of a fragmented MP4 as it arrives.
30881
+ *
30882
+ * The clip STREAM (`/clip-stream/…`, D597) is an fMP4 written by ffmpeg with
30883
+ * `frag_keyframe+empty_moov+default_base_moof` while it is still reading the
30884
+ * camera: a `moov` with an EMPTY sample table, then one `moof`+`mdat` per
30885
+ * GOP, and no trailer until the end. Nothing in this directory can read that
30886
+ * shape as it flows — `clip-mp4-index.ts` wants `stbl` and refuses it
30887
+ * `no-samples` by design, `segment-demux-js.ts` (mp4box) wants a WHOLE
30888
+ * fragment in hand — so this module walks the boxes itself, byte by byte, and
30889
+ * emits every video sample the moment its bytes are in.
30890
+ *
30891
+ * ## What it emits and when
30892
+ *
30893
+ * {@link AccessUnit}s, in DECODE order, `ptsMs` anchored at 0 on the first
30894
+ * sample's composition time — the same contract `demuxSegmentFullJs` keeps,
30895
+ * so a streamed clip and a stored one push the same shape. A sample is
30896
+ * emitted as soon as ITS bytes have arrived, not when its `mdat` closes: a
30897
+ * GOP is a second or two of video, and a feeder that waited for the box would
30898
+ * hand the viewer that second late for nothing.
30899
+ *
30900
+ * Sync samples are fused with the parameter sets (`avcc-annexb.ts`), delta
30901
+ * samples are converted plain; the codec and the `avcC`/`hvcC` come from the
30902
+ * `moov`. ONE video track is followed, and — since D600 — ONE audio track
30903
+ * beside it; every other track's bytes in an `mdat` are skipped, never
30904
+ * emitted.
30905
+ *
30906
+ * ## The audio track, and the one clock both tracks are on
30907
+ *
30908
+ * A clip stream carries sound already transcoded to the session's codec
30909
+ * (D600): Opus, whose mp4 sample IS the RTP payload, or µ-law in a QuickTime
30910
+ * `ulaw` entry. Audio samples are handed to {@link Fmp4StreamDemuxOptions.onAudio}
30911
+ * as they land, interleaved with the video in FILE-OFFSET order — which is
30912
+ * the order the bytes arrive, and the only order a forward-only window can
30913
+ * drain in.
30914
+ *
30915
+ * **Both tracks are anchored on ONE origin**, the first sample of either to
30916
+ * be emitted, and every `ptsMs` is measured from it in exact integer
30917
+ * arithmetic over the two timescales. Anchoring each track on its own first
30918
+ * sample would zero out the offset BETWEEN them — which is the whole content
30919
+ * of lip sync — and would do it invisibly, in a way that only shows up as
30920
+ * drift on a long clip.
30921
+ *
30922
+ * An audio track whose sample entry is neither Opus nor µ-law is REPORTED
30923
+ * ({@link Fmp4StreamDemuxer.audio}) and not followed. It is never a refusal:
30924
+ * a codec this process cannot forward is a reason to play the clip silent,
30925
+ * never a reason to lose the video.
30926
+ *
30927
+ * ## What it refuses, by name
30928
+ *
30929
+ * A `moof` before any `moov` (`no-moov`), a `moov` with no video track
30930
+ * (`no-video-track`), a codec that is neither AVC nor HEVC
30931
+ * (`unsupported-codec`), and a box or a sample that cannot fit where it says
30932
+ * it is (`malformed`). Each is thrown ONCE as {@link Fmp4DemuxError}; the
30933
+ * demuxer is dead after it and says so on every further push. The bytes come
30934
+ * off a camera through two processes — a refusal here is a fact about the
30935
+ * stream, never a crash.
30936
+ *
30937
+ * ## Duration
30938
+ *
30939
+ * Unknown until the stream ENDS. `end()` answers the sum of the sample
30940
+ * durations, in ms, or `null` when no sample was ever emitted — never `0`
30941
+ * (D393). The caller learns the length when the camera has finished, which
30942
+ * is the truth about a forward-only source.
30943
+ */
30944
+ var Fmp4DemuxError = class extends Error {
30945
+ reason;
30946
+ constructor(reason, message) {
30947
+ super(message);
30948
+ this.reason = reason;
30949
+ this.name = "Fmp4DemuxError";
30950
+ }
30951
+ };
30952
+ var BOX_HEADER = 8;
30953
+ var LARGE_BOX_HEADER = 16;
30954
+ var FULLBOX = 4;
30955
+ /** `sample_is_non_sync_sample`, ISO/IEC 14496-12 § 8.8.3.1. */
30956
+ var FLAG_NON_SYNC = 65536;
30957
+ var TFHD_BASE_DATA_OFFSET = 1;
30958
+ var TFHD_SAMPLE_DESC_INDEX = 2;
30959
+ var TFHD_DEFAULT_DURATION = 8;
30960
+ var TFHD_DEFAULT_SIZE = 16;
30961
+ var TFHD_DEFAULT_FLAGS = 32;
30962
+ var TFHD_DEFAULT_BASE_IS_MOOF = 131072;
30963
+ var TRUN_DATA_OFFSET = 1;
30964
+ var TRUN_FIRST_SAMPLE_FLAGS = 4;
30965
+ var TRUN_SAMPLE_DURATION = 256;
30966
+ var TRUN_SAMPLE_SIZE = 512;
30967
+ var TRUN_SAMPLE_FLAGS = 1024;
30968
+ var TRUN_SAMPLE_CTS = 2048;
30969
+ /** VisualSampleEntry fields between the sample-entry header and its children. */
30970
+ var VISUAL_SAMPLE_ENTRY_FIELDS = 78;
30971
+ /**
30972
+ * AudioSampleEntry fixed fields, ISO/IEC 14496-12 § 12.2.3: the 8-byte
30973
+ * `SampleEntry` (6 reserved + `data_reference_index`), then version, revision,
30974
+ * vendor, `channelcount`, `samplesize`, `compression_id`, `packet_size` and a
30975
+ * 16.16 `samplerate`.
30976
+ */
30977
+ var AUDIO_SAMPLE_ENTRY_FIELDS = 28;
30978
+ var AUDIO_ENTRY_CHANNELS_AT = 16;
30979
+ var AUDIO_ENTRY_SAMPLE_RATE_AT = 24;
30980
+ /** `cts` in `timescale` ticks, as milliseconds from `origin`. Exact. */
30981
+ function ptsMsFrom(origin, cts, timescale) {
30982
+ if (timescale === origin.timescale) return Math.round((cts - origin.ticks) * 1e3 / timescale);
30983
+ const numerator = (cts * origin.timescale - origin.ticks * timescale) * 1e3;
30984
+ return Math.round(numerator / (timescale * origin.timescale));
30985
+ }
30986
+ function fourcc(bytes, at) {
30987
+ let out = "";
30988
+ for (let i = 0; i < 4; i++) out += String.fromCharCode(bytes[at + i] ?? 0);
30989
+ return out;
30990
+ }
30991
+ function u32(view, at) {
30992
+ return view.getUint32(at);
30993
+ }
30994
+ function u64(view, at) {
30995
+ return view.getUint32(at) * 4294967296 + view.getUint32(at + 4);
30996
+ }
30997
+ /** The direct children of `[start, end)`, or `null` when a size does not fit. */
30998
+ function childBoxes(bytes, start, end) {
30999
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31000
+ const out = [];
31001
+ let off = start;
31002
+ while (off + BOX_HEADER <= end) {
31003
+ const declared = u32(view, off);
31004
+ const type = fourcc(bytes, off + 4);
31005
+ let size = declared;
31006
+ let contentStart = off + BOX_HEADER;
31007
+ if (declared === 1) {
31008
+ if (off + LARGE_BOX_HEADER > end) return null;
31009
+ size = u64(view, off + 8);
31010
+ contentStart = off + LARGE_BOX_HEADER;
31011
+ } else if (declared === 0) size = end - off;
31012
+ if (size < contentStart - off) return null;
31013
+ const boxEnd = off + size;
31014
+ if (boxEnd > end) return null;
31015
+ out.push({
31016
+ type,
31017
+ start: off,
31018
+ end: boxEnd,
31019
+ contentStart
31020
+ });
31021
+ off = boxEnd;
31022
+ }
31023
+ return out;
31024
+ }
31025
+ function findBox(boxes, type) {
31026
+ if (boxes === null) return null;
31027
+ for (const b of boxes) if (b.type === type) return b;
31028
+ return null;
31029
+ }
31030
+ function handlerType(bytes, hdlr) {
31031
+ const at = hdlr.contentStart + FULLBOX + 4;
31032
+ return at + 4 <= hdlr.end ? fourcc(bytes, at) : "";
31033
+ }
31034
+ function trackId(bytes, tkhd) {
31035
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31036
+ const version = bytes[tkhd.contentStart] ?? 0;
31037
+ const at = tkhd.contentStart + FULLBOX + (version === 1 ? 16 : 8);
31038
+ return at + 4 <= tkhd.end ? u32(view, at) : null;
31039
+ }
31040
+ function mediaTimescale(bytes, mdhd) {
31041
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31042
+ const version = bytes[mdhd.contentStart] ?? 0;
31043
+ const at = mdhd.contentStart + FULLBOX + (version === 1 ? 16 : 8);
31044
+ if (at + 4 > mdhd.end) return null;
31045
+ const ts = u32(view, at);
31046
+ return ts > 0 ? ts : null;
31047
+ }
31048
+ /** The decoder config out of `stsd`, header stripped, with the codec it names. */
31049
+ function sampleDescription(bytes, stsd) {
31050
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31051
+ const countAt = stsd.contentStart + FULLBOX;
31052
+ if (countAt + 4 > stsd.end) return null;
31053
+ const count = u32(view, countAt);
31054
+ const entries = childBoxes(bytes, countAt + 4, stsd.end);
31055
+ if (entries === null || count === 0 || entries.length === 0) return null;
31056
+ for (const entry of entries) {
31057
+ const isAvc = entry.type === "avc1" || entry.type === "avc3";
31058
+ const isHevc = entry.type === "hvc1" || entry.type === "hev1";
31059
+ if (!isAvc && !isHevc) continue;
31060
+ const config = findBox(childBoxes(bytes, entry.contentStart + VISUAL_SAMPLE_ENTRY_FIELDS, entry.end), isAvc ? "avcC" : "hvcC");
31061
+ if (config === null) return null;
31062
+ return {
31063
+ codec: isAvc ? "H264" : "H265",
31064
+ description: bytes.subarray(config.contentStart, config.end)
31065
+ };
31066
+ }
31067
+ return "unsupported";
31068
+ }
31069
+ function audioSampleDescription(bytes, stsd) {
31070
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31071
+ const countAt = stsd.contentStart + FULLBOX;
31072
+ if (countAt + 4 > stsd.end) return null;
31073
+ const entry = childBoxes(bytes, countAt + 4, stsd.end)?.[0];
31074
+ if (entry === void 0) return null;
31075
+ if (entry.contentStart + AUDIO_SAMPLE_ENTRY_FIELDS > entry.end) return null;
31076
+ const codec = entry.type === "Opus" ? "opus" : entry.type === "ulaw" ? "pcmu" : null;
31077
+ if (codec === null) return { unsupported: entry.type };
31078
+ return {
31079
+ codec,
31080
+ channels: view.getUint16(entry.contentStart + AUDIO_ENTRY_CHANNELS_AT),
31081
+ sampleRateHz: view.getUint16(entry.contentStart + AUDIO_ENTRY_SAMPLE_RATE_AT)
31082
+ };
31083
+ }
31084
+ function readTrex(bytes, moov, id) {
31085
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31086
+ const mvex = findBox(childBoxes(bytes, moov.contentStart, moov.end), "mvex");
31087
+ const none = {
31088
+ duration: null,
31089
+ size: null,
31090
+ flags: null
31091
+ };
31092
+ if (mvex === null) return none;
31093
+ for (const trex of childBoxes(bytes, mvex.contentStart, mvex.end) ?? []) {
31094
+ if (trex.type !== "trex" || trex.contentStart + FULLBOX + 20 > trex.end) continue;
31095
+ const at = trex.contentStart + FULLBOX;
31096
+ if (u32(view, at) !== id) continue;
31097
+ return {
31098
+ duration: u32(view, at + 8),
31099
+ size: u32(view, at + 12),
31100
+ flags: u32(view, at + 16)
31101
+ };
31102
+ }
31103
+ return none;
31104
+ }
31105
+ /** The `trak`s of a `moov` whose handler is `want`, already unpacked. */
31106
+ function traksOfHandler(bytes, moov, want) {
31107
+ const kids = childBoxes(bytes, moov.contentStart, moov.end);
31108
+ if (kids === null) return "malformed";
31109
+ const out = [];
31110
+ for (const trak of kids) {
31111
+ if (trak.type !== "trak") continue;
31112
+ const trakKids = childBoxes(bytes, trak.contentStart, trak.end);
31113
+ const mdia = findBox(trakKids, "mdia");
31114
+ const tkhd = findBox(trakKids, "tkhd");
31115
+ if (mdia === null || tkhd === null) continue;
31116
+ const mdiaKids = childBoxes(bytes, mdia.contentStart, mdia.end);
31117
+ const hdlr = findBox(mdiaKids, "hdlr");
31118
+ if (hdlr === null || handlerType(bytes, hdlr) !== want) continue;
31119
+ const id = trackId(bytes, tkhd);
31120
+ const mdhd = findBox(mdiaKids, "mdhd");
31121
+ const minf = findBox(mdiaKids, "minf");
31122
+ if (id === null || mdhd === null || minf === null) return "malformed";
31123
+ const timescale = mediaTimescale(bytes, mdhd);
31124
+ const stbl = findBox(childBoxes(bytes, minf.contentStart, minf.end), "stbl");
31125
+ const stsd = findBox(stbl ? childBoxes(bytes, stbl.contentStart, stbl.end) : null, "stsd");
31126
+ if (timescale === null || stsd === null) return "malformed";
31127
+ out.push({
31128
+ id,
31129
+ timescale,
31130
+ stsd
31131
+ });
31132
+ }
31133
+ return out;
31134
+ }
31135
+ /** The one video track of a `moov`, or the refusal that stands in for it. */
31136
+ function readVideoTrack(bytes, moov) {
31137
+ const traks = traksOfHandler(bytes, moov, "vide");
31138
+ if (traks === "malformed") return "malformed";
31139
+ for (const trak of traks) {
31140
+ const desc = sampleDescription(bytes, trak.stsd);
31141
+ if (desc === null) return "malformed";
31142
+ if (desc === "unsupported") return "unsupported-codec";
31143
+ let nalLengthSize;
31144
+ try {
31145
+ nalLengthSize = require_segment_demux_js.parseNalLengthSize(desc.description, desc.codec === "H265");
31146
+ } catch {
31147
+ return "malformed";
31148
+ }
31149
+ return {
31150
+ id: trak.id,
31151
+ timescale: trak.timescale,
31152
+ codec: desc.codec,
31153
+ description: desc.description,
31154
+ nalLengthSize,
31155
+ trex: readTrex(bytes, moov, trak.id)
31156
+ };
31157
+ }
31158
+ return "no-video-track";
31159
+ }
31160
+ /**
31161
+ * The one audio track of a `moov`, or what to REPORT instead of one.
31162
+ *
31163
+ * Never a refusal: sound this process cannot forward is a silent clip, and a
31164
+ * silent clip is a clip. The only thing that must not happen is silence
31165
+ * nobody can account for, which is what {@link Fmp4AudioState} is for.
31166
+ */
31167
+ function readAudioTrack$1(bytes, moov) {
31168
+ const traks = traksOfHandler(bytes, moov, "soun");
31169
+ if (traks === "malformed") return { kind: "absent" };
31170
+ for (const trak of traks) {
31171
+ const desc = audioSampleDescription(bytes, trak.stsd);
31172
+ if (desc === null) return {
31173
+ kind: "unsupported",
31174
+ sampleEntry: ""
31175
+ };
31176
+ if ("unsupported" in desc) return {
31177
+ kind: "unsupported",
31178
+ sampleEntry: desc.unsupported
31179
+ };
31180
+ return {
31181
+ id: trak.id,
31182
+ timescale: trak.timescale,
31183
+ codec: desc.codec,
31184
+ sampleRateHz: desc.sampleRateHz,
31185
+ channels: desc.channels,
31186
+ trex: readTrex(bytes, moov, trak.id)
31187
+ };
31188
+ }
31189
+ return { kind: "absent" };
31190
+ }
31191
+ function readTfhd(view, tfhd) {
31192
+ const flags = u32(view, tfhd.contentStart) & 16777215;
31193
+ let at = tfhd.contentStart + FULLBOX;
31194
+ if (at + 4 > tfhd.end) return null;
31195
+ const id = u32(view, at);
31196
+ at += 4;
31197
+ let baseDataOffset = null;
31198
+ if (flags & TFHD_BASE_DATA_OFFSET) {
31199
+ if (at + 8 > tfhd.end) return null;
31200
+ baseDataOffset = u64(view, at);
31201
+ at += 8;
31202
+ }
31203
+ if (flags & TFHD_SAMPLE_DESC_INDEX) at += 4;
31204
+ let duration = null;
31205
+ let size = null;
31206
+ let sampleFlags = null;
31207
+ if (flags & TFHD_DEFAULT_DURATION) {
31208
+ if (at + 4 > tfhd.end) return null;
31209
+ duration = u32(view, at);
31210
+ at += 4;
31211
+ }
31212
+ if (flags & TFHD_DEFAULT_SIZE) {
31213
+ if (at + 4 > tfhd.end) return null;
31214
+ size = u32(view, at);
31215
+ at += 4;
31216
+ }
31217
+ if (flags & TFHD_DEFAULT_FLAGS) {
31218
+ if (at + 4 > tfhd.end) return null;
31219
+ sampleFlags = u32(view, at);
31220
+ }
31221
+ return {
31222
+ trackId: id,
31223
+ baseDataOffset,
31224
+ defaultBaseIsMoof: (flags & TFHD_DEFAULT_BASE_IS_MOOF) !== 0,
31225
+ defaults: {
31226
+ duration,
31227
+ size,
31228
+ flags: sampleFlags
31229
+ }
31230
+ };
31231
+ }
31232
+ function readTfdt(view, bytes, tfdt) {
31233
+ const version = bytes[tfdt.contentStart] ?? 0;
31234
+ const at = tfdt.contentStart + FULLBOX;
31235
+ if (version === 1) return at + 8 <= tfdt.end ? u64(view, at) : null;
31236
+ return at + 4 <= tfdt.end ? u32(view, at) : null;
31237
+ }
31238
+ /** The samples one `trun` announces, or `null` when it does not fit. */
31239
+ function readTrun(args) {
31240
+ const { view, trun, base, trex, traf } = args;
31241
+ const version = view.getUint8(trun.contentStart);
31242
+ const flags = u32(view, trun.contentStart) & 16777215;
31243
+ let at = trun.contentStart + FULLBOX;
31244
+ if (at + 4 > trun.end) return null;
31245
+ const count = u32(view, at);
31246
+ at += 4;
31247
+ let dataOffset = 0;
31248
+ if (flags & TRUN_DATA_OFFSET) {
31249
+ if (at + 4 > trun.end) return null;
31250
+ dataOffset = view.getInt32(at);
31251
+ at += 4;
31252
+ }
31253
+ let firstSampleFlags = null;
31254
+ if (flags & TRUN_FIRST_SAMPLE_FLAGS) {
31255
+ if (at + 4 > trun.end) return null;
31256
+ firstSampleFlags = u32(view, at);
31257
+ at += 4;
31258
+ }
31259
+ const out = [];
31260
+ let offset = base + dataOffset;
31261
+ let dts = args.startDts;
31262
+ for (let i = 0; i < count; i++) {
31263
+ let duration = traf.defaults.duration ?? trex.duration;
31264
+ let size = traf.defaults.size ?? trex.size;
31265
+ let sampleFlags = traf.defaults.flags ?? trex.flags;
31266
+ let ctsOffset = 0;
31267
+ if (flags & TRUN_SAMPLE_DURATION) {
31268
+ if (at + 4 > trun.end) return null;
31269
+ duration = u32(view, at);
31270
+ at += 4;
31271
+ }
31272
+ if (flags & TRUN_SAMPLE_SIZE) {
31273
+ if (at + 4 > trun.end) return null;
31274
+ size = u32(view, at);
31275
+ at += 4;
31276
+ }
31277
+ if (flags & TRUN_SAMPLE_FLAGS) {
31278
+ if (at + 4 > trun.end) return null;
31279
+ sampleFlags = u32(view, at);
31280
+ at += 4;
31281
+ } else if (i === 0 && firstSampleFlags !== null) sampleFlags = firstSampleFlags;
31282
+ if (flags & TRUN_SAMPLE_CTS) {
31283
+ if (at + 4 > trun.end) return null;
31284
+ ctsOffset = version === 0 ? u32(view, at) : view.getInt32(at);
31285
+ at += 4;
31286
+ }
31287
+ if (duration === null || size === null) return null;
31288
+ const sync = sampleFlags === null ? i === 0 : (sampleFlags & FLAG_NON_SYNC) === 0;
31289
+ out.push({
31290
+ offset,
31291
+ size,
31292
+ dts,
31293
+ cts: dts + ctsOffset,
31294
+ duration,
31295
+ sync
31296
+ });
31297
+ offset += size;
31298
+ dts += duration;
31299
+ }
31300
+ return out;
31301
+ }
31302
+ /**
31303
+ * Every sample a `moof` announces for the followed tracks, in FILE-OFFSET
31304
+ * order, or `null`.
31305
+ *
31306
+ * Offset order, not decode order across tracks: the window is forward-only
31307
+ * and drains what has arrived, so two tracks whose runs are laid out one
31308
+ * after another inside one `mdat` — which is how ffmpeg writes them — must
31309
+ * be drained in the order their bytes land. Within one track the offsets are
31310
+ * increasing anyway, so each track's own decode order is preserved.
31311
+ */
31312
+ function readMoof(bytes, moof, tracks) {
31313
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31314
+ const kids = childBoxes(bytes, moof.contentStart, moof.end);
31315
+ if (kids === null) return null;
31316
+ const out = [];
31317
+ for (const traf of kids) {
31318
+ if (traf.type !== "traf") continue;
31319
+ const trafKids = childBoxes(bytes, traf.contentStart, traf.end);
31320
+ const tfhdBox = findBox(trafKids, "tfhd");
31321
+ if (tfhdBox === null) return null;
31322
+ const tfhd = readTfhd(view, tfhdBox);
31323
+ if (tfhd === null) return null;
31324
+ const kind = tfhd.trackId === tracks.video.id ? "video" : tracks.audio !== null && tfhd.trackId === tracks.audio.id ? "audio" : null;
31325
+ if (kind === null) continue;
31326
+ const trex = kind === "video" ? tracks.video.trex : tracks.audio?.trex ?? null;
31327
+ if (trex === null) continue;
31328
+ const tfdtBox = findBox(trafKids, "tfdt");
31329
+ const startDts = tfdtBox === null ? 0 : readTfdt(view, bytes, tfdtBox);
31330
+ if (startDts === null) return null;
31331
+ const base = tfhd.baseDataOffset ?? moof.start;
31332
+ let dts = startDts;
31333
+ for (const trun of trafKids ?? []) {
31334
+ if (trun.type !== "trun") continue;
31335
+ const samples = readTrun({
31336
+ view,
31337
+ trun,
31338
+ base,
31339
+ startDts: dts,
31340
+ trex,
31341
+ traf: tfhd
31342
+ });
31343
+ if (samples === null) return null;
31344
+ for (const sample of samples) out.push({
31345
+ kind,
31346
+ sample
31347
+ });
31348
+ const last = samples[samples.length - 1];
31349
+ if (last !== void 0) dts = last.dts + last.duration;
31350
+ }
31351
+ }
31352
+ return out.toSorted((a, b) => a.sample.offset - b.sample.offset);
31353
+ }
31354
+ /** Bytes are ABSOLUTE file offsets here, so the demuxer can reason about a
31355
+ * sample announced by a `moof` whose `mdat` has not fully arrived. */
31356
+ var ByteWindow = class {
31357
+ held = new Uint8Array(0);
31358
+ /** Absolute offset of `held[0]`. */
31359
+ base = 0;
31360
+ /** Bytes still to DISCARD from the front of what arrives next — a skip
31361
+ * past the end of what is held (the tail of an mdat nobody follows). */
31362
+ skipAhead = 0;
31363
+ get start() {
31364
+ return this.base;
31365
+ }
31366
+ get end() {
31367
+ return this.base + this.held.length;
31368
+ }
31369
+ get length() {
31370
+ return this.held.length;
31371
+ }
31372
+ append(chunk) {
31373
+ let incoming = chunk;
31374
+ if (this.skipAhead > 0) {
31375
+ const drop = Math.min(this.skipAhead, incoming.length);
31376
+ this.skipAhead -= drop;
31377
+ this.base += drop;
31378
+ incoming = incoming.subarray(drop);
31379
+ }
31380
+ if (incoming.length === 0) return;
31381
+ if (this.held.length === 0) {
31382
+ this.held = incoming;
31383
+ return;
31384
+ }
31385
+ const next = new Uint8Array(this.held.length + incoming.length);
31386
+ next.set(this.held, 0);
31387
+ next.set(incoming, this.held.length);
31388
+ this.held = next;
31389
+ }
31390
+ /** Drop everything before absolute `offset`, including bytes not yet arrived. */
31391
+ consumeTo(offset) {
31392
+ if (offset <= this.base) return;
31393
+ const drop = Math.min(offset - this.base, this.held.length);
31394
+ this.held = this.held.subarray(drop);
31395
+ this.base += drop;
31396
+ if (offset > this.base) {
31397
+ this.skipAhead += offset - this.base;
31398
+ this.base = offset;
31399
+ this.held = new Uint8Array(0);
31400
+ }
31401
+ }
31402
+ slice(absStart, absEnd) {
31403
+ return this.held.subarray(absStart - this.base, absEnd - this.base);
31404
+ }
31405
+ view() {
31406
+ return this.held;
31407
+ }
31408
+ };
31409
+ function createFmp4StreamDemuxer(options = {}) {
31410
+ const onAudio = options.onAudio;
31411
+ const window = new ByteWindow();
31412
+ let track = null;
31413
+ let audioTrack = null;
31414
+ let audioState = { kind: "unknown" };
31415
+ let dead = null;
31416
+ /** Samples announced by the current `moof`, not yet emitted, offset order. */
31417
+ let announced = [];
31418
+ /** Absolute end of the `mdat` the announced samples live in, once known. */
31419
+ let mdatEnd = null;
31420
+ /** The ONE origin both tracks are measured from — the first sample emitted. */
31421
+ let origin = null;
31422
+ let samples = 0;
31423
+ let audioSamples = 0;
31424
+ let durationTicks = 0;
31425
+ const refuse = (reason, message) => {
31426
+ dead = new Fmp4DemuxError(reason, message);
31427
+ throw dead;
31428
+ };
31429
+ /** Anchor the shared origin on the first sample of EITHER track. */
31430
+ const anchor = (cts, timescale) => {
31431
+ origin ??= {
31432
+ ticks: cts,
31433
+ timescale
31434
+ };
31435
+ return origin;
31436
+ };
31437
+ const toAccessUnit = (sample, data) => {
31438
+ if (track === null) throw new Error("unreachable: sample before moov");
31439
+ const at = anchor(sample.cts, track.timescale);
31440
+ const isHevc = track.codec === "H265";
31441
+ let annexB;
31442
+ try {
31443
+ annexB = sample.sync ? require_segment_demux_js.buildKeyframeAnnexB(data, track.description, isHevc) : require_segment_demux_js.avccSampleToAnnexB(data, track.nalLengthSize);
31444
+ } catch (err) {
31445
+ return refuse("malformed", `sample ${samples} is not a valid AVCC/HVCC sample: ${String(err)}`);
31446
+ }
31447
+ samples += 1;
31448
+ durationTicks += sample.duration;
31449
+ return {
31450
+ data: annexB,
31451
+ ptsMs: ptsMsFrom(at, sample.cts, track.timescale),
31452
+ keyframe: sample.sync,
31453
+ codec: track.codec
31454
+ };
31455
+ };
31456
+ const emitAudio = (sample, data) => {
31457
+ if (audioTrack === null || onAudio === void 0) return;
31458
+ const at = anchor(sample.cts, audioTrack.timescale);
31459
+ audioSamples += 1;
31460
+ onAudio({
31461
+ data: data.slice(),
31462
+ ptsMs: ptsMsFrom(at, sample.cts, audioTrack.timescale),
31463
+ durationMs: Math.round(sample.duration * 1e3 / audioTrack.timescale),
31464
+ codec: audioTrack.codec,
31465
+ sampleRateHz: audioTrack.sampleRateHz,
31466
+ channels: audioTrack.channels
31467
+ });
31468
+ };
31469
+ /** Emit every announced sample whose bytes are in, in order; stop at the first that is not. */
31470
+ const drainAnnounced = (out) => {
31471
+ while (announced.length > 0) {
31472
+ const next = announced[0];
31473
+ if (next === void 0) return;
31474
+ const sampleEnd = next.sample.offset + next.sample.size;
31475
+ if (mdatEnd !== null && sampleEnd > mdatEnd) refuse("malformed", `a sample runs past its mdat (${sampleEnd} > ${mdatEnd})`);
31476
+ if (next.sample.offset < window.start) refuse("malformed", `a sample points behind the bytes still held (${next.sample.offset} < ${window.start})`);
31477
+ if (sampleEnd > window.end) return;
31478
+ const bytes = window.slice(next.sample.offset, sampleEnd);
31479
+ if (next.kind === "video") out.push(toAccessUnit(next.sample, bytes));
31480
+ else emitAudio(next.sample, bytes);
31481
+ announced = announced.slice(1);
31482
+ window.consumeTo(sampleEnd);
31483
+ }
31484
+ if (mdatEnd !== null) {
31485
+ window.consumeTo(mdatEnd);
31486
+ mdatEnd = null;
31487
+ }
31488
+ };
31489
+ const push = (chunk) => {
31490
+ if (dead !== null) throw dead;
31491
+ window.append(chunk);
31492
+ const out = [];
31493
+ for (;;) {
31494
+ if (announced.length > 0 && mdatEnd !== null) {
31495
+ drainAnnounced(out);
31496
+ if (announced.length > 0) return out;
31497
+ }
31498
+ const held = window.view();
31499
+ if (held.length < BOX_HEADER) return out;
31500
+ const view = new DataView(held.buffer, held.byteOffset, held.byteLength);
31501
+ const declared = u32(view, 0);
31502
+ const type = fourcc(held, 4);
31503
+ let size = declared;
31504
+ let contentStart = BOX_HEADER;
31505
+ if (declared === 1) {
31506
+ if (held.length < LARGE_BOX_HEADER) return out;
31507
+ size = u64(view, 8);
31508
+ contentStart = LARGE_BOX_HEADER;
31509
+ } else if (declared === 0) refuse("malformed", `a "${type}" box extends to end-of-file, which a stream never reaches`);
31510
+ if (size < contentStart) refuse("malformed", `a "${type}" box declares ${size} bytes`);
31511
+ const boxStart = window.start;
31512
+ const boxEnd = boxStart + size;
31513
+ if (type === "mdat") {
31514
+ if (track === null) refuse("no-moov", "media arrived before any moov");
31515
+ if (announced.length === 0) {
31516
+ window.consumeTo(boxEnd);
31517
+ continue;
31518
+ }
31519
+ mdatEnd = boxEnd;
31520
+ window.consumeTo(boxStart + contentStart);
31521
+ continue;
31522
+ }
31523
+ if (held.length < size) return out;
31524
+ if (type === "moov") {
31525
+ const moov = {
31526
+ type,
31527
+ start: 0,
31528
+ end: size,
31529
+ contentStart
31530
+ };
31531
+ const read = readVideoTrack(held, moov);
31532
+ if (typeof read === "string") refuse(read, `the moov cannot be followed: ${read}`);
31533
+ else track = read;
31534
+ const audio = readAudioTrack$1(held, moov);
31535
+ if ("kind" in audio) audioState = audio;
31536
+ else {
31537
+ audioTrack = audio;
31538
+ audioState = {
31539
+ kind: "ready",
31540
+ codec: audio.codec,
31541
+ sampleRateHz: audio.sampleRateHz,
31542
+ channels: audio.channels
31543
+ };
31544
+ }
31545
+ } else if (type === "moof") {
31546
+ if (track === null) refuse("no-moov", "a moof arrived before any moov");
31547
+ const announcedHere = readMoof(held, {
31548
+ type,
31549
+ start: 0,
31550
+ end: size,
31551
+ contentStart
31552
+ }, {
31553
+ video: track,
31554
+ audio: onAudio === void 0 ? null : audioTrack
31555
+ });
31556
+ if (announcedHere === null) refuse("malformed", "a moof cannot be read");
31557
+ announced = announcedHere.map((p) => ({
31558
+ kind: p.kind,
31559
+ sample: {
31560
+ ...p.sample,
31561
+ offset: p.sample.offset + boxStart
31562
+ }
31563
+ }));
31564
+ }
31565
+ window.consumeTo(boxEnd);
31566
+ }
31567
+ };
31568
+ return {
31569
+ push,
31570
+ end: () => ({
31571
+ durationMs: track !== null && samples > 0 && durationTicks > 0 ? durationTicks * 1e3 / track.timescale : null,
31572
+ samples,
31573
+ audioSamples,
31574
+ trailingBytes: window.length,
31575
+ pendingSamples: announced.length
31576
+ }),
31577
+ get ready() {
31578
+ return track !== null;
31579
+ },
31580
+ get codec() {
31581
+ return track?.codec ?? null;
31582
+ },
31583
+ get audio() {
31584
+ return audioState;
31585
+ },
31586
+ get pendingBytes() {
31587
+ return window.length;
31588
+ }
31589
+ };
31590
+ }
31591
+ //#endregion
31592
+ //#region src/stream-broker/recorded/clip-stream-feeder.ts
31593
+ /**
31594
+ * clip-stream-feeder — a clip plays FROM A STREAM, forward-only (D597).
31595
+ *
31596
+ * The second clip feeder, beside `clip-file-source.ts` + `RecordedFeeder`
31597
+ * and not in their place. The file path fetches the whole clip by handle,
31598
+ * indexes `stbl`, and lets the recorded feeder PULL access units by time.
31599
+ * This path is the other shape: the provider's clip STREAM
31600
+ * (`/clip-stream/…`, stage 1) is a fragmented MP4 written from the first
31601
+ * muxed byte, with no index and no length, and the bytes reach this process
31602
+ * as the camera delivers them. Nothing here pulls — the stream PUSHES, the
31603
+ * demuxer emits access units as their bytes land, and this feeder paces them
31604
+ * onto the session's track at the playback rate.
31605
+ *
31606
+ * ## Forward-only, and what that means for every verb
31607
+ *
31608
+ * The camera offers no seek inside a recording (operator's decision from the
31609
+ * packet capture, 2026-09-22): a replay names a FILE, and the bytes behind
31610
+ * the head were never kept. So:
31611
+ *
31612
+ * - `seek(t)` with `t` behind the HEAD (the last frame pushed) is REFUSED BY
31613
+ * NAME — logged, the position re-sent so the viewer's playhead snaps back
31614
+ * to the truth, and nothing else changes. "Back to the start" is as
31615
+ * impossible as any other backward move; `forward` is measured against the
31616
+ * head, never against zero (the viewer's `clipSeekAllowed` says the same).
31617
+ * - `seek(t)` ahead of the head is honoured if the bytes have arrived: what
31618
+ * was buffered before the last keyframe ≤ `t` is dropped, the run from that
31619
+ * keyframe to `t` is BURST (decode priming, as the recorded feeder's seek
31620
+ * prefix does), and pacing resumes from `t`. If the bytes have NOT arrived,
31621
+ * the seek waits for them and lands when they do — a stream cannot answer
31622
+ * faster than the camera sends.
31623
+ * - `stepFrame(-1)` and the scrub verbs are refused by name; `stepFrame(+1)`
31624
+ * pushes the next buffered unit.
31625
+ * - `setRate` is the PLAYER's rate over frames already here ({0} ∪ [0.25, 4],
31626
+ * the recorded domain). The camera's own `playSpeed` (8, 16 — real, the app
31627
+ * reopens the replay at 8) is a different mechanism and is NOT wired: it
31628
+ * would go on the dial (`videoclips.dialClipStream` → the cmd-5 replay's
31629
+ * `<playSpeed>`), costing a new stream, and is named in D597 as open.
31630
+ *
31631
+ * ## Duration is unknown until it is known
31632
+ *
31633
+ * A stream has no length up front. `durationMs()` resolves when the stream
31634
+ * ENDS, with the demuxer's own sum, or with `null` when it was cut or left
31635
+ * before it ended — never `0` (D393). The session reports it then, named by
31636
+ * clip, exactly as it reports the file path's measure.
31637
+ *
31638
+ * ## D9/D18, stated
31639
+ *
31640
+ * Media crosses the process boundary here COMPRESSED, in one TCP stream, at
31641
+ * the camera's delivery rate — the same shape the live path ingests from an
31642
+ * RTSP camera, and the same shape the file path's one by-handle fetch has,
31643
+ * spread over time instead of held whole. What D9 forbids is RAW frames at
31644
+ * frame-rate across processes, and nothing raw exists until the session's
31645
+ * decoder, which is downstream of this process. The backlog is bounded
31646
+ * ({@link CLIP_STREAM_BUFFER_MAX_BYTES}, the file path's own ceiling): above
31647
+ * it the socket is PAUSED (D447 on the consuming hop — read no chunk you have
31648
+ * not taken), and the producer's drain gate is what decides a consumer that
31649
+ * never drains. Frames behind the head are released as soon as they are
31650
+ * pushed, which is the same fact as "no backward seek".
31651
+ *
31652
+ * ## Sound, and the one clock it shares with the picture (D600)
31653
+ *
31654
+ * The stream carries audio ALREADY in the session's negotiated codec — the
31655
+ * provider transcodes before it muxes, because it is the process that holds
31656
+ * the camera's AAC and it already runs an ffmpeg. So this feeder decodes
31657
+ * nothing: it frames (`clip-stream-audio.ts`) and paces, on ONE schedule with
31658
+ * the video, on the SAME `ptsMs` axis the demuxer anchors both tracks on, and
31659
+ * off ONE {@link MonotonicClock} whose output is converted to each track's
31660
+ * RTP rate ({@link ClipStreamFeeder.stampAudio}). One clock rather than the
31661
+ * recorded path's pair: two clocks re-base on their own last output at every
31662
+ * discontinuity, so each seek leaves them a frame further apart.
31663
+ *
31664
+ * Two rules keep it honest, and both exist because a wrong one is inaudible
31665
+ * in a unit test:
31666
+ *
31667
+ * - **Audio is never pushed late.** ffmpeg's muxer writes a track's run when
31668
+ * its own fragment closes, so the first seconds of sound can land after the
31669
+ * picture they belong to (measured: 43 ms behind at the head on a 60x
31670
+ * delivery, and nothing after). A unit whose moment has passed by more than
31671
+ * {@link CLIP_STREAM_AUDIO_LATE_MS} is DROPPED and counted — never pushed
31672
+ * in a burst the receiver would have to discard anyway, and never silently.
31673
+ * - **Audio plays at 1x only**, like the recorded feeder's: a rate change
31674
+ * would need a resample this path has no decoder for, and pitch-shifted
31675
+ * sound is worse than none. Units dropped for the rate are counted apart.
31676
+ *
31677
+ * What the clip turned out to carry — sound, or silence and WHY — settles on
31678
+ * {@link ClipStreamFeeder.audio} the moment the `moov` is read, and is what
31679
+ * the session reports to the viewer. A dial that promises a codec promises
31680
+ * the codec, never the content: whether the recording has a microphone track
31681
+ * is only ever knowable from the stream.
31682
+ *
31683
+ * ## Every exit is logged
31684
+ *
31685
+ * Opened, first frame (with the time since the seek and since the dial),
31686
+ * ended, cut (by reason), refused (by the provider's code), disposed, every
31687
+ * refused verb, and every backpressure transition — each with `deviceId` in
31688
+ * `data`, which the broker's `onLog` puts in `tags`.
31689
+ */
31690
+ /**
31691
+ * The same ceiling as the file path (`CLIP_SOURCE_MAX_BYTES`): a clip the
31692
+ * file path would refuse `too-large` is one this path stops taking. Frames
31693
+ * behind the head are released as they are pushed, so the backlog is the
31694
+ * distance between arrival and playback, not the clip.
31695
+ */
31696
+ var CLIP_STREAM_BUFFER_MAX_BYTES = CLIP_SOURCE_MAX_BYTES;
31697
+ /** The RTP clock each codec's re-stamped timestamps are counted on. */
31698
+ var AUDIO_CLOCK_HZ = {
31699
+ opus: 48e3,
31700
+ pcmu: 8e3
31701
+ };
31702
+ /** A forward seek is a discontinuity for the receiver, like the recorded feeder's. */
31703
+ var SEEK_DISCONTINUITY_MS = 250;
31704
+ var WAIT_TICK_MS = 250;
31705
+ var POSITION_INTERVAL_MS = 1e3;
31706
+ function createClipStreamFeeder(deps) {
31707
+ return new ClipStreamFeeder(deps);
31708
+ }
31709
+ var ClipStreamFeeder = class {
31710
+ deps;
31711
+ /**
31712
+ * Forward-only, and no backward frame-step: the bytes behind the head were
31713
+ * never kept, and the camera offers no replay-from-offset. Stated here so
31714
+ * the viewer can DISABLE those verbs rather than offer them and have every
31715
+ * use refused — the whole content of this report.
31716
+ */
31717
+ transport = {
31718
+ seek: "forward",
31719
+ stepBack: false
31720
+ };
31721
+ phase = "idle";
31722
+ connection = null;
31723
+ demuxer;
31724
+ clock = new MonotonicClock();
31725
+ /** The RTP rate the audio sender negotiated — a CONVERSION, not a clock. */
31726
+ audioClockHz;
31727
+ audioAsk;
31728
+ audioPush;
31729
+ audioFramer = null;
31730
+ audioQueue = [];
31731
+ audioQueuedBytes = 0;
31732
+ audioPushed = 0;
31733
+ audioDroppedLate = 0;
31734
+ audioDroppedRate = 0;
31735
+ audioDroppedSeek = 0;
31736
+ firstAudioLogged = false;
31737
+ audioReport = null;
31738
+ audioWaiters = [];
31739
+ clockHasAdvanced = false;
31740
+ rate = 1;
31741
+ queue = [];
31742
+ queuedBytes = 0;
31743
+ headMs = null;
31744
+ arrivedMs = null;
31745
+ pendingSeekMs = null;
31746
+ pendingSteps = 0;
31747
+ seekToken = 0;
31748
+ anchorWall = 0;
31749
+ anchorPts = 0;
31750
+ paceTimer = null;
31751
+ positionTimer = null;
31752
+ stallTimer = null;
31753
+ socketPaused = false;
31754
+ streamEnded = false;
31755
+ streamEnd = null;
31756
+ duration = null;
31757
+ durationWaiters = [];
31758
+ lastAnnounced = null;
31759
+ scrubRefusedFor = void 0;
31760
+ pushed = 0;
31761
+ dropped = 0;
31762
+ bytesIn = 0;
31763
+ dialedAt = null;
31764
+ seekStartedAt = null;
31765
+ firstFrameLogged = false;
31766
+ maxBufferedBytes;
31767
+ stallTimeoutMs;
31768
+ now;
31769
+ tags;
31770
+ constructor(deps) {
31771
+ this.deps = deps;
31772
+ this.maxBufferedBytes = deps.maxBufferedBytes ?? CLIP_STREAM_BUFFER_MAX_BYTES;
31773
+ this.stallTimeoutMs = deps.stallTimeoutMs ?? 15e3;
31774
+ this.now = deps.now ?? (() => Date.now());
31775
+ this.tags = {
31776
+ deviceId: deps.handle.deviceId,
31777
+ clipId: deps.handle.clipId
31778
+ };
31779
+ const push = this.resolveAudioPush(deps);
31780
+ this.audioAsk = push === null ? "none" : deps.audio ?? "none";
31781
+ this.audioPush = this.audioAsk === "none" ? null : push;
31782
+ this.audioClockHz = this.audioAsk === "none" ? AUDIO_CLOCK_HZ.pcmu : AUDIO_CLOCK_HZ[this.audioAsk];
31783
+ this.demuxer = createFmp4StreamDemuxer(this.audioPush === null ? {} : { onAudio: (unit) => this.onAudioUnit(unit) });
31784
+ if (this.audioPush === null) this.settleAudio({
31785
+ carried: false,
31786
+ codec: null,
31787
+ reason: "not-negotiated"
31788
+ });
31789
+ }
31790
+ /**
31791
+ * The sink function for the negotiated codec, or `null`.
31792
+ *
31793
+ * Mirrors `RecordedFeeder.resolveAudioPush`: the sink is what decides, and
31794
+ * a session that offered a codec on the dial but wired no push for it would
31795
+ * otherwise make the provider transcode for nobody.
31796
+ */
31797
+ resolveAudioPush(deps) {
31798
+ const { sink, audio } = deps;
31799
+ if (audio === "opus") {
31800
+ const opus = sink.pushAudioOpus;
31801
+ return opus === void 0 ? null : (data, ts) => opus.call(sink, data, ts);
31802
+ }
31803
+ if (audio === "pcmu") {
31804
+ const pcmu = sink.pushAudioPcmu;
31805
+ return pcmu === void 0 ? null : (data, ts) => pcmu.call(sink, data, ts);
31806
+ }
31807
+ return null;
31808
+ }
31809
+ async seek(epochMs) {
31810
+ const token = ++this.seekToken;
31811
+ if (this.phase === "disposed" || this.phase === "cut" || this.phase === "refused") {
31812
+ this.log("clip-stream:seek-after-exit", {
31813
+ targetMs: epochMs,
31814
+ phase: this.phase
31815
+ });
31816
+ return;
31817
+ }
31818
+ if (this.phase === "ended") {
31819
+ this.log("clip-stream:seek-at-end", {
31820
+ targetMs: epochMs,
31821
+ headMs: this.headMs
31822
+ });
31823
+ if (this.headMs !== null) this.deps.onPosition(this.headMs);
31824
+ return;
31825
+ }
31826
+ if (this.headMs !== null && epochMs < this.headMs) {
31827
+ this.log("clip-stream:seek-backward-refused", {
31828
+ targetMs: epochMs,
31829
+ headMs: this.headMs,
31830
+ behindMs: this.headMs - epochMs
31831
+ });
31832
+ this.deps.onPosition(this.headMs);
31833
+ return;
31834
+ }
31835
+ this.seekStartedAt = this.now();
31836
+ this.stopPacing();
31837
+ this.pendingSeekMs = epochMs;
31838
+ if (this.phase === "idle") {
31839
+ await this.open(token);
31840
+ if (token !== this.seekToken) return;
31841
+ if (!this.isStreaming()) return;
31842
+ } else if (this.headMs !== null && epochMs > this.headMs && this.clockHasAdvanced) {
31843
+ this.clock.markDiscontinuity(SEEK_DISCONTINUITY_MS);
31844
+ this.emitState("loading", "seek-forward", token);
31845
+ this.log("clip-stream:seek-forward", {
31846
+ targetMs: epochMs,
31847
+ headMs: this.headMs,
31848
+ arrivedMs: this.arrivedMs,
31849
+ queued: this.queue.length
31850
+ });
31851
+ }
31852
+ this.resolvePendingSeek(token);
31853
+ }
31854
+ setRate(rate) {
31855
+ const next = clampPlaybackRate(rate);
31856
+ if (next === 0) {
31857
+ this.rate = 0;
31858
+ this.stopPacing();
31859
+ this.stopPositionReports();
31860
+ if (this.phase === "streaming" && this.pendingSeekMs === null) this.emitState("paused", "set-rate-zero", this.seekToken);
31861
+ return;
31862
+ }
31863
+ this.clock.setRate(next);
31864
+ const wasPaused = this.rate === 0;
31865
+ this.rate = next;
31866
+ if (this.phase !== "streaming" || this.pendingSeekMs !== null) return;
31867
+ if (wasPaused) this.emitState("playing", "set-rate-resume", this.seekToken);
31868
+ this.startPacing();
31869
+ }
31870
+ stepFrame(dir) {
31871
+ if (this.rate !== 0 || this.phase !== "streaming") return;
31872
+ if (dir === -1) {
31873
+ this.log("clip-stream:step-backward-refused", { headMs: this.headMs });
31874
+ return;
31875
+ }
31876
+ if (!this.pushNext()) {
31877
+ if (this.streamEnded) {
31878
+ this.log("clip-stream:step-at-end", { headMs: this.headMs });
31879
+ return;
31880
+ }
31881
+ this.pendingSteps += 1;
31882
+ }
31883
+ }
31884
+ beginScrub(dragId) {
31885
+ if (this.scrubRefusedFor === dragId) return;
31886
+ this.scrubRefusedFor = dragId;
31887
+ this.log("clip-stream:scrub-refused", {
31888
+ dragId,
31889
+ reason: "forward-only",
31890
+ headMs: this.headMs
31891
+ });
31892
+ }
31893
+ scrubTo() {}
31894
+ scrubAck() {}
31895
+ durationMs() {
31896
+ if (this.duration !== null) return Promise.resolve(this.duration.value);
31897
+ return new Promise((resolve) => this.durationWaiters.push(resolve));
31898
+ }
31899
+ /**
31900
+ * What this clip turned out to carry as sound — the answer the viewer needs
31901
+ * to say "this clip has no audio" rather than leave the operator wondering.
31902
+ *
31903
+ * Settles as soon as the stream's `moov` has been read, which is the first
31904
+ * fragment, so it lands beside `durationMs()`'s much later answer rather
31905
+ * than waiting for the stream to end.
31906
+ */
31907
+ audio() {
31908
+ const settled = this.audioReport;
31909
+ if (settled !== null) return Promise.resolve(settled);
31910
+ return new Promise((resolve) => this.audioWaiters.push(resolve));
31911
+ }
31912
+ dispose() {
31913
+ if (this.phase === "disposed") return;
31914
+ const before = this.phase;
31915
+ this.phase = "disposed";
31916
+ this.teardown();
31917
+ this.settleDuration(null);
31918
+ this.settleAudio({
31919
+ carried: false,
31920
+ codec: null,
31921
+ reason: "stream-failed"
31922
+ });
31923
+ this.log("clip-stream:disposed", {
31924
+ phaseBefore: before,
31925
+ pushed: this.pushed,
31926
+ dropped: this.dropped,
31927
+ queued: this.queue.length,
31928
+ bytesIn: this.bytesIn,
31929
+ headMs: this.headMs
31930
+ });
31931
+ this.queue = [];
31932
+ this.queuedBytes = 0;
31933
+ this.audioQueue = [];
31934
+ this.audioQueuedBytes = 0;
31935
+ }
31936
+ /** A method, not a field read: `open()` moves the phase behind an await
31937
+ * that control-flow analysis does not see through. */
31938
+ isStreaming() {
31939
+ return this.phase === "streaming";
31940
+ }
31941
+ async open(token) {
31942
+ this.phase = "opening";
31943
+ this.emitState("loading", "dial", token);
31944
+ this.dialedAt = this.now();
31945
+ let answer;
31946
+ try {
31947
+ answer = await this.deps.dial(this.deps.handle, this.audioAsk);
31948
+ } catch (err) {
31949
+ answer = {
31950
+ kind: "refused",
31951
+ code: "fetch-failed",
31952
+ detail: describe(err)
31953
+ };
31954
+ }
31955
+ if (this.phase !== "opening") return;
31956
+ if (answer.kind !== "dialed") {
31957
+ this.settleAudio({
31958
+ carried: false,
31959
+ codec: null,
31960
+ reason: "stream-failed"
31961
+ });
31962
+ this.refuse(answer.kind === "unavailable" ? "stream-unavailable" : answer.code, answer.detail, token, answer.kind === "unavailable" ? "the provider predates videoclips.dialClipStream — deploy provider-reolink, or the file path" : void 0);
31963
+ return;
31964
+ }
31965
+ if (this.deps.handle.profile === "high" && answer.served !== void 0 && answer.served !== "high") this.deps.onLog?.("clip stream: no main twin — streaming the sub copy for a `high` ask", {
31966
+ deviceId: this.deps.handle.deviceId,
31967
+ asked: "high",
31968
+ served: answer.served
31969
+ });
31970
+ if (this.audioAsk !== "none" && answer.servedAudio !== this.audioAsk) {
31971
+ this.settleAudio({
31972
+ carried: false,
31973
+ codec: null,
31974
+ reason: "not-offered",
31975
+ detail: answer.audioReason ?? answer.servedAudio ?? "the provider did not say"
31976
+ });
31977
+ this.log("clip-stream:audio-not-offered", {
31978
+ asked: this.audioAsk,
31979
+ servedAudio: answer.servedAudio ?? null,
31980
+ audioReason: answer.audioReason ?? null
31981
+ });
31982
+ }
31983
+ let opened;
31984
+ try {
31985
+ opened = await this.deps.open(answer.url, {
31986
+ onData: (chunk) => this.onData(chunk),
31987
+ onEnd: () => this.onEnd(),
31988
+ onError: (err) => this.cut("stream-failed", err.message)
31989
+ });
31990
+ } catch (err) {
31991
+ opened = {
31992
+ kind: "refused",
31993
+ status: 0,
31994
+ code: "fetch-failed",
31995
+ detail: describe(err)
31996
+ };
31997
+ }
31998
+ if (this.phase !== "opening") {
31999
+ if (opened.kind === "open") opened.connection.destroy();
32000
+ return;
32001
+ }
32002
+ if (opened.kind === "refused") {
32003
+ this.refuse(opened.code, opened.detail, token, void 0, opened.status);
32004
+ return;
32005
+ }
32006
+ this.connection = opened.connection;
32007
+ this.phase = "streaming";
32008
+ this.armStall();
32009
+ this.applyBackpressure();
32010
+ this.log("clip-stream:opened", {
32011
+ sinceDialMs: this.now() - (this.dialedAt ?? this.now()),
32012
+ expiresInMs: answer.expiresAtMs - Date.now()
32013
+ });
32014
+ }
32015
+ refuse(code, detail, token, hint, status) {
32016
+ this.phase = "refused";
32017
+ this.teardown();
32018
+ this.settleDuration(null);
32019
+ this.settleAudio({
32020
+ carried: false,
32021
+ codec: null,
32022
+ reason: "stream-failed"
32023
+ });
32024
+ this.log("clip-stream:refused", {
32025
+ code,
32026
+ detail,
32027
+ ...hint !== void 0 ? { hint } : {},
32028
+ ...status !== void 0 ? { status } : {}
32029
+ });
32030
+ this.emitState("error", `refused:${code}`, token);
32031
+ }
32032
+ /**
32033
+ * Bytes are taken while OPENING too. Node flushes body bytes it already
32034
+ * holds on `process.nextTick` the moment the `data` listener attaches,
32035
+ * and that tick runs BEFORE the promise continuation that flips this
32036
+ * feeder to `streaming` — so the head of the stream (ftyp, moov) arrives
32037
+ * while the phase still says `opening`. Dropping it there turned every
32038
+ * live stream into `no-moov` while a fake opener, which delivered only
32039
+ * after resolving, kept the suite green (2026-09-22).
32040
+ */
32041
+ takesBytes() {
32042
+ return this.phase === "streaming" || this.phase === "opening";
32043
+ }
32044
+ onData(chunk) {
32045
+ if (!this.takesBytes()) return;
32046
+ this.bytesIn += chunk.byteLength;
32047
+ this.armStall();
32048
+ let units;
32049
+ try {
32050
+ units = this.demuxer.push(chunk);
32051
+ } catch (err) {
32052
+ const reason = err instanceof Fmp4DemuxError ? err.reason : "malformed";
32053
+ this.cut("demux-refused", `${reason}: ${describe(err)}`);
32054
+ return;
32055
+ }
32056
+ for (const unit of units) {
32057
+ this.queue.push(unit);
32058
+ this.queuedBytes += unit.data.byteLength;
32059
+ this.arrivedMs = unit.ptsMs;
32060
+ }
32061
+ this.settleAudioFromMoov();
32062
+ if (units.length > 0 || this.audioQueue.length > 0) this.onUnitsArrived();
32063
+ this.applyBackpressure();
32064
+ }
32065
+ /**
32066
+ * What the stream really carries, decided from the `moov` and said ONCE.
32067
+ *
32068
+ * This is the answer a dial cannot give: the provider promises a codec
32069
+ * before the camera has delivered a single audio frame, and whether the
32070
+ * recording HAS sound is written in the container it produced. A track in a
32071
+ * codec this process cannot forward, or in one the session did not
32072
+ * negotiate, is named here and never re-encoded — a second transcode on
32073
+ * this hop is exactly the backlog this path is built to avoid.
32074
+ */
32075
+ settleAudioFromMoov() {
32076
+ if (this.audioReport !== null || this.audioPush === null) return;
32077
+ const state = this.demuxer.audio;
32078
+ if (state.kind === "unknown") return;
32079
+ if (state.kind === "absent") {
32080
+ this.settleAudio({
32081
+ carried: false,
32082
+ codec: null,
32083
+ reason: "no-audio-track"
32084
+ });
32085
+ this.log("clip-stream:audio-absent", { asked: this.audioAsk });
32086
+ return;
32087
+ }
32088
+ if (state.kind === "unsupported") {
32089
+ this.settleAudio({
32090
+ carried: false,
32091
+ codec: null,
32092
+ reason: "unsupported-codec"
32093
+ });
32094
+ this.log("clip-stream:audio-unsupported", {
32095
+ asked: this.audioAsk,
32096
+ sampleEntry: state.sampleEntry
32097
+ });
32098
+ return;
32099
+ }
32100
+ if (state.codec !== this.audioAsk) {
32101
+ this.settleAudio({
32102
+ carried: false,
32103
+ codec: null,
32104
+ reason: "codec-mismatch"
32105
+ });
32106
+ this.log("clip-stream:audio-codec-mismatch", {
32107
+ asked: this.audioAsk,
32108
+ got: state.codec
32109
+ });
32110
+ return;
32111
+ }
32112
+ this.audioFramer = createClipAudioFramer(state.codec);
32113
+ this.settleAudio({
32114
+ carried: true,
32115
+ codec: state.codec,
32116
+ reason: null
32117
+ });
32118
+ this.log("clip-stream:audio-ready", {
32119
+ codec: state.codec,
32120
+ sampleRateHz: state.sampleRateHz,
32121
+ channels: state.channels
32122
+ });
32123
+ }
32124
+ /** One demuxed audio sample, framed and queued on the same axis as video. */
32125
+ onAudioUnit(unit) {
32126
+ this.settleAudioFromMoov();
32127
+ const framer = this.audioFramer;
32128
+ if (framer === null) return;
32129
+ for (const frame of framer.push(unit)) {
32130
+ this.audioQueue.push(frame);
32131
+ this.audioQueuedBytes += frame.data.byteLength;
32132
+ }
32133
+ }
32134
+ onUnitsArrived() {
32135
+ if (this.pendingSeekMs !== null) {
32136
+ this.resolvePendingSeek(this.seekToken);
32137
+ return;
32138
+ }
32139
+ while (this.rate === 0 && this.pendingSteps > 0 && this.pushNext()) this.pendingSteps -= 1;
32140
+ if (this.rate > 0 && this.paceTimer === null) this.tick();
32141
+ }
32142
+ onEnd() {
32143
+ if (!this.takesBytes()) return;
32144
+ this.streamEnded = true;
32145
+ this.clearStall();
32146
+ this.streamEnd = this.demuxer.end();
32147
+ this.log("clip-stream:ended", {
32148
+ samples: this.streamEnd.samples,
32149
+ durationMs: this.streamEnd.durationMs,
32150
+ bytesIn: this.bytesIn,
32151
+ trailingBytes: this.streamEnd.trailingBytes,
32152
+ pendingSamples: this.streamEnd.pendingSamples,
32153
+ queued: this.queue.length
32154
+ });
32155
+ this.settleDuration(this.streamEnd.durationMs);
32156
+ if (this.pendingSeekMs !== null) {
32157
+ this.resolvePendingSeek(this.seekToken);
32158
+ return;
32159
+ }
32160
+ if (this.rate > 0 && this.paceTimer === null) this.tick();
32161
+ }
32162
+ cut(reason, detail) {
32163
+ if (this.phase !== "streaming" && this.phase !== "opening") return;
32164
+ this.phase = "cut";
32165
+ this.teardown();
32166
+ this.settleDuration(null);
32167
+ this.settleAudio({
32168
+ carried: false,
32169
+ codec: null,
32170
+ reason: "stream-failed"
32171
+ });
32172
+ this.log("clip-stream:cut", {
32173
+ reason,
32174
+ detail,
32175
+ pushed: this.pushed,
32176
+ queued: this.queue.length,
32177
+ bytesIn: this.bytesIn,
32178
+ headMs: this.headMs,
32179
+ arrivedMs: this.arrivedMs,
32180
+ ...this.audioTally()
32181
+ });
32182
+ this.emitState("error", `cut:${reason}`, this.seekToken);
32183
+ }
32184
+ armStall() {
32185
+ this.clearStall();
32186
+ this.stallTimer = setTimeout(() => this.cut("stream-stalled", `no bytes for ${this.stallTimeoutMs} ms`), this.stallTimeoutMs);
32187
+ }
32188
+ clearStall() {
32189
+ if (this.stallTimer !== null) clearTimeout(this.stallTimer);
32190
+ this.stallTimer = null;
32191
+ }
32192
+ /** D447 on the consuming hop: pause the socket above the bound, resume below half. */
32193
+ applyBackpressure() {
32194
+ if (this.connection === null) return;
32195
+ const held = this.queuedBytes + this.audioQueuedBytes + this.demuxer.pendingBytes;
32196
+ if (!this.socketPaused && held > this.maxBufferedBytes) {
32197
+ this.socketPaused = true;
32198
+ this.connection?.pause();
32199
+ this.log("clip-stream:backpressure-paused", {
32200
+ heldBytes: held,
32201
+ maxBytes: this.maxBufferedBytes
32202
+ });
32203
+ } else if (this.socketPaused && held < this.maxBufferedBytes / 2) {
32204
+ this.socketPaused = false;
32205
+ this.connection?.resume();
32206
+ this.log("clip-stream:backpressure-resumed", { heldBytes: held });
32207
+ }
32208
+ }
32209
+ /**
32210
+ * Land a pending seek if its bytes are here. Called on the seek itself,
32211
+ * on every arrival while one is pending, and on end-of-stream (the last
32212
+ * chance: a target past the end lands on the last frame and the clip ends).
32213
+ */
32214
+ resolvePendingSeek(token) {
32215
+ const target = this.pendingSeekMs;
32216
+ if (target === null || this.phase !== "streaming") return;
32217
+ let keep = 0;
32218
+ for (let i = 0; i < this.queue.length; i++) {
32219
+ const unit = this.queue[i];
32220
+ if (unit === void 0 || unit.ptsMs > target) break;
32221
+ if (unit.keyframe) keep = i;
32222
+ }
32223
+ if (keep > 0) {
32224
+ for (let i = 0; i < keep; i++) this.queuedBytes -= this.queue[i]?.data.byteLength ?? 0;
32225
+ this.dropped += keep;
32226
+ this.queue = this.queue.slice(keep);
32227
+ }
32228
+ if (!(this.arrivedMs !== null && this.arrivedMs >= target) && !this.streamEnded) return;
32229
+ if (this.queue.length === 0) {
32230
+ if (this.streamEnded) {
32231
+ this.pendingSeekMs = null;
32232
+ this.log("clip-stream:seek-past-end", {
32233
+ targetMs: target,
32234
+ headMs: this.headMs
32235
+ });
32236
+ this.finish();
32237
+ }
32238
+ return;
32239
+ }
32240
+ let burst = 0;
32241
+ while (this.queue.length > 0) {
32242
+ const next = this.queue[0];
32243
+ if (next === void 0 || burst > 0 && next.ptsMs > target) break;
32244
+ this.pushNext();
32245
+ burst += 1;
32246
+ }
32247
+ this.pendingSeekMs = null;
32248
+ this.dropAudioBefore(this.headMs);
32249
+ this.log("clip-stream:seek-landed", {
32250
+ targetMs: target,
32251
+ landedMs: this.headMs,
32252
+ droppedUnits: keep,
32253
+ burstUnits: burst,
32254
+ queued: this.queue.length,
32255
+ streamEnded: this.streamEnded
32256
+ });
32257
+ if (this.headMs !== null) this.deps.onPosition(this.headMs);
32258
+ if (this.rate === 0) {
32259
+ this.emitState("paused", "seek-landed-paused", token);
32260
+ return;
32261
+ }
32262
+ this.emitState("playing", "seek-landed", token);
32263
+ this.startPacing();
32264
+ }
32265
+ /**
32266
+ * The audio RTP timestamp for a source instant — off the SAME clock as the
32267
+ * video, converted to the negotiated rate.
32268
+ *
32269
+ * ONE mapping from source time to output time, not two. The recorded path
32270
+ * runs a second `MonotonicClock` for audio and keeps the pair aligned by
32271
+ * calling `markSeek()` on both, and that alignment is only as good as the
32272
+ * discipline: each clock re-bases on its OWN last output, so every
32273
+ * discontinuity leaves the two a frame further apart than it found them —
32274
+ * a skew that is invisible on a short clip, grows with each seek, and is
32275
+ * exactly the drift this path is not going to pay for twice. Deriving the
32276
+ * audio stamp from the video clock's own output makes the alignment
32277
+ * structural: there is nothing to keep in step.
32278
+ *
32279
+ * The conversion is exact to within a rounding tick (the video clock counts
32280
+ * 90 kHz, so an output instant is known to 1/90 ms) and is computed from
32281
+ * the source pts every time, never accumulated.
32282
+ */
32283
+ stampAudio(ptsMs) {
32284
+ return Math.round(this.clock.stamp(ptsMs) * this.audioClockHz / 9e4);
32285
+ }
32286
+ /** Forget sound the playhead has already passed — a forward seek's share. */
32287
+ dropAudioBefore(headMs) {
32288
+ if (headMs === null || this.audioQueue.length === 0) return;
32289
+ let drop = 0;
32290
+ while (drop < this.audioQueue.length) {
32291
+ const frame = this.audioQueue[drop];
32292
+ if (frame === void 0 || frame.ptsMs + frame.durationMs >= headMs) break;
32293
+ this.audioQueuedBytes -= frame.data.byteLength;
32294
+ drop += 1;
32295
+ }
32296
+ if (drop === 0) return;
32297
+ this.audioQueue = this.audioQueue.slice(drop);
32298
+ this.audioDroppedSeek += drop;
32299
+ }
32300
+ startPacing() {
32301
+ this.stopPacing();
32302
+ this.anchorWall = this.now();
32303
+ this.anchorPts = this.headMs ?? this.queue[0]?.ptsMs ?? 0;
32304
+ this.startPositionReports();
32305
+ this.tick();
32306
+ }
32307
+ stopPacing() {
32308
+ if (this.paceTimer !== null) clearTimeout(this.paceTimer);
32309
+ this.paceTimer = null;
32310
+ }
32311
+ /**
32312
+ * Push every unit that is due; sleep until the next; wait for data, or end.
32313
+ *
32314
+ * ONE schedule for both tracks, ordered by `ptsMs` with VIDEO first on a
32315
+ * tie — the same ordering `RecordedFeeder.paceSegment` builds, so a clip
32316
+ * and the recorder's own footage interleave identically. The two clocks
32317
+ * stay separate (each codec's RTP rate) but they stamp the SAME axis.
32318
+ */
32319
+ tick() {
32320
+ this.paceTimer = null;
32321
+ if (this.phase !== "streaming" || this.rate === 0 || this.pendingSeekMs !== null) return;
32322
+ for (;;) {
32323
+ const video = this.queue[0];
32324
+ const audio = this.audioQueue[0];
32325
+ if (video === void 0 && audio === void 0) {
32326
+ if (this.streamEnded) this.finish();
32327
+ return;
32328
+ }
32329
+ const takeVideo = video !== void 0 && (audio === void 0 || video.ptsMs <= audio.ptsMs);
32330
+ const ptsMs = takeVideo ? video?.ptsMs ?? 0 : audio?.ptsMs ?? 0;
32331
+ const wait = this.anchorWall + (ptsMs - this.anchorPts) / this.rate - this.now();
32332
+ if (takeVideo) {
32333
+ if (wait > 0) {
32334
+ this.paceTimer = setTimeout(() => this.tick(), Math.min(wait, WAIT_TICK_MS));
32335
+ return;
32336
+ }
32337
+ this.pushNext();
32338
+ continue;
32339
+ }
32340
+ if (wait > 0) {
32341
+ this.paceTimer = setTimeout(() => this.tick(), Math.min(wait, WAIT_TICK_MS));
32342
+ return;
32343
+ }
32344
+ this.pushNextAudio(-wait);
32345
+ }
32346
+ }
32347
+ /**
32348
+ * Push or DROP the head of the audio queue. False only when it was empty.
32349
+ *
32350
+ * `lateMs` is how far the frame's moment has already passed. Past the
32351
+ * bound it is dropped and counted: a receiver would discard it anyway, and
32352
+ * pushing a run of them is the burst that makes a clip sound broken rather
32353
+ * than silent. At any rate but 1x it is dropped for a different reason,
32354
+ * counted apart, because resampling would need a decoder this path has not
32355
+ * got and pitch-shifted sound is worse than none.
32356
+ */
32357
+ pushNextAudio(lateMs) {
32358
+ const frame = this.audioQueue[0];
32359
+ if (frame === void 0) return false;
32360
+ this.audioQueue = this.audioQueue.slice(1);
32361
+ this.audioQueuedBytes -= frame.data.byteLength;
32362
+ if (this.rate !== 1) {
32363
+ this.audioDroppedRate += 1;
32364
+ return true;
32365
+ }
32366
+ if (lateMs > 120) {
32367
+ this.audioDroppedLate += 1;
32368
+ return true;
32369
+ }
32370
+ this.audioPush?.(frame.data, this.stampAudio(frame.ptsMs));
32371
+ this.audioPushed += 1;
32372
+ if (!this.firstAudioLogged) {
32373
+ this.firstAudioLogged = true;
32374
+ this.log("clip-stream:first-audio", {
32375
+ ptsMs: frame.ptsMs,
32376
+ lateMs: Math.round(lateMs),
32377
+ headMs: this.headMs,
32378
+ droppedLate: this.audioDroppedLate
32379
+ });
32380
+ }
32381
+ this.applyBackpressure();
32382
+ return true;
32383
+ }
32384
+ /** Push the head of the queue onto the track. False when the queue is empty. */
32385
+ pushNext() {
32386
+ const unit = this.queue.shift();
32387
+ if (unit === void 0) return false;
32388
+ this.queuedBytes -= unit.data.byteLength;
32389
+ this.deps.sink.pushAccessUnit(unit.data, this.clock.stamp(unit.ptsMs), unit.codec);
32390
+ this.clockHasAdvanced = true;
32391
+ this.headMs = this.headMs === null ? unit.ptsMs : Math.max(this.headMs, unit.ptsMs);
32392
+ this.pushed += 1;
32393
+ if (!this.firstFrameLogged) {
32394
+ this.firstFrameLogged = true;
32395
+ const at = this.now();
32396
+ this.log("clip-stream:first-frame", {
32397
+ sinceSeekMs: this.seekStartedAt === null ? null : at - this.seekStartedAt,
32398
+ sinceDialMs: this.dialedAt === null ? null : at - this.dialedAt,
32399
+ bytesIn: this.bytesIn,
32400
+ ptsMs: unit.ptsMs,
32401
+ codec: unit.codec
32402
+ });
32403
+ }
32404
+ this.applyBackpressure();
32405
+ return true;
32406
+ }
32407
+ finish() {
32408
+ if (this.phase !== "streaming") return;
32409
+ this.phase = "ended";
32410
+ this.settleAudio({
32411
+ carried: false,
32412
+ codec: null,
32413
+ reason: this.demuxer.ready ? "no-audio-track" : "stream-failed"
32414
+ });
32415
+ this.stopPacing();
32416
+ this.stopPositionReports();
32417
+ if (this.headMs !== null) this.deps.onPosition(this.headMs);
32418
+ this.log("clip-stream:played-out", {
32419
+ pushed: this.pushed,
32420
+ dropped: this.dropped,
32421
+ headMs: this.headMs,
32422
+ durationMs: this.streamEnd?.durationMs ?? null,
32423
+ ...this.audioTally()
32424
+ });
32425
+ this.emitState("ended", "stream-played-out", this.seekToken);
32426
+ }
32427
+ startPositionReports() {
32428
+ if (this.positionTimer !== null) return;
32429
+ this.positionTimer = setInterval(() => {
32430
+ if (this.headMs !== null && this.phase === "streaming") this.deps.onPosition(this.headMs);
32431
+ }, POSITION_INTERVAL_MS);
32432
+ }
32433
+ stopPositionReports() {
32434
+ if (this.positionTimer !== null) clearInterval(this.positionTimer);
32435
+ this.positionTimer = null;
32436
+ }
32437
+ emitState(to, reason, seekToken) {
32438
+ const from = this.lastAnnounced;
32439
+ this.lastAnnounced = to;
32440
+ this.deps.onState({
32441
+ from,
32442
+ to,
32443
+ reason,
32444
+ seekToken
32445
+ });
32446
+ }
32447
+ /** The audio counters every exit line carries. Silence is never unexplained. */
32448
+ audioTally() {
32449
+ return {
32450
+ audioAsked: this.audioAsk,
32451
+ audioCarried: this.audioReport?.carried ?? false,
32452
+ audioSilentReason: this.audioReport?.reason ?? null,
32453
+ audioPushed: this.audioPushed,
32454
+ audioDroppedLate: this.audioDroppedLate,
32455
+ audioDroppedRate: this.audioDroppedRate,
32456
+ audioDroppedSeek: this.audioDroppedSeek,
32457
+ audioQueued: this.audioQueue.length
32458
+ };
32459
+ }
32460
+ settleAudio(report) {
32461
+ if (this.audioReport !== null) return;
32462
+ this.audioReport = report;
32463
+ const waiters = this.audioWaiters.splice(0);
32464
+ for (const resolve of waiters) resolve(report);
32465
+ }
32466
+ settleDuration(value) {
32467
+ if (this.duration !== null) return;
32468
+ this.duration = {
32469
+ settled: true,
32470
+ value
32471
+ };
32472
+ const waiters = this.durationWaiters.splice(0);
32473
+ for (const resolve of waiters) resolve(value);
32474
+ }
32475
+ teardown() {
32476
+ this.stopPacing();
32477
+ this.stopPositionReports();
32478
+ this.clearStall();
32479
+ const connection = this.connection;
32480
+ this.connection = null;
32481
+ connection?.destroy();
32482
+ }
32483
+ log(msg, data) {
32484
+ this.deps.onLog?.(msg, {
32485
+ ...this.tags,
32486
+ ...data
32487
+ });
32488
+ }
32489
+ };
32490
+ function describe(err) {
32491
+ return err instanceof Error ? err.message : String(err);
32492
+ }
32493
+ function headerText(res, name) {
32494
+ const value = res.headers[name];
32495
+ if (Array.isArray(value)) return value[0];
32496
+ return value;
32497
+ }
32498
+ function createHttpClipStreamOpener(options = {}) {
32499
+ const headersTimeoutMs = options.headersTimeoutMs ?? 3e4;
32500
+ return (url, handlers) => new Promise((resolve) => {
32501
+ let settled = false;
32502
+ const settle = (value) => {
32503
+ if (settled) return;
32504
+ settled = true;
32505
+ clearTimeout(timer);
32506
+ resolve(value);
32507
+ };
32508
+ let target;
32509
+ try {
32510
+ target = new URL(url);
32511
+ } catch (err) {
32512
+ settle({
32513
+ kind: "refused",
32514
+ status: 0,
32515
+ code: "bad-url",
32516
+ detail: String(err)
32517
+ });
32518
+ return;
32519
+ }
32520
+ if (target.protocol !== "http:") {
32521
+ settle({
32522
+ kind: "refused",
32523
+ status: 0,
32524
+ code: "bad-url",
32525
+ detail: `a clip stream is dialled over plain http on the provider's listener, not ${target.protocol}`
32526
+ });
32527
+ return;
32528
+ }
32529
+ const req = (0, node_http.request)(target, {
32530
+ method: "GET",
32531
+ headers: { accept: "video/mp4" }
32532
+ });
32533
+ const timer = setTimeout(() => {
32534
+ req.destroy(/* @__PURE__ */ new Error(`no status line in ${headersTimeoutMs} ms`));
32535
+ }, headersTimeoutMs);
32536
+ req.on("error", (err) => {
32537
+ settle({
32538
+ kind: "refused",
32539
+ status: 0,
32540
+ code: "connect-failed",
32541
+ detail: err.message
32542
+ });
32543
+ });
32544
+ req.on("response", (res) => {
32545
+ const status = res.statusCode ?? 0;
32546
+ if (status !== 200) {
32547
+ res.resume();
32548
+ settle({
32549
+ kind: "refused",
32550
+ status,
32551
+ code: headerText(res, "x-camstack-reason-code") ?? `http-${status}`,
32552
+ detail: headerText(res, "x-camstack-reason") ?? ""
32553
+ });
32554
+ return;
32555
+ }
32556
+ if (headerText(res, "x-camstack-clip-transport") !== "forward-only") {
32557
+ req.destroy();
32558
+ settle({
32559
+ kind: "refused",
32560
+ status,
32561
+ code: "not-a-stream",
32562
+ detail: "the URL answered 200 without declaring the forward-only clip transport"
32563
+ });
32564
+ return;
32565
+ }
32566
+ res.on("data", (chunk) => handlers.onData(chunk));
32567
+ res.on("end", () => handlers.onEnd());
32568
+ res.on("error", (err) => handlers.onError(err));
32569
+ settle({
32570
+ kind: "open",
32571
+ connection: {
32572
+ pause: () => {
32573
+ res.pause();
32574
+ },
32575
+ resume: () => {
32576
+ res.resume();
32577
+ },
32578
+ destroy: () => {
32579
+ req.destroy();
32580
+ }
32581
+ }
32582
+ });
32583
+ });
32584
+ req.end();
32585
+ });
32586
+ }
32587
+ //#endregion
30593
32588
  //#region src/stream-broker/recorded/demux-off-thread.ts
30594
32589
  /**
30595
32590
  * Off-thread JS demux client — mp4box parse on a `worker_threads` Worker
@@ -31336,6 +33331,7 @@ var BrokerWebrtcServer = class {
31336
33331
  readRecordedSegmentBytes;
31337
33332
  readRecordedGopBytes;
31338
33333
  readClipBytes;
33334
+ dialClipStream;
31339
33335
  resolveFfmpegPath;
31340
33336
  /**
31341
33337
  * THE ffmpeg primitive, for the transcode leg. `null` until the manager wires
@@ -31429,6 +33425,7 @@ var BrokerWebrtcServer = class {
31429
33425
  this.readRecordedSegmentBytes = options.readRecordedSegmentBytes;
31430
33426
  this.readRecordedGopBytes = options.readRecordedGopBytes;
31431
33427
  this.readClipBytes = options.readClipBytes;
33428
+ this.dialClipStream = options.dialClipStream;
31432
33429
  this.resolveFfmpegPath = options.resolveFfmpegPath;
31433
33430
  }
31434
33431
  registerBroker(brokerId, broker) {
@@ -32149,14 +34146,93 @@ var BrokerWebrtcServer = class {
32149
34146
  };
32150
34147
  } : void 0;
32151
34148
  const readClip = this.readClipBytes;
34149
+ const dialClip = this.dialClipStream;
34150
+ const openClipStream = createHttpClipStreamOpener();
32152
34151
  let timeline;
32153
34152
  /**
32154
34153
  * ONE feeder builder for BOTH sources (D575). `clip === null` is the
32155
34154
  * recorder's own footage; a clip swaps only the two byte closures —
32156
34155
  * everything else (demux, sink, audio codec branch, state/position/log
32157
34156
  * seams) is identical, which is the whole claim the clip path makes.
34157
+ *
34158
+ * Since D597 a clip has TWO paths, and which one it gets is decided
34159
+ * ONCE, here, by `chooseClipPath`, and logged with its reason: the
34160
+ * STREAM for the sub twin (forward-only, first frame at the first
34161
+ * fragment) whenever this broker can dial one, the FILE for `high` and
34162
+ * for a broker with no dial. Never an implicit fallthrough.
32158
34163
  */
32159
34164
  const buildFeeder = (profile, clip) => {
34165
+ if (clip) {
34166
+ const choice = chooseClipPath({
34167
+ profile: clip.profile,
34168
+ streamDialWired: dialClip !== void 0,
34169
+ fileReadWired: readClip !== void 0
34170
+ });
34171
+ sessionLogger.info("clip playback: path chosen", {
34172
+ tags: { deviceId: clip.deviceId },
34173
+ meta: {
34174
+ sessionId,
34175
+ clipId: clip.clipId,
34176
+ provider: clip.provider,
34177
+ profile: clip.profile,
34178
+ path: choice.path,
34179
+ reason: choice.reason
34180
+ }
34181
+ });
34182
+ const clipLog = (msg, data) => sessionLogger.info(msg, {
34183
+ tags: { deviceId: clip.deviceId },
34184
+ meta: {
34185
+ sessionId,
34186
+ clipId: clip.clipId,
34187
+ ...data
34188
+ }
34189
+ });
34190
+ const onClipState = (t) => {
34191
+ sessionLogger.info("recorded: feeder state", {
34192
+ tags: { deviceId: clip.deviceId },
34193
+ meta: {
34194
+ sessionId,
34195
+ deviceId,
34196
+ profile,
34197
+ clipId: clip.clipId,
34198
+ path: choice.path,
34199
+ from: t.from,
34200
+ to: t.to,
34201
+ reason: t.reason,
34202
+ seekToken: t.seekToken
34203
+ }
34204
+ });
34205
+ timeline._emitState(t.to);
34206
+ };
34207
+ if (choice.path === "none") return createRefusedClipFeeder({
34208
+ handle: clip,
34209
+ reason: choice.reason,
34210
+ onState: onClipState,
34211
+ onLog: clipLog
34212
+ });
34213
+ if (choice.path === "stream" && dialClip !== void 0) return createClipStreamFeeder({
34214
+ handle: clip,
34215
+ dial: (handle, audio) => dialClip({
34216
+ deviceId: handle.deviceId,
34217
+ clipId: handle.clipId,
34218
+ provider: handle.provider,
34219
+ profile: handle.profile,
34220
+ audio
34221
+ }),
34222
+ open: openClipStream,
34223
+ audio: session.isOpusAudio ? "opus" : "pcmu",
34224
+ sink: session.isOpusAudio ? {
34225
+ pushAccessUnit: (data, ts, codec) => session.pushRecordedAccessUnit(data, ts, codec),
34226
+ pushAudioOpus: (opus, ts) => session.writeRecordedAudioOpus(Buffer.from(opus), ts)
34227
+ } : {
34228
+ pushAccessUnit: (data, ts, codec) => session.pushRecordedAccessUnit(data, ts, codec),
34229
+ pushAudioPcmu: (pcmu, ts) => session.writeRecordedAudio(Buffer.from(pcmu), ts)
34230
+ },
34231
+ onState: onClipState,
34232
+ onPosition: (epochMs) => timeline._emitPosition(epochMs),
34233
+ onLog: clipLog
34234
+ });
34235
+ }
32160
34236
  const opusSession = session.isOpusAudio;
32161
34237
  const audioSink = opusSession ? {
32162
34238
  pushAccessUnit: (data, ts, codec) => session.pushRecordedAccessUnit(data, ts, codec),
@@ -32270,7 +34346,7 @@ var BrokerWebrtcServer = class {
32270
34346
  });
32271
34347
  } : void 0,
32272
34348
  makeFeeder: (profile) => buildFeeder(profile, null),
32273
- ...readClip ? { makeClipFeeder: ({ handle }) => buildFeeder(handle.profile ?? "clip", handle) } : {},
34349
+ ...readClip || dialClip ? { makeClipFeeder: ({ handle }) => buildFeeder(handle.profile ?? "clip", handle) } : {},
32274
34350
  onLog: (msg, data) => sessionLogger.warn(msg, {
32275
34351
  tags: { deviceId },
32276
34352
  meta: {
@@ -33315,6 +35391,51 @@ var WebrtcSessionProvider = class WebrtcSessionProvider {
33315
35391
  }
33316
35392
  };
33317
35393
  //#endregion
35394
+ //#region src/stream-broker/recorded/clip-stream-dial-answer.ts
35395
+ /**
35396
+ * clip-stream-dial-answer — what a THROWN `videoclips.dialClipStream` means.
35397
+ *
35398
+ * The cap call has three ways to fail, and the feeder must tell them apart
35399
+ * because they lead to different lines and different states:
35400
+ *
35401
+ * - the provider PREDATES the method (`NOT_IMPLEMENTED`, "does not
35402
+ * implement") — the stream does not exist on this cluster yet, which is a
35403
+ * deploy-order fact, not a clip fact;
35404
+ * - the provider REFUSED in the clip vocabulary (`clipExportFailure`):
35405
+ * `sleeping`, `catalog-miss`, `clip-in-progress`, `unsupported-option`… —
35406
+ * the code is kept, the prose travels with it;
35407
+ * - anything else — the transport, a bug — is `fetch-failed` with the prose
35408
+ * verbatim, exactly as the export engine treats an unknown error (D572).
35409
+ */
35410
+ function trpcCodeOf(err) {
35411
+ if (typeof err !== "object" || err === null) return null;
35412
+ const shaped = err;
35413
+ if (typeof shaped.data?.code === "string") return shaped.data.code;
35414
+ if (typeof shaped.code === "string") return shaped.code;
35415
+ return null;
35416
+ }
35417
+ function dialAnswerFromError(err) {
35418
+ const message = err instanceof Error ? err.message : String(err);
35419
+ if (trpcCodeOf(err) === "NOT_IMPLEMENTED" || /does not implement/i.test(message)) return {
35420
+ kind: "unavailable",
35421
+ detail: message
35422
+ };
35423
+ const code = require_dist.clipExportFailureOf(message);
35424
+ if (code !== null) {
35425
+ const sep = message.indexOf(":");
35426
+ return {
35427
+ kind: "refused",
35428
+ code,
35429
+ detail: sep === -1 ? "" : message.slice(sep + 1).trim()
35430
+ };
35431
+ }
35432
+ return {
35433
+ kind: "refused",
35434
+ code: "fetch-failed",
35435
+ detail: message
35436
+ };
35437
+ }
35438
+ //#endregion
33318
35439
  //#region src/stream-broker/event-loop-stall-monitor.ts
33319
35440
  var DEFAULT_INTERVAL_MS = 100;
33320
35441
  var DEFAULT_THRESHOLD_MS = 250;
@@ -33789,6 +35910,27 @@ var StreamBrokerAddon = class extends require_dist.BaseAddon {
33789
35910
  });
33790
35911
  return new Uint8Array(Buffer.from(res.base64, "base64"));
33791
35912
  },
35913
+ dialClipStream: async (args) => {
35914
+ try {
35915
+ const dial = await this.ctx.api.videoclips.dialClipStream.query({
35916
+ deviceId: args.deviceId,
35917
+ clipId: args.clipId,
35918
+ provider: args.provider,
35919
+ ...args.profile !== null ? { profile: args.profile } : {},
35920
+ audio: args.audio
35921
+ });
35922
+ return {
35923
+ kind: "dialed",
35924
+ url: dial.url,
35925
+ expiresAtMs: dial.expiresAtMs,
35926
+ ...typeof dial.served === "string" ? { served: dial.served } : {},
35927
+ ...typeof dial.servedAudio === "string" ? { servedAudio: dial.servedAudio } : {},
35928
+ ...typeof dial.audioReason === "string" ? { audioReason: dial.audioReason } : {}
35929
+ };
35930
+ } catch (err) {
35931
+ return dialAnswerFromError(err);
35932
+ }
35933
+ },
33792
35934
  readRecordedGopBytes: async (args) => {
33793
35935
  try {
33794
35936
  return await this.ctx.api.recording.readGopBytes.query({