@torrent-tv/proxy 2.86.0 → 2.87.1

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 (385) hide show
  1. package/CHANGELOG.md +2142 -1915
  2. package/CLAUDE.md +322 -197
  3. package/assets/calibration/NOTICE.md +2 -2
  4. package/bin/cli.js +684 -616
  5. package/biome.json +409 -233
  6. package/docs/browser-proxy-contract.md +94 -0
  7. package/docs/container-architecture.md +218 -213
  8. package/docs/download-architecture.md +7 -7
  9. package/docs/encode-architecture.md +287 -11
  10. package/docs/encode-run-state.md +2 -2
  11. package/docs/logs.md +97 -97
  12. package/docs/{disk-architecture.md → storage-architecture.md} +107 -1
  13. package/openspec/changes/adaptive-bitrate/design.md +2 -2
  14. package/openspec/changes/adaptive-bitrate/proposal.md +1 -1
  15. package/openspec/changes/adaptive-bitrate/tasks.md +1 -1
  16. package/openspec/changes/chunked-request-bodies/design.md +1 -1
  17. package/openspec/changes/chunked-request-bodies/proposal.md +1 -1
  18. package/openspec/changes/chunked-request-bodies/tasks.md +1 -1
  19. package/openspec/changes/cold-start/design.md +1 -1
  20. package/openspec/changes/disk-cap/proposal.md +1 -1
  21. package/openspec/changes/proxy-observability/proposal.md +2 -2
  22. package/openspec/changes/track-selection/proposal.md +1 -1
  23. package/openspec/changes/transcode-quality/proposal.md +1 -1
  24. package/package.json +2 -2
  25. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  26. package/research/double-spawn-2026-09-10.md +171 -171
  27. package/routes/api/playback-plan/post.js +2 -2
  28. package/routes/api/sources/files/get.js +1 -1
  29. package/routes/api/sources/stats/get.js +1 -1
  30. package/routes/api/sources/warm/post.js +10 -3
  31. package/routes/api/subtitles/get.js +230 -230
  32. package/routes/api/transcode-sessions/fragment-far/post.js +3 -3
  33. package/routes/api/transcode-sessions/net-report/post.js +129 -94
  34. package/routes/api/transcode-sessions/post.js +160 -139
  35. package/routes/api/transcode-sessions/progress/get.js +3 -3
  36. package/routes/api/transcode-sessions/release/post.js +3 -3
  37. package/routes/api/transcode-sessions/seek/post.js +12 -3
  38. package/routes/stream/get.js +2 -2
  39. package/routes/transcode/audio-file/get.js +15 -6
  40. package/routes/transcode/audio-warm/get.js +9 -6
  41. package/routes/transcode/session-file/get.js +52 -268
  42. package/routes/transcode/variant-file/get.js +46 -7
  43. package/routes/transcode/variant-warm/get.js +19 -6
  44. package/scripts/render-run-graph.js +1 -1
  45. package/server.js +720 -465
  46. package/services/encode/CoverageMap.js +428 -428
  47. package/services/encode/CushionReport.js +345 -0
  48. package/services/encode/EncodeAdmission.js +332 -0
  49. package/services/{orchestrators → encode}/EncodeOrchestrator.js +307 -29
  50. package/services/encode/EncodeRun.js +16 -9
  51. package/services/encode/EncodeRuns.js +1229 -0
  52. package/services/encode/Encoder.js +18 -2
  53. package/services/encode/EncoderSelection.js +38 -0
  54. package/services/encode/LocalObservations.js +359 -0
  55. package/services/encode/NvencEncoder.js +5 -0
  56. package/services/encode/OutputOpening.js +755 -0
  57. package/services/encode/OutputTimes.js +579 -0
  58. package/services/encode/Renditions.js +2162 -0
  59. package/services/encode/RunProgress.js +84 -0
  60. package/services/encode/SoftwareEncoder.js +12 -7
  61. package/services/encode/args.js +402 -186
  62. package/services/encode/calibration.js +361 -0
  63. package/services/encode/contention.js +128 -128
  64. package/services/encode/encode-run-state.js +16 -20
  65. package/services/encode/fingerprint.js +322 -0
  66. package/services/{hwaccel.js → encode/hwaccel.js} +82 -42
  67. package/services/{learned-median.js → encode/learned-median.js} +11 -0
  68. package/services/encode/output/EncodedOutput.js +57 -0
  69. package/services/encode/output/Output.js +131 -0
  70. package/services/{output/LiveOutputs.js → encode/output/OutputCatalog.js} +141 -40
  71. package/services/encode/output/OutputSpec.js +390 -0
  72. package/services/{output → encode/output}/Timeline.js +12 -37
  73. package/services/encode/output/index.js +20 -0
  74. package/services/encode/output/ladder.js +32 -0
  75. package/services/{output → encode/output}/playlists.js +3 -0
  76. package/services/encode/output-key-format.js +21 -0
  77. package/services/encode/quality/EncodeCost.js +1083 -0
  78. package/services/encode/quality/HostLoad.js +525 -0
  79. package/services/encode/quality/HostTimings.js +249 -0
  80. package/services/encode/quality/OutputQualityState.js +28 -0
  81. package/services/encode/quality/QualityController.js +935 -0
  82. package/services/encode/quality/QualityOffer.js +416 -0
  83. package/services/encode/quality/drain-threat.js +47 -0
  84. package/services/encode/quality/link-budget.js +254 -0
  85. package/services/encode/quality/output-format.js +266 -0
  86. package/services/encode/quality/serving-output.js +216 -0
  87. package/services/encode/quality/visible-rung.js +57 -0
  88. package/services/encode/run-command.js +23 -20
  89. package/services/encode/run-inputs.js +42 -0
  90. package/services/{segment-formats → encode/segment-formats}/fmp4.js +63 -0
  91. package/services/{segment-formats → encode/segment-formats}/index.js +3 -3
  92. package/services/encode/segment-formats/init-compat.js +324 -0
  93. package/services/{segment-formats → encode/segment-formats}/mp4-boxes.js +8 -3
  94. package/services/encode/throughput.js +97 -0
  95. package/services/{orchestrators → media}/ContainerOrchestrator.js +45 -7
  96. package/services/media/KeyframeTables.js +316 -0
  97. package/services/{source → media}/SourceFile.js +7 -55
  98. package/services/{torrent-worker/subtitle-cues.js → media/SubtitleCues.js} +103 -132
  99. package/services/media/SubtitleOrchestrator.js +167 -0
  100. package/services/{audio-inventory.js → media/audio-inventory.js} +7 -1
  101. package/services/{container → media/container}/AviContainer.js +266 -266
  102. package/services/{container → media/container}/Container.js +596 -601
  103. package/services/media/container/ContainerFactory.js +56 -0
  104. package/services/media/container/KeyframeTable.js +272 -0
  105. package/services/{container → media/container}/MatroskaContainer.js +1485 -1485
  106. package/services/{container → media/container}/Mp4Container.js +1244 -1244
  107. package/services/{container → media/container}/SubtitleFileContainer.js +322 -322
  108. package/services/media/keyframe-probe.js +157 -0
  109. package/services/media/media-info-probe.js +93 -0
  110. package/services/{playback-planner.js → media/playback-planner.js} +82 -39
  111. package/services/{tracks → media/tracks}/AudioTrack.js +131 -131
  112. package/services/{tracks → media/tracks}/language-detect.js +174 -174
  113. package/services/server/OutputLifecycle.js +418 -0
  114. package/services/server/SegmentServing.js +1146 -0
  115. package/services/server/ViewerRequests.js +522 -0
  116. package/services/server/controllers/PlaybackController.js +36 -0
  117. package/services/{controllers → server/controllers}/SubtitleController.js +17 -33
  118. package/services/server/http-responses.js +60 -0
  119. package/services/server/transcode-session-files.js +325 -0
  120. package/services/server/wire-outputs.js +983 -0
  121. package/services/storage/Diagnostics.js +202 -0
  122. package/services/storage/MachineBudget.js +235 -0
  123. package/services/{piece-store → storage}/allowance.js +107 -107
  124. package/services/{core-dumps.js → storage/core-dumps.js} +1 -1
  125. package/services/{files → storage/files}/CompletedFiles.js +116 -1
  126. package/services/{disk → storage}/free.js +33 -0
  127. package/services/storage/machine-memory.js +56 -0
  128. package/services/{memory-report.js → storage/memory-report.js} +575 -596
  129. package/services/{piece-store → storage/piece-store}/piece-disk-store.js +0 -7
  130. package/services/{piece-store → storage/piece-store}/shared-piece-store.js +28 -21
  131. package/services/{encode → storage/segment-store}/SegmentStore.js +395 -8
  132. package/services/storage/wire.js +175 -0
  133. package/services/{download → torrent/download}/SwarmSelection.js +2 -2
  134. package/services/{download → torrent/download}/registry.js +2 -2
  135. package/services/torrent/files.js +1 -1
  136. package/services/torrent/piece-store-of.js +29 -0
  137. package/services/{torrent-pool.js → torrent/torrent-pool.js} +26 -119
  138. package/services/{torrent-worker → torrent/worker}/background-fill.js +2 -2
  139. package/services/{torrent-worker → torrent/worker}/channel.js +271 -250
  140. package/services/{torrent-worker → torrent/worker}/client.js +77 -58
  141. package/services/torrent/worker/held-bytes.js +155 -0
  142. package/services/{torrent-worker → torrent/worker}/install-webrtc-shim.js +1 -1
  143. package/services/{torrent-worker → torrent/worker}/piece-reader.js +21 -30
  144. package/services/{torrent-worker → torrent/worker}/pool-adapter.js +101 -137
  145. package/services/{torrent-worker → torrent/worker}/protocol.js +26 -30
  146. package/services/torrent/worker/resume-warm.js +107 -0
  147. package/services/{torrent-worker → torrent/worker}/webrtc-shim.js +2 -2
  148. package/services/{torrent-worker → torrent/worker}/worker.js +68 -159
  149. package/services/{data-channel-handler.js → transport/data-channel-handler.js} +1356 -1356
  150. package/services/{delivery-probe.js → transport/delivery-probe.js} +842 -842
  151. package/services/{health-collector.js → transport/health-collector.js} +6 -35
  152. package/services/{packet-witness.js → transport/packet-witness.js} +808 -784
  153. package/services/{port-mapper.js → transport/port-mapper.js} +1 -1
  154. package/services/{usrsctp-state.js → transport/usrsctp-state.js} +1 -1
  155. package/services/{webrtc-manager.js → transport/webrtc-manager.js} +1 -1
  156. package/services/viewer/Assignments.js +340 -0
  157. package/services/{priority → viewer}/PriorityMap.js +390 -390
  158. package/services/{priority → viewer}/PriorityOrchestrator.js +325 -320
  159. package/services/viewer/Viewer.js +657 -483
  160. package/services/viewer/Viewers.js +368 -257
  161. package/services/viewer/active-output.js +37 -54
  162. package/services/viewer/audio-start.js +4 -3
  163. package/services/viewer/buffer-trend.js +81 -0
  164. package/services/viewer/choices.js +647 -0
  165. package/services/viewer/positions.js +126 -120
  166. package/services/viewer/report-intake.js +46 -45
  167. package/test/asset-paths.test.js +28 -0
  168. package/test/audio-inventory.test.js +3 -3
  169. package/test/audio-track-bitrate.test.js +40 -0
  170. package/test/auto-quality-step.test.js +707 -515
  171. package/test/available-share.test.js +1 -1
  172. package/test/background-fill.test.js +1 -1
  173. package/test/client-log-file.test.js +110 -110
  174. package/test/completed-files.test.js +1 -1
  175. package/test/concurrent-cost.test.js +13 -3
  176. package/test/container-media-info.test.js +3 -3
  177. package/test/core-dumps.test.js +1 -1
  178. package/test/coverage-follows-the-disk.test.js +46 -9
  179. package/test/cue-push-carries-what-the-walk-found.test.js +58 -0
  180. package/test/cut-times-timeline.test.js +2 -2
  181. package/test/cuts-follow-published-grid.test.js +9 -8
  182. package/test/data-channel-frame.test.js +1 -1
  183. package/test/dead-torrent-handle.test.js +1 -1
  184. package/test/declared-rates.test.js +2 -2
  185. package/test/declared-tracks.test.js +2 -2
  186. package/test/decode-cost-fit.test.js +1 -1
  187. package/test/decode-cost.test.js +34 -25
  188. package/test/delivery-probe.test.js +1 -1
  189. package/test/demand-register.test.js +4 -4
  190. package/test/dht-bootstrap.test.js +1 -1
  191. package/test/drain-threat.test.js +65 -0
  192. package/test/duplicate-add.test.js +1 -1
  193. package/test/encode-admission.test.js +146 -0
  194. package/test/encode-cost.test.js +199 -10
  195. package/test/encode-orchestrator.test.js +39 -1
  196. package/test/encode-plan-viewers.test.js +2 -2
  197. package/test/encode-run.test.js +2 -2
  198. package/test/encode-slope.test.js +1 -1
  199. package/test/encoded-output.test.js +39 -0
  200. package/test/encoder-readings.test.js +1 -1
  201. package/test/every-claimant-is-told-its-share.test.js +152 -0
  202. package/test/failed-encoder-still-serves-what-exists.test.js +354 -0
  203. package/test/failed-start-breaker.test.js +81 -30
  204. package/test/fastest-wires.test.js +1 -1
  205. package/test/ffmpeg-stream-counts.test.js +1 -1
  206. package/test/file-edges.test.js +3 -3
  207. package/test/fmp4-self-contained.test.js +1 -1
  208. package/test/fmp4-timestamps.test.js +1 -1
  209. package/test/fmp4-track-completeness.test.js +1 -1
  210. package/test/format-keyed-outputs.test.js +78 -0
  211. package/test/health-metrics.test.js +3 -2
  212. package/test/held-request-width.test.js +22 -23
  213. package/test/helpers/encode-run.js +136 -136
  214. package/test/helpers/held-file.js +29 -0
  215. package/test/helpers/manager.js +57 -0
  216. package/test/helpers/output-spec.js +63 -0
  217. package/test/host-load.test.js +1 -1
  218. package/test/index-accuracy-is-about-the-file.test.js +128 -0
  219. package/test/init-compat.test.js +219 -0
  220. package/test/input-lost-quiets-the-plan.test.js +1 -1
  221. package/test/input-unavailable.test.js +1 -1
  222. package/test/keeping-period.test.js +4 -4
  223. package/test/keyframe-index-accuracy.test.js +97 -45
  224. package/test/keyframe-table-budget.test.js +28 -33
  225. package/test/keyframes-belong-to-the-file.test.js +174 -57
  226. package/test/learned-median.test.js +1 -1
  227. package/test/letting-go-asks-who-is-watching.test.js +191 -0
  228. package/test/limit-row-by-area.test.js +60 -0
  229. package/test/link-budget.test.js +111 -0
  230. package/test/link-probe.test.js +56 -56
  231. package/test/local-encoding-data.test.js +273 -0
  232. package/test/look-ahead-coverage.test.js +1 -1
  233. package/test/manager-surface.test.js +294 -0
  234. package/test/matroska-blocks.test.js +0 -0
  235. package/test/matroska-cues-track.test.js +1 -1
  236. package/test/memory-budget.test.js +2 -2
  237. package/test/mp4-composition-times.test.js +0 -0
  238. package/test/mp4-subtitles.test.js +1 -1
  239. package/test/one-authority.test.js +45 -41
  240. package/test/one-budget.test.js +149 -0
  241. package/test/one-output-one-session.test.js +204 -0
  242. package/test/{disk-space.test.js → one-resource-divided.test.js} +30 -10
  243. package/test/one-serving-output-per-height.test.js +458 -0
  244. package/test/orchestrator-wired.test.js +46 -45
  245. package/test/output-catalog.test.js +39 -0
  246. package/test/output-shape.test.js +4 -51
  247. package/test/output-spec.test.js +251 -157
  248. package/test/output-speed.test.js +17 -15
  249. package/test/outputs-being-prepared.test.js +40 -0
  250. package/test/packet-witness-ring.test.js +2 -2
  251. package/test/packet-witness.test.js +1 -1
  252. package/test/piece-disk-store.test.js +2 -2
  253. package/test/piece-from-whole-file.test.js +1 -1
  254. package/test/piece-lru.test.js +1 -1
  255. package/test/piece-reader.test.js +2 -2
  256. package/test/piece-store-eviction.test.js +1 -1
  257. package/test/piece-store-never-refuses.test.js +1 -1
  258. package/test/piece-store-reservations.test.js +1 -1
  259. package/test/piece-store-slow-disk.test.js +1 -1
  260. package/test/piece-tail.test.js +1 -1
  261. package/test/plan-host-timings.test.js +1 -1
  262. package/test/plan-is-steady.test.js +225 -225
  263. package/test/priority-map-download.test.js +4 -4
  264. package/test/priority-map-emptied.test.js +204 -205
  265. package/test/priority-map-per-output.test.js +215 -209
  266. package/test/priority-map.test.js +1 -1
  267. package/test/probe-wedge-certainty.test.js +1 -1
  268. package/test/produced-copy-choice.test.js +15 -60
  269. package/test/quality-offer.test.js +162 -0
  270. package/test/quality-variants.test.js +1229 -1167
  271. package/test/rate-control-in-the-output.test.js +180 -0
  272. package/test/read-survives-withdrawal.test.js +2 -2
  273. package/test/read-window.test.js +10 -3
  274. package/test/resume-warm.test.js +1 -1
  275. package/test/run-inputs.test.js +80 -0
  276. package/test/run-position-follows-published-grid.test.js +13 -11
  277. package/test/run-progress.test.js +65 -0
  278. package/test/same-height-switch.test.js +664 -0
  279. package/test/seek-frees-the-viewers-requests.test.js +217 -0
  280. package/test/seek-landing.test.js +13 -13
  281. package/test/seek-target-not-superseded.test.js +28 -29
  282. package/test/segment-closed-on-both-branches.test.js +96 -0
  283. package/test/segment-cut-times.test.js +1 -1
  284. package/test/segment-hold-supersede.test.js +28 -35
  285. package/test/segment-judged-by-its-own-header.test.js +221 -0
  286. package/test/segment-serve-wiring.test.js +130 -39
  287. package/test/segment-store-eviction.test.js +22 -7
  288. package/test/segment-store.test.js +98 -14
  289. package/test/segment-true-start.test.js +1 -1
  290. package/test/segments-are-shared.test.js +17 -16
  291. package/test/send-chunk-bytes.test.js +1 -1
  292. package/test/serving-existing-output.test.js +102 -0
  293. package/test/serving-output.test.js +102 -0
  294. package/test/shared-piece-store.test.js +1 -1
  295. package/test/source-file.test.js +1 -1
  296. package/test/stale-request-after-seek.test.js +17 -21
  297. package/test/startup-readings.test.js +10 -5
  298. package/test/subtitle-cue-framing.test.js +5 -5
  299. package/test/subtitle-cue-source.test.js +36 -38
  300. package/test/subtitle-cue-walk.test.js +11 -10
  301. package/test/subtitle-defaults.test.js +1 -1
  302. package/test/subtitle-language.test.js +4 -4
  303. package/test/subtitle-track-numbering.test.js +5 -4
  304. package/test/supply-margin.test.js +1 -1
  305. package/test/swarm-follows-readers.test.js +3 -3
  306. package/test/swarm-reach.test.js +1 -1
  307. package/test/swarm-selection.test.js +3 -3
  308. package/test/tail-duplication.test.js +1 -1
  309. package/test/the-channel-has-both-ends.test.js +91 -0
  310. package/test/the-torrent-thread-serves-bytes.test.js +107 -0
  311. package/test/thread-pool.test.js +3 -3
  312. package/test/timeline.test.js +1 -12
  313. package/test/torrent-cost.test.js +1 -1
  314. package/test/tracks-begin-together.test.js +38 -24
  315. package/test/tunnel-renewal.test.js +1 -1
  316. package/test/two-viewers-one-picture.test.js +120 -87
  317. package/test/upload-hurry.test.js +3 -3
  318. package/test/usrsctp-state.test.js +1 -1
  319. package/test/video-facts.test.js +1 -1
  320. package/test/viewer-assignments.test.js +143 -0
  321. package/test/viewer-outputs.test.js +60 -52
  322. package/test/viewer-presence.test.js +22 -25
  323. package/test/viewer-requests-admission.test.js +89 -0
  324. package/test/viewer-states.test.js +189 -202
  325. package/test/viewer-subtitle-subscription.test.js +1 -1
  326. package/test/viewer.test.js +139 -136
  327. package/test/viewing-generations.test.js +390 -0
  328. package/test/visible-rung.test.js +37 -0
  329. package/test/wait-ledger.test.js +1 -1
  330. package/test/webrtc-shim.test.js +3 -3
  331. package/test/wedge-certainty.test.js +2 -2
  332. package/test/withdraw-piece-claim.test.js +2 -2
  333. package/test/worker-channel.test.js +1 -31
  334. package/utils/client-log-file.js +151 -151
  335. package/scripts/read-container-index.mjs +0 -108
  336. package/services/container/ContainerFactory.js +0 -122
  337. package/services/controllers/PlaybackController.js +0 -33
  338. package/services/disk/DiskSpace.js +0 -150
  339. package/services/disk/wire.js +0 -61
  340. package/services/hls-session-manager.js +0 -9608
  341. package/services/orchestrators/SubtitleOrchestrator.js +0 -160
  342. package/services/output/Output.js +0 -94
  343. package/services/output/OutputSpec.js +0 -202
  344. package/services/output/index.js +0 -1
  345. package/services/output/ladder.js +0 -26
  346. package/services/produced-index.js +0 -222
  347. package/services/quality/EncodeCost.js +0 -555
  348. package/services/torrent-worker/container-tracks.js +0 -414
  349. package/services/viewer/link-readings.js +0 -56
  350. package/test/produced-index.test.js +0 -142
  351. /package/{services → bin}/thread-pool.js +0 -0
  352. /package/services/{available-share.js → encode/available-share.js} +0 -0
  353. /package/services/{decode-cost-fit.js → encode/decode-cost-fit.js} +0 -0
  354. /package/services/{encoder-readings.js → encode/encoder-readings.js} +0 -0
  355. /package/services/{host-load.js → encode/host-load.js} +0 -0
  356. /package/services/{output → encode/output}/cut-grid.js +0 -0
  357. /package/services/{output → encode/output}/rates.js +0 -0
  358. /package/services/{segment-formats → encode/segment-formats}/mpegts.js +0 -0
  359. /package/services/{torrent-cost.js → encode/torrent-cost.js} +0 -0
  360. /package/services/{container → media/container}/ebml-reader.js +0 -0
  361. /package/services/{ffmpeg-banner.js → media/ffmpeg-banner.js} +0 -0
  362. /package/services/{tracks → media/tracks}/ContainerTrack.js +0 -0
  363. /package/services/{tracks → media/tracks}/ImageSubtitleTrack.js +0 -0
  364. /package/services/{tracks → media/tracks}/SubtitleTrack.js +0 -0
  365. /package/services/{tracks → media/tracks}/TextSubtitleTrack.js +0 -0
  366. /package/services/{tracks → media/tracks}/VideoTrack.js +0 -0
  367. /package/services/{tracks → media/tracks}/index.js +0 -0
  368. /package/services/{files → storage/files}/piece-from-whole-file.js +0 -0
  369. /package/services/{disk → storage}/keep.js +0 -0
  370. /package/services/{piece-store → storage/piece-store}/piece-lru.js +0 -0
  371. /package/services/{disk → storage}/returns.js +0 -0
  372. /package/services/{demand → torrent/demand}/DemandRegister.js +0 -0
  373. /package/services/{demand → torrent/demand}/Urgency.js +0 -0
  374. /package/services/{demand → torrent/demand}/Window.js +0 -0
  375. /package/services/{demand → torrent/demand}/index.js +0 -0
  376. /package/services/{demand → torrent/demand}/pieces.js +0 -0
  377. /package/services/{download → torrent/download}/withdraw-claim.js +0 -0
  378. /package/services/{supply-margin.js → torrent/supply-margin.js} +0 -0
  379. /package/services/{torrent-source-key.js → torrent/torrent-source-key.js} +0 -0
  380. /package/services/{torrent-worker → torrent/worker}/fastest-wires.js +0 -0
  381. /package/services/{torrent-worker → torrent/worker}/handle-state.js +0 -0
  382. /package/services/{nat-classifier.js → transport/nat-classifier.js} +0 -0
  383. /package/services/{registry-api.js → transport/registry-api.js} +0 -0
  384. /package/services/{tunnel-client.js → transport/tunnel-client.js} +0 -0
  385. /package/services/{priority → viewer}/WaitLedger.js +0 -0
