@torrent-tv/proxy 2.89.17 → 2.89.19

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 (300) hide show
  1. package/README.md +14 -3
  2. package/package.json +10 -2
  3. package/server.js +0 -11
  4. package/utils/client-log-file.js +147 -42
  5. package/.claude/commands/opsx/apply.md +0 -155
  6. package/.claude/commands/opsx/archive.md +0 -160
  7. package/.claude/commands/opsx/explore.md +0 -174
  8. package/.claude/commands/opsx/propose.md +0 -109
  9. package/.claude/commands/opsx/sync.md +0 -143
  10. package/.claude/skills/openspec-apply-change/SKILL.md +0 -159
  11. package/.claude/skills/openspec-archive-change/SKILL.md +0 -117
  12. package/.claude/skills/openspec-explore/SKILL.md +0 -289
  13. package/.claude/skills/openspec-propose/SKILL.md +0 -113
  14. package/.claude/skills/openspec-sync-specs/SKILL.md +0 -147
  15. package/.dockerignore +0 -5
  16. package/AGENTS.md +0 -363
  17. package/CHANGELOG.md +0 -2377
  18. package/Dockerfile +0 -30
  19. package/biome.json +0 -412
  20. package/corpus.tmp.json +0 -1
  21. package/docs/browser-proxy-contract.md +0 -155
  22. package/docs/container-architecture.md +0 -273
  23. package/docs/download-architecture.md +0 -261
  24. package/docs/encode-architecture.md +0 -765
  25. package/docs/encode-run-state.md +0 -96
  26. package/docs/logs.md +0 -151
  27. package/docs/playback-start-forecast.md +0 -113
  28. package/docs/socks5-egress-research.md +0 -114
  29. package/docs/storage-architecture.md +0 -273
  30. package/knip.json +0 -14
  31. package/main2.cpuprofile +0 -1
  32. package/openspec/changes/adaptive-bitrate/.openspec.yaml +0 -0
  33. package/openspec/changes/adaptive-bitrate/design.md +0 -92
  34. package/openspec/changes/adaptive-bitrate/proposal.md +0 -56
  35. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +0 -51
  36. package/openspec/changes/adaptive-bitrate/tasks.md +0 -40
  37. package/openspec/changes/binary-distribution/.openspec.yaml +0 -2
  38. package/openspec/changes/binary-distribution/proposal.md +0 -53
  39. package/openspec/changes/chunked-request-bodies/design.md +0 -81
  40. package/openspec/changes/chunked-request-bodies/proposal.md +0 -53
  41. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +0 -30
  42. package/openspec/changes/chunked-request-bodies/tasks.md +0 -40
  43. package/openspec/changes/cold-start/design.md +0 -98
  44. package/openspec/changes/cold-start/proposal.md +0 -62
  45. package/openspec/changes/cold-start/specs/cold-start/spec.md +0 -47
  46. package/openspec/changes/cold-start/tasks.md +0 -48
  47. package/openspec/changes/disk-cap/.openspec.yaml +0 -2
  48. package/openspec/changes/disk-cap/proposal.md +0 -37
  49. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +0 -25
  50. package/openspec/changes/disk-cap/tasks.md +0 -19
  51. package/openspec/changes/proxy-observability/.openspec.yaml +0 -2
  52. package/openspec/changes/proxy-observability/design.md +0 -38
  53. package/openspec/changes/proxy-observability/proposal.md +0 -49
  54. package/openspec/changes/proxy-observability/specs/observability/spec.md +0 -33
  55. package/openspec/changes/proxy-observability/tasks.md +0 -21
  56. package/openspec/changes/seek-debounce/.openspec.yaml +0 -0
  57. package/openspec/changes/seek-debounce/design.md +0 -118
  58. package/openspec/changes/seek-debounce/proposal.md +0 -59
  59. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +0 -41
  60. package/openspec/changes/seek-debounce/tasks.md +0 -33
  61. package/openspec/changes/subtitle-language/.openspec.yaml +0 -2
  62. package/openspec/changes/subtitle-language/proposal.md +0 -50
  63. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +0 -34
  64. package/openspec/changes/subtitle-language/tasks.md +0 -26
  65. package/openspec/changes/track-selection/.openspec.yaml +0 -2
  66. package/openspec/changes/track-selection/design.md +0 -47
  67. package/openspec/changes/track-selection/proposal.md +0 -45
  68. package/openspec/changes/track-selection/specs/track-selection/spec.md +0 -41
  69. package/openspec/changes/track-selection/tasks.md +0 -20
  70. package/openspec/changes/transcode-quality/.openspec.yaml +0 -2
  71. package/openspec/changes/transcode-quality/proposal.md +0 -51
  72. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +0 -139
  73. package/openspec/changes/transcode-quality/tasks.md +0 -63
  74. package/openspec/config.yaml +0 -38
  75. package/public/.well-known/appspecific/com.chrome.devtools.json +0 -1
  76. package/research/delivery-wedge-stand-ladder-2026-09-13.md +0 -329
  77. package/research/double-spawn-2026-09-10.md +0 -171
  78. package/research/handover-2026-09-13.md +0 -209
  79. package/research/handover-reader-claims-removal-2026-09-12.md +0 -166
  80. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +0 -175
  81. package/research/preset-benchmark-size-scaling-2026-08-22.md +0 -55
  82. package/research/priority-map-is-the-truth-2026-09-12.md +0 -225
  83. package/research/valgrind-replica-2026-08-27/logs.tar.gz +0 -0
  84. package/scripts/build-audio-samples.mjs +0 -31
  85. package/scripts/render-run-graph.js +0 -173
  86. package/test/asset-paths.test.js +0 -28
  87. package/test/audio-buffer-continuity.test.js +0 -77
  88. package/test/audio-calibration.test.js +0 -23
  89. package/test/audio-inventory.test.js +0 -212
  90. package/test/audio-track-bitrate.test.js +0 -40
  91. package/test/audio-warm-recorded.test.js +0 -63
  92. package/test/auto-quality-step.test.js +0 -762
  93. package/test/available-share.test.js +0 -76
  94. package/test/background-fill.test.js +0 -190
  95. package/test/client-log-file.test.js +0 -126
  96. package/test/client-logs-route.test.js +0 -34
  97. package/test/coalesce.test.js +0 -39
  98. package/test/completed-files.test.js +0 -115
  99. package/test/concurrent-cost.test.js +0 -148
  100. package/test/container-media-info.test.js +0 -228
  101. package/test/contention.test.js +0 -76
  102. package/test/core-dumps.test.js +0 -57
  103. package/test/coverage-follows-the-disk.test.js +0 -233
  104. package/test/coverage-map.test.js +0 -195
  105. package/test/cue-push-carries-what-the-walk-found.test.js +0 -58
  106. package/test/cut-times-timeline.test.js +0 -65
  107. package/test/cuts-follow-published-grid.test.js +0 -109
  108. package/test/data-channel-frame.test.js +0 -63
  109. package/test/data-channel-viewer-lifecycle.test.js +0 -193
  110. package/test/dead-torrent-handle.test.js +0 -52
  111. package/test/declared-rates.test.js +0 -116
  112. package/test/declared-tracks.test.js +0 -43
  113. package/test/decode-cost-fit.test.js +0 -202
  114. package/test/decode-cost.test.js +0 -475
  115. package/test/delivery-probe-field.test.js +0 -173
  116. package/test/delivery-probe.test.js +0 -382
  117. package/test/demand-register.test.js +0 -206
  118. package/test/dht-bootstrap.test.js +0 -72
  119. package/test/drain-threat.test.js +0 -65
  120. package/test/duplicate-add.test.js +0 -48
  121. package/test/encode-admission.test.js +0 -170
  122. package/test/encode-cost.test.js +0 -275
  123. package/test/encode-exit.test.js +0 -111
  124. package/test/encode-orchestrator.test.js +0 -426
  125. package/test/encode-plan-viewers.test.js +0 -725
  126. package/test/encode-plan.test.js +0 -545
  127. package/test/encode-run-state.test.js +0 -314
  128. package/test/encode-run.test.js +0 -248
  129. package/test/encode-slope.test.js +0 -46
  130. package/test/encoded-output.test.js +0 -39
  131. package/test/encoder-kinds.test.js +0 -122
  132. package/test/encoder-readings.test.js +0 -68
  133. package/test/episode-naming.test.js +0 -131
  134. package/test/every-claimant-is-told-its-share.test.js +0 -152
  135. package/test/failed-encoder-still-serves-what-exists.test.js +0 -354
  136. package/test/failed-start-breaker.test.js +0 -202
  137. package/test/fastest-wires.test.js +0 -143
  138. package/test/ffmpeg-stream-counts.test.js +0 -101
  139. package/test/file-edges.test.js +0 -191
  140. package/test/files-in-use.test.js +0 -34
  141. package/test/fixtures/audio-frame-boundary.json +0 -1
  142. package/test/fixtures/refused-audio-20261002.json +0 -10
  143. package/test/fmp4-presentation-clock.test.js +0 -148
  144. package/test/fmp4-self-contained.test.js +0 -80
  145. package/test/fmp4-timestamps.test.js +0 -140
  146. package/test/fmp4-track-completeness.test.js +0 -41
  147. package/test/format-keyed-outputs.test.js +0 -78
  148. package/test/health-metrics.test.js +0 -36
  149. package/test/held-read-ownership.test.js +0 -77
  150. package/test/held-request-width.test.js +0 -157
  151. package/test/helpers/encode-run.js +0 -248
  152. package/test/helpers/held-file.js +0 -77
  153. package/test/helpers/manager.js +0 -62
  154. package/test/helpers/matroska-file.js +0 -296
  155. package/test/helpers/output-spec.js +0 -63
  156. package/test/host-load.test.js +0 -68
  157. package/test/index-accuracy-is-about-the-file.test.js +0 -128
  158. package/test/init-compat.test.js +0 -219
  159. package/test/input-lost-quiets-the-plan.test.js +0 -261
  160. package/test/input-unavailable.test.js +0 -48
  161. package/test/installation-id.test.js +0 -39
  162. package/test/keeping-period.test.js +0 -83
  163. package/test/keyframe-index-accuracy.test.js +0 -188
  164. package/test/keyframe-table-budget.test.js +0 -123
  165. package/test/keyframes-belong-to-the-file.test.js +0 -249
  166. package/test/learned-median.test.js +0 -37
  167. package/test/letting-go-asks-who-is-watching.test.js +0 -191
  168. package/test/limit-row-by-area.test.js +0 -60
  169. package/test/link-budget.test.js +0 -111
  170. package/test/link-probe.test.js +0 -56
  171. package/test/local-encoding-data.test.js +0 -273
  172. package/test/logger-repeats.test.js +0 -134
  173. package/test/look-ahead-coverage.test.js +0 -56
  174. package/test/manager-surface.test.js +0 -291
  175. package/test/matroska-blocks.test.js +0 -0
  176. package/test/matroska-cluster-discovery.test.js +0 -323
  177. package/test/matroska-cues-track.test.js +0 -192
  178. package/test/media-time.test.js +0 -62
  179. package/test/memory-budget.test.js +0 -390
  180. package/test/move-cost.test.js +0 -216
  181. package/test/mp4-composition-times.test.js +0 -0
  182. package/test/mp4-subtitles.test.js +0 -173
  183. package/test/one-authority.test.js +0 -285
  184. package/test/one-budget.test.js +0 -149
  185. package/test/one-output-one-session.test.js +0 -273
  186. package/test/one-resource-divided.test.js +0 -185
  187. package/test/one-serving-output-per-height.test.js +0 -458
  188. package/test/orchestrator-wired.test.js +0 -207
  189. package/test/output-catalog.test.js +0 -39
  190. package/test/output-shape.test.js +0 -21
  191. package/test/output-spec.test.js +0 -251
  192. package/test/output-speed.test.js +0 -92
  193. package/test/outputs-being-prepared.test.js +0 -40
  194. package/test/packet-witness-ring.test.js +0 -236
  195. package/test/packet-witness.test.js +0 -148
  196. package/test/piece-completeness.test.js +0 -78
  197. package/test/piece-disk-store.test.js +0 -401
  198. package/test/piece-from-whole-file.test.js +0 -129
  199. package/test/piece-lru.test.js +0 -319
  200. package/test/piece-reader.test.js +0 -162
  201. package/test/piece-selection-offset.test.js +0 -85
  202. package/test/piece-store-eviction.test.js +0 -595
  203. package/test/piece-store-never-refuses.test.js +0 -153
  204. package/test/piece-store-reservations.test.js +0 -403
  205. package/test/piece-store-slow-disk.test.js +0 -159
  206. package/test/piece-tail.test.js +0 -97
  207. package/test/plan-host-timings.test.js +0 -68
  208. package/test/plan-is-steady.test.js +0 -225
  209. package/test/playback-readiness.test.js +0 -518
  210. package/test/playback-start-regressions.test.js +0 -101
  211. package/test/priority-map-download.test.js +0 -174
  212. package/test/priority-map-emptied.test.js +0 -204
  213. package/test/priority-map-per-output.test.js +0 -215
  214. package/test/priority-map.test.js +0 -163
  215. package/test/probe-wedge-certainty.test.js +0 -60
  216. package/test/produced-copy-choice.test.js +0 -213
  217. package/test/published-timeline.test.js +0 -83
  218. package/test/quality-offer.test.js +0 -160
  219. package/test/quality-variants.test.js +0 -1274
  220. package/test/rate-control-in-the-output.test.js +0 -181
  221. package/test/read-survives-withdrawal.test.js +0 -164
  222. package/test/read-window.test.js +0 -365
  223. package/test/reads-not-yet-arrived.test.js +0 -218
  224. package/test/resume-warm.test.js +0 -39
  225. package/test/run-graph-drift.test.js +0 -37
  226. package/test/run-inputs.test.js +0 -80
  227. package/test/run-intervals.test.js +0 -100
  228. package/test/run-position-follows-published-grid.test.js +0 -89
  229. package/test/run-progress.test.js +0 -64
  230. package/test/same-height-switch.test.js +0 -663
  231. package/test/seek-frees-the-viewers-requests.test.js +0 -217
  232. package/test/seek-landing.test.js +0 -109
  233. package/test/seek-target-not-superseded.test.js +0 -195
  234. package/test/segment-closed-on-both-branches.test.js +0 -114
  235. package/test/segment-cut-times.test.js +0 -63
  236. package/test/segment-demand.test.js +0 -80
  237. package/test/segment-hold-supersede.test.js +0 -91
  238. package/test/segment-judged-by-its-own-header.test.js +0 -222
  239. package/test/segment-serve-wiring.test.js +0 -481
  240. package/test/segment-store-eviction.test.js +0 -247
  241. package/test/segment-store.test.js +0 -352
  242. package/test/segment-true-start.test.js +0 -86
  243. package/test/segments-are-shared.test.js +0 -176
  244. package/test/send-chunk-bytes.test.js +0 -86
  245. package/test/serving-existing-output.test.js +0 -111
  246. package/test/serving-output.test.js +0 -102
  247. package/test/shared-piece-store.test.js +0 -310
  248. package/test/sidecar-naming.test.js +0 -142
  249. package/test/soundtrack-rate-bound.test.js +0 -289
  250. package/test/source-file.test.js +0 -133
  251. package/test/source-files-route.test.js +0 -113
  252. package/test/source-registry.test.js +0 -18
  253. package/test/split-a-stretch.test.js +0 -139
  254. package/test/stale-request-after-seek.test.js +0 -158
  255. package/test/startup-readings.test.js +0 -124
  256. package/test/stream-route.test.js +0 -237
  257. package/test/subtitle-cue-framing.test.js +0 -200
  258. package/test/subtitle-cue-source.test.js +0 -102
  259. package/test/subtitle-cue-walk.test.js +0 -385
  260. package/test/subtitle-cursor.test.js +0 -63
  261. package/test/subtitle-defaults.test.js +0 -118
  262. package/test/subtitle-language.test.js +0 -252
  263. package/test/subtitle-metadata.test.js +0 -15
  264. package/test/subtitle-plan-waits.test.js +0 -125
  265. package/test/subtitle-track-numbering.test.js +0 -391
  266. package/test/supply-margin.test.js +0 -190
  267. package/test/swarm-follows-readers.test.js +0 -174
  268. package/test/swarm-reach.test.js +0 -130
  269. package/test/swarm-selection.test.js +0 -220
  270. package/test/tail-duplication.test.js +0 -167
  271. package/test/the-channel-has-both-ends.test.js +0 -91
  272. package/test/the-torrent-thread-serves-bytes.test.js +0 -107
  273. package/test/thread-pool.test.js +0 -53
  274. package/test/timeline.test.js +0 -84
  275. package/test/torrent-contents.test.js +0 -235
  276. package/test/torrent-cost.test.js +0 -71
  277. package/test/torrent-files.test.js +0 -221
  278. package/test/torrent-naming.test.js +0 -255
  279. package/test/tracks-begin-together.test.js +0 -209
  280. package/test/tunnel-renewal.test.js +0 -167
  281. package/test/two-viewers-one-picture.test.js +0 -508
  282. package/test/upload-hurry.test.js +0 -186
  283. package/test/usrsctp-state.test.js +0 -131
  284. package/test/video-facts.test.js +0 -102
  285. package/test/viewer-assignments.test.js +0 -143
  286. package/test/viewer-outputs.test.js +0 -285
  287. package/test/viewer-presence.test.js +0 -116
  288. package/test/viewer-requests-admission.test.js +0 -92
  289. package/test/viewer-starts-on-map.test.js +0 -27
  290. package/test/viewer-states.test.js +0 -192
  291. package/test/viewer-subtitle-subscription.test.js +0 -96
  292. package/test/viewer.test.js +0 -139
  293. package/test/viewing-generations.test.js +0 -390
  294. package/test/visible-rung.test.js +0 -37
  295. package/test/wait-ledger.test.js +0 -100
  296. package/test/webrtc-shim.test.js +0 -149
  297. package/test/wedge-certainty.test.js +0 -131
  298. package/test/whole-file-held-read.test.js +0 -30
  299. package/test/withdraw-piece-claim.test.js +0 -212
  300. package/test/worker-channel.test.js +0 -158