@@ -23,10 +23,10 @@
23
23
  /**
24
24
  * @param {import("fastify").FastifyRequest} req
25
25
  * @param {import("fastify").FastifyReply} reply
26
- * @param {{ hlsSessionManager: import("../../../../services/hls-session-manager.js").HlsSessionManager }} deps
26
+ * @param {{ viewerRequests: object }} deps
27
27
  * @returns {Promise<void>}
28
28
  */
29
- export async function handleApiTranscodeSessionSeekPost(req, reply, { hlsSessionManager }) {
29
+ export async function handleApiTranscodeSessionSeekPost(req, reply, { viewerRequests }) {
30
30
  const sessionId = req.params?.sessionId;
31
31
  const body = req.body && typeof req.body === "object" ? req.body : {};
32
32
  const positionSeconds = Number(body.positionSeconds);
@@ -40,7 +40,16 @@ export async function handleApiTranscodeSessionSeekPost(req, reply, { hlsSession
40
40
  // has to move with them or their next request is judged against where they
41
41
  // were before the jump.
42
42
  const consumerId = typeof body.consumerId === "string" ? body.consumerId.trim() : "";
43
- const applied = hlsSessionManager.requestSeek(sessionId, positionSeconds, consumerId);
43
+ if (!consumerId) {
44
+ reply.code(400);
45
+ return reply.send({ error: "consumerId is required." });
46
+ }
47
+ // Which viewing their requests now belong to. The page raises this before it
48
+ // sends anything and stamps every request with it, so a request that crosses
49
+ // this message is still read against the viewing it was made in. A page that
50
+ // states nothing leaves the generation where it is.
51
+ const generation = Number(body.generation);
52
+ const applied = viewerRequests.requestSeek(sessionId, positionSeconds, consumerId, generation);
44
53
  if (!applied) {
45
54
  // Unknown or disposed session — nothing to steer. Not an error worth
46
55
  // surfacing to the viewer: the seek will be handled by whatever session
@@ -45,7 +45,7 @@ const TORRENT_READY_TIMEOUT_MS = 10_000;
45
45
  * The underlying add is NOT cancelled: it keeps running and warms the pool, so
46
46
  * the client's next attempt is likely to find it ready.
47
47
  *
48
- * @param {import("../../services/torrent-pool.js").TorrentPool} torrentPool
48
+ * @param {import("../../services/torrent/torrent-pool.js").TorrentPool} torrentPool
49
49
  * @param {string} sourceType
50
50
  * @param {string} source
51
51
  * @returns {Promise<import("webtorrent").Torrent>}
@@ -76,7 +76,7 @@ async function waitForTorrent(torrentPool, sourceType, source) {
76
76
  *
77
77
  * @param {import("fastify").FastifyRequest} req
78
78
  * @param {import("fastify").FastifyReply} reply
79
- * @param {{ sourceRegistry: ReturnType<import("../../store/source-registry.js").createSourceRegistry>, torrentPool: import("../../services/torrent-pool.js").TorrentPool }} deps
79
+ * @param {{ sourceRegistry: ReturnType<import("../../store/source-registry.js").createSourceRegistry>, torrentPool: import("../../services/torrent/torrent-pool.js").TorrentPool }} deps
80
80
  * @returns {Promise<void>}
81
81
  */
82
82
  /**
@@ -12,15 +12,15 @@
12
12
  * instead of it: both encoders run, one for the rung and one for the audio.
13
13
  */
14
14
 
15
- import { serveSessionFile } from "../session-file/get.js";
15
+ import { consumerOf, refusedAsStale, serveSessionFile } from "../../../services/server/transcode-session-files.js";
16
16
 
17
17
  /**
18
18
  * @param {import("fastify").FastifyRequest} req
19
19
  * @param {import("fastify").FastifyReply} reply
20
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
20
+ * @param {{ renditions: object, serving: object, viewerRequests: object }} deps
21
21
  * @returns {Promise<void>}
22
22
  */
23
- export async function handleTranscodeAudioFileGet(req, reply, { hlsSessionManager }) {
23
+ export async function handleTranscodeAudioFileGet(req, reply, { renditions, serving, viewerRequests }) {
24
24
  const baseSessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
25
25
  const trackIndex = Number(req.params.trackIndex);
26
26
  const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
@@ -29,8 +29,16 @@ export async function handleTranscodeAudioFileGet(req, reply, { hlsSessionManage
29
29
  // soundtrack each of them chose is their own: without this, a segment request
30
30
  // from one viewer would be read as everybody moving to that track, and the
31
31
  // other viewer's encoder would be stopped once per segment.
32
- const consumerId = typeof req.query?.consumer === "string" ? req.query.consumer : "";
33
- const resolved = await hlsSessionManager.resolveAudioRenditionFile(
32
+ const consumerId = consumerOf(req);
33
+ // Before resolving, for the same reason as a step's: resolving can make the
34
+ // soundtrack and registers this viewer on it. A soundtrack keeps no record of
35
+ // what answered a repeat — it is not a height — but a request for a viewing
36
+ // that has been left is refused here like any other, and the response that
37
+ // does go out holds its output while it is being sent (`serveSessionFile`).
38
+ if (refusedAsStale(req, reply, viewerRequests, fileName)) {
39
+ return reply;
40
+ }
41
+ const resolved = await renditions.resolveAudioRenditionFile(
34
42
  baseSessionId,
35
43
  trackIndex,
36
44
  fileName,
@@ -48,7 +56,8 @@ export async function handleTranscodeAudioFileGet(req, reply, { hlsSessionManage
48
56
  }
49
57
 
50
58
  return serveSessionFile(req, reply, {
51
- hlsSessionManager,
59
+ serving,
60
+ viewerRequests,
52
61
  sessionId: resolved.sessionId,
53
62
  fileName
54
63
  });
@@ -12,7 +12,7 @@
12
12
  * switch into bytes that already exist; 503 while it is still being made.
13
13
  */
14
14
 
15
- import { waitForSessionFile } from "../session-file/get.js";
15
+ import { waitForSessionFile } from "../../../services/server/transcode-session-files.js";
16
16
 
17
17
  /** How long to hold the request before telling the caller to retry. */
18
18
  const WARM_WAIT_MS = 12_000;
@@ -20,9 +20,9 @@ const WARM_WAIT_MS = 12_000;
20
20
  /**
21
21
  * @param {import("fastify").FastifyRequest} req
22
22
  * @param {import("fastify").FastifyReply} reply
23
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
23
+ * @param {{ renditions: object, serving: object, viewerRequests: object }} deps
24
24
  */
25
- export async function handleTranscodeAudioWarmGet(req, reply, { hlsSessionManager }) {
25
+ export async function handleTranscodeAudioWarmGet(req, reply, { renditions, serving, viewerRequests }) {
26
26
  const baseSessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
27
27
  const trackIndex = Number(req.params.track);
28
28
  const positionSeconds = Number(req.query?.position);
@@ -40,10 +40,13 @@ export async function handleTranscodeAudioWarmGet(req, reply, { hlsSessionManage
40
40
  // listening to something else, and preparing a track must not be read as
41
41
  // everybody moving to it.
42
42
  const consumerId = typeof req.query?.consumer === "string" ? req.query.consumer : "";
43
+ if (!consumerId) {
44
+ return reply.code(400).send({ error: "A consumer is required." });
45
+ }
43
46
 
44
47
  let prepared;
45
48
  try {
46
- prepared = await hlsSessionManager.prepareAudioTrack(
49
+ prepared = await renditions.prepareAudioTrack(
47
50
  baseSessionId,
48
51
  trackIndex,
49
52
  positionSeconds,
@@ -59,10 +62,10 @@ export async function handleTranscodeAudioWarmGet(req, reply, { hlsSessionManage
59
62
  }
60
63
 
61
64
  const result = await waitForSessionFile(
62
- hlsSessionManager,
65
+ serving,
63
66
  prepared.sessionId,
64
67
  prepared.fileName,
65
- WARM_WAIT_MS
68
+ { holdMs: WARM_WAIT_MS }
66
69
  );
67
70
  if (result.kind === "file") {
68
71
  // The bytes are the player's to fetch; the handle opened to reach them is
@@ -1,268 +1,52 @@
1
- import { logger } from "../../../utils/logger.js";
2
- import { bandOf, waits } from "../../../services/priority/WaitLedger.js";
3
-
4
- /**
5
- * How long a request for a not-yet-produced file is held before answering with
6
- * a retryable 503.
7
- *
8
- * MEASUREMENT MODE (2026-08-02): deliberately far above any plausible player
9
- * deadline, so OUR limit never fires first. Whatever ends the wait is then the
10
- * player's own behaviour — which is exactly what we need to observe. The
11
- * `[hold]` log line at the call site records, per request, whether the segment
12
- * arrived, whether we gave up, or whether the CLIENT aborted, and after how
13
- * long.
14
- *
15
- * The previous value (2 s) was chosen to dodge a reported iOS AVPlayer ~3.5 s
16
- * response-header deadline. That deadline never appears in our own logs (no
17
- * -12889 across six hours of production logs), and every reference project
18
- * holds rather than refusing: Jellyfin and hls-vod-too hold unbounded,
19
- * hls-media-server holds 10 s. The early refusal is what made the player probe
20
- * scattered positions, which then steered the encoder off target. Choose the
21
- * final value from what this measurement shows, not from a number read
22
- * elsewhere.
23
- */
24
- const SEGMENT_WAIT_MS = 60_000;
25
-
26
- /**
27
- * Serve HLS playlist and segment files from an active transcode session.
28
- *
29
- * Briefly waits for the requested file to appear, then answers with a
30
- * retryable 503 rather than holding the connection, so clients — in
31
- * particular iOS's native HLS player — never hit their own response
32
- * deadline while a segment is still being produced.
33
- *
34
- * GET /transcode/:sessionId/:fileName
35
- *
36
- * @param {import("fastify").FastifyRequest} req
37
- * @param {import("fastify").FastifyReply} reply
38
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
39
- * @returns {Promise<void>}
40
- */
41
- export async function handleTranscodeSessionFileGet(req, reply, { hlsSessionManager }) {
42
- const sessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
43
- const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
44
- return serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName });
45
- }
46
-
47
- /**
48
- * Serve one playlist or segment from a named session.
49
- *
50
- * Split from the route above because the variant route
51
- * (`/transcode/:sessionId/v/:height/:fileName`) serves the same files from
52
- * another session of the same family, and must hold, log and answer them
53
- * identically — a switch of quality must not go through a different code path
54
- * from the stream it switches away from.
55
- *
56
- * @param {import("fastify").FastifyRequest} req
57
- * @param {import("fastify").FastifyReply} reply
58
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager, sessionId: string, fileName: string }} params
59
- * @returns {Promise<void>}
60
- */
61
- export async function serveSessionFile(req, reply, { hlsSessionManager, sessionId, fileName }) {
62
- // Which viewer is asking. One session serves everyone watching a copied
63
- // picture, so without it their positions collapse into one and a seek by the
64
- // viewer in front releases the requests held for the viewer behind. Absent on
65
- // a plain HTTP transport, where no loader of ours builds the URL, and then
66
- // the single shared position decides as it always did.
67
- const consumerId = typeof req.query?.consumer === "string" ? req.query.consumer : "";
68
- // Hold the request only briefly, then answer "retry" instead of waiting for
69
- // the segment. iOS's native HLS player (AVPlayer) enforces a hard ~3.5 s
70
- // deadline on RESPONSE HEADERS and raises -12889 ("No response for media
71
- // file") when it passes — it then cancels in-flight requests, probes
72
- // neighbouring positions and can restart the stream from the beginning. That
73
- // is exactly the post-seek "player thrashing" seen in the field, because a
74
- // seek restarts ffmpeg and the first segment then takes far longer than 3.5 s
75
- // to appear. Holding the connection for 30 s (as this did) guaranteed the
76
- // timeout on every seek. A short hold keeps the fast path intact (a ready or
77
- // nearly-ready segment is still served on the first request) while a slow one
78
- // gets a prompt retryable answer, which resets the player's own deadline.
79
- // hls.js is unaffected: it consumes the 503 through its retry policy, whose
80
- // budget the client widens to match (see hls-player.js fragLoadPolicy).
81
- // Instrumented wait. `clientAborted` flips when the player drops the
82
- // connection while we are still holding it — the single most informative
83
- // signal about its real patience, and observable only from this side.
84
- const holdStartedAt = Date.now();
85
- let clientAborted = false;
86
- const onClientAbort = () => { clientAborted = true; };
87
- req.raw.on("close", onClientAbort);
88
-
89
- const result = await waitForSessionFile(
90
- hlsSessionManager,
91
- sessionId,
92
- fileName,
93
- SEGMENT_WAIT_MS,
94
- consumerId
95
- );
96
-
97
- req.raw.off("close", onClientAbort);
98
- const heldMs = Date.now() - holdStartedAt;
99
- if (result.isPlaylist !== true) {
100
- const outcome = clientAborted
101
- ? "client-aborted"
102
- : result.kind === "ok" ? "served" : result.kind;
103
- // AGAINST THE RANK THE MAP GAVE IT. A wait is the only thing that says
104
- // whether the prioritisation is being used well, and this is the one place
105
- // in the proxy where a viewer is measurably waiting for a named segment.
106
- // Recorded even when the segment was served at once: a run of short waits
107
- // at the top rank is what "the urgent zone is being served first" looks
108
- // like, and without them the table would hold only the failures.
109
- const ranked = result.ranked ?? null;
110
- if (ranked) {
111
- waits.note(ranked.address, heldMs, ranked.rank, ranked.topRank);
112
- }
113
- logger.info(
114
- `[hold] ${fileName} ${outcome} after ${heldMs}ms` +
115
- (ranked ? ` (the map wants it ${bandOf(ranked.rank, ranked.topRank)}, rank ${ranked.rank} of ${ranked.topRank})` : "")
116
- );
117
- }
118
-
119
- if (result.kind === "not-found") {
120
- return reply.code(404).send({ error: "Transcode session file was not found." });
121
- }
122
- if (result.kind === "superseded") {
123
- // The viewer moved while this was being held, and this segment is not the
124
- // one they moved TO — that case is kept and waited out, see
125
- // `waitForSessionFile`. Answer at once so the player can ask for where it
126
- // is now; `Retry-After: 0` because there is nothing to wait for.
127
- //
128
- // Named in the log, because a refusal that says only "superseded" is what
129
- // made the 2026-08-18 freeze take a day to explain: the player was refused
130
- // the segment at its own seek target and nothing recorded which segment or
131
- // where the viewer was.
132
- logger.info(
133
- `[hold] ${fileName} refused: the viewer is at ` +
134
- `${hlsSessionManager.viewerPositionOf(sessionId, consumerId).toFixed(1)}s and this is not the segment there`
135
- );
136
- reply.header("Retry-After", "0");
137
- return reply.code(503).send({ error: "Superseded by a seek." });
138
- }
139
- if (result.kind === "warming-up") {
140
- // The segment is still being produced (e.g. just after a seek-restart).
141
- // Return a retryable 503 — never 202, which hls.js cannot consume as a
142
- // media segment — so the player retries the fetch shortly.
143
- reply.header("Retry-After", "1");
144
- // `Retry-After` tells the player to re-request THIS segment after a short
145
- // pause. Without it a bare 503 reads as "nothing here", and the player goes
146
- // looking elsewhere: because our synthetic VOD playlist lists every segment
147
- // of the file, it believes they all exist and SCANS them (field log: one
148
- // user seek produced probes at #617, #717, #732…). That scan is what used
149
- // to steer the encoder off the real target. Whether iOS's native player
150
- // honours the hint is not guaranteed — its behaviour is closed — but this
151
- // is the standard, correct way to say "wait, don't look elsewhere", and
152
- // hls.js already retries the same fragment regardless.
153
- reply.header("Retry-After", "1");
154
- // SAY WHY, to the viewer and not only to the log. The same refusal is
155
- // written into `proxy.log` with the rank the priority map gave this
156
- // segment; the page had no access to that and told the viewer it did not
157
- // know why. Field 2026-09-14: `rank 1 of 100` — nothing was making it —
158
- // printed sixty seconds before the page gave up saying the opposite.
159
- return reply.code(503).send({
160
- error: "Transcode segment is still being produced.",
161
- reason: warmingReason(result.ranked ?? null)
162
- });
163
- }
164
- if (result.kind === "failed") {
165
- return reply.code(500).send({ error: result.message });
166
- }
167
-
168
- if (result.isPlaylist) {
169
- reply.header("Cache-Control", "no-store");
170
- } else {
171
- reply.header("Cache-Control", "public, max-age=60");
172
- }
173
- reply.header("Content-Type", result.contentType);
174
- return reply.send(result.stream);
175
- }
176
-
177
- /**
178
- * Poll `hlsSessionManager.getFileStream()` until the file is available,
179
- * the session fails, or the timeout elapses.
180
- *
181
- * @param {import("../../../services/hls-session-manager.js").HlsSessionManager} hlsSessionManager
182
- * @param {string} sessionId
183
- * @param {string} fileName
184
- * @param {number} timeoutMs
185
- * @param {string} [consumerId] - Which viewer is asking, where the transport
186
- * carries it. Their own position is what decides whether a held request has
187
- * been made pointless by a seek — a session can have several viewers, and
188
- * the seek epoch belongs to all of them.
189
- * @returns {Promise<Awaited<ReturnType<import("../../../services/hls-session-manager.js").HlsSessionManager["getFileStream"]>>>}
190
- */
191
- export async function waitForSessionFile(hlsSessionManager, sessionId, fileName, timeoutMs, consumerId = "") {
192
- const startedAt = Date.now();
193
- // One sequence number for THIS request, reused by every poll below, so the
194
- // session can tell a newly-arrived request apart from an old one polling
195
- // again — see HlsSessionManager#ensureEncodingFor for the encoder ping-pong
196
- // this prevents when one seek-bar scrub fires several segment requests.
197
- const requestSeq = hlsSessionManager.nextRequestSeq(sessionId);
198
- // The viewer's position when this request was made. A seek makes every held
199
- // request stale — it asks for a segment nobody is going to watch — and hls.js
200
- // keeps only ONE fragment load outstanding, so holding on blocks the request
201
- // the player actually needs now. Measured: 57 s of a 58 s backward seek was
202
- // this wait, and the segment the viewer wanted took 15 ms once it was asked
203
- // for.
204
- let seekEpoch = hlsSessionManager.seekEpoch(sessionId);
205
- /** @type {{ address: string, rank: number, topRank: number } | null} */
206
- let lastRanked = null;
207
- while (Date.now() - startedAt < timeoutMs) {
208
- const result = await hlsSessionManager.getFileStream(sessionId, fileName, {
209
- requestSeq,
210
- consumerId
211
- });
212
- if (result.kind !== "warming-up") {
213
- return result;
214
- }
215
- if (hlsSessionManager.seekEpoch(sessionId) !== seekEpoch) {
216
- // A seek moved the epoch. Whether THIS request is stale depends on which
217
- // segment it asks for: the one the viewer has just landed on races the
218
- // seek notification and would otherwise be refused at the exact moment it
219
- // is needed — measured 2026-08-18, two 503s within 80 ms on the segment
220
- // at the seek target, after which the player never asked for it again.
221
- if (!hlsSessionManager.requestStillWanted(sessionId, fileName, consumerId)) {
222
- return { kind: "superseded" };
223
- }
224
- logger.info(
225
- `[hold] ${fileName} kept across a seek: it is the segment the viewer now needs`
226
- );
227
- seekEpoch = hlsSessionManager.seekEpoch(sessionId);
228
- }
229
- // The rank the map last gave this segment, so a request that runs out of
230
- // patience is still counted against what it was promised. Kept across the
231
- // polls because the timeout path has no result of its own.
232
- lastRanked = result.ranked ?? lastRanked;
233
- await delay(300);
234
- }
235
- return { kind: "warming-up", ranked: lastRanked };
236
- }
237
-
238
- /**
239
- * Resolve after a given number of milliseconds.
240
- *
241
- * @param {number} ms
242
- * @returns {Promise<void>}
243
- */
244
- function delay(ms) {
245
- return new Promise((resolve) => {
246
- setTimeout(resolve, ms);
247
- });
248
- }
249
-
250
- /**
251
- * What to tell the VIEWER when a segment is not there yet.
252
- *
253
- * In their terms — the film, and what is being done about it — never in ours: a
254
- * viewer cannot read a log, does not know what a rank is, and is not helped by
255
- * being told which of our parts is waiting for which other.
256
- *
257
- * @param {{ rank: number, topRank: number } | null} ranked - What the priority
258
- * map thought of this segment while the request was held.
259
- * @returns {string}
260
- */
261
- function warmingReason(ranked) {
262
- if (ranked && ranked.rank < ranked.topRank) {
263
- // The map is working somewhere else in the film. That is the answer the
264
- // failure of 2026-09-14 needed and nobody was given.
265
- return "This part of the film is not being prepared yet — the proxy is working further along.";
266
- }
267
- return "This part of the film is still being prepared.";
268
- }
1
+ import {
2
+ consumerOf,
3
+ refusedAsStale,
4
+ serveSessionFile,
5
+ statedGenerationOf
6
+ } from "../../../services/server/transcode-session-files.js";
7
+
8
+ /**
9
+ * How long a request for a file not yet produced is held: AS LONG AS THE
10
+ * REQUESTER SAYS IT WILL WAIT, and no longer than it stays.
11
+ *
12
+ * The patience is the requester's. It used to be a figure of this route's own,
13
+ * 60 s, beside the page's own 60 s — two chosen numbers that happened to agree,
14
+ * so the "retry" this route sent at its deadline reached a page that had given
15
+ * up at the same instant. The page now states how long it will hold the answer
16
+ * open (`X-Hold-Ms`, its own deadline less the round trip it has measured), and
17
+ * this route answers by then. A requester that states nothing is held until it
18
+ * closes the connection, which every HTTP client does when it stops waiting.
19
+ */
20
+
21
+ /**
22
+ * Serve HLS playlist and segment files from an active transcode session.
23
+ *
24
+ * Briefly waits for the requested file to appear, then answers with a
25
+ * retryable 503 rather than holding the connection, so clients — in
26
+ * particular iOS's native HLS player — never hit their own response
27
+ * deadline while a segment is still being produced.
28
+ *
29
+ * GET /transcode/:sessionId/:fileName
30
+ *
31
+ * @param {import("fastify").FastifyRequest} req
32
+ * @param {import("fastify").FastifyReply} reply
33
+ * @param {{ serving: object, viewerRequests: object }} deps
34
+ * @returns {Promise<void>}
35
+ */
36
+ export async function handleTranscodeSessionFileGet(req, reply, { serving, viewerRequests }) {
37
+ const sessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
38
+ const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
39
+ if (refusedAsStale(req, reply, viewerRequests, fileName)) {
40
+ return reply;
41
+ }
42
+ // This route names its output outright, so the answer is recorded rather
43
+ // than chosen — under the height the output is named after, which is where
44
+ // the step route answers with the picture itself.
45
+ viewerRequests.noteAnsweredDirectly(
46
+ sessionId,
47
+ consumerOf(req),
48
+ statedGenerationOf(req),
49
+ fileName
50
+ );
51
+ return serveSessionFile(req, reply, { serving, viewerRequests, sessionId, fileName });
52
+ }
@@ -13,15 +13,21 @@
13
13
  * no variant is ever asked for unless the viewer picked it.
14
14
  */
15
15
 
16
- import { serveSessionFile } from "../session-file/get.js";
16
+ import { replyAssignmentLost, replyOutputUnavailable } from "../../../services/server/http-responses.js";
17
+ import {
18
+ consumerOf,
19
+ refusedAsStale,
20
+ serveSessionFile,
21
+ statedGenerationOf
22
+ } from "../../../services/server/transcode-session-files.js";
17
23
 
18
24
  /**
19
25
  * @param {import("fastify").FastifyRequest} req
20
26
  * @param {import("fastify").FastifyReply} reply
21
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
27
+ * @param {{ renditions: object, serving: object, viewerRequests: object }} deps
22
28
  * @returns {Promise<void>}
23
29
  */
24
- export async function handleTranscodeVariantFileGet(req, reply, { hlsSessionManager }) {
30
+ export async function handleTranscodeVariantFileGet(req, reply, { renditions, serving, viewerRequests }) {
25
31
  const baseSessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
26
32
  const height = Number(req.params.height);
27
33
  const fileName = typeof req.params.fileName === "string" ? req.params.fileName : "";
@@ -29,13 +35,45 @@ export async function handleTranscodeVariantFileGet(req, reply, { hlsSessionMana
29
35
  // Which viewer is asking. Two viewers of one picture can be on two rungs, and
30
36
  // a segment request is what says which rung a viewer is watching — read as
31
37
  // the session's own, one of them would take the other off their step.
32
- const consumerId = typeof req.query?.consumer === "string" ? req.query.consumer : "";
33
- const resolved = await hlsSessionManager.resolveVariantFile(
38
+ const consumerId = consumerOf(req);
39
+ // Before resolving: resolving can make a step and registers this viewer on
40
+ // it, which a request for a viewing they have already left must not do.
41
+ if (refusedAsStale(req, reply, viewerRequests, fileName)) {
42
+ return reply;
43
+ }
44
+ const resolved = await renditions.resolveVariantFile(
34
45
  baseSessionId,
35
46
  height,
36
47
  fileName,
37
- consumerId
48
+ consumerId,
49
+ // Which viewing this request was made in: a repeat within it is answered
50
+ // by whatever answered it the first time.
51
+ statedGenerationOf(req)
38
52
  );
53
+ if (resolved.unavailable) {
54
+ return replyOutputUnavailable(reply, resolved.unavailable);
55
+ }
56
+ if (resolved.lost) {
57
+ return replyAssignmentLost(reply, resolved.lost);
58
+ }
59
+ if (resolved.recover) {
60
+ // The very piece that was given, from what its gone output left in the
61
+ // store — held for as long as it is being sent, like any other answer.
62
+ const stored = await serving.storedPieceOf(resolved.recover.key, resolved.recover.likeId, fileName);
63
+ if (!stored) {
64
+ return replyAssignmentLost(reply, { reason: "the stored piece went while it was being fetched" });
65
+ }
66
+ const release = viewerRequests.holdResponseForKey(resolved.recover.key, consumerId);
67
+ reply.raw?.once?.("finish", release);
68
+ reply.raw?.once?.("close", release);
69
+ reply.raw?.once?.("error", release);
70
+ if (!reply.raw || reply.raw.destroyed || reply.raw.writableEnded) {
71
+ release();
72
+ }
73
+ reply.header("Cache-Control", "public, max-age=60");
74
+ reply.header("Content-Type", stored.contentType);
75
+ return reply.send(stored.stream);
76
+ }
39
77
  if (resolved.error) {
40
78
  // Preparing the variant failed — a probe, a keyframe index, an input that
41
79
  // is not there yet. Retryable, like every other not-ready answer on this
@@ -49,7 +87,8 @@ export async function handleTranscodeVariantFileGet(req, reply, { hlsSessionMana
49
87
  }
50
88
 
51
89
  return serveSessionFile(req, reply, {
52
- hlsSessionManager,
90
+ serving,
91
+ viewerRequests,
53
92
  sessionId: resolved.sessionId,
54
93
  fileName
55
94
  });
@@ -13,7 +13,9 @@
13
13
  * knowing there is something to fetch.
14
14
  */
15
15
 
16
- import { waitForSessionFile } from "../session-file/get.js";
16
+ import { replyOutputUnavailable } from "../../../services/server/http-responses.js";
17
+ import { waitForSessionFile } from "../../../services/server/transcode-session-files.js";
18
+ import { OUTPUT_UNAVAILABLE } from "../../../services/encode/output/index.js";
17
19
 
18
20
  /** How long to hold the warm-up request before telling the caller to retry. */
19
21
  const WARM_WAIT_MS = 30_000;
@@ -21,10 +23,10 @@ const WARM_WAIT_MS = 30_000;
21
23
  /**
22
24
  * @param {import("fastify").FastifyRequest} req
23
25
  * @param {import("fastify").FastifyReply} reply
24
- * @param {{ hlsSessionManager: import("../../../services/hls-session-manager.js").HlsSessionManager }} deps
26
+ * @param {{ renditions: object, serving: object, viewerRequests: object }} deps
25
27
  * @returns {Promise<void>}
26
28
  */
27
- export async function handleTranscodeVariantWarmGet(req, reply, { hlsSessionManager }) {
29
+ export async function handleTranscodeVariantWarmGet(req, reply, { renditions, serving, viewerRequests }) {
28
30
  const baseSessionId = typeof req.params.sessionId === "string" ? req.params.sessionId : "";
29
31
  const height = Number(req.params.height);
30
32
  const positionSeconds = Number(req.query?.position);
@@ -32,11 +34,22 @@ export async function handleTranscodeVariantWarmGet(req, reply, { hlsSessionMana
32
34
  if (!Number.isInteger(height) || height <= 0 || !Number.isFinite(positionSeconds) || positionSeconds < 0) {
33
35
  return reply.code(400).send({ error: "A height and a non-negative position are required." });
34
36
  }
37
+ // Whose warm-up this is. It was not passed at all, so every warm-up was
38
+ // written onto a nameless viewer instead of the person who asked for it.
39
+ const consumerId = typeof req.query?.consumer === "string" ? req.query.consumer : "";
40
+ if (!consumerId) {
41
+ return reply.code(400).send({ error: "A consumer is required." });
42
+ }
35
43
 
36
44
  let prepared;
37
45
  try {
38
- prepared = await hlsSessionManager.prepareVariant(baseSessionId, height, positionSeconds);
46
+ prepared = await renditions.prepareVariant(baseSessionId, height, positionSeconds, consumerId);
39
47
  } catch (error) {
48
+ if (error?.code === OUTPUT_UNAVAILABLE) {
49
+ // Nothing at that height suits this viewer's link. Said at once and not
50
+ // as "retry": the page tells the viewer and keeps what is playing.
51
+ return replyOutputUnavailable(reply, error.details);
52
+ }
40
53
  const message = error instanceof Error ? error.message : String(error);
41
54
  reply.header("Retry-After", "1");
42
55
  return reply.code(503).send({ error: `Could not prepare the quality variant: ${message}` });
@@ -46,10 +59,10 @@ export async function handleTranscodeVariantWarmGet(req, reply, { hlsSessionMana
46
59
  }
47
60
 
48
61
  const result = await waitForSessionFile(
49
- hlsSessionManager,
62
+ serving,
50
63
  prepared.sessionId,
51
64
  prepared.fileName,
52
- WARM_WAIT_MS
65
+ { holdMs: WARM_WAIT_MS }
53
66
  );
54
67
  if (result.kind === "file") {
55
68
  // The bytes are not sent — the player fetches them itself the moment it
@@ -103,7 +103,7 @@ export function renderRunGraphMarkdown() {
103
103
  "# The encoder run — states and transitions",
104
104
  "",
105
105
  "One run of one ffmpeg inside one transcode session. The table this is drawn",
106
- "from is executed by `services/hls-session-manager.js`; every transition a real",
106
+ "from is executed by `services/encode/EncodeRuns.js`; every transition a real",
107
107
  "run makes is logged as state, event and target, so a run that takes an edge",
108
108
  "absent here is a violation the log names.",
109
109
  "",