@@ -1,518 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import test from "node:test";
3
- import { predictPlaybackReadiness, RateTrend, forecastRate } from "../services/viewer/playback-readiness.js";
4
-
5
- /** Served ranges in milliseconds, as the format states them for a piece. */
6
- function servedMs(...pairs) {
7
- return { timescale: 1000n, ranges: pairs.map(([start, end]) => ({ start: BigInt(start), end: BigInt(end), frame: 0n })) };
8
- }
9
-
10
- function input(overrides = {}) {
11
- const now = 10_000;
12
- return {
13
- now,
14
- positionSeconds: 0,
15
- durationSeconds: 8,
16
- bufferedAheadSeconds: 0,
17
- bufferLimitSeconds: 8,
18
- reserveSeconds: 4,
19
- lookaheadSeconds: 8,
20
- sources: [{ id: "source", complete: true, bytesPerMediaSecond: 1, readings: [] }],
21
- tracks: [{
22
- id: "video",
23
- sourceIds: ["source"],
24
- processedSeconds: 4,
25
- bitsPerMediaSecond: 80,
26
- readings: [{ at: now - 1_000, value: 2 }, { at: now, value: 2 }],
27
- segments: [
28
- { index: 0, startSeconds: 0, endSeconds: 4 },
29
- { index: 1, startSeconds: 4, endSeconds: 8 }
30
- ],
31
- readySegmentIndices: [0],
32
- segmentSizesBytes: new Map([[0, 40]])
33
- }],
34
- linkReadings: [{ at: now - 1_000, value: 320 }, { at: now, value: 320 }],
35
- ...overrides
36
- };
37
- }
38
-
39
- test("finds the first safe start from segments delivered before playback", () => {
40
- const forecast = predictPlaybackReadiness(input());
41
-
42
- assert.equal(forecast.ready, false);
43
- assert.equal(forecast.reason, "minimum-safe-delay");
44
- assert.ok(Math.abs(forecast.delaySeconds - 1) < 1e-8);
45
- });
46
-
47
- test("startup delay is independent of browser capacity and proxy lookahead", () => {
48
- for (const bufferLimitSeconds of [undefined, null, 0, 1, 120]) {
49
- for (const lookaheadSeconds of [undefined, 0, 4, 120]) {
50
- assert.equal(predictPlaybackReadiness(input({ bufferLimitSeconds, lookaheadSeconds })).delaySeconds, 1);
51
- }
52
- }
53
- });
54
-
55
- test("missing duration or required audio cannot be reported as fully buffered", () => {
56
- assert.equal(predictPlaybackReadiness(input({ durationSeconds: 0 })).reason, "incomplete-state");
57
- assert.equal(predictPlaybackReadiness(input({ requiredAudio: true, bufferedAheadSeconds: 8 })).ready, false);
58
- });
59
-
60
- test("held input does not require a second download or a download rate", () => {
61
- const state = input();
62
- state.sources[0] = { id: "source", complete: false, bytesPerMediaSecond: 1, readings: [],
63
- residence: [{ start: 0, end: 8, location: "disk" }] };
64
- assert.equal(predictPlaybackReadiness(state).delaySeconds, 1);
65
- });
66
-
67
- test("subtracts progress only inside the actual processing run interval", () => {
68
- const state = input({ reserveSeconds: 0 });
69
- state.tracks[0].readySegmentIndices = [];
70
- state.tracks[0].readings = [{ at: state.now, value: 1 }];
71
- state.tracks[0].processingRanges = [{ start: 0, end: 3 }];
72
- assert.equal(predictPlaybackReadiness(state).delaySeconds, 2);
73
- state.tracks[0].processingRanges = [{ start: 40, end: 50 }];
74
- assert.equal(predictPlaybackReadiness(state).delaySeconds, 5);
75
- });
76
-
77
- test("future cuts follow measured track time without inventing a permanent clock gap", () => {
78
- const state = input({ bufferedAheadSeconds: 3.9, reserveSeconds: 0 });
79
- state.tracks[0].clientRanges = [{ start: 0, end: 3.9 }];
80
- state.tracks[0].segments[0].mediaRanges = servedMs([0, 3900]);
81
- const forecast = predictPlaybackReadiness(state);
82
- assert.notEqual(forecast.reason, "no-safe-start-found");
83
- state.tracks[0].segments[1].mediaRanges = servedMs([4000, 8000]);
84
- state.tracks[0].readySegmentIndices = [0, 1];
85
- assert.equal(predictPlaybackReadiness(state).reason, "media-continuity-unavailable");
86
- });
87
-
88
- test("a held range is compared with served ticks exactly, not through rounded seconds", () => {
89
- // 1418768 ticks of 16000 is 88.673 s; as a double the page reports the end
90
- // of the same media as 88.67299999999999. Measured 2026-10-02: the float
91
- // comparison called this join a hole.
92
- const state = input({ durationSeconds: 92.673, reserveSeconds: 0 });
93
- const piece = (start, end) => ({ timescale: 16000n, ranges: [{ start, end, frame: 672n }] });
94
- state.tracks[0].clientRanges = [{ start: 0, end: 88.67299999999999 }];
95
- state.tracks[0].segments = [
96
- { index: 0, startSeconds: 0, endSeconds: 88.673, mediaRanges: piece(0n, 1418768n) },
97
- { index: 1, startSeconds: 88.673, endSeconds: 92.673, mediaRanges: piece(1418768n, 1482768n) }
98
- ];
99
- state.tracks[0].readySegmentIndices = [0, 1];
100
- state.tracks[0].segmentSizesBytes = new Map([[0, 40], [1, 40]]);
101
- const forecast = predictPlaybackReadiness(state);
102
- assert.notEqual(forecast.reason, "media-continuity-unavailable");
103
- assert.equal(forecast.preparedSegments, 2);
104
- });
105
-
106
- test("a browser range within Chromium's microsecond truncation still holds the piece", () => {
107
- const state = input({ reserveSeconds: 0 });
108
- state.tracks[0].segments[0].mediaRanges = servedMs([0, 4000]);
109
- state.tracks[0].clientRanges = [{ start: 0, end: 4 - 2e-6 }];
110
- state.tracks[0].readySegmentIndices = [0, 1];
111
- state.tracks[0].segmentSizesBytes = new Map([[0, 40], [1, 40]]);
112
- const held = predictPlaybackReadiness(state);
113
- state.tracks[0].clientRanges = [];
114
- const absent = predictPlaybackReadiness(state);
115
- assert.ok(held.delaySeconds < absent.delaySeconds);
116
- });
117
-
118
- test("keeps source-stall reserve in the proxy's prepared timeline, not the capped browser buffer", () => {
119
- const now = 10_000;
120
- const forecast = predictPlaybackReadiness({
121
- now,
122
- positionSeconds: 0,
123
- durationSeconds: 12,
124
- bufferedAheadSeconds: 2,
125
- bufferLimitSeconds: 4,
126
- reserveSeconds: 6,
127
- lookaheadSeconds: 12,
128
- sources: [{
129
- id: "source",
130
- complete: false,
131
- bytesPerMediaSecond: 1,
132
- readings: [{ at: now - 1_000, value: 2 }, { at: now, value: 2 }]
133
- }],
134
- tracks: [{
135
- id: "video",
136
- sourceIds: ["source"],
137
- processedSeconds: 8,
138
- bitsPerMediaSecond: 80,
139
- readings: [{ at: now - 1_000, value: 2 }, { at: now, value: 2 }],
140
- segments: [
141
- { index: 0, startSeconds: 0, endSeconds: 2 },
142
- { index: 1, startSeconds: 2, endSeconds: 4 },
143
- { index: 2, startSeconds: 4, endSeconds: 6 },
144
- { index: 3, startSeconds: 6, endSeconds: 8 },
145
- { index: 4, startSeconds: 8, endSeconds: 10 },
146
- { index: 5, startSeconds: 10, endSeconds: 12 }
147
- ],
148
- readySegmentIndices: [0, 1, 2, 3],
149
- segmentSizesBytes: new Map([[0, 20], [1, 20], [2, 20], [3, 20], [4, 20], [5, 20]])
150
- }],
151
- linkReadings: [{ at: now - 1_000, value: 80_000 }, { at: now, value: 80_000 }]
152
- });
153
-
154
- assert.equal(forecast.ready, true);
155
- assert.equal(forecast.reason, "trajectory-safe-now");
156
- assert.equal(forecast.reserveSeconds, 6);
157
- assert.equal(forecast.bufferedSeconds, 2);
158
- assert.equal(forecast.preparedSegments, 4);
159
- });
160
-
161
- test("overlaps source reads and encoding, then waits for the slower stage and delivery", () => {
162
- const source = {
163
- id: "source",
164
- complete: false,
165
- bytesPerMediaSecond: 1,
166
- readings: [{ at: 9_000, value: 2 }, { at: 10_000, value: 2 }]
167
- };
168
- const track = {
169
- ...input().tracks[0],
170
- readings: [{ at: 9_000, value: 10 }, { at: 10_000, value: 10 }]
171
- };
172
- const forecast = predictPlaybackReadiness(input({ sources: [source], tracks: [track] }));
173
-
174
- assert.equal(forecast.ready, false);
175
- assert.ok(Math.abs(forecast.delaySeconds - 2) < 1e-8);
176
- });
177
-
178
- test("does not produce an ETA without a client-link measurement", () => {
179
- const forecast = predictPlaybackReadiness(input({ linkReadings: [] }));
180
-
181
- assert.equal(forecast.ready, false);
182
- assert.equal(forecast.delaySeconds, null);
183
- assert.equal(forecast.reason, "link-rate-unavailable");
184
- });
185
-
186
- test("rate forecast does not extrapolate acceleration beyond observed service", () => {
187
- const trend = new RateTrend();
188
- trend.add(9_000, 2);
189
- trend.add(10_000, 4);
190
-
191
- const predicted = forecastRate(trend.snapshot(), 10_000, 2);
192
- assert.equal(predicted, 4);
193
- assert.equal(forecastRate(trend.snapshot(), 100_000, 2), 4);
194
- });
195
-
196
- test("uses each track's own segment boundaries and requires continuous coverage on both tracks", () => {
197
- const common = input();
198
- const video = common.tracks[0];
199
- const audio = {
200
- id: "audio",
201
- sourceIds: ["source"],
202
- processedSeconds: 2,
203
- bitsPerMediaSecond: 80,
204
- readings: [{ at: 9_000, value: 2 }, { at: 10_000, value: 2 }],
205
- segments: [
206
- { index: 7, startSeconds: 0, endSeconds: 2 },
207
- { index: 8, startSeconds: 2, endSeconds: 4 },
208
- { index: 9, startSeconds: 4, endSeconds: 8 }
209
- ],
210
- readySegmentIndices: [7],
211
- segmentSizesBytes: new Map([[7, 20], [8, 20], [9, 40]])
212
- };
213
- video.segmentSizesBytes.set(1, 40);
214
- const forecast = predictPlaybackReadiness({
215
- ...common,
216
- requiredAudio: true,
217
- tracks: [video, audio]
218
- });
219
-
220
- assert.equal(forecast.ready, false);
221
- assert.equal(forecast.reason, "minimum-safe-delay");
222
- assert.ok(forecast.delaySeconds > 0);
223
- assert.equal(forecast.preparedSegments, 2);
224
- });
225
-
226
- test("does not start when only one required track has continuous coverage", () => {
227
- const common = input();
228
- const audio = {
229
- id: "audio",
230
- sourceIds: ["source"],
231
- processedSeconds: 0,
232
- bitsPerMediaSecond: 80,
233
- readings: [{ at: 9_000, value: 2 }, { at: 10_000, value: 2 }],
234
- segments: [
235
- { index: 4, startSeconds: 0, endSeconds: 2 },
236
- { index: 5, startSeconds: 3, endSeconds: 8 }
237
- ],
238
- readySegmentIndices: [4, 5],
239
- segmentSizesBytes: new Map([[4, 20], [5, 50]])
240
- };
241
- const forecast = predictPlaybackReadiness({
242
- ...common,
243
- requiredAudio: true,
244
- tracks: [common.tracks[0], audio]
245
- });
246
-
247
- assert.equal(forecast.ready, false);
248
- assert.equal(forecast.delaySeconds, null);
249
- assert.equal(forecast.reason, "media-continuity-unavailable");
250
- });
251
-
252
- test("downloads an overlapping source interval once when two tracks use the same file", () => {
253
- const now = 10_000;
254
- const readings = [{ at: now - 1_000, value: 10 }, { at: now, value: 10 }];
255
- const tracks = [
256
- {
257
- id: "video",
258
- sourceIds: ["shared"],
259
- processedSeconds: 2,
260
- bitsPerMediaSecond: 80,
261
- readings,
262
- segments: [
263
- { index: 0, startSeconds: 0, endSeconds: 2 },
264
- { index: 1, startSeconds: 2, endSeconds: 4 }
265
- ],
266
- readySegmentIndices: [0],
267
- segmentSizesBytes: new Map([[0, 20], [1, 20]])
268
- },
269
- {
270
- id: "audio",
271
- sourceIds: ["shared"],
272
- processedSeconds: 0,
273
- bitsPerMediaSecond: 80,
274
- readings,
275
- segments: [{ index: 11, startSeconds: 0, endSeconds: 4 }],
276
- readySegmentIndices: [],
277
- segmentSizesBytes: new Map([[11, 40]])
278
- }
279
- ];
280
- const forecast = predictPlaybackReadiness({
281
- now,
282
- positionSeconds: 0,
283
- durationSeconds: 4,
284
- bufferedAheadSeconds: 0,
285
- bufferLimitSeconds: 4,
286
- reserveSeconds: 4,
287
- lookaheadSeconds: 4,
288
- requiredAudio: true,
289
- sources: [{
290
- id: "shared",
291
- complete: false,
292
- bytesPerMediaSecond: 1,
293
- readings: [{ at: now - 1_000, value: 2 }, { at: now, value: 2 }]
294
- }],
295
- tracks,
296
- linkReadings: [{ at: now - 1_000, value: 10_000 }, { at: now, value: 10_000 }]
297
- });
298
-
299
- assert.equal(forecast.ready, false);
300
- assert.equal(forecast.reason, "minimum-safe-delay");
301
- assert.ok(Math.abs(forecast.delaySeconds - 2.032) < 1e-8);
302
- });
303
-
304
- test("downloads sources on separate torrents concurrently", () => {
305
- const now = 10_000;
306
- const readings = [{ at: now - 1_000, value: 10 }, { at: now, value: 10 }];
307
- const tracks = [
308
- {
309
- id: "video",
310
- sourceIds: ["video-source"],
311
- processedSeconds: 0,
312
- bitsPerMediaSecond: 80,
313
- readings,
314
- segments: [{ index: 0, startSeconds: 0, endSeconds: 4 }],
315
- readySegmentIndices: [],
316
- segmentSizesBytes: new Map([[0, 40]])
317
- },
318
- {
319
- id: "audio",
320
- sourceIds: ["audio-source"],
321
- processedSeconds: 0,
322
- bitsPerMediaSecond: 80,
323
- readings,
324
- segments: [{ index: 7, startSeconds: 0, endSeconds: 4 }],
325
- readySegmentIndices: [],
326
- segmentSizesBytes: new Map([[7, 40]])
327
- }
328
- ];
329
- const sourceReadings = [{ at: now - 1_000, value: 2 }, { at: now, value: 2 }];
330
- const forecast = predictPlaybackReadiness({
331
- now,
332
- positionSeconds: 0,
333
- durationSeconds: 4,
334
- bufferedAheadSeconds: 0,
335
- bufferLimitSeconds: 4,
336
- reserveSeconds: 4,
337
- lookaheadSeconds: 4,
338
- requiredAudio: true,
339
- sources: [
340
- { id: "video-source", complete: false, bytesPerMediaSecond: 1, readings: sourceReadings },
341
- { id: "audio-source", complete: false, bytesPerMediaSecond: 1, readings: sourceReadings }
342
- ],
343
- tracks,
344
- linkReadings: [{ at: now - 1_000, value: 10_000 }, { at: now, value: 10_000 }]
345
- });
346
-
347
- assert.equal(forecast.ready, false);
348
- assert.equal(forecast.reason, "minimum-safe-delay");
349
- assert.ok(Math.abs(forecast.delaySeconds - 2.064) < 1e-8);
350
- });
351
-
352
- test("shares one torrent download rate across distinct video and audio files", () => {
353
- const now = 10_000;
354
- const readings = [{ at: now - 1_000, value: 10 }, { at: now, value: 10 }];
355
- const sourceReadings = [{ at: now - 1_000, value: 2 }, { at: now, value: 2 }];
356
- const tracks = [
357
- {
358
- id: "video",
359
- sourceIds: ["torrent:video-file"],
360
- processedSeconds: 0,
361
- bitsPerMediaSecond: 80,
362
- readings,
363
- segments: [{ index: 0, startSeconds: 0, endSeconds: 4 }],
364
- readySegmentIndices: [],
365
- segmentSizesBytes: new Map([[0, 40]])
366
- },
367
- {
368
- id: "audio",
369
- sourceIds: ["torrent:audio-file"],
370
- processedSeconds: 0,
371
- bitsPerMediaSecond: 80,
372
- readings,
373
- segments: [{ index: 7, startSeconds: 0, endSeconds: 4 }],
374
- readySegmentIndices: [],
375
- segmentSizesBytes: new Map([[7, 40]])
376
- }
377
- ];
378
- const forecast = predictPlaybackReadiness({
379
- now,
380
- positionSeconds: 0,
381
- durationSeconds: 4,
382
- bufferedAheadSeconds: 0,
383
- bufferLimitSeconds: 4,
384
- reserveSeconds: 4,
385
- lookaheadSeconds: 4,
386
- requiredAudio: true,
387
- sources: [
388
- {
389
- id: "torrent:video-file",
390
- serviceId: "torrent",
391
- complete: false,
392
- bytesPerMediaSecond: 1,
393
- readings: sourceReadings
394
- },
395
- {
396
- id: "torrent:audio-file",
397
- serviceId: "torrent",
398
- complete: false,
399
- bytesPerMediaSecond: 1,
400
- readings: sourceReadings
401
- }
402
- ],
403
- tracks,
404
- linkReadings: [{ at: now - 1_000, value: 10_000 }, { at: now, value: 10_000 }]
405
- });
406
-
407
- assert.equal(forecast.ready, false);
408
- assert.equal(forecast.reason, "minimum-safe-delay");
409
- assert.ok(Math.abs(forecast.delaySeconds - 4.032) < 1e-8);
410
- });
411
-
412
- test("does not invent future encode acceleration to start an unsustainable output", () => {
413
- const now = 1_000;
414
- const forecast = predictPlaybackReadiness({
415
- now,
416
- positionSeconds: 0,
417
- durationSeconds: 8,
418
- bufferedAheadSeconds: 4,
419
- bufferLimitSeconds: 8,
420
- reserveSeconds: 1,
421
- lookaheadSeconds: 4,
422
- sources: [{ id: "source", complete: true, bytesPerMediaSecond: 1, readings: [] }],
423
- tracks: [{
424
- id: "video",
425
- sourceIds: ["source"],
426
- processedSeconds: 4,
427
- bitsPerMediaSecond: 2,
428
- readings: [{ at: 0, value: 0.1 }, { at: now, value: 0.15 }],
429
- segments: [
430
- { index: 0, startSeconds: 0, endSeconds: 4 },
431
- { index: 1, startSeconds: 4, endSeconds: 6 },
432
- { index: 2, startSeconds: 6, endSeconds: 8 }
433
- ],
434
- readySegmentIndices: [0],
435
- segmentSizesBytes: new Map([[0, 1], [1, 1], [2, 1]])
436
- }],
437
- linkReadings: [{ at: 0, value: 105.3 }, { at: now, value: 100 }]
438
- });
439
-
440
- assert.equal(forecast.ready, false);
441
- assert.equal(forecast.reason, "minimum-safe-delay");
442
- assert.ok(Math.abs(forecast.delaySeconds - (4 / 0.15 + 8 / 100 - 6)) < 1e-8);
443
- });
444
-
445
- test("a faster old probe cannot turn positive delivery into a permanent zero rate", () => {
446
- const trend = new RateTrend(120);
447
- trend.add(0, 112_400_000);
448
- trend.add(66_757, 37_640_000);
449
- assert.equal(forecastRate(trend.snapshot(), 20 * 60_000, 1443.97), 37_640_000);
450
- assert.equal(trend.add(66_757, 37_640_000), false);
451
- });
452
-
453
- test("the measurement window retains one boundary sample and discards older history", () => {
454
- const trend = new RateTrend(2);
455
- trend.add(0, 100);
456
- trend.add(1_000, 40);
457
- trend.add(4_000, 20);
458
- assert.equal(trend.snapshot().count, 2);
459
- assert.equal(trend.snapshot().meanValue, 30);
460
- });
461
-
462
- function preparedEpisode(bufferLimitSeconds, linkReadings) {
463
- const durationSeconds = 1443.97;
464
- const boundaries = [0, 10.01, 18.977, 29.488];
465
- for (let index = 4; index <= 197; index += 1) {
466
- boundaries.push(29.488 + (durationSeconds - 29.488) * (index - 3) / 194);
467
- }
468
- const segments = boundaries.slice(0, -1).map((startSeconds, index) => ({
469
- index, startSeconds, endSeconds: boundaries[index + 1]
470
- }));
471
- const tracks = [9_257_176.8, 189_000].map((bitsPerMediaSecond, index) => ({
472
- id: String(index),
473
- sourceIds: ["source"],
474
- processedSeconds: 1437.269,
475
- bitsPerMediaSecond,
476
- readings: [],
477
- segments,
478
- readySegmentIndices: segments.map(({ index }) => index),
479
- segmentSizesBytes: new Map(segments.map((segment) => [segment.index,
480
- (segment.endSeconds - segment.startSeconds) * bitsPerMediaSecond / 8]))
481
- }));
482
- return {
483
- now: 20 * 60_000,
484
- positionSeconds: 0,
485
- durationSeconds,
486
- bufferedAheadSeconds: 19.187,
487
- bufferLimitSeconds,
488
- reserveSeconds: 5.674,
489
- lookaheadSeconds: 120,
490
- requiredAudio: true,
491
- sources: [{ id: "source", complete: false, bytesPerMediaSecond: 1, readings: [{ at: 0, value: 0 }] }],
492
- tracks,
493
- linkReadings
494
- };
495
- }
496
-
497
- test("starts a prepared episode despite an old declining link measurement", () => {
498
- const forecast = predictPlaybackReadiness(preparedEpisode(120, [
499
- { at: 0, value: 112_400_000 }, { at: 66_757, value: 37_640_000 }
500
- ]));
501
- assert.equal(forecast.preparedSegments, 394);
502
- assert.equal(forecast.ready, true);
503
- assert.equal(forecast.reason, "trajectory-safe-now");
504
- });
505
-
506
- test("the measured buffer proves capacity even when the loader reports a smaller target", () => {
507
- const forecast = predictPlaybackReadiness(preparedEpisode(9.092, [{ at: 0, value: 37_640_000 }]));
508
- assert.equal(forecast.ready, true);
509
- });
510
-
511
- test("a measured source stop is not replaced by hypothetical future recovery", () => {
512
- const forecast = predictPlaybackReadiness(input({ sources: [{
513
- id: "source", complete: false, bytesPerMediaSecond: 1,
514
- readings: [{ at: 9_000, value: 10 }, { at: 10_000, value: 0 }]
515
- }] }));
516
- assert.equal(forecast.ready, false);
517
- assert.equal(forecast.delaySeconds, null);
518
- });
@@ -1,101 +0,0 @@
1
- import test from "node:test";
2
- import assert from "node:assert/strict";
3
- import { EventEmitter } from "node:events";
4
- import { EncodeRun } from "../services/encode/EncodeRun.js";
5
- import { SoftwareEncoder } from "../services/encode/SoftwareEncoder.js";
6
- import { predictPlaybackReadiness } from "../services/viewer/playback-readiness.js";
7
-
8
- function encode() {
9
- const child = new EventEmitter();
10
- child.stdio = [null, new EventEmitter(), new EventEmitter(), new EventEmitter()];
11
- child.stderr = child.stdio[2];
12
- child.kill = () => {};
13
- const published = [];
14
- const ended = [];
15
- const run = new EncodeRun({
16
- address: "fake", from: 0, to: 1, encoder: new SoftwareEncoder(),
17
- buildArgs: () => [], spawn: () => child, logger: { info() {}, warn() {} },
18
- indexOfName: (name) => Number(name),
19
- onClosed: (name) => { published.push(name); return name; },
20
- onEnded: (value) => ended.push(value)
21
- });
22
- return { child, run, published, ended };
23
- }
24
-
25
- test("a clean short input never publishes its flushed tail", () => {
26
- const { child, published, ended } = encode();
27
- child.stderr.emit("data", "Stream ends prematurely at 1048576");
28
- child.stdio[3].emit("data", "0\n");
29
- child.emit("close", 0, null);
30
- assert.deepEqual(published, []);
31
- assert.equal(ended[0].ending, "short");
32
- assert.equal(ended[0].reached, -1);
33
- });
34
-
35
- test("completed cuts survive a failure but its last flushed file does not", () => {
36
- const { child, published } = encode();
37
- child.stdio[3].emit("data", "0\n");
38
- child.stderr.emit("data", "Stream ends prematurely at 1048576");
39
- child.stdio[3].emit("data", "1\n");
40
- child.emit("close", 0, null);
41
- assert.deepEqual(published, ["0"]);
42
- });
43
-
44
- test("normal completion drains the filename pipe before publishing the tail", () => {
45
- const { child, published, ended } = encode();
46
- child.emit("exit", 0, null);
47
- assert.deepEqual(ended, []);
48
- child.stdio[3].emit("data", "0\n1\n");
49
- child.emit("close", 0, null);
50
- assert.deepEqual(published, ["0", "1"]);
51
- assert.equal(ended[0].ending, "complete");
52
- });
53
-
54
- test("stopping retains an earlier closed cut and rejects the signal-flushed tail", () => {
55
- const { child, run, published } = encode();
56
- child.stdio[3].emit("data", "0\n");
57
- run.stop("a different range is needed");
58
- child.stdio[3].emit("data", "1\n");
59
- child.emit("close", null, "SIGTERM");
60
- assert.deepEqual(published, ["0"]);
61
- });
62
-
63
- function state(clientRanges) {
64
- return {
65
- now: 10_000, positionSeconds: 0, durationSeconds: 8,
66
- bufferedAheadSeconds: 8, bufferLimitSeconds: 8, reserveSeconds: 0, lookaheadSeconds: 8,
67
- sources: [{ id: "source", complete: true }],
68
- linkReadings: [{ at: 10_000, value: 80 }],
69
- tracks: [{ id: "video", sourceIds: ["source"], processedSeconds: 8,
70
- bitsPerMediaSecond: 80, readings: [{ at: 10_000, value: 2 }],
71
- readySegmentIndices: [0, 1], segmentSizesBytes: new Map([[0, 40], [1, 40]]),
72
- clientRanges, segments: [{ index: 0, startSeconds: 0, endSeconds: 4 },
73
- { index: 1, startSeconds: 4, endSeconds: 8 }] }]
74
- };
75
- }
76
-
77
- test("future browser ranges are retained rather than transferred again", () => {
78
- const forecast = predictPlaybackReadiness(state([{ start: 4, end: 8 }]));
79
- assert.equal(forecast.ready, false);
80
- assert.equal(forecast.reason, "minimum-safe-delay");
81
- assert.ok(Math.abs(forecast.delaySeconds - 4) < 1e-8);
82
- });
83
-
84
- test("a real media hole is not hidden by the scalar buffer or ready file count", () => {
85
- const input = state([{ start: 0, end: 3 }, { start: 4, end: 8 }]);
86
- // A ready piece always has its coverage read; the hole is between them.
87
- input.tracks[0].segments[0].mediaRanges = { timescale: 1000n, ranges: [{ start: 0n, end: 3000n, frame: 0n }] };
88
- input.tracks[0].segments[1].mediaRanges = { timescale: 1000n, ranges: [{ start: 4000n, end: 8000n, frame: 0n }] };
89
- const forecast = predictPlaybackReadiness(input);
90
- assert.equal(forecast.ready, false);
91
- assert.equal(forecast.bufferedSeconds, 3);
92
- assert.equal(forecast.reason, "media-continuity-unavailable");
93
- });
94
-
95
- test("missing work before global progress can still be produced", () => {
96
- const input = state([]);
97
- input.tracks[0].readySegmentIndices = [1];
98
- const forecast = predictPlaybackReadiness(input);
99
- assert.equal(forecast.reason, "minimum-safe-delay");
100
- assert.ok(Number.isFinite(forecast.delaySeconds));
101
- });