@torrent-tv/proxy 2.87.5 → 2.87.7

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 (463) hide show
  1. package/.claude/commands/opsx/apply.md +155 -155
  2. package/.claude/commands/opsx/archive.md +160 -160
  3. package/.claude/commands/opsx/explore.md +174 -174
  4. package/.claude/commands/opsx/propose.md +109 -109
  5. package/.claude/commands/opsx/sync.md +143 -143
  6. package/.claude/skills/openspec-apply-change/SKILL.md +159 -159
  7. package/.claude/skills/openspec-archive-change/SKILL.md +117 -117
  8. package/.claude/skills/openspec-explore/SKILL.md +289 -289
  9. package/.claude/skills/openspec-propose/SKILL.md +113 -113
  10. package/.claude/skills/openspec-sync-specs/SKILL.md +147 -147
  11. package/.dockerignore +5 -5
  12. package/AGENTS.md +322 -322
  13. package/CHANGELOG.md +8 -0
  14. package/Dockerfile +30 -30
  15. package/LICENSE +16 -16
  16. package/README.md +369 -369
  17. package/assets/calibration/NOTICE.md +100 -100
  18. package/assets/diagnostics/sctpstate.gdb +31 -31
  19. package/bin/cli.js +684 -684
  20. package/bin/thread-pool.js +30 -30
  21. package/biome.json +411 -411
  22. package/docs/browser-proxy-contract.md +101 -94
  23. package/docs/container-architecture.md +218 -218
  24. package/docs/download-architecture.md +261 -261
  25. package/docs/encode-architecture.md +729 -729
  26. package/docs/encode-run-state.md +96 -96
  27. package/docs/logs.md +97 -97
  28. package/docs/socks5-egress-research.md +114 -114
  29. package/docs/storage-architecture.md +267 -267
  30. package/knip.json +14 -14
  31. package/openspec/changes/adaptive-bitrate/design.md +92 -92
  32. package/openspec/changes/adaptive-bitrate/proposal.md +56 -56
  33. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +51 -51
  34. package/openspec/changes/adaptive-bitrate/tasks.md +40 -40
  35. package/openspec/changes/binary-distribution/.openspec.yaml +2 -2
  36. package/openspec/changes/binary-distribution/proposal.md +53 -53
  37. package/openspec/changes/chunked-request-bodies/design.md +81 -81
  38. package/openspec/changes/chunked-request-bodies/proposal.md +53 -53
  39. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +30 -30
  40. package/openspec/changes/chunked-request-bodies/tasks.md +40 -40
  41. package/openspec/changes/cold-start/design.md +98 -98
  42. package/openspec/changes/cold-start/proposal.md +62 -62
  43. package/openspec/changes/cold-start/specs/cold-start/spec.md +47 -47
  44. package/openspec/changes/cold-start/tasks.md +48 -48
  45. package/openspec/changes/disk-cap/.openspec.yaml +2 -2
  46. package/openspec/changes/disk-cap/proposal.md +37 -37
  47. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +25 -25
  48. package/openspec/changes/disk-cap/tasks.md +19 -19
  49. package/openspec/changes/proxy-observability/.openspec.yaml +2 -2
  50. package/openspec/changes/proxy-observability/design.md +38 -38
  51. package/openspec/changes/proxy-observability/proposal.md +49 -49
  52. package/openspec/changes/proxy-observability/specs/observability/spec.md +33 -33
  53. package/openspec/changes/proxy-observability/tasks.md +21 -21
  54. package/openspec/changes/seek-debounce/design.md +118 -118
  55. package/openspec/changes/seek-debounce/proposal.md +59 -59
  56. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +41 -41
  57. package/openspec/changes/seek-debounce/tasks.md +33 -33
  58. package/openspec/changes/subtitle-language/.openspec.yaml +2 -2
  59. package/openspec/changes/subtitle-language/proposal.md +50 -50
  60. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +34 -34
  61. package/openspec/changes/subtitle-language/tasks.md +26 -26
  62. package/openspec/changes/track-selection/.openspec.yaml +2 -2
  63. package/openspec/changes/track-selection/design.md +47 -47
  64. package/openspec/changes/track-selection/proposal.md +45 -45
  65. package/openspec/changes/track-selection/specs/track-selection/spec.md +41 -41
  66. package/openspec/changes/track-selection/tasks.md +20 -20
  67. package/openspec/changes/transcode-quality/.openspec.yaml +2 -2
  68. package/openspec/changes/transcode-quality/proposal.md +51 -51
  69. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +139 -139
  70. package/openspec/changes/transcode-quality/tasks.md +63 -63
  71. package/openspec/config.yaml +38 -38
  72. package/package.json +1 -1
  73. package/public/.well-known/appspecific/com.chrome.devtools.json +1 -1
  74. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  75. package/research/double-spawn-2026-09-10.md +171 -171
  76. package/research/handover-2026-09-13.md +209 -209
  77. package/research/handover-reader-claims-removal-2026-09-12.md +166 -166
  78. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +175 -175
  79. package/research/preset-benchmark-size-scaling-2026-08-22.md +55 -55
  80. package/research/priority-map-is-the-truth-2026-09-12.md +225 -225
  81. package/routes/api/client-logs/post.js +86 -86
  82. package/routes/api/delivery-sink/get.js +72 -72
  83. package/routes/api/link-probe/get.js +51 -51
  84. package/routes/api/playback-plan/audio-tracks/post.js +29 -0
  85. package/routes/api/playback-plan/post.js +53 -53
  86. package/routes/api/sources/files/get.js +100 -100
  87. package/routes/api/sources/post.js +36 -36
  88. package/routes/api/sources/stats/get.js +132 -132
  89. package/routes/api/subtitles/get.js +230 -230
  90. package/routes/api/transcode-sessions/fragment-far/post.js +60 -60
  91. package/routes/api/transcode-sessions/net-report/post.js +129 -129
  92. package/routes/api/transcode-sessions/post.js +160 -160
  93. package/routes/api/transcode-sessions/progress/get.js +27 -27
  94. package/routes/api/transcode-sessions/release/post.js +41 -41
  95. package/routes/api/transcode-sessions/seek/post.js +63 -63
  96. package/routes/health/get.js +13 -13
  97. package/routes/healthz/get.js +13 -13
  98. package/routes/stream/get.js +361 -361
  99. package/routes/transcode/audio-file/get.js +64 -64
  100. package/routes/transcode/audio-warm/get.js +84 -84
  101. package/routes/transcode/session-file/get.js +52 -52
  102. package/routes/transcode/variant-file/get.js +95 -95
  103. package/routes/transcode/variant-warm/get.js +84 -84
  104. package/scripts/render-run-graph.js +173 -173
  105. package/server.js +4 -0
  106. package/services/encode/CoverageMap.js +428 -428
  107. package/services/encode/CushionReport.js +345 -345
  108. package/services/encode/EncodeAdmission.js +332 -332
  109. package/services/encode/EncodeOrchestrator.js +1217 -1217
  110. package/services/encode/EncodePlan.js +1366 -1366
  111. package/services/encode/EncodeRun.js +776 -776
  112. package/services/encode/EncodeRuns.js +1229 -1229
  113. package/services/encode/Encoder.js +142 -142
  114. package/services/encode/EncoderSelection.js +38 -38
  115. package/services/encode/LocalObservations.js +359 -359
  116. package/services/encode/NvencEncoder.js +50 -50
  117. package/services/encode/OutputOpening.js +755 -755
  118. package/services/encode/OutputTimes.js +579 -579
  119. package/services/encode/QsvEncoder.js +58 -58
  120. package/services/encode/Renditions.js +2162 -2162
  121. package/services/encode/RunProgress.js +84 -84
  122. package/services/encode/SegmentDemand.js +95 -95
  123. package/services/encode/SoftwareEncoder.js +121 -121
  124. package/services/encode/V4l2m2mEncoder.js +53 -53
  125. package/services/encode/VaapiEncoder.js +66 -66
  126. package/services/encode/args.js +416 -416
  127. package/services/encode/available-share.js +89 -89
  128. package/services/encode/calibration.js +361 -361
  129. package/services/encode/contention.js +128 -128
  130. package/services/encode/decode-cost-fit.js +363 -363
  131. package/services/encode/encode-exit.js +114 -114
  132. package/services/encode/encode-run-state.js +551 -551
  133. package/services/encode/encoder-readings.js +45 -45
  134. package/services/encode/fingerprint.js +322 -322
  135. package/services/encode/host-load.js +174 -174
  136. package/services/encode/hwaccel.js +1894 -1894
  137. package/services/encode/index.js +9 -9
  138. package/services/encode/learned-median.js +90 -90
  139. package/services/encode/output/EncodedOutput.js +57 -57
  140. package/services/encode/output/Output.js +131 -131
  141. package/services/encode/output/OutputCatalog.js +414 -414
  142. package/services/encode/output/OutputSpec.js +390 -390
  143. package/services/encode/output/Timeline.js +308 -308
  144. package/services/encode/output/cut-grid.js +107 -107
  145. package/services/encode/output/index.js +20 -20
  146. package/services/encode/output/ladder.js +32 -32
  147. package/services/encode/output/playlists.js +284 -284
  148. package/services/encode/output/rates.js +125 -125
  149. package/services/encode/output-key-format.js +21 -21
  150. package/services/encode/quality/EncodeCost.js +1083 -1083
  151. package/services/encode/quality/HostLoad.js +525 -525
  152. package/services/encode/quality/HostTimings.js +249 -249
  153. package/services/encode/quality/OutputQualityState.js +28 -28
  154. package/services/encode/quality/QualityController.js +935 -935
  155. package/services/encode/quality/QualityOffer.js +416 -416
  156. package/services/encode/quality/drain-threat.js +47 -47
  157. package/services/encode/quality/link-budget.js +254 -254
  158. package/services/encode/quality/output-format.js +266 -266
  159. package/services/encode/quality/serving-output.js +216 -216
  160. package/services/encode/quality/visible-rung.js +57 -57
  161. package/services/encode/run-budget.js +97 -97
  162. package/services/encode/run-command.js +747 -747
  163. package/services/encode/run-costs.js +155 -155
  164. package/services/encode/run-inputs.js +42 -42
  165. package/services/encode/segment-formats/fmp4.js +437 -437
  166. package/services/encode/segment-formats/index.js +77 -77
  167. package/services/encode/segment-formats/init-compat.js +324 -324
  168. package/services/encode/segment-formats/mp4-boxes.js +311 -311
  169. package/services/encode/segment-formats/mpegts.js +134 -134
  170. package/services/encode/start-stop-cost.js +178 -178
  171. package/services/encode/throughput.js +97 -97
  172. package/services/encode/torrent-cost.js +88 -88
  173. package/services/identity/installation-id.js +38 -38
  174. package/services/media/ContainerOrchestrator.js +149 -149
  175. package/services/media/KeyframeTables.js +316 -316
  176. package/services/media/SourceFile.js +298 -298
  177. package/services/media/SubtitleCues.js +520 -520
  178. package/services/media/SubtitleOrchestrator.js +167 -167
  179. package/services/media/audio-inventory.js +245 -212
  180. package/services/media/container/AviContainer.js +266 -266
  181. package/services/media/container/Container.js +603 -603
  182. package/services/media/container/ContainerFactory.js +56 -56
  183. package/services/media/container/KeyframeTable.js +272 -272
  184. package/services/media/container/MatroskaContainer.js +1494 -1494
  185. package/services/media/container/Mp4Container.js +1244 -1244
  186. package/services/media/container/SubtitleFileContainer.js +322 -322
  187. package/services/media/container/ebml-reader.js +159 -159
  188. package/services/media/ffmpeg-banner.js +260 -260
  189. package/services/media/keyframe-probe.js +157 -157
  190. package/services/media/media-info-probe.js +93 -93
  191. package/services/media/playback-planner.js +131 -59
  192. package/services/media/tracks/AudioTrack.js +131 -131
  193. package/services/media/tracks/ContainerTrack.js +75 -75
  194. package/services/media/tracks/ImageSubtitleTrack.js +19 -19
  195. package/services/media/tracks/SubtitleTrack.js +38 -38
  196. package/services/media/tracks/TextSubtitleTrack.js +287 -287
  197. package/services/media/tracks/VideoTrack.js +33 -33
  198. package/services/media/tracks/index.js +15 -15
  199. package/services/media/tracks/language-detect.js +174 -174
  200. package/services/server/OutputLifecycle.js +418 -418
  201. package/services/server/SegmentServing.js +1146 -1146
  202. package/services/server/ViewerRequests.js +522 -522
  203. package/services/server/WaitLedger.js +142 -142
  204. package/services/server/controllers/PlaybackController.js +40 -36
  205. package/services/server/controllers/SubtitleController.js +109 -109
  206. package/services/server/http-responses.js +60 -60
  207. package/services/server/transcode-session-files.js +325 -325
  208. package/services/server/wire-outputs.js +983 -983
  209. package/services/storage/Diagnostics.js +202 -202
  210. package/services/storage/MachineBudget.js +235 -235
  211. package/services/storage/allowance.js +107 -107
  212. package/services/storage/core-dumps.js +106 -106
  213. package/services/storage/files/CompletedFiles.js +391 -391
  214. package/services/storage/files/piece-from-whole-file.js +118 -118
  215. package/services/storage/free.js +71 -71
  216. package/services/storage/keep.js +48 -48
  217. package/services/storage/machine-memory.js +56 -56
  218. package/services/storage/memory-report.js +575 -575
  219. package/services/storage/piece-store/piece-disk-store.js +516 -516
  220. package/services/storage/piece-store/piece-lru.js +495 -495
  221. package/services/storage/piece-store/shared-piece-store.js +1880 -1880
  222. package/services/storage/returns.js +103 -103
  223. package/services/storage/segment-store/SegmentStore.js +1196 -1196
  224. package/services/storage/wire.js +175 -175
  225. package/services/torrent/Contents.js +324 -324
  226. package/services/torrent/demand/DemandRegister.js +202 -202
  227. package/services/torrent/demand/Urgency.js +137 -137
  228. package/services/torrent/demand/Window.js +118 -118
  229. package/services/torrent/demand/index.js +8 -8
  230. package/services/torrent/demand/pieces.js +140 -140
  231. package/services/torrent/download/SwarmSelection.js +386 -386
  232. package/services/torrent/download/registry.js +102 -102
  233. package/services/torrent/download/withdraw-claim.js +80 -80
  234. package/services/torrent/files.js +455 -455
  235. package/services/torrent/naming.js +619 -619
  236. package/services/torrent/piece-store-of.js +29 -29
  237. package/services/torrent/supply-margin.js +262 -262
  238. package/services/torrent/torrent-pool.js +2 -2
  239. package/services/torrent/worker/background-fill.js +213 -213
  240. package/services/torrent/worker/channel.js +271 -271
  241. package/services/torrent/worker/fastest-wires.js +342 -342
  242. package/services/torrent/worker/handle-state.js +23 -23
  243. package/services/torrent/worker/held-bytes.js +155 -155
  244. package/services/torrent/worker/install-webrtc-shim.js +32 -32
  245. package/services/torrent/worker/piece-reader.js +1078 -1078
  246. package/services/torrent/worker/pool-adapter.js +1 -1
  247. package/services/torrent/worker/resume-warm.js +107 -107
  248. package/services/torrent/worker/webrtc-shim.js +219 -219
  249. package/services/transport/data-channel-handler.js +1356 -1356
  250. package/services/transport/delivery-probe.js +842 -842
  251. package/services/transport/health-collector.js +53 -53
  252. package/services/transport/nat-classifier.js +231 -231
  253. package/services/transport/packet-witness.js +808 -808
  254. package/services/transport/port-mapper.js +235 -235
  255. package/services/transport/registry-api.js +81 -81
  256. package/services/transport/tunnel-client.js +447 -447
  257. package/services/transport/usrsctp-state.js +175 -175
  258. package/services/transport/webrtc-manager.js +484 -484
  259. package/services/viewer/Assignments.js +340 -340
  260. package/services/viewer/PriorityMap.js +390 -390
  261. package/services/viewer/PriorityOrchestrator.js +325 -325
  262. package/services/viewer/Viewer.js +657 -657
  263. package/services/viewer/Viewers.js +368 -368
  264. package/services/viewer/active-output.js +37 -37
  265. package/services/viewer/audio-start.js +46 -46
  266. package/services/viewer/buffer-trend.js +81 -81
  267. package/services/viewer/choices.js +647 -647
  268. package/services/viewer/positions.js +126 -126
  269. package/services/viewer/report-intake.js +46 -46
  270. package/store/source-registry.js +71 -71
  271. package/test/asset-paths.test.js +28 -28
  272. package/test/audio-inventory.test.js +212 -176
  273. package/test/audio-track-bitrate.test.js +40 -40
  274. package/test/auto-quality-step.test.js +707 -707
  275. package/test/available-share.test.js +76 -76
  276. package/test/background-fill.test.js +190 -190
  277. package/test/client-log-file.test.js +110 -110
  278. package/test/completed-files.test.js +115 -115
  279. package/test/concurrent-cost.test.js +148 -148
  280. package/test/container-media-info.test.js +228 -228
  281. package/test/contention.test.js +76 -76
  282. package/test/core-dumps.test.js +57 -57
  283. package/test/coverage-follows-the-disk.test.js +228 -228
  284. package/test/coverage-map.test.js +195 -195
  285. package/test/cue-push-carries-what-the-walk-found.test.js +58 -58
  286. package/test/cut-times-timeline.test.js +65 -65
  287. package/test/cuts-follow-published-grid.test.js +109 -109
  288. package/test/data-channel-frame.test.js +63 -63
  289. package/test/dead-torrent-handle.test.js +52 -52
  290. package/test/declared-rates.test.js +116 -116
  291. package/test/declared-tracks.test.js +35 -35
  292. package/test/decode-cost-fit.test.js +202 -202
  293. package/test/decode-cost.test.js +462 -462
  294. package/test/delivery-probe.test.js +393 -393
  295. package/test/demand-register.test.js +206 -206
  296. package/test/dht-bootstrap.test.js +72 -72
  297. package/test/drain-threat.test.js +65 -65
  298. package/test/duplicate-add.test.js +48 -48
  299. package/test/encode-admission.test.js +146 -146
  300. package/test/encode-cost.test.js +283 -283
  301. package/test/encode-exit.test.js +111 -111
  302. package/test/encode-orchestrator.test.js +419 -419
  303. package/test/encode-plan-viewers.test.js +718 -718
  304. package/test/encode-plan.test.js +545 -545
  305. package/test/encode-run-state.test.js +314 -314
  306. package/test/encode-run.test.js +161 -161
  307. package/test/encode-slope.test.js +46 -46
  308. package/test/encoded-output.test.js +39 -39
  309. package/test/encoder-kinds.test.js +122 -122
  310. package/test/encoder-readings.test.js +46 -46
  311. package/test/every-claimant-is-told-its-share.test.js +152 -152
  312. package/test/failed-encoder-still-serves-what-exists.test.js +354 -354
  313. package/test/failed-start-breaker.test.js +202 -202
  314. package/test/fastest-wires.test.js +143 -143
  315. package/test/ffmpeg-stream-counts.test.js +78 -78
  316. package/test/file-edges.test.js +191 -191
  317. package/test/fmp4-self-contained.test.js +80 -80
  318. package/test/fmp4-timestamps.test.js +140 -140
  319. package/test/fmp4-track-completeness.test.js +41 -41
  320. package/test/format-keyed-outputs.test.js +78 -78
  321. package/test/health-metrics.test.js +38 -38
  322. package/test/held-request-width.test.js +157 -157
  323. package/test/helpers/encode-run.js +136 -136
  324. package/test/helpers/held-file.js +29 -29
  325. package/test/helpers/manager.js +57 -57
  326. package/test/helpers/output-spec.js +63 -63
  327. package/test/host-load.test.js +68 -68
  328. package/test/index-accuracy-is-about-the-file.test.js +128 -128
  329. package/test/init-compat.test.js +219 -219
  330. package/test/input-lost-quiets-the-plan.test.js +261 -261
  331. package/test/input-unavailable.test.js +48 -48
  332. package/test/installation-id.test.js +39 -39
  333. package/test/keeping-period.test.js +83 -83
  334. package/test/keyframe-index-accuracy.test.js +188 -188
  335. package/test/keyframe-table-budget.test.js +102 -102
  336. package/test/keyframes-belong-to-the-file.test.js +249 -249
  337. package/test/learned-median.test.js +37 -37
  338. package/test/letting-go-asks-who-is-watching.test.js +191 -191
  339. package/test/limit-row-by-area.test.js +60 -60
  340. package/test/link-budget.test.js +111 -111
  341. package/test/link-probe.test.js +56 -56
  342. package/test/local-encoding-data.test.js +273 -273
  343. package/test/logger-repeats.test.js +134 -134
  344. package/test/look-ahead-coverage.test.js +56 -56
  345. package/test/manager-surface.test.js +292 -292
  346. package/test/matroska-cues-track.test.js +192 -192
  347. package/test/memory-budget.test.js +390 -390
  348. package/test/move-cost.test.js +216 -216
  349. package/test/mp4-subtitles.test.js +173 -173
  350. package/test/one-authority.test.js +285 -285
  351. package/test/one-budget.test.js +149 -149
  352. package/test/one-output-one-session.test.js +204 -204
  353. package/test/one-resource-divided.test.js +185 -185
  354. package/test/one-serving-output-per-height.test.js +458 -458
  355. package/test/orchestrator-wired.test.js +206 -206
  356. package/test/output-catalog.test.js +39 -39
  357. package/test/output-shape.test.js +21 -21
  358. package/test/output-spec.test.js +251 -251
  359. package/test/output-speed.test.js +88 -88
  360. package/test/outputs-being-prepared.test.js +40 -40
  361. package/test/packet-witness-ring.test.js +236 -236
  362. package/test/packet-witness.test.js +148 -148
  363. package/test/piece-disk-store.test.js +381 -381
  364. package/test/piece-from-whole-file.test.js +129 -129
  365. package/test/piece-lru.test.js +319 -319
  366. package/test/piece-reader.test.js +162 -162
  367. package/test/piece-selection-offset.test.js +85 -85
  368. package/test/piece-store-eviction.test.js +560 -560
  369. package/test/piece-store-never-refuses.test.js +153 -153
  370. package/test/piece-store-reservations.test.js +318 -318
  371. package/test/piece-store-slow-disk.test.js +159 -159
  372. package/test/piece-tail.test.js +97 -97
  373. package/test/plan-host-timings.test.js +68 -68
  374. package/test/plan-is-steady.test.js +225 -225
  375. package/test/priority-map-download.test.js +174 -174
  376. package/test/priority-map-emptied.test.js +204 -204
  377. package/test/priority-map-per-output.test.js +215 -215
  378. package/test/priority-map.test.js +163 -163
  379. package/test/probe-wedge-certainty.test.js +60 -60
  380. package/test/produced-copy-choice.test.js +213 -213
  381. package/test/published-timeline.test.js +83 -83
  382. package/test/quality-offer.test.js +162 -162
  383. package/test/quality-variants.test.js +1229 -1229
  384. package/test/rate-control-in-the-output.test.js +180 -180
  385. package/test/read-survives-withdrawal.test.js +164 -164
  386. package/test/read-window.test.js +365 -365
  387. package/test/resume-warm.test.js +39 -39
  388. package/test/run-graph-drift.test.js +37 -37
  389. package/test/run-inputs.test.js +80 -80
  390. package/test/run-intervals.test.js +100 -100
  391. package/test/run-position-follows-published-grid.test.js +89 -89
  392. package/test/run-progress.test.js +65 -65
  393. package/test/same-height-switch.test.js +664 -664
  394. package/test/seek-frees-the-viewers-requests.test.js +217 -217
  395. package/test/seek-landing.test.js +109 -109
  396. package/test/seek-target-not-superseded.test.js +195 -195
  397. package/test/segment-closed-on-both-branches.test.js +96 -96
  398. package/test/segment-cut-times.test.js +63 -63
  399. package/test/segment-demand.test.js +80 -80
  400. package/test/segment-hold-supersede.test.js +91 -91
  401. package/test/segment-judged-by-its-own-header.test.js +221 -221
  402. package/test/segment-serve-wiring.test.js +475 -475
  403. package/test/segment-store-eviction.test.js +247 -247
  404. package/test/segment-store.test.js +323 -323
  405. package/test/segment-true-start.test.js +86 -86
  406. package/test/segments-are-shared.test.js +176 -176
  407. package/test/send-chunk-bytes.test.js +86 -86
  408. package/test/serving-existing-output.test.js +102 -102
  409. package/test/serving-output.test.js +102 -102
  410. package/test/shared-piece-store.test.js +310 -310
  411. package/test/sidecar-naming.test.js +142 -142
  412. package/test/source-file.test.js +133 -133
  413. package/test/source-files-route.test.js +103 -103
  414. package/test/source-registry.test.js +18 -18
  415. package/test/split-a-stretch.test.js +139 -139
  416. package/test/stale-request-after-seek.test.js +158 -158
  417. package/test/startup-readings.test.js +124 -124
  418. package/test/stream-route.test.js +182 -182
  419. package/test/subtitle-cue-framing.test.js +200 -200
  420. package/test/subtitle-cue-source.test.js +102 -102
  421. package/test/subtitle-cue-walk.test.js +370 -370
  422. package/test/subtitle-cursor.test.js +63 -63
  423. package/test/subtitle-defaults.test.js +118 -118
  424. package/test/subtitle-language.test.js +252 -252
  425. package/test/subtitle-track-numbering.test.js +386 -386
  426. package/test/supply-margin.test.js +177 -177
  427. package/test/swarm-follows-readers.test.js +174 -174
  428. package/test/swarm-reach.test.js +130 -130
  429. package/test/swarm-selection.test.js +220 -220
  430. package/test/tail-duplication.test.js +167 -167
  431. package/test/the-channel-has-both-ends.test.js +91 -91
  432. package/test/the-torrent-thread-serves-bytes.test.js +107 -107
  433. package/test/thread-pool.test.js +53 -53
  434. package/test/timeline.test.js +84 -84
  435. package/test/torrent-contents.test.js +235 -235
  436. package/test/torrent-cost.test.js +71 -71
  437. package/test/torrent-files.test.js +221 -221
  438. package/test/torrent-naming.test.js +255 -255
  439. package/test/tracks-begin-together.test.js +209 -209
  440. package/test/tunnel-renewal.test.js +167 -167
  441. package/test/two-viewers-one-picture.test.js +407 -407
  442. package/test/upload-hurry.test.js +186 -186
  443. package/test/usrsctp-state.test.js +131 -131
  444. package/test/video-facts.test.js +102 -102
  445. package/test/viewer-assignments.test.js +143 -143
  446. package/test/viewer-outputs.test.js +285 -285
  447. package/test/viewer-presence.test.js +116 -116
  448. package/test/viewer-requests-admission.test.js +89 -89
  449. package/test/viewer-states.test.js +189 -189
  450. package/test/viewer-subtitle-subscription.test.js +96 -96
  451. package/test/viewer.test.js +139 -139
  452. package/test/viewing-generations.test.js +390 -390
  453. package/test/visible-rung.test.js +37 -37
  454. package/test/wait-ledger.test.js +100 -100
  455. package/test/webrtc-shim.test.js +149 -149
  456. package/test/wedge-certainty.test.js +131 -131
  457. package/test/withdraw-piece-claim.test.js +212 -212
  458. package/test/worker-channel.test.js +158 -158
  459. package/utils/client-log-file.js +151 -151
  460. package/utils/logger.js +312 -312
  461. package/utils/parse-range.js +25 -25
  462. package/utils/perf.js +59 -59
  463. package/{services/torrent → utils}/torrent-source-key.js +38 -38
@@ -1,1229 +1,1229 @@
1
- /**
2
- * The encoders of this proxy, from the plan's decision to the end of the process.
3
- *
4
- * The plan (`EncodePlan`, through `EncodeOrchestrator`) decides where a run goes;
5
- * this builds it with the arguments its output needs, follows what it produces,
6
- * says how far it got, and accounts for how it ended. Nothing here decides where
7
- * an encoder should be.
8
- */
9
-
10
- import path from "node:path";
11
- import { spawn } from "node:child_process";
12
- import { contentionPenalty } from "./contention.js";
13
- import { ENCODE_RUN_STATE, liveRunsOf, runStateOf, wireState } from "./encode-run-state.js";
14
- import { ENCODE_EXIT } from "./encode-exit.js";
15
- import { EncodeRun } from "./EncodeRun.js";
16
- import { computeOutputDimensions } from "./args.js";
17
- import { buildRunCommand, trueStartOf } from "./run-command.js";
18
-
19
- /**
20
- * @typedef {Object} SegmentFiles
21
- * @property {(address: string) => string} pathFor
22
- * @property {(address: string) => Buffer | null} initOf
23
- * @property {(address: string) => string} directoryFor
24
- * @property {(address: string, makingName: string, format: object) => string | null} publish
25
- */
26
- // How far the accounting of a backward restart looks for work about to be done
27
- // twice. It runs on the restart path and a session an hour in has thousands of
28
- // segments; the figure is for a comparison, not an inventory.
29
- const BACKWARD_RESTART_SCAN_SEGMENTS = 300;
30
- const PROGRESS_LOG_INTERVAL_MS = 5_000;
31
- /**
32
- * Format a seconds value as `HH:MM:SS`, or `"n/a"` if not finite.
33
- *
34
- * @param {number} seconds
35
- * @returns {string}
36
- */
37
- export function formatSeconds(seconds) {
38
- if (!Number.isFinite(seconds) || seconds < 0) {
39
- return "n/a";
40
- }
41
- const total = Math.floor(seconds);
42
- const hours = Math.floor(total / 3600);
43
- const minutes = Math.floor((total % 3600) / 60);
44
- const rest = total % 60;
45
- return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(rest).padStart(2, "0")}`;
46
- }
47
- /**
48
- * Where this session's encoding begins: the earliest number any live run of it
49
- * was given.
50
- *
51
- * @param {object[] | Set<object>} runs
52
- * @returns {number | null}
53
- */
54
- export function earliestRunStart(runs) {
55
- const live = liveRunsOf(runs);
56
- return live.length > 0 ? live[0].from : null;
57
- }
58
- /**
59
- * The live run of this session that was given this number, if any.
60
- *
61
- * A run with an explicit stretch owns the whole of it. One WITHOUT an end owns
62
- * only as far as it has actually got: claiming the rest of the film would make
63
- * it the owner of every number in front of it, including ones another run was
64
- * expressly given.
65
- *
66
- * @param {object[] | Set<object>} runs
67
- * @param {number} index
68
- * @returns {object | null}
69
- */
70
- function ownRunMaking(runs, index) {
71
- let answer = null;
72
- for (const run of liveRunsOf(runs)) {
73
- if (run.from > index) {
74
- break; // Ordered by start, so nothing further can hold this number.
75
- }
76
- if (Number.isInteger(run.to) && run.to >= run.from && index > run.to) {
77
- continue; // Its stretch ends before this number.
78
- }
79
- answer = run;
80
- }
81
- return answer;
82
- }
83
-
84
- /**
85
- * Whether an encoder run died because its INPUT went away, rather than because
86
- * of anything about the encode itself.
87
- *
88
- * These are the messages the read path and ffmpeg's HTTP client produce when
89
- * the torrent is gone, being re-added, or has no data for the range yet — all
90
- * of them temporary by nature: the source can be added again and the pieces
91
- * fetched again.
92
- *
93
- * @param {string} message
94
- * @returns {boolean}
95
- */
96
- export function isInputUnavailable(message) {
97
- const text = typeof message === "string" ? message : "";
98
- return (
99
- /Error reading HTTP response/i.test(text) ||
100
- /not found in (?:magnet|torrent):/i.test(text) ||
101
- /Unknown source/i.test(text) ||
102
- /is gone and cannot be re-added/i.test(text) ||
103
- /Read error at pos/i.test(text) ||
104
- /Server returned 5\d\d/i.test(text) ||
105
- /Input\/output error/i.test(text) ||
106
- /Connection reset by peer/i.test(text) ||
107
- /End of file/i.test(text)
108
- );
109
- }
110
-
111
- /**
112
- * The ffmpeg command as one readable line.
113
- *
114
- * Everything is shown as passed except the list of cut times, which is one
115
- * value per segment — 830 of them on a two-hour film, about 7 KB of log for a
116
- * single run, repeated on every restart. The count and the two ends say
117
- * everything the list is ever consulted for: whether cutting was explicit at
118
- * all, how far it reaches, and where it starts.
119
- *
120
- * @param {string[]} args
121
- * @returns {string}
122
- */
123
- export function describeFfmpegArgs(args) {
124
- const parts = [];
125
- for (let index = 0; index < args.length; index += 1) {
126
- const value = args[index];
127
- // Both take the same list, and a keyframe-grid variant passes it twice.
128
- // `-force_key_frames` also takes an expression, which is short and is left
129
- // alone — only a list is folded.
130
- if (
131
- (value === "-segment_times" || value === "-force_key_frames") &&
132
- typeof args[index + 1] === "string" &&
133
- args[index + 1].includes(",")
134
- ) {
135
- const times = args[index + 1].split(",");
136
- parts.push(value, `<${times.length} cuts ${times[0]}..${times[times.length - 1]}>`);
137
- index += 1;
138
- continue;
139
- }
140
- parts.push(value);
141
- }
142
- return parts.join(" ");
143
- }
144
-
145
- export class EncodeRuns {
146
- #inputState = new WeakMap();
147
- /** What this reads and asks of the rest of the proxy, and nothing else. @type {object} */
148
- #host;
149
-
150
- /**
151
- * Whether a re-decision of what encoders should exist is already queued for
152
- * the end of this turn. See `planEncodersSoon`.
153
- * @type {boolean}
154
- */
155
- #planScheduled = false;
156
-
157
- /**
158
- * @param {object} host - `logger`, `viewerSecondsOn`, `noteRunStarting`, `inputOf`, `producedNumbers`, `servesAudioSeparately`, `disposeSession`, `contentionPenalties`, `encodeCost`, `encodeOrchestrator`, `encoders`, `ffmpegBin`, `outputTimes`, `outputs`, `priority`, `segmentDurationSec`, `segmentFiles`, `videoEncoder`
159
- * @param {SegmentFiles} host.segmentFiles - The storage operations used to
160
- * write and inspect output files.
161
- */
162
- constructor(host) {
163
- this.#host = host;
164
- }
165
-
166
- #inputStateFor(output) {
167
- let state = this.#inputState.get(output);
168
- if (!state) {
169
- state = {
170
- readWindowBytes: 0,
171
- inputRetryCount: 0,
172
- backwardRestarts: { count: 0, segmentsBack: 0, worstBack: 0, remade: 0 },
173
- firstWantedAt: new Map(),
174
- /** Bytes the swarm has delivered to this output's own input reads. */
175
- inputBytes: 0,
176
- /** The mismatch between the held init and the run's size last said, so it is said once. */
177
- initSizeSaid: ""
178
- };
179
- this.#inputState.set(output, state);
180
- }
181
- return state;
182
- }
183
-
184
- setReadWindow(output, bytes) {
185
- this.#inputStateFor(output).readWindowBytes = Number.isFinite(bytes) ? bytes : 0;
186
- }
187
-
188
- readWindowFor(output) {
189
- return this.#inputStateFor(output).readWindowBytes;
190
- }
191
-
192
- noteWanted(output, index, at = Date.now()) {
193
- const wanted = this.#inputStateFor(output).firstWantedAt;
194
- if (!wanted.has(index)) wanted.set(index, at);
195
- }
196
-
197
- resetInputRetry(output) {
198
- this.#inputStateFor(output).inputRetryCount = 0;
199
- }
200
-
201
- /**
202
- * Count bytes the swarm has delivered to one output's own input read.
203
- *
204
- * Called by the `/stream` route for every fragment it writes to an encoder.
205
- * Cheap on purpose — one addition, no clock, no log — because it runs per
206
- * fragment on the path that feeds ffmpeg.
207
- *
208
- * @param {string} outputId
209
- * @param {number} bytes
210
- * @returns {void}
211
- */
212
- noteInputBytes(outputId, bytes) {
213
- if (!outputId || !(bytes > 0)) {
214
- return;
215
- }
216
- const output = this.#host.outputs.get(outputId);
217
- if (output) {
218
- this.#inputStateFor(output).inputBytes += bytes;
219
- }
220
- }
221
-
222
- /**
223
- * @param {object} output
224
- * @returns {number}
225
- */
226
- inputBytesOf(output) {
227
- return output ? this.#inputStateFor(output).inputBytes : 0;
228
- }
229
-
230
- runsOf(output) {
231
- return output ? this.#host.encodeOrchestrator.runsOn(output.outputKey) : [];
232
- }
233
-
234
- /**
235
- * Where the earliest live encoder of this output began, or null with none.
236
- *
237
- * @param {object} output
238
- * @returns {number | null}
239
- */
240
- earliestStartOf(output) {
241
- return earliestRunStart(this.runsOf(output));
242
- }
243
-
244
- isLive(output) {
245
- return Boolean(output && this.#host.outputs.get(output.id) === output);
246
- }
247
-
248
- liveRunsOf(output) {
249
- return liveRunsOf(this.runsOf(output));
250
- }
251
-
252
- runStateOf(output) {
253
- return output ? this.#host.encodeOrchestrator.stateOf(output.outputKey) : runStateOf([]);
254
- }
255
-
256
- lastErrorOf(output) {
257
- return output ? this.#host.encodeOrchestrator.errorOf(output.outputKey) : "";
258
- }
259
-
260
- /**
261
- * Whether this output's encoding has failed for good.
262
- *
263
- * @param {object} output
264
- * @returns {boolean}
265
- */
266
- hasFailed(output) {
267
- return this.runStateOf(output) === ENCODE_RUN_STATE.ENDED_FAILED;
268
- }
269
-
270
- /**
271
- * Whether this output's input went away and is being fetched again.
272
- *
273
- * @param {object} output
274
- * @returns {boolean}
275
- */
276
- isWaitingForInput(output) {
277
- return this.runStateOf(output) === ENCODE_RUN_STATE.RETRY_WAIT;
278
- }
279
-
280
- /**
281
- * The run state in the words the browser is told.
282
- *
283
- * @param {object} output
284
- * @returns {string}
285
- */
286
- wireStateOf(output) {
287
- return wireState(this.runStateOf(output));
288
- }
289
-
290
- /**
291
- * What went wrong, when this output's encoding has failed; empty otherwise.
292
- *
293
- * @param {object} output
294
- * @returns {string}
295
- */
296
- failureOf(output) {
297
- return this.hasFailed(output) ? this.lastErrorOf(output) : "";
298
- }
299
-
300
- progressOf(output, index = null) {
301
- if (!output) {
302
- return null;
303
- }
304
- const at = Number.isInteger(index)
305
- ? index
306
- : this.#host.outputTimes.segmentIndexForTime(output, this.#host.viewerSecondsOn(output));
307
- const progress = this.#host.encodeOrchestrator.progressOf(output.outputKey, at);
308
- if (progress) {
309
- return progress;
310
- }
311
- const start = this.#host.viewerSecondsOn(output);
312
- const total = Number(output.file?.durationSeconds);
313
- return {
314
- processedSeconds: start,
315
- startPositionSeconds: start,
316
- totalSeconds: Number.isFinite(total) && total > 0 ? total : null,
317
- percent: 0,
318
- remainingSeconds: Number.isFinite(total) && total > 0 ? Math.max(0, total - start) : null,
319
- speed: "",
320
- updatedAt: this.#host.outputs.startedAt(output) ?? Date.now()
321
- };
322
- }
323
-
324
- planEncodersNow() {
325
- const byOutput = this.#outputsWithSessions();
326
- for (const [address, sessions] of byOutput) {
327
- // From the TIMELINE, which is where how a file is cut has lived since
328
- // 2.76.0. Read off the session it left, this was `undefined` on every
329
- // session ever made: the map then held no length, and the walk that
330
- // gives a run its end ran to MAX_SAFE_INTEGER — the main thread spun at
331
- // 100% and the proxy answered nothing, measured on the addon host
332
- // 2026-09-05 with the stack read out of the live process.
333
- const segmentCount = Number(sessions[0].timeline?.segmentCount) || 0;
334
- if (segmentCount > 0) {
335
- this.#host.encodeOrchestrator.setSegmentCount(address, segmentCount);
336
- }
337
- }
338
- // THE MAP IS BUILT ONCE, IN ITS OWN LAYER, AND BOTH SIDES READ THAT ONE.
339
- //
340
- // It used to be built twice: once here, per viewer, converted and stated to
341
- // the encoding as a window each, and once again inside `publishFor` for the
342
- // downloading. Two answers to one question, and the encoding's copy carried
343
- // the viewer's NAME as the key of a claim — against the rule that the
344
- // encoding and the viewer are not connected at all.
345
- this.#host.priority.publishFor({
346
- sessionGroups: byOutput.values(),
347
- });
348
- // FROM THE VIEWERS OF THAT OUTPUT, not from the viewers of the film.
349
- //
350
- // Both scopes are right for what asks them: the swarm is asked for bytes of
351
- // a FILE, which every output of it reads, and encoders are placed per
352
- // OUTPUT, which a person watching 480p wants nothing of at 1080p. Handed the
353
- // film's map, the plan wanted an encoder on every output of it, and what
354
- // stopped the unwatched ones was this class killing them by its own
355
- // judgement — while the viewer's own move announced itself and had the plan
356
- // start them again.
357
- //
358
- // Converted into each output's own numbering, because two outputs of one
359
- // film are cut independently and the same second is a different number in
360
- // each: 454 pieces against 401 on the field file.
361
- for (const [address, sessions] of byOutput) {
362
- const timeline = sessions[0].timeline;
363
- this.#host.encodeOrchestrator.notePriorityMap(
364
- address,
365
- timeline?.inSegments?.(
366
- this.#host.priority.mapForOutput(address),
367
- Number(timeline?.segmentCount) || 0
368
- ) ?? []
369
- );
370
- }
371
- this.#host.encodeOrchestrator.reconcile();
372
- }
373
-
374
- /**
375
- * Re-decide what encoders should exist, once, after the change that is being
376
- * made now.
377
- *
378
- * COALESCED, NOT DELAYED. One turn of the event loop may carry several
379
- * changes — a viewer arrives and is placed and states their soundtrack — and
380
- * each of them is a reason to re-decide, while re-deciding three times in a
381
- * row would give the same answer three times and could act on a half-built
382
- * state. So the decision is taken once, after the current turn, and no
383
- * interval is involved: there is nothing to choose and nothing to tune.
384
- *
385
- * @returns {void}
386
- */
387
- planEncodersSoon() {
388
- if (this.#planScheduled) {
389
- return;
390
- }
391
- this.#planScheduled = true;
392
- queueMicrotask(() => {
393
- this.#planScheduled = false;
394
- try {
395
- this.planEncodersNow();
396
- } catch (error) {
397
- const message = error instanceof Error ? error.message : String(error);
398
- this.#host.logger.warn(`transcode could not re-plan encoders: ${message}`);
399
- }
400
- });
401
- }
402
-
403
- /**
404
- * The live sessions grouped by the output they produce.
405
- *
406
- * @returns {Map<string, HlsSession[]>}
407
- */
408
- #outputsWithSessions() {
409
- /** @type {Map<string, HlsSession[]>} */
410
- const byOutput = new Map();
411
- for (const session of this.#host.outputs.values()) {
412
- const address = session.outputKey ?? "";
413
- if (!address) {
414
- continue;
415
- }
416
- byOutput.set(address, [...(byOutput.get(address) ?? []), session]);
417
- }
418
- return byOutput;
419
- }
420
-
421
- forgetEncodingOfGone(output) {
422
- if (this.#host.outputs.outputsOn(output.outputKey).length === 0) {
423
- this.#host.encodeOrchestrator.forgetOutput(output.outputKey);
424
- }
425
- }
426
-
427
- /**
428
- * Make an encoder for this output, beginning at this segment.
429
- *
430
- * Another run of the SAME session, not another session. A run at another
431
- * position is not a different output — the material, the tracks, the grid and
432
- * the box are all the same, and only where it begins differs, which changes
433
- * no byte of what is produced. Both write into the output's one directory and
434
- * either viewer is served whatever either has made.
435
- *
436
- * @param {string} address
437
- * @param {number} from
438
- * @param {number} to
439
- * @param {string} because - The plan's own words for why it placed this one.
440
- * @returns {object | null}
441
- */
442
- makeRunAt(address, from, to, because) {
443
- let base = null;
444
- for (const session of this.#host.outputs.values()) {
445
- if (session.outputKey === address) {
446
- base = session;
447
- break;
448
- }
449
- }
450
- if (!base) {
451
- return null;
452
- }
453
- // A position that has already failed to start, this many times running,
454
- // will fail again: nothing about it has changed between the attempts, which
455
- // is exactly why the attempts keep taking the same fraction of a second and
456
- // ending the same way.
457
- //
458
- // The plan is arithmetic over coverage and demand, and neither of them
459
- // knows that a process refused to start — so without asking here, the plan
460
- // commands the same start, the run ends, the ended stretch goes back to the
461
- // map, and the plan commands it again. Measured 2026-09-05 with ffmpeg
462
- // absent: fifty passes of the plan in the time a probe took to notice, as
463
- // fast as spawning could fail. The five-second timer this decision used to
464
- // sit on hid that, at the price of hiding it in the field too — the loop
465
- // seen there ran for sixteen minutes and read as "a restart every five
466
- // seconds".
467
- //
468
- // A DIFFERENT position is unaffected and gets its own budget, and the count
469
- // resets the moment a run at this one does real work.
470
- if (!this.#host.encodeOrchestrator.mayStartAt(address, from)) {
471
- return null;
472
- }
473
- // The encoder is built and handed back in this same call. Nothing here
474
- // waits: the one thing this path used to wait for was the death of the run
475
- // it was replacing, and that killing is gone. Answering with nothing while
476
- // the encoder was built behind the answer is what let the same stretch be
477
- // started over and over — 684 starts in 482 seconds of field 2026-09-05.
478
- try {
479
- return this.#startEncodeRun(base, from, because, { to });
480
- } catch (error) {
481
- const message = error instanceof Error ? error.message : String(error);
482
- this.#host.logger.warn(`transcode could not start a run at #${from} of ${address}: ${message}`);
483
- return null;
484
- }
485
- }
486
-
487
- /**
488
- * How many encoders this machine can afford on one output at once.
489
- *
490
- * Measured rather than chosen, and it is the same arithmetic that decides
491
- * which quality steps are offered: what a second job costs here is not
492
- * assumed to be nothing. Field 2026-09-03 on the addon host, `testsrc2`
493
- * through libx264 `ultrafast` — at 854x480 one run makes 7.12x and two make
494
- * 4.20x and 4.16x, so both stay far above realtime and a second is
495
- * affordable; at 1920x1080 one makes 1.96x and two make 0.99x and 0.98x, so
496
- * the machine is full at one.
497
- *
498
- * @param {string} address
499
- * @returns {number}
500
- */
501
- maxRunsForOutput(address) {
502
- const alone = this.#host.encodeCost.speedForOutput(address);
503
- if (!(alone > 0)) {
504
- // The host measured nothing at all, which is a broken startup rather than
505
- // a state to plan around. One encoder is what it keeps.
506
- return 1;
507
- }
508
- let affordable = 1;
509
- for (;;) {
510
- const { penalty, measured, from } = contentionPenalty(affordable, this.#host.contentionPenalties);
511
- // An UNMEASURED penalty is 1, and that is the honest answer to "what does
512
- // a second job cost" only in the sense that nothing has been measured —
513
- // it is not a statement that a second job is free. Measured on the addon
514
- // host it is 1.70x at 854x480 and 1.98x at 1920x1080, so taking 1 would
515
- // let a machine that cannot hold two runs start two. Where nothing has
516
- // been measured, one is what it has.
517
- //
518
- // `from` is the concurrency the reading came from, and past the measured
519
- // range `contentionPenalty` HOLDS the largest reading rather than continue
520
- // a curve two points cannot describe. Held is the right answer to "what
521
- // does this cost", and the wrong one to "may I start another": it would
522
- // price a fifth encoder at what a second was measured to cost, and a fast
523
- // host would keep dividing until some chosen ceiling stopped it. So the
524
- // ladder stops where the measurements stop — the bound on how many
525
- // encoders may run is measured, like the budget itself, and there is no
526
- // constant here to overrule it.
527
- if (!measured || from !== affordable || !(alone / penalty >= 1)) {
528
- break;
529
- }
530
- affordable += 1;
531
- }
532
- return affordable;
533
- }
534
-
535
- /**
536
- * A segment this run made has just been served.
537
- *
538
- * The one event whose raising is guarded by the state, and the guard is a
539
- * READ of the state rather than a second copy of it: "something has been
540
- * produced" is a level, not an edge, so without the guard every served
541
- * segment would raise it and the log would fill with refusals.
542
- *
543
- * Whose file it is decides the rest, and the directory answers that outright:
544
- * runs write into one each, and serving searches them newest-first, so a
545
- * segment left by an EARLIER run is served routinely. Comparing indices
546
- * instead would have been fooled by the ordinary case of a backward seek —
547
- * the new run starts at #10, the old one left #50 on disk, and #50 is above
548
- * the new run's start index while saying nothing about it.
549
- *
550
- * @param {HlsSession} session
551
- * @param {string} filePath - Where the served segment was actually found.
552
- * @returns {void}
553
- */
554
- noteRunProducedSegment(session, filePath) {
555
- if (typeof filePath !== "string" || path.dirname(filePath) !== this.#host.segmentFiles.pathFor(session.outputKey ?? "")) {
556
- return;
557
- }
558
- const index = session.segmentFormat.segmentIndexFromName(path.basename(filePath));
559
- if (index === null) {
560
- return;
561
- }
562
- // Whichever run was given this number. An output has as many runs as the
563
- // machine affords, and a segment moves the one it belongs to out of
564
- // starting — not whichever happens to be listed first.
565
- ownRunMaking(this.runsOf(session), index)?.noteProduced(index);
566
- }
567
-
568
- /**
569
- * Say so when a run is about to encode a picture the init segment already in
570
- * the player's hands does not describe.
571
- *
572
- * This is the whole class of fault named in one line, and it exists because
573
- * the fault is otherwise SILENT: no layer reports an error, the encoder is
574
- * healthy, segments are served in milliseconds, and what the viewer gets is
575
- * either a band of macroblock garbage or a picture that never appears. The
576
- * shape is the one 2.48.0 uses for the TIME a run begins at — a run states
577
- * where it really landed, and a disagreement with what was published is
578
- * named rather than left to be inferred from a browser's own reading.
579
- *
580
- * The size the init describes is read from the init's own bytes, not taken
581
- * from our record of what the encoder was told, because those two disagreeing
582
- * IS the fault. The size the run will produce is computed by the same
583
- * arithmetic the scale filter performs, so a source smaller than the target
584
- * box is not reported as a disagreement.
585
- *
586
- * Said once per distinct pair of sizes: a run repeated at the same wrong size
587
- * has nothing further to say.
588
- *
589
- * @param {HlsSession} session
590
- * @returns {void}
591
- */
592
- #warnIfRunLeavesTheInitBehind(session) {
593
- const initBytes = this.#host.segmentFiles.initOf(session.outputKey ?? "");
594
- if (!session.spec.transcodesVideo || !initBytes || initBytes.length === 0) {
595
- return; // nothing served yet, or nothing being encoded
596
- }
597
- const format = session.segmentFormat;
598
- if (typeof format?.initVideoSize !== "function") {
599
- return; // a self-describing container (MPEG-TS) cannot have this fault
600
- }
601
- const described = format.initVideoSize(initBytes);
602
- if (!described) {
603
- return;
604
- }
605
- if (!(session.output.encodeWidth > 0) || !(session.output.encodeHeight > 0)) {
606
- return; // the run keeps the encoder's own default box; nothing was told
607
- }
608
- const producing = computeOutputDimensions(
609
- session.output.encodeWidth,
610
- session.output.encodeHeight,
611
- session.file.width,
612
- session.file.height
613
- );
614
- if (!producing) {
615
- return; // the source size is unknown, so nothing can be predicted
616
- }
617
- if (described.width === producing.w && described.height === producing.h) {
618
- return;
619
- }
620
- const said = `${described.width}x${described.height}->${producing.w}x${producing.h}`;
621
- const inputState = this.#inputStateFor(session);
622
- if (inputState.initSizeSaid === said) {
623
- return;
624
- }
625
- inputState.initSizeSaid = said;
626
- this.#host.logger.warn(
627
- `transcode ${session.id} is about to encode ${producing.w}x${producing.h} while the init segment ` +
628
- `the player holds describes ${described.width}x${described.height} "${session.file.name}" — ` +
629
- `every fragment of this run will be decoded against parameter sets for a picture that is not ` +
630
- `being made. A change of size is a change of variant; nothing here should have moved it.`
631
- );
632
- }
633
-
634
- /**
635
- * How far a run starting here may work before it meets somebody else's
636
- * material — read off the ONE coverage map, never worked out again here.
637
- *
638
- * This replaced `planRunInterval`, which was a second authority over the same
639
- * question and answered it by different rules: it walked the whole track for
640
- * the first free number, MOVED the start there itself, and counted every live
641
- * run as claiming up to `head + look-ahead`. Measured in the field
642
- * 2026-09-05: the plan commanded a start at #46, this moved it to #78 — the
643
- * "run moved forward from #46 to #78" line — and the plan, seeing a run
644
- * outside the window it had asked for, killed it as unwanted and commanded
645
- * the same start again, 350-700ms per cycle, dozens of times, the viewer's
646
- * picture stopped for 125 seconds.
647
- *
648
- * Where a run STARTS is the plan's decision and arrives here as an argument.
649
- * All this answers is where it must stop, and the answer is a fact of the
650
- * map: the free stretch from that number on. `-1` means the end of the track,
651
- * which is what "no end" is written as everywhere here.
652
- *
653
- * @param {object} session
654
- * @param {number} startIndex
655
- * @param {object | null} exceptRun - The run being replaced, whose own claim
656
- * is not somebody else's material.
657
- * @returns {number} The last number to work through, or `-1` for the end.
658
- */
659
- #runEndFrom(session, startIndex, exceptRun = null) {
660
- return this.#host.encodeOrchestrator.freeStretchEnd({
661
- address: session.outputKey ?? "",
662
- from: startIndex,
663
- exceptRun,
664
- segmentCount: Number(session.timeline?.segmentCount) || 0
665
- });
666
- }
667
-
668
- // Returns the encoder it built, or nothing when there was nothing to build.
669
- // It waits for nothing: the one thing it used to wait for was the death of
670
- // the run it was replacing, and that killing is gone.
671
- #startEncodeRun(session, startIndex, because, ordered = null) {
672
- if (!this.isLive(session)) {
673
- return null;
674
- }
675
- // A new run starts the quality budget's reckoning afresh: a pair of
676
- // readings spanning the restart would count the gap between two runs as
677
- // slow encoding. Told, not written: those readings are the budget's.
678
- this.#host.noteRunStarting(session);
679
- // Where a restart's seconds go. A seek costs 5-8 s in the field and the
680
- // recorded reason — waiting for the previous ffmpeg to exit, measured at
681
- // 0.54-1.47 s — does not account for it. Before rebuilding the hottest path
682
- // in the proxy on a guess, make each stage state its own cost.
683
- const restartEnteredAt = Date.now();
684
- // Reads where the old run began BEFORE the new one takes its place, and
685
- // does not await: everything below is the restart path, which is measured
686
- // in milliseconds and has been worked on twice to keep it that way.
687
- this.#accountBackwardRestart(session, startIndex);
688
- // STARTING AN ENCODER STOPS NOTHING. It used to stop any live run whose own
689
- // start was not below this one's — a rule left over from when a session
690
- // held exactly one run and "the previous one" meant "the only one". Once a
691
- // an output could hold several, that rule began killing runs the plan had
692
- // decided to keep: 294 stops for this reason in eight minutes of field
693
- // 2026-09-05, against four starts asked for by a viewer. Who is stopped is
694
- // decided in one place, and it is not this one.
695
- //
696
- // HOW FAR IT MAY WORK is decided by the same one place and arrives here as
697
- // an argument. Reading it off the coverage map a second time was the last
698
- // remaining second answer to that question: the plan computed `to`, passed
699
- // it, and the parameter list did not name it.
700
- const runEnd = Number.isInteger(ordered?.to)
701
- ? ordered.to
702
- : this.#runEndFrom(session, startIndex, null);
703
- // The restart backs off a segment or two from what was asked for, so the
704
- // request that prompted it is recorded under a HIGHER index than the run
705
- // starts at. Looking it up by the start index alone found nothing and the
706
- // line never printed once.
707
- let wantedAt = null;
708
- const inputState = this.#inputStateFor(session);
709
- for (const [index, at] of inputState.firstWantedAt) {
710
- if (index >= startIndex && (wantedAt === null || at < wantedAt)) {
711
- wantedAt = at;
712
- }
713
- }
714
- if (typeof wantedAt === "number") {
715
- this.#host.logger.info(
716
- `transcode ${session.id} restart for #${startIndex} decided ` +
717
- `${restartEnteredAt - wantedAt}ms after it was first asked for`
718
- );
719
- }
720
- // Cleared with the run that could not answer them. These are "how long has
721
- // this segment gone unanswered", and the question only means anything about
722
- // the run in force: a timestamp kept from an abandoned scan probe minutes
723
- // ago says a fresh request has already waited long enough, which is how the
724
- // behind-head repair came to fire on the very first poll instead of waiting
725
- // for the seek that should move the encoder. It also stops the map growing
726
- // for the life of a session.
727
- inputState.firstWantedAt.clear();
728
- // WHERE THIS NUMBER REALLY BEGINS, when a produced piece has said so and
729
- // the table the player holds still says otherwise.
730
- //
731
- // Only a soundtrack, and only because a soundtrack has no keyframes: it
732
- // begins exactly where it is asked to, to within one audio frame, while the
733
- // picture can begin nowhere but a real keyframe and so moves forward to the
734
- // next one — up to three seconds, measured 2026-08-17. Left at the published
735
- // time, the sound of one film starts up to that far from its picture after
736
- // every restart, each correctly labelled with where it really is, and the
737
- // viewer gets sound with no new picture for the difference.
738
- //
739
- // DERIVED HERE rather than handed in. It used to arrive as an argument from
740
- // the one caller that knew it — the correction that had just measured it —
741
- // which meant the instant was only ever right for a run that caller
742
- // started. Any run the plan placed at a corrected number got the published
743
- // time and landed apart again. It is a fact of the FILE's cutting, held in
744
- // the live table every session of the file shares, so it is read from
745
- // there.
746
- const positionSecondsOverride = session.spec.carries === "audio-only"
747
- ? trueStartOf(session.timeline, startIndex)
748
- : undefined;
749
-
750
- // What is still read off the session for a run. The list is the measure of
751
- // how far a session still is from being the three things a run is built
752
- // from — the material, what is produced of it, and the stretch — and it
753
- // shrinks in place as those are taken off. There is deliberately no method
754
- // wrapping it: a named adapter with one caller is a thing to remember to
755
- // delete, and this is a thing that disappears by being emptied.
756
- const inputs = this.#host.inputOf(session);
757
- const { args, safeIndex, startSeconds, cutTimes } = buildRunCommand({
758
- keyframes: session.keyframes,
759
- inputFile: inputs.inputFile,
760
- audioFile: inputs.audioFile,
761
- inputUrl: inputs.inputUrl,
762
- audioInputUrl: inputs.audioInputUrl,
763
- timeline: session.timeline,
764
- output: session.output,
765
- segmentFormat: session.segmentFormat,
766
- transcodeVideo: session.spec.transcodesVideo,
767
- transcodeAudio: session.spec.transcodesAudio,
768
- audioOnly: session.spec.carries === "audio-only",
769
- audioSeparate: this.#host.servesAudioSeparately(session),
770
- audioSourceTrackIndex: session.spec.audioSourceTrackIndex,
771
- rateControl: session.spec.video?.encode?.rateControl ?? null,
772
- startIndex,
773
- endIndex: runEnd,
774
- positionSecondsOverride,
775
- videoEncoder: this.#host.videoEncoder,
776
- segmentDurationSec: this.#host.segmentDurationSec
777
- });
778
-
779
- // The exact command, every run. An encode failure is otherwise reported
780
- // with ffmpeg's message and nothing about what it was asked to do, and the
781
- // two are not always deducible from each other: 2026-08-04 a run died with
782
- // "Cannot write moov atom before AC3 packets" although both muxing paths
783
- // were verified to handle a copied AC-3 track on this very host, so the
784
- // arguments that run actually received are the missing evidence. One line
785
- // per run, and a run happens at most every few seconds.
786
- // Numbered, because a burst of seeks starts several runs in one second and
787
- // every line about them carries the SESSION id, which is the same for all.
788
- // Without a run number the command that failed cannot be told from the two
789
- // that succeeded around it — which is exactly the state the unexplained
790
- // `Cannot write moov atom before AC3 packets` was found in.
791
-
792
- // One directory for the output, and every run writes straight into it.
793
- //
794
- // Runs used to be kept apart by a directory each, because two of them
795
- // writing one segment name at the same time produce a file belonging to
796
- // neither — which was also the only reason a restart ever had to wait for
797
- // its predecessor to die. Intervals remove that by construction: a run is
798
- // given a stretch nobody else holds and stops at the end of it, so no two
799
- // runs ever want the same number. What is left of the old reason — a run
800
- // killed mid-piece leaving a partial file — is not a correctness problem
801
- // either, because the store serves a segment only once its closure is
802
- // proven, and it is cleared up when the run ends.
803
- const run = new EncodeRun({
804
- address: session.outputKey ?? session.id,
805
- encoder: this.#host.videoEncoder,
806
- from: safeIndex,
807
- to: runEnd,
808
- buildArgs: () => args,
809
- argsDescribed: describeFfmpegArgs(args),
810
- // Whether this run cuts at times we gave it. Decides how a segment is
811
- // judged finished — see getFileStream.
812
- usesExplicitCuts: Boolean(cutTimes && cutTimes.length > 0),
813
- startSeconds,
814
- totalSeconds: Number(session.file.durationSeconds) || null,
815
- spawn: (spawnArgs) =>
816
- spawn(this.#host.ffmpegBin, spawnArgs, {
817
- cwd: this.#host.segmentFiles.directoryFor(session.outputKey ?? ""),
818
- // A fourth channel: the encoder names every piece it has CLOSED on it,
819
- // which is the only proof a piece is whole.
820
- stdio: ["ignore", "pipe", "pipe", "pipe"]
821
- }),
822
- logger: this.#host.logger,
823
- // The film's last segment number, which is what tells "it reached the
824
- // end" from "its input dried up": ffmpeg exits zero for both and over a
825
- // torrent cannot tell them apart.
826
- lastSegmentIndex: () =>
827
- session.timeline?.segmentCount > 0 ? session.timeline.segmentCount - 1 : null,
828
- inputUnavailable: (message) => isInputUnavailable(message),
829
- onProgress: (report) => this.#noteRunProgress(session, run, report),
830
- indexOfName: (name) => session.segmentFormat.segmentIndexFromName(name),
831
- // Why this encoder exists, recorded with its argument list. It used to be
832
- // handed to a separate `start` call; there is no separate call now.
833
- because,
834
- onClosed: (name) => this.#host.segmentFiles.publish(session.outputKey ?? "", name, session.segmentFormat),
835
- onEnded: (ended) => this.noteRunEnded(session, run, ended)
836
- });
837
- // THE ONE FAULT THAT IS OTHERWISE SILENT, asked before this run produces a
838
- // frame. It lost its caller in a refactor on 2026-09-04 and had none until
839
- // 2026-09-15 — not by a decision, which is why it is back rather than gone.
840
- this.#warnIfRunLeavesTheInitBehind(session);
841
-
842
- this.#host.logger.info(
843
- `transcode ${session.id} encode-run #${safeIndex}..#${runEnd} from segment #${safeIndex} ` +
844
- `(+${Date.now() - restartEnteredAt}ms since the restart was asked for) ` +
845
- `(${formatSeconds(startSeconds)}) "${session.file.name}"`
846
- );
847
- // The four numbers a run is positioned by, said once, because their
848
- // disagreement is invisible everywhere else. The two tables are printed
849
- // side by side: while they differ, every cut of this run is off by the
850
- // difference, and nothing downstream can tell that from a bad index.
851
- const liveStart = this.#host.outputTimes.segmentStartTime(session, safeIndex);
852
- this.#host.logger.info(
853
- `transcode ${session.id} run #${safeIndex}..#${runEnd} positioned at ${startSeconds.toFixed(3)}s ` +
854
- `for boundary #${safeIndex} (published ${startSeconds.toFixed(3)}s, ` +
855
- `live ${liveStart.toFixed(3)}s, apart ${(liveStart - startSeconds).toFixed(3)}s), ` +
856
- `numbering from #${safeIndex}`
857
- );
858
- return run;
859
- }
860
-
861
- /**
862
- * Log a report from a run that still belongs to this output. The run owns the
863
- * clock and has already placed its relative ffmpeg time on the source
864
- * timeline.
865
- *
866
- * @param {HlsSession} session
867
- * @param {import("./EncodeRun.js").EncodeRun} run
868
- * @param {object} progress
869
- * @returns {void}
870
- */
871
- #noteRunProgress(session, run, progress) {
872
- if (!this.isLive(session) || !this.runsOf(session).includes(run)) {
873
- return;
874
- }
875
- if (run.progress.shouldLog(PROGRESS_LOG_INTERVAL_MS)) {
876
- this.#host.logger.info(
877
- `transcode ${session.id} "${session.file.name}" ${progress.percent.toFixed(1)}% ` +
878
- `(${formatSeconds(progress.processedSeconds)} / ${formatSeconds(progress.totalSeconds)})` +
879
- ` speed=${progress.speed || "n/a"}`
880
- );
881
- }
882
- }
883
-
884
- /**
885
- * What to do about a run that has ended.
886
- *
887
- * The run says how it ended and why; this decides what follows, which is a
888
- * question about the SESSION and the machine rather than about the process:
889
- * whether to condemn a hardware encoder, whether to wait for the input to
890
- * come back, whether a position has failed often enough to stop retrying it.
891
- *
892
- * An ending from a run the session has already replaced is not ignored — it
893
- * is simply not about this session's current run, so nothing that describes
894
- * the current run is written from it. That is what the identity check used to
895
- * be for, and it is one condition now instead of one at the head of every
896
- * handler.
897
- *
898
- * @param {HlsSession} session
899
- * @param {import("./EncodeRun.js").EncodeRun} run
900
- * @param {import("./EncodeRun.js").RunEnded} ended
901
- * @returns {void}
902
- */
903
- noteRunEnded(session, run, ended) {
904
- // First, and for every run whatever else follows: the stretch it held goes
905
- // back to the map. A run whose claim is never released tells the plan that
906
- // numbers nobody is making are being made, and nothing is ever started
907
- // there again.
908
- // WAS this run still the session's when it ended? Asked before it is
909
- // removed, because after the removal the question always answers "no" —
910
- // and it was asked after, so every line below this point was unreachable.
911
- //
912
- // Measured 2026-09-05 over both of the field host's log files: zero
913
- // occurrences of this handler's own `encode-run #… failed:` line and zero
914
- // of `fast failure at segment`, across every session this proxy has ever
915
- // run. So the fallback from a failed hardware encoder to software, the
916
- // retry when the torrent data goes away, the limit on retrying a position
917
- // that keeps failing, and the error line naming the ffmpeg command have all
918
- // been dead code — which is also why nothing ever stopped the restart loop
919
- // recorded in the field the same day.
920
- const wasCurrent = this.runsOf(session).includes(run);
921
- this.#host.encodeOrchestrator.noteEnded(ended);
922
- // What this admitted encode was seen to do, kept for the next time this
923
- // host prices the same mode (roadmap item 97, step 14). Whatever the
924
- // ending: a run that failed still ran at the speed it ran at, and the
925
- // segments it closed still carried what they carried.
926
- this.#host.observeEncodeEnded?.(session);
927
- // A stretch went back to the map, so what should be running has changed.
928
- // Said here rather than waited for: this is the moment it became true.
929
- this.planEncodersSoon();
930
- // ANYBODY ALREADY WAITING IS TOLD, and it is asked HERE because every
931
- // ending passes this line while the handlers below return one by one. It
932
- // used to be the last statement of this method, which the `SHORT` branch —
933
- // ffmpeg exiting 0 having produced less than the playlist promises —
934
- // returns before reaching. That ending is terminal (`ENDED_FAILED`), so a
935
- // request arriving after it is answered with the failure at once while a
936
- // wait that BEGAN before it sat out its whole deadline, or, where the page
937
- // states none, until the viewer disconnected.
938
- //
939
- // THE CONDITION IS THE OUTPUT'S ACTUAL STATE, not this run's ending.
940
- // `stateOf` answers from the LIVE runs and falls back to the last ending
941
- // only when none remain, so one run of several ending in failure wakes
942
- // nobody — something is still making it — and the last one does. Asked
943
- // after `noteEnded` has recorded the ending, so the woken request reads the
944
- // final state; and it re-reads the disk first, because a piece already made
945
- // is served whatever became of the encoder.
946
- if (this.hasFailed(session)) {
947
- this.#host.invalidateWaits(session);
948
- // A move to this output being prepared for a viewer waits for a segment
949
- // that nothing will now make, and no publication will ever end that wait.
950
- // This is the event that does (roadmap item 97, step 12).
951
- this.#host.productionFailed(session);
952
- }
953
- if (!this.isLive(session)) {
954
- this.forgetEncodingOfGone(session);
955
- return;
956
- }
957
- if (!wasCurrent) {
958
- // A run the session had already replaced. It has logged its own ending;
959
- // nothing about the session follows from it.
960
- return;
961
- }
962
- const lastError = this.lastErrorOf(session) || ended.because;
963
- if (ended.ending === ENCODE_EXIT.STOPPED || ended.ending === ENCODE_EXIT.GONE) {
964
- return;
965
- }
966
- if (ended.ending === ENCODE_EXIT.COMPLETE) {
967
- this.#host.logger.info(`transcode ${session.id} encode-run complete "${session.file.name}"`);
968
- return;
969
- }
970
- if (ended.ending === ENCODE_EXIT.SHORT) {
971
- // ffmpeg exits 0 both when it reaches the end of the file and when its
972
- // input simply stops producing bytes — over HTTP the two look identical
973
- // to it. Field 2026-08-05: the torrent's download died, the read ended,
974
- // and a run that had made 188 segments of 624 reported itself complete;
975
- // the player then consumed what was on disk and froze on the first
976
- // segment nobody was making. So the claim is checked against the playlist
977
- // we published, and a run that stopped short is a FAILURE that can be
978
- // restarted, not a finished file.
979
- this.#host.logger.error(
980
- `transcode ${session.id} encode-run #${ended.from}..#${ended.to} ended early: ` +
981
- `${ended.because} "${session.file.name}"`
982
- );
983
- return;
984
- }
985
- // Runtime safety net: if a hardware encode fails, downgrade this proxy to
986
- // software encoding for all sessions and restart this one, so playback is
987
- // never permanently broken by a hardware/driver issue.
988
- //
989
- // Asked only of a genuine encoder failure. It used to be asked of every
990
- // non-zero exit, so a run whose TORRENT DATA went away — which says nothing
991
- // whatever about the encoder — condemned a working NVENC or QuickSync to
992
- // software for the life of the process, and started an extra run at the old
993
- // index while it was at it.
994
- if (ended.ending === ENCODE_EXIT.FAILED && session.spec.transcodesVideo && this.#host.videoEncoder.kind !== "software") {
995
- const failed = this.#host.encoders.useSoftware();
996
- const failedEncoder = failed?.name ?? this.#host.videoEncoder.name;
997
- this.#host.logger.warn(
998
- `transcode ${session.id} hardware encoder ${failedEncoder} failed ` +
999
- `(${lastError}); falling back to software libx264, and every output named by ${failedEncoder} is closed`
1000
- );
1001
- // An output IS its format, and the encoder is part of it: pieces from
1002
- // another encoder cannot be decoded with the header this output's viewers
1003
- // already hold. So the outputs made by the failed encoder end here, and
1004
- // the next request for that picture names a format built on the new one.
1005
- //
1006
- // THE ONE DISPOSAL THAT DOES NOT ASK WHETHER ASSIGNMENTS STILL HOLD IT, and
1007
- // deliberately. Every other one asks, because a repeat of a request must be
1008
- // answered by what answered it first. Here what answered it first has no
1009
- // encoder that can go on making it: holding the output keeps only a name
1010
- // whose next segment nobody will produce. A response already being sent is
1011
- // not cut short by this — it reads from a file already opened.
1012
- for (const other of [...this.#host.outputs.values()]) {
1013
- if (other.spec.video?.encode?.encoder === failedEncoder) {
1014
- void this.#host.disposeSession(other.id);
1015
- }
1016
- }
1017
- this.planEncodersSoon();
1018
- return;
1019
- }
1020
- // Losing the INPUT is not the session failing — it is the data not being
1021
- // there YET. The torrent can be re-added and re-downloaded, so the honest
1022
- // answer to the viewer is "still working", not an error screen. Field
1023
- // 2026-08-06: a torrent evicted mid-seek took the film with it, the run
1024
- // died on `File 0 not found`, the session went terminal and answered 500 to
1025
- // every request from then on — although the swarm was there and the data
1026
- // would have come back in seconds. The circuit breaker below stays for what
1027
- // it was built for, a target that genuinely cannot be encoded; it must not
1028
- // condemn a session whose data merely went away.
1029
- if (ended.ending === ENCODE_EXIT.INPUT_LOST) {
1030
- const inputState = this.#inputStateFor(session);
1031
- inputState.inputRetryCount += 1;
1032
- this.#host.logger.warn(
1033
- `transcode ${session.id} encode-run #${ended.from}..#${ended.to} lost its input ` +
1034
- `(${lastError}) (attempt ${inputState.inputRetryCount})`
1035
- );
1036
- // HOW LONG TO WAIT IS THE PLAN'S, and this says only what happened. The
1037
- // delay used to be timed here, against the dead run, which the plan never
1038
- // consults — so it placed a fresh run at the same spot as fast as ffmpeg
1039
- // could fail there: 2432 starts in 23 minutes in the field 2026-09-12,
1040
- // against a delay that had reached its 15 s ceiling long before. The
1041
- // orchestrator holds it now, beside the decision it governs.
1042
- if (run.state === ENCODE_RUN_STATE.RETRY_WAIT) {
1043
- run.retryDue();
1044
- }
1045
- return;
1046
- }
1047
- // A run that exits THIS fast never did real work: it failed at the start
1048
- // itself — opening the input, spawning the process — rather than
1049
- // mid-stream. Consecutive fast failures at the SAME position are counted so
1050
- // that whoever commands a start can stop commanding one that keeps failing.
1051
- //
1052
- // COUNTED AT EVERY POSITION, #0 included. It used to be counted only past
1053
- // #0, because it was written for seek restarts and a seek is never to the
1054
- // beginning. That left the one position the plan commands FIRST with no
1055
- // count at all, and the plan re-commanding a start that cannot succeed is
1056
- // an unbounded loop: measured 2026-09-05, ffmpeg failing to spawn produced
1057
- // fifty passes of the plan before a probe stopped it, as fast as the
1058
- // failures arrived.
1059
- const failure = this.#host.encodeOrchestrator.noteStartFailure(
1060
- session.outputKey,
1061
- ended.from,
1062
- ended.livedMs
1063
- );
1064
- if (failure.count > 0) {
1065
- this.#host.logger.warn(
1066
- `transcode ${session.id} fast failure at segment #${ended.from} ` +
1067
- `(${ended.livedMs}ms) — ${failure.count}/${failure.limit} consecutive`
1068
- );
1069
- if (failure.blocked) {
1070
- this.#host.logger.error(
1071
- `transcode ${session.id} will not be started at #${ended.from} again: ` +
1072
- `${failure.count} starts there failed within ${failure.fastMs}ms each ` +
1073
- `(${lastError}) "${session.file.name}"`
1074
- );
1075
- }
1076
- }
1077
- this.#host.logger.error(
1078
- `transcode ${session.id} encode-run #${ended.from}..#${ended.to} failed: ${lastError}` +
1079
- ` — ${this.#describeTrackSelection(session)}` +
1080
- `\n ffmpeg ${run.argsDescribed || "(command not recorded)"}`
1081
- );
1082
- }
1083
-
1084
- /**
1085
- * What this run asked the source for, against what the source said it has.
1086
- *
1087
- * Written for one failure in particular: every `-map` this proxy builds ends
1088
- * in `?`, so ffmpeg drops a mapping for an absent stream without complaint,
1089
- * and a run whose mappings ALL drop produces a file with nothing in it —
1090
- * reported as `Output file does not contain any stream`, exit 255. Read from
1091
- * the exit code alone that is indistinguishable from any other refusal. Read
1092
- * beside the tracks the file actually holds it is unmistakable, and it names
1093
- * which side is wrong: an audio index past the end of the list is ours, no
1094
- * streams at all is the source's.
1095
- *
1096
- * @param {HlsSession} session
1097
- * @returns {string}
1098
- */
1099
- #describeTrackSelection(session) {
1100
- const wanted = [];
1101
- // Named exactly as the command line names them, second input included: a
1102
- // refusal whose message describes a different mapping than the one that was
1103
- // refused is the reading that cost a wrong diagnosis before.
1104
- const inputs = this.#host.inputOf(session);
1105
- const audioInput = inputs.audioInputUrl.length > 0 ? 1 : 0;
1106
- const audioTrack = session.spec.audioSourceTrackIndex;
1107
- if (session.spec.carries === "audio-only") {
1108
- wanted.push(`audio 0:a:${audioTrack}`);
1109
- } else if (this.#host.servesAudioSeparately(session)) {
1110
- wanted.push("video 0:v:0");
1111
- } else {
1112
- wanted.push("video 0:v:0", `audio ${audioInput}:a:${audioTrack}`);
1113
- }
1114
- const counts = session.file.streamCounts;
1115
- const held = counts
1116
- ? `the source holds ${counts.video} video, ${counts.audio} audio, ` +
1117
- `${counts.subtitle} subtitle` +
1118
- (counts.other > 0 ? `, ${counts.other} other` : "")
1119
- : "what the source holds was not recorded";
1120
- return `this run asked for ${wanted.join(" + ")}, and ${held}`;
1121
- }
1122
-
1123
- /**
1124
- * Stop this session's encoder without replacing it.
1125
- *
1126
- * A variant nobody is watching must not go on encoding: the host has one
1127
- * encoder's worth of capacity, and the whole point of switching quality
1128
- * seamlessly is that the new rung gets it. The session itself stays — its
1129
- * produced segments remain servable, and switching back restarts it from
1130
- * where the viewer then is.
1131
- *
1132
- * @param {HlsSession} session
1133
- * @param {string} reason - Named in the log; a stopped encoder is otherwise
1134
- * indistinguishable from one that died.
1135
- * @returns {void}
1136
- */
1137
- stopEncodeRun(session, reason) {
1138
- const running = this.liveRunsOf(session);
1139
- if (running.length === 0) {
1140
- return;
1141
- }
1142
- // Every run this session has going. The callers all mean the session's
1143
- // encoding as a whole: a rung nobody is watching any more, a session being
1144
- // disposed. A run that had already finished or failed is not among them,
1145
- // which is what keeps a stop from erasing how it actually ended.
1146
- for (const run of running) {
1147
- // The run resumes itself if it was suspended — a stopped process does not
1148
- // act on SIGTERM until it is continued — records the cause, and answers
1149
- // its own exit. Nothing here has to null a field so that the exit is read
1150
- // correctly, because there is no shared field left to misread.
1151
- run.stop(reason);
1152
- // Nothing is cleared up from here. What this run left open is under its
1153
- // own working name, and the encoding layer removes it when the run's
1154
- // ending reaches it — one place, and it needs neither the stretch nor the
1155
- // init bytes this method used to fetch to judge a file by its contents.
1156
- }
1157
- this.#host.logger.info(`transcode ${session.id} ${running.length} encoder(s) stopped: ${reason}`);
1158
- }
1159
-
1160
- /**
1161
- * What moving the encoder BACKWARDS costs, said out loud when it happens.
1162
- *
1163
- * Nothing already written is lost — every run keeps its own directory and
1164
- * {@link SegmentStore#pathOfName} serves the union of all of them
1165
- * — so the price of a restart is not the files. It is two other things, and
1166
- * neither was ever counted:
1167
- *
1168
- * - **work done twice.** The new run begins at the target and encodes
1169
- * forward through segments the old run had already finished. ffmpeg cannot
1170
- * know they exist, so it makes them again.
1171
- * - **the viewer in front.** While the run walks back up to where it already
1172
- * was, nothing new is being made ahead of them, and their cushion drains.
1173
- *
1174
- * Both are what decides whether a session should be allowed a SECOND
1175
- * concurrent run instead — roadmap item 64. That question cannot be answered
1176
- * from taste, and this is the reading it needs: how often it happens at all,
1177
- * how far back, and how much of the walk is a repeat.
1178
- *
1179
- * Nothing here is awaited by the caller. Everything below the call site is
1180
- * the restart path, which is measured in milliseconds and has been worked on
1181
- * twice to keep it that way; a reading that delays the thing it is reading
1182
- * about is not a reading. The figures that MUST be taken before the new run
1183
- * exists are taken synchronously, and only the file counting is left to run
1184
- * on its own — against the directories that existed at this instant, so what
1185
- * the new run is about to write cannot be counted as already there.
1186
- *
1187
- * @param {HlsSession} session
1188
- * @param {number} startIndex - Where the new run will begin.
1189
- * @returns {void}
1190
- */
1191
- #accountBackwardRestart(session, startIndex) {
1192
- const previousStart = earliestRunStart(this.runsOf(session));
1193
- if (!Number.isInteger(previousStart) || !Number.isInteger(startIndex) || startIndex >= previousStart) {
1194
- // A first run, or one moving forward. Neither costs anything here: a
1195
- // forward restart skips material it never made.
1196
- return;
1197
- }
1198
- const processed = Number(this.progressOf(session)?.processedSeconds);
1199
- const head = Number.isFinite(processed)
1200
- ? Math.max(previousStart, this.#host.outputTimes.segmentIndexForTime(session, processed))
1201
- : previousStart;
1202
- // Bounded: a session an hour in has thousands of segments, and the count is
1203
- // for a comparison, not an inventory.
1204
- const last = Math.min(head, startIndex + BACKWARD_RESTART_SCAN_SEGMENTS);
1205
- const accounting = this.#inputStateFor(session).backwardRestarts;
1206
- accounting.count += 1;
1207
- accounting.segmentsBack += previousStart - startIndex;
1208
- accounting.worstBack = Math.max(accounting.worstBack, previousStart - startIndex);
1209
-
1210
- void (async () => {
1211
- let alreadyOnDisk = 0;
1212
- for (const index of this.#host.producedNumbers(session)) {
1213
- if (index >= startIndex && index <= last) {
1214
- alreadyOnDisk += 1;
1215
- }
1216
- }
1217
- accounting.remade += alreadyOnDisk;
1218
- this.#host.logger.info(
1219
- `transcode ${session.id} moving the encoder BACK from #${previousStart} to #${startIndex} ` +
1220
- `(head was #${head}): ${alreadyOnDisk} of the ${last - startIndex + 1} segment(s) it will walk through ` +
1221
- `are already on disk and will be made again, and nothing is produced ahead of #${head} until it gets ` +
1222
- `back there — ${accounting.count} backward restart(s) this session, worst ${accounting.worstBack} ` +
1223
- `segment(s) back, ${accounting.remade} segment(s) remade in total (roadmap 64)`
1224
- );
1225
- })().catch(() => {
1226
- // silent-ok: a reading that fails is not worth ending a restart over.
1227
- });
1228
- }
1229
- }
1
+ /**
2
+ * The encoders of this proxy, from the plan's decision to the end of the process.
3
+ *
4
+ * The plan (`EncodePlan`, through `EncodeOrchestrator`) decides where a run goes;
5
+ * this builds it with the arguments its output needs, follows what it produces,
6
+ * says how far it got, and accounts for how it ended. Nothing here decides where
7
+ * an encoder should be.
8
+ */
9
+
10
+ import path from "node:path";
11
+ import { spawn } from "node:child_process";
12
+ import { contentionPenalty } from "./contention.js";
13
+ import { ENCODE_RUN_STATE, liveRunsOf, runStateOf, wireState } from "./encode-run-state.js";
14
+ import { ENCODE_EXIT } from "./encode-exit.js";
15
+ import { EncodeRun } from "./EncodeRun.js";
16
+ import { computeOutputDimensions } from "./args.js";
17
+ import { buildRunCommand, trueStartOf } from "./run-command.js";
18
+
19
+ /**
20
+ * @typedef {Object} SegmentFiles
21
+ * @property {(address: string) => string} pathFor
22
+ * @property {(address: string) => Buffer | null} initOf
23
+ * @property {(address: string) => string} directoryFor
24
+ * @property {(address: string, makingName: string, format: object) => string | null} publish
25
+ */
26
+ // How far the accounting of a backward restart looks for work about to be done
27
+ // twice. It runs on the restart path and a session an hour in has thousands of
28
+ // segments; the figure is for a comparison, not an inventory.
29
+ const BACKWARD_RESTART_SCAN_SEGMENTS = 300;
30
+ const PROGRESS_LOG_INTERVAL_MS = 5_000;
31
+ /**
32
+ * Format a seconds value as `HH:MM:SS`, or `"n/a"` if not finite.
33
+ *
34
+ * @param {number} seconds
35
+ * @returns {string}
36
+ */
37
+ export function formatSeconds(seconds) {
38
+ if (!Number.isFinite(seconds) || seconds < 0) {
39
+ return "n/a";
40
+ }
41
+ const total = Math.floor(seconds);
42
+ const hours = Math.floor(total / 3600);
43
+ const minutes = Math.floor((total % 3600) / 60);
44
+ const rest = total % 60;
45
+ return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}:${String(rest).padStart(2, "0")}`;
46
+ }
47
+ /**
48
+ * Where this session's encoding begins: the earliest number any live run of it
49
+ * was given.
50
+ *
51
+ * @param {object[] | Set<object>} runs
52
+ * @returns {number | null}
53
+ */
54
+ export function earliestRunStart(runs) {
55
+ const live = liveRunsOf(runs);
56
+ return live.length > 0 ? live[0].from : null;
57
+ }
58
+ /**
59
+ * The live run of this session that was given this number, if any.
60
+ *
61
+ * A run with an explicit stretch owns the whole of it. One WITHOUT an end owns
62
+ * only as far as it has actually got: claiming the rest of the film would make
63
+ * it the owner of every number in front of it, including ones another run was
64
+ * expressly given.
65
+ *
66
+ * @param {object[] | Set<object>} runs
67
+ * @param {number} index
68
+ * @returns {object | null}
69
+ */
70
+ function ownRunMaking(runs, index) {
71
+ let answer = null;
72
+ for (const run of liveRunsOf(runs)) {
73
+ if (run.from > index) {
74
+ break; // Ordered by start, so nothing further can hold this number.
75
+ }
76
+ if (Number.isInteger(run.to) && run.to >= run.from && index > run.to) {
77
+ continue; // Its stretch ends before this number.
78
+ }
79
+ answer = run;
80
+ }
81
+ return answer;
82
+ }
83
+
84
+ /**
85
+ * Whether an encoder run died because its INPUT went away, rather than because
86
+ * of anything about the encode itself.
87
+ *
88
+ * These are the messages the read path and ffmpeg's HTTP client produce when
89
+ * the torrent is gone, being re-added, or has no data for the range yet — all
90
+ * of them temporary by nature: the source can be added again and the pieces
91
+ * fetched again.
92
+ *
93
+ * @param {string} message
94
+ * @returns {boolean}
95
+ */
96
+ export function isInputUnavailable(message) {
97
+ const text = typeof message === "string" ? message : "";
98
+ return (
99
+ /Error reading HTTP response/i.test(text) ||
100
+ /not found in (?:magnet|torrent):/i.test(text) ||
101
+ /Unknown source/i.test(text) ||
102
+ /is gone and cannot be re-added/i.test(text) ||
103
+ /Read error at pos/i.test(text) ||
104
+ /Server returned 5\d\d/i.test(text) ||
105
+ /Input\/output error/i.test(text) ||
106
+ /Connection reset by peer/i.test(text) ||
107
+ /End of file/i.test(text)
108
+ );
109
+ }
110
+
111
+ /**
112
+ * The ffmpeg command as one readable line.
113
+ *
114
+ * Everything is shown as passed except the list of cut times, which is one
115
+ * value per segment — 830 of them on a two-hour film, about 7 KB of log for a
116
+ * single run, repeated on every restart. The count and the two ends say
117
+ * everything the list is ever consulted for: whether cutting was explicit at
118
+ * all, how far it reaches, and where it starts.
119
+ *
120
+ * @param {string[]} args
121
+ * @returns {string}
122
+ */
123
+ export function describeFfmpegArgs(args) {
124
+ const parts = [];
125
+ for (let index = 0; index < args.length; index += 1) {
126
+ const value = args[index];
127
+ // Both take the same list, and a keyframe-grid variant passes it twice.
128
+ // `-force_key_frames` also takes an expression, which is short and is left
129
+ // alone — only a list is folded.
130
+ if (
131
+ (value === "-segment_times" || value === "-force_key_frames") &&
132
+ typeof args[index + 1] === "string" &&
133
+ args[index + 1].includes(",")
134
+ ) {
135
+ const times = args[index + 1].split(",");
136
+ parts.push(value, `<${times.length} cuts ${times[0]}..${times[times.length - 1]}>`);
137
+ index += 1;
138
+ continue;
139
+ }
140
+ parts.push(value);
141
+ }
142
+ return parts.join(" ");
143
+ }
144
+
145
+ export class EncodeRuns {
146
+ #inputState = new WeakMap();
147
+ /** What this reads and asks of the rest of the proxy, and nothing else. @type {object} */
148
+ #host;
149
+
150
+ /**
151
+ * Whether a re-decision of what encoders should exist is already queued for
152
+ * the end of this turn. See `planEncodersSoon`.
153
+ * @type {boolean}
154
+ */
155
+ #planScheduled = false;
156
+
157
+ /**
158
+ * @param {object} host - `logger`, `viewerSecondsOn`, `noteRunStarting`, `inputOf`, `producedNumbers`, `servesAudioSeparately`, `disposeSession`, `contentionPenalties`, `encodeCost`, `encodeOrchestrator`, `encoders`, `ffmpegBin`, `outputTimes`, `outputs`, `priority`, `segmentDurationSec`, `segmentFiles`, `videoEncoder`
159
+ * @param {SegmentFiles} host.segmentFiles - The storage operations used to
160
+ * write and inspect output files.
161
+ */
162
+ constructor(host) {
163
+ this.#host = host;
164
+ }
165
+
166
+ #inputStateFor(output) {
167
+ let state = this.#inputState.get(output);
168
+ if (!state) {
169
+ state = {
170
+ readWindowBytes: 0,
171
+ inputRetryCount: 0,
172
+ backwardRestarts: { count: 0, segmentsBack: 0, worstBack: 0, remade: 0 },
173
+ firstWantedAt: new Map(),
174
+ /** Bytes the swarm has delivered to this output's own input reads. */
175
+ inputBytes: 0,
176
+ /** The mismatch between the held init and the run's size last said, so it is said once. */
177
+ initSizeSaid: ""
178
+ };
179
+ this.#inputState.set(output, state);
180
+ }
181
+ return state;
182
+ }
183
+
184
+ setReadWindow(output, bytes) {
185
+ this.#inputStateFor(output).readWindowBytes = Number.isFinite(bytes) ? bytes : 0;
186
+ }
187
+
188
+ readWindowFor(output) {
189
+ return this.#inputStateFor(output).readWindowBytes;
190
+ }
191
+
192
+ noteWanted(output, index, at = Date.now()) {
193
+ const wanted = this.#inputStateFor(output).firstWantedAt;
194
+ if (!wanted.has(index)) wanted.set(index, at);
195
+ }
196
+
197
+ resetInputRetry(output) {
198
+ this.#inputStateFor(output).inputRetryCount = 0;
199
+ }
200
+
201
+ /**
202
+ * Count bytes the swarm has delivered to one output's own input read.
203
+ *
204
+ * Called by the `/stream` route for every fragment it writes to an encoder.
205
+ * Cheap on purpose — one addition, no clock, no log — because it runs per
206
+ * fragment on the path that feeds ffmpeg.
207
+ *
208
+ * @param {string} outputId
209
+ * @param {number} bytes
210
+ * @returns {void}
211
+ */
212
+ noteInputBytes(outputId, bytes) {
213
+ if (!outputId || !(bytes > 0)) {
214
+ return;
215
+ }
216
+ const output = this.#host.outputs.get(outputId);
217
+ if (output) {
218
+ this.#inputStateFor(output).inputBytes += bytes;
219
+ }
220
+ }
221
+
222
+ /**
223
+ * @param {object} output
224
+ * @returns {number}
225
+ */
226
+ inputBytesOf(output) {
227
+ return output ? this.#inputStateFor(output).inputBytes : 0;
228
+ }
229
+
230
+ runsOf(output) {
231
+ return output ? this.#host.encodeOrchestrator.runsOn(output.outputKey) : [];
232
+ }
233
+
234
+ /**
235
+ * Where the earliest live encoder of this output began, or null with none.
236
+ *
237
+ * @param {object} output
238
+ * @returns {number | null}
239
+ */
240
+ earliestStartOf(output) {
241
+ return earliestRunStart(this.runsOf(output));
242
+ }
243
+
244
+ isLive(output) {
245
+ return Boolean(output && this.#host.outputs.get(output.id) === output);
246
+ }
247
+
248
+ liveRunsOf(output) {
249
+ return liveRunsOf(this.runsOf(output));
250
+ }
251
+
252
+ runStateOf(output) {
253
+ return output ? this.#host.encodeOrchestrator.stateOf(output.outputKey) : runStateOf([]);
254
+ }
255
+
256
+ lastErrorOf(output) {
257
+ return output ? this.#host.encodeOrchestrator.errorOf(output.outputKey) : "";
258
+ }
259
+
260
+ /**
261
+ * Whether this output's encoding has failed for good.
262
+ *
263
+ * @param {object} output
264
+ * @returns {boolean}
265
+ */
266
+ hasFailed(output) {
267
+ return this.runStateOf(output) === ENCODE_RUN_STATE.ENDED_FAILED;
268
+ }
269
+
270
+ /**
271
+ * Whether this output's input went away and is being fetched again.
272
+ *
273
+ * @param {object} output
274
+ * @returns {boolean}
275
+ */
276
+ isWaitingForInput(output) {
277
+ return this.runStateOf(output) === ENCODE_RUN_STATE.RETRY_WAIT;
278
+ }
279
+
280
+ /**
281
+ * The run state in the words the browser is told.
282
+ *
283
+ * @param {object} output
284
+ * @returns {string}
285
+ */
286
+ wireStateOf(output) {
287
+ return wireState(this.runStateOf(output));
288
+ }
289
+
290
+ /**
291
+ * What went wrong, when this output's encoding has failed; empty otherwise.
292
+ *
293
+ * @param {object} output
294
+ * @returns {string}
295
+ */
296
+ failureOf(output) {
297
+ return this.hasFailed(output) ? this.lastErrorOf(output) : "";
298
+ }
299
+
300
+ progressOf(output, index = null) {
301
+ if (!output) {
302
+ return null;
303
+ }
304
+ const at = Number.isInteger(index)
305
+ ? index
306
+ : this.#host.outputTimes.segmentIndexForTime(output, this.#host.viewerSecondsOn(output));
307
+ const progress = this.#host.encodeOrchestrator.progressOf(output.outputKey, at);
308
+ if (progress) {
309
+ return progress;
310
+ }
311
+ const start = this.#host.viewerSecondsOn(output);
312
+ const total = Number(output.file?.durationSeconds);
313
+ return {
314
+ processedSeconds: start,
315
+ startPositionSeconds: start,
316
+ totalSeconds: Number.isFinite(total) && total > 0 ? total : null,
317
+ percent: 0,
318
+ remainingSeconds: Number.isFinite(total) && total > 0 ? Math.max(0, total - start) : null,
319
+ speed: "",
320
+ updatedAt: this.#host.outputs.startedAt(output) ?? Date.now()
321
+ };
322
+ }
323
+
324
+ planEncodersNow() {
325
+ const byOutput = this.#outputsWithSessions();
326
+ for (const [address, sessions] of byOutput) {
327
+ // From the TIMELINE, which is where how a file is cut has lived since
328
+ // 2.76.0. Read off the session it left, this was `undefined` on every
329
+ // session ever made: the map then held no length, and the walk that
330
+ // gives a run its end ran to MAX_SAFE_INTEGER — the main thread spun at
331
+ // 100% and the proxy answered nothing, measured on the addon host
332
+ // 2026-09-05 with the stack read out of the live process.
333
+ const segmentCount = Number(sessions[0].timeline?.segmentCount) || 0;
334
+ if (segmentCount > 0) {
335
+ this.#host.encodeOrchestrator.setSegmentCount(address, segmentCount);
336
+ }
337
+ }
338
+ // THE MAP IS BUILT ONCE, IN ITS OWN LAYER, AND BOTH SIDES READ THAT ONE.
339
+ //
340
+ // It used to be built twice: once here, per viewer, converted and stated to
341
+ // the encoding as a window each, and once again inside `publishFor` for the
342
+ // downloading. Two answers to one question, and the encoding's copy carried
343
+ // the viewer's NAME as the key of a claim — against the rule that the
344
+ // encoding and the viewer are not connected at all.
345
+ this.#host.priority.publishFor({
346
+ sessionGroups: byOutput.values(),
347
+ });
348
+ // FROM THE VIEWERS OF THAT OUTPUT, not from the viewers of the film.
349
+ //
350
+ // Both scopes are right for what asks them: the swarm is asked for bytes of
351
+ // a FILE, which every output of it reads, and encoders are placed per
352
+ // OUTPUT, which a person watching 480p wants nothing of at 1080p. Handed the
353
+ // film's map, the plan wanted an encoder on every output of it, and what
354
+ // stopped the unwatched ones was this class killing them by its own
355
+ // judgement — while the viewer's own move announced itself and had the plan
356
+ // start them again.
357
+ //
358
+ // Converted into each output's own numbering, because two outputs of one
359
+ // film are cut independently and the same second is a different number in
360
+ // each: 454 pieces against 401 on the field file.
361
+ for (const [address, sessions] of byOutput) {
362
+ const timeline = sessions[0].timeline;
363
+ this.#host.encodeOrchestrator.notePriorityMap(
364
+ address,
365
+ timeline?.inSegments?.(
366
+ this.#host.priority.mapForOutput(address),
367
+ Number(timeline?.segmentCount) || 0
368
+ ) ?? []
369
+ );
370
+ }
371
+ this.#host.encodeOrchestrator.reconcile();
372
+ }
373
+
374
+ /**
375
+ * Re-decide what encoders should exist, once, after the change that is being
376
+ * made now.
377
+ *
378
+ * COALESCED, NOT DELAYED. One turn of the event loop may carry several
379
+ * changes — a viewer arrives and is placed and states their soundtrack — and
380
+ * each of them is a reason to re-decide, while re-deciding three times in a
381
+ * row would give the same answer three times and could act on a half-built
382
+ * state. So the decision is taken once, after the current turn, and no
383
+ * interval is involved: there is nothing to choose and nothing to tune.
384
+ *
385
+ * @returns {void}
386
+ */
387
+ planEncodersSoon() {
388
+ if (this.#planScheduled) {
389
+ return;
390
+ }
391
+ this.#planScheduled = true;
392
+ queueMicrotask(() => {
393
+ this.#planScheduled = false;
394
+ try {
395
+ this.planEncodersNow();
396
+ } catch (error) {
397
+ const message = error instanceof Error ? error.message : String(error);
398
+ this.#host.logger.warn(`transcode could not re-plan encoders: ${message}`);
399
+ }
400
+ });
401
+ }
402
+
403
+ /**
404
+ * The live sessions grouped by the output they produce.
405
+ *
406
+ * @returns {Map<string, HlsSession[]>}
407
+ */
408
+ #outputsWithSessions() {
409
+ /** @type {Map<string, HlsSession[]>} */
410
+ const byOutput = new Map();
411
+ for (const session of this.#host.outputs.values()) {
412
+ const address = session.outputKey ?? "";
413
+ if (!address) {
414
+ continue;
415
+ }
416
+ byOutput.set(address, [...(byOutput.get(address) ?? []), session]);
417
+ }
418
+ return byOutput;
419
+ }
420
+
421
+ forgetEncodingOfGone(output) {
422
+ if (this.#host.outputs.outputsOn(output.outputKey).length === 0) {
423
+ this.#host.encodeOrchestrator.forgetOutput(output.outputKey);
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Make an encoder for this output, beginning at this segment.
429
+ *
430
+ * Another run of the SAME session, not another session. A run at another
431
+ * position is not a different output — the material, the tracks, the grid and
432
+ * the box are all the same, and only where it begins differs, which changes
433
+ * no byte of what is produced. Both write into the output's one directory and
434
+ * either viewer is served whatever either has made.
435
+ *
436
+ * @param {string} address
437
+ * @param {number} from
438
+ * @param {number} to
439
+ * @param {string} because - The plan's own words for why it placed this one.
440
+ * @returns {object | null}
441
+ */
442
+ makeRunAt(address, from, to, because) {
443
+ let base = null;
444
+ for (const session of this.#host.outputs.values()) {
445
+ if (session.outputKey === address) {
446
+ base = session;
447
+ break;
448
+ }
449
+ }
450
+ if (!base) {
451
+ return null;
452
+ }
453
+ // A position that has already failed to start, this many times running,
454
+ // will fail again: nothing about it has changed between the attempts, which
455
+ // is exactly why the attempts keep taking the same fraction of a second and
456
+ // ending the same way.
457
+ //
458
+ // The plan is arithmetic over coverage and demand, and neither of them
459
+ // knows that a process refused to start — so without asking here, the plan
460
+ // commands the same start, the run ends, the ended stretch goes back to the
461
+ // map, and the plan commands it again. Measured 2026-09-05 with ffmpeg
462
+ // absent: fifty passes of the plan in the time a probe took to notice, as
463
+ // fast as spawning could fail. The five-second timer this decision used to
464
+ // sit on hid that, at the price of hiding it in the field too — the loop
465
+ // seen there ran for sixteen minutes and read as "a restart every five
466
+ // seconds".
467
+ //
468
+ // A DIFFERENT position is unaffected and gets its own budget, and the count
469
+ // resets the moment a run at this one does real work.
470
+ if (!this.#host.encodeOrchestrator.mayStartAt(address, from)) {
471
+ return null;
472
+ }
473
+ // The encoder is built and handed back in this same call. Nothing here
474
+ // waits: the one thing this path used to wait for was the death of the run
475
+ // it was replacing, and that killing is gone. Answering with nothing while
476
+ // the encoder was built behind the answer is what let the same stretch be
477
+ // started over and over — 684 starts in 482 seconds of field 2026-09-05.
478
+ try {
479
+ return this.#startEncodeRun(base, from, because, { to });
480
+ } catch (error) {
481
+ const message = error instanceof Error ? error.message : String(error);
482
+ this.#host.logger.warn(`transcode could not start a run at #${from} of ${address}: ${message}`);
483
+ return null;
484
+ }
485
+ }
486
+
487
+ /**
488
+ * How many encoders this machine can afford on one output at once.
489
+ *
490
+ * Measured rather than chosen, and it is the same arithmetic that decides
491
+ * which quality steps are offered: what a second job costs here is not
492
+ * assumed to be nothing. Field 2026-09-03 on the addon host, `testsrc2`
493
+ * through libx264 `ultrafast` — at 854x480 one run makes 7.12x and two make
494
+ * 4.20x and 4.16x, so both stay far above realtime and a second is
495
+ * affordable; at 1920x1080 one makes 1.96x and two make 0.99x and 0.98x, so
496
+ * the machine is full at one.
497
+ *
498
+ * @param {string} address
499
+ * @returns {number}
500
+ */
501
+ maxRunsForOutput(address) {
502
+ const alone = this.#host.encodeCost.speedForOutput(address);
503
+ if (!(alone > 0)) {
504
+ // The host measured nothing at all, which is a broken startup rather than
505
+ // a state to plan around. One encoder is what it keeps.
506
+ return 1;
507
+ }
508
+ let affordable = 1;
509
+ for (;;) {
510
+ const { penalty, measured, from } = contentionPenalty(affordable, this.#host.contentionPenalties);
511
+ // An UNMEASURED penalty is 1, and that is the honest answer to "what does
512
+ // a second job cost" only in the sense that nothing has been measured —
513
+ // it is not a statement that a second job is free. Measured on the addon
514
+ // host it is 1.70x at 854x480 and 1.98x at 1920x1080, so taking 1 would
515
+ // let a machine that cannot hold two runs start two. Where nothing has
516
+ // been measured, one is what it has.
517
+ //
518
+ // `from` is the concurrency the reading came from, and past the measured
519
+ // range `contentionPenalty` HOLDS the largest reading rather than continue
520
+ // a curve two points cannot describe. Held is the right answer to "what
521
+ // does this cost", and the wrong one to "may I start another": it would
522
+ // price a fifth encoder at what a second was measured to cost, and a fast
523
+ // host would keep dividing until some chosen ceiling stopped it. So the
524
+ // ladder stops where the measurements stop — the bound on how many
525
+ // encoders may run is measured, like the budget itself, and there is no
526
+ // constant here to overrule it.
527
+ if (!measured || from !== affordable || !(alone / penalty >= 1)) {
528
+ break;
529
+ }
530
+ affordable += 1;
531
+ }
532
+ return affordable;
533
+ }
534
+
535
+ /**
536
+ * A segment this run made has just been served.
537
+ *
538
+ * The one event whose raising is guarded by the state, and the guard is a
539
+ * READ of the state rather than a second copy of it: "something has been
540
+ * produced" is a level, not an edge, so without the guard every served
541
+ * segment would raise it and the log would fill with refusals.
542
+ *
543
+ * Whose file it is decides the rest, and the directory answers that outright:
544
+ * runs write into one each, and serving searches them newest-first, so a
545
+ * segment left by an EARLIER run is served routinely. Comparing indices
546
+ * instead would have been fooled by the ordinary case of a backward seek —
547
+ * the new run starts at #10, the old one left #50 on disk, and #50 is above
548
+ * the new run's start index while saying nothing about it.
549
+ *
550
+ * @param {HlsSession} session
551
+ * @param {string} filePath - Where the served segment was actually found.
552
+ * @returns {void}
553
+ */
554
+ noteRunProducedSegment(session, filePath) {
555
+ if (typeof filePath !== "string" || path.dirname(filePath) !== this.#host.segmentFiles.pathFor(session.outputKey ?? "")) {
556
+ return;
557
+ }
558
+ const index = session.segmentFormat.segmentIndexFromName(path.basename(filePath));
559
+ if (index === null) {
560
+ return;
561
+ }
562
+ // Whichever run was given this number. An output has as many runs as the
563
+ // machine affords, and a segment moves the one it belongs to out of
564
+ // starting — not whichever happens to be listed first.
565
+ ownRunMaking(this.runsOf(session), index)?.noteProduced(index);
566
+ }
567
+
568
+ /**
569
+ * Say so when a run is about to encode a picture the init segment already in
570
+ * the player's hands does not describe.
571
+ *
572
+ * This is the whole class of fault named in one line, and it exists because
573
+ * the fault is otherwise SILENT: no layer reports an error, the encoder is
574
+ * healthy, segments are served in milliseconds, and what the viewer gets is
575
+ * either a band of macroblock garbage or a picture that never appears. The
576
+ * shape is the one 2.48.0 uses for the TIME a run begins at — a run states
577
+ * where it really landed, and a disagreement with what was published is
578
+ * named rather than left to be inferred from a browser's own reading.
579
+ *
580
+ * The size the init describes is read from the init's own bytes, not taken
581
+ * from our record of what the encoder was told, because those two disagreeing
582
+ * IS the fault. The size the run will produce is computed by the same
583
+ * arithmetic the scale filter performs, so a source smaller than the target
584
+ * box is not reported as a disagreement.
585
+ *
586
+ * Said once per distinct pair of sizes: a run repeated at the same wrong size
587
+ * has nothing further to say.
588
+ *
589
+ * @param {HlsSession} session
590
+ * @returns {void}
591
+ */
592
+ #warnIfRunLeavesTheInitBehind(session) {
593
+ const initBytes = this.#host.segmentFiles.initOf(session.outputKey ?? "");
594
+ if (!session.spec.transcodesVideo || !initBytes || initBytes.length === 0) {
595
+ return; // nothing served yet, or nothing being encoded
596
+ }
597
+ const format = session.segmentFormat;
598
+ if (typeof format?.initVideoSize !== "function") {
599
+ return; // a self-describing container (MPEG-TS) cannot have this fault
600
+ }
601
+ const described = format.initVideoSize(initBytes);
602
+ if (!described) {
603
+ return;
604
+ }
605
+ if (!(session.output.encodeWidth > 0) || !(session.output.encodeHeight > 0)) {
606
+ return; // the run keeps the encoder's own default box; nothing was told
607
+ }
608
+ const producing = computeOutputDimensions(
609
+ session.output.encodeWidth,
610
+ session.output.encodeHeight,
611
+ session.file.width,
612
+ session.file.height
613
+ );
614
+ if (!producing) {
615
+ return; // the source size is unknown, so nothing can be predicted
616
+ }
617
+ if (described.width === producing.w && described.height === producing.h) {
618
+ return;
619
+ }
620
+ const said = `${described.width}x${described.height}->${producing.w}x${producing.h}`;
621
+ const inputState = this.#inputStateFor(session);
622
+ if (inputState.initSizeSaid === said) {
623
+ return;
624
+ }
625
+ inputState.initSizeSaid = said;
626
+ this.#host.logger.warn(
627
+ `transcode ${session.id} is about to encode ${producing.w}x${producing.h} while the init segment ` +
628
+ `the player holds describes ${described.width}x${described.height} "${session.file.name}" — ` +
629
+ `every fragment of this run will be decoded against parameter sets for a picture that is not ` +
630
+ `being made. A change of size is a change of variant; nothing here should have moved it.`
631
+ );
632
+ }
633
+
634
+ /**
635
+ * How far a run starting here may work before it meets somebody else's
636
+ * material — read off the ONE coverage map, never worked out again here.
637
+ *
638
+ * This replaced `planRunInterval`, which was a second authority over the same
639
+ * question and answered it by different rules: it walked the whole track for
640
+ * the first free number, MOVED the start there itself, and counted every live
641
+ * run as claiming up to `head + look-ahead`. Measured in the field
642
+ * 2026-09-05: the plan commanded a start at #46, this moved it to #78 — the
643
+ * "run moved forward from #46 to #78" line — and the plan, seeing a run
644
+ * outside the window it had asked for, killed it as unwanted and commanded
645
+ * the same start again, 350-700ms per cycle, dozens of times, the viewer's
646
+ * picture stopped for 125 seconds.
647
+ *
648
+ * Where a run STARTS is the plan's decision and arrives here as an argument.
649
+ * All this answers is where it must stop, and the answer is a fact of the
650
+ * map: the free stretch from that number on. `-1` means the end of the track,
651
+ * which is what "no end" is written as everywhere here.
652
+ *
653
+ * @param {object} session
654
+ * @param {number} startIndex
655
+ * @param {object | null} exceptRun - The run being replaced, whose own claim
656
+ * is not somebody else's material.
657
+ * @returns {number} The last number to work through, or `-1` for the end.
658
+ */
659
+ #runEndFrom(session, startIndex, exceptRun = null) {
660
+ return this.#host.encodeOrchestrator.freeStretchEnd({
661
+ address: session.outputKey ?? "",
662
+ from: startIndex,
663
+ exceptRun,
664
+ segmentCount: Number(session.timeline?.segmentCount) || 0
665
+ });
666
+ }
667
+
668
+ // Returns the encoder it built, or nothing when there was nothing to build.
669
+ // It waits for nothing: the one thing it used to wait for was the death of
670
+ // the run it was replacing, and that killing is gone.
671
+ #startEncodeRun(session, startIndex, because, ordered = null) {
672
+ if (!this.isLive(session)) {
673
+ return null;
674
+ }
675
+ // A new run starts the quality budget's reckoning afresh: a pair of
676
+ // readings spanning the restart would count the gap between two runs as
677
+ // slow encoding. Told, not written: those readings are the budget's.
678
+ this.#host.noteRunStarting(session);
679
+ // Where a restart's seconds go. A seek costs 5-8 s in the field and the
680
+ // recorded reason — waiting for the previous ffmpeg to exit, measured at
681
+ // 0.54-1.47 s — does not account for it. Before rebuilding the hottest path
682
+ // in the proxy on a guess, make each stage state its own cost.
683
+ const restartEnteredAt = Date.now();
684
+ // Reads where the old run began BEFORE the new one takes its place, and
685
+ // does not await: everything below is the restart path, which is measured
686
+ // in milliseconds and has been worked on twice to keep it that way.
687
+ this.#accountBackwardRestart(session, startIndex);
688
+ // STARTING AN ENCODER STOPS NOTHING. It used to stop any live run whose own
689
+ // start was not below this one's — a rule left over from when a session
690
+ // held exactly one run and "the previous one" meant "the only one". Once a
691
+ // an output could hold several, that rule began killing runs the plan had
692
+ // decided to keep: 294 stops for this reason in eight minutes of field
693
+ // 2026-09-05, against four starts asked for by a viewer. Who is stopped is
694
+ // decided in one place, and it is not this one.
695
+ //
696
+ // HOW FAR IT MAY WORK is decided by the same one place and arrives here as
697
+ // an argument. Reading it off the coverage map a second time was the last
698
+ // remaining second answer to that question: the plan computed `to`, passed
699
+ // it, and the parameter list did not name it.
700
+ const runEnd = Number.isInteger(ordered?.to)
701
+ ? ordered.to
702
+ : this.#runEndFrom(session, startIndex, null);
703
+ // The restart backs off a segment or two from what was asked for, so the
704
+ // request that prompted it is recorded under a HIGHER index than the run
705
+ // starts at. Looking it up by the start index alone found nothing and the
706
+ // line never printed once.
707
+ let wantedAt = null;
708
+ const inputState = this.#inputStateFor(session);
709
+ for (const [index, at] of inputState.firstWantedAt) {
710
+ if (index >= startIndex && (wantedAt === null || at < wantedAt)) {
711
+ wantedAt = at;
712
+ }
713
+ }
714
+ if (typeof wantedAt === "number") {
715
+ this.#host.logger.info(
716
+ `transcode ${session.id} restart for #${startIndex} decided ` +
717
+ `${restartEnteredAt - wantedAt}ms after it was first asked for`
718
+ );
719
+ }
720
+ // Cleared with the run that could not answer them. These are "how long has
721
+ // this segment gone unanswered", and the question only means anything about
722
+ // the run in force: a timestamp kept from an abandoned scan probe minutes
723
+ // ago says a fresh request has already waited long enough, which is how the
724
+ // behind-head repair came to fire on the very first poll instead of waiting
725
+ // for the seek that should move the encoder. It also stops the map growing
726
+ // for the life of a session.
727
+ inputState.firstWantedAt.clear();
728
+ // WHERE THIS NUMBER REALLY BEGINS, when a produced piece has said so and
729
+ // the table the player holds still says otherwise.
730
+ //
731
+ // Only a soundtrack, and only because a soundtrack has no keyframes: it
732
+ // begins exactly where it is asked to, to within one audio frame, while the
733
+ // picture can begin nowhere but a real keyframe and so moves forward to the
734
+ // next one — up to three seconds, measured 2026-08-17. Left at the published
735
+ // time, the sound of one film starts up to that far from its picture after
736
+ // every restart, each correctly labelled with where it really is, and the
737
+ // viewer gets sound with no new picture for the difference.
738
+ //
739
+ // DERIVED HERE rather than handed in. It used to arrive as an argument from
740
+ // the one caller that knew it — the correction that had just measured it —
741
+ // which meant the instant was only ever right for a run that caller
742
+ // started. Any run the plan placed at a corrected number got the published
743
+ // time and landed apart again. It is a fact of the FILE's cutting, held in
744
+ // the live table every session of the file shares, so it is read from
745
+ // there.
746
+ const positionSecondsOverride = session.spec.carries === "audio-only"
747
+ ? trueStartOf(session.timeline, startIndex)
748
+ : undefined;
749
+
750
+ // What is still read off the session for a run. The list is the measure of
751
+ // how far a session still is from being the three things a run is built
752
+ // from — the material, what is produced of it, and the stretch — and it
753
+ // shrinks in place as those are taken off. There is deliberately no method
754
+ // wrapping it: a named adapter with one caller is a thing to remember to
755
+ // delete, and this is a thing that disappears by being emptied.
756
+ const inputs = this.#host.inputOf(session);
757
+ const { args, safeIndex, startSeconds, cutTimes } = buildRunCommand({
758
+ keyframes: session.keyframes,
759
+ inputFile: inputs.inputFile,
760
+ audioFile: inputs.audioFile,
761
+ inputUrl: inputs.inputUrl,
762
+ audioInputUrl: inputs.audioInputUrl,
763
+ timeline: session.timeline,
764
+ output: session.output,
765
+ segmentFormat: session.segmentFormat,
766
+ transcodeVideo: session.spec.transcodesVideo,
767
+ transcodeAudio: session.spec.transcodesAudio,
768
+ audioOnly: session.spec.carries === "audio-only",
769
+ audioSeparate: this.#host.servesAudioSeparately(session),
770
+ audioSourceTrackIndex: session.spec.audioSourceTrackIndex,
771
+ rateControl: session.spec.video?.encode?.rateControl ?? null,
772
+ startIndex,
773
+ endIndex: runEnd,
774
+ positionSecondsOverride,
775
+ videoEncoder: this.#host.videoEncoder,
776
+ segmentDurationSec: this.#host.segmentDurationSec
777
+ });
778
+
779
+ // The exact command, every run. An encode failure is otherwise reported
780
+ // with ffmpeg's message and nothing about what it was asked to do, and the
781
+ // two are not always deducible from each other: 2026-08-04 a run died with
782
+ // "Cannot write moov atom before AC3 packets" although both muxing paths
783
+ // were verified to handle a copied AC-3 track on this very host, so the
784
+ // arguments that run actually received are the missing evidence. One line
785
+ // per run, and a run happens at most every few seconds.
786
+ // Numbered, because a burst of seeks starts several runs in one second and
787
+ // every line about them carries the SESSION id, which is the same for all.
788
+ // Without a run number the command that failed cannot be told from the two
789
+ // that succeeded around it — which is exactly the state the unexplained
790
+ // `Cannot write moov atom before AC3 packets` was found in.
791
+
792
+ // One directory for the output, and every run writes straight into it.
793
+ //
794
+ // Runs used to be kept apart by a directory each, because two of them
795
+ // writing one segment name at the same time produce a file belonging to
796
+ // neither — which was also the only reason a restart ever had to wait for
797
+ // its predecessor to die. Intervals remove that by construction: a run is
798
+ // given a stretch nobody else holds and stops at the end of it, so no two
799
+ // runs ever want the same number. What is left of the old reason — a run
800
+ // killed mid-piece leaving a partial file — is not a correctness problem
801
+ // either, because the store serves a segment only once its closure is
802
+ // proven, and it is cleared up when the run ends.
803
+ const run = new EncodeRun({
804
+ address: session.outputKey ?? session.id,
805
+ encoder: this.#host.videoEncoder,
806
+ from: safeIndex,
807
+ to: runEnd,
808
+ buildArgs: () => args,
809
+ argsDescribed: describeFfmpegArgs(args),
810
+ // Whether this run cuts at times we gave it. Decides how a segment is
811
+ // judged finished — see getFileStream.
812
+ usesExplicitCuts: Boolean(cutTimes && cutTimes.length > 0),
813
+ startSeconds,
814
+ totalSeconds: Number(session.file.durationSeconds) || null,
815
+ spawn: (spawnArgs) =>
816
+ spawn(this.#host.ffmpegBin, spawnArgs, {
817
+ cwd: this.#host.segmentFiles.directoryFor(session.outputKey ?? ""),
818
+ // A fourth channel: the encoder names every piece it has CLOSED on it,
819
+ // which is the only proof a piece is whole.
820
+ stdio: ["ignore", "pipe", "pipe", "pipe"]
821
+ }),
822
+ logger: this.#host.logger,
823
+ // The film's last segment number, which is what tells "it reached the
824
+ // end" from "its input dried up": ffmpeg exits zero for both and over a
825
+ // torrent cannot tell them apart.
826
+ lastSegmentIndex: () =>
827
+ session.timeline?.segmentCount > 0 ? session.timeline.segmentCount - 1 : null,
828
+ inputUnavailable: (message) => isInputUnavailable(message),
829
+ onProgress: (report) => this.#noteRunProgress(session, run, report),
830
+ indexOfName: (name) => session.segmentFormat.segmentIndexFromName(name),
831
+ // Why this encoder exists, recorded with its argument list. It used to be
832
+ // handed to a separate `start` call; there is no separate call now.
833
+ because,
834
+ onClosed: (name) => this.#host.segmentFiles.publish(session.outputKey ?? "", name, session.segmentFormat),
835
+ onEnded: (ended) => this.noteRunEnded(session, run, ended)
836
+ });
837
+ // THE ONE FAULT THAT IS OTHERWISE SILENT, asked before this run produces a
838
+ // frame. It lost its caller in a refactor on 2026-09-04 and had none until
839
+ // 2026-09-15 — not by a decision, which is why it is back rather than gone.
840
+ this.#warnIfRunLeavesTheInitBehind(session);
841
+
842
+ this.#host.logger.info(
843
+ `transcode ${session.id} encode-run #${safeIndex}..#${runEnd} from segment #${safeIndex} ` +
844
+ `(+${Date.now() - restartEnteredAt}ms since the restart was asked for) ` +
845
+ `(${formatSeconds(startSeconds)}) "${session.file.name}"`
846
+ );
847
+ // The four numbers a run is positioned by, said once, because their
848
+ // disagreement is invisible everywhere else. The two tables are printed
849
+ // side by side: while they differ, every cut of this run is off by the
850
+ // difference, and nothing downstream can tell that from a bad index.
851
+ const liveStart = this.#host.outputTimes.segmentStartTime(session, safeIndex);
852
+ this.#host.logger.info(
853
+ `transcode ${session.id} run #${safeIndex}..#${runEnd} positioned at ${startSeconds.toFixed(3)}s ` +
854
+ `for boundary #${safeIndex} (published ${startSeconds.toFixed(3)}s, ` +
855
+ `live ${liveStart.toFixed(3)}s, apart ${(liveStart - startSeconds).toFixed(3)}s), ` +
856
+ `numbering from #${safeIndex}`
857
+ );
858
+ return run;
859
+ }
860
+
861
+ /**
862
+ * Log a report from a run that still belongs to this output. The run owns the
863
+ * clock and has already placed its relative ffmpeg time on the source
864
+ * timeline.
865
+ *
866
+ * @param {HlsSession} session
867
+ * @param {import("./EncodeRun.js").EncodeRun} run
868
+ * @param {object} progress
869
+ * @returns {void}
870
+ */
871
+ #noteRunProgress(session, run, progress) {
872
+ if (!this.isLive(session) || !this.runsOf(session).includes(run)) {
873
+ return;
874
+ }
875
+ if (run.progress.shouldLog(PROGRESS_LOG_INTERVAL_MS)) {
876
+ this.#host.logger.info(
877
+ `transcode ${session.id} "${session.file.name}" ${progress.percent.toFixed(1)}% ` +
878
+ `(${formatSeconds(progress.processedSeconds)} / ${formatSeconds(progress.totalSeconds)})` +
879
+ ` speed=${progress.speed || "n/a"}`
880
+ );
881
+ }
882
+ }
883
+
884
+ /**
885
+ * What to do about a run that has ended.
886
+ *
887
+ * The run says how it ended and why; this decides what follows, which is a
888
+ * question about the SESSION and the machine rather than about the process:
889
+ * whether to condemn a hardware encoder, whether to wait for the input to
890
+ * come back, whether a position has failed often enough to stop retrying it.
891
+ *
892
+ * An ending from a run the session has already replaced is not ignored — it
893
+ * is simply not about this session's current run, so nothing that describes
894
+ * the current run is written from it. That is what the identity check used to
895
+ * be for, and it is one condition now instead of one at the head of every
896
+ * handler.
897
+ *
898
+ * @param {HlsSession} session
899
+ * @param {import("./EncodeRun.js").EncodeRun} run
900
+ * @param {import("./EncodeRun.js").RunEnded} ended
901
+ * @returns {void}
902
+ */
903
+ noteRunEnded(session, run, ended) {
904
+ // First, and for every run whatever else follows: the stretch it held goes
905
+ // back to the map. A run whose claim is never released tells the plan that
906
+ // numbers nobody is making are being made, and nothing is ever started
907
+ // there again.
908
+ // WAS this run still the session's when it ended? Asked before it is
909
+ // removed, because after the removal the question always answers "no" —
910
+ // and it was asked after, so every line below this point was unreachable.
911
+ //
912
+ // Measured 2026-09-05 over both of the field host's log files: zero
913
+ // occurrences of this handler's own `encode-run #… failed:` line and zero
914
+ // of `fast failure at segment`, across every session this proxy has ever
915
+ // run. So the fallback from a failed hardware encoder to software, the
916
+ // retry when the torrent data goes away, the limit on retrying a position
917
+ // that keeps failing, and the error line naming the ffmpeg command have all
918
+ // been dead code — which is also why nothing ever stopped the restart loop
919
+ // recorded in the field the same day.
920
+ const wasCurrent = this.runsOf(session).includes(run);
921
+ this.#host.encodeOrchestrator.noteEnded(ended);
922
+ // What this admitted encode was seen to do, kept for the next time this
923
+ // host prices the same mode (roadmap item 97, step 14). Whatever the
924
+ // ending: a run that failed still ran at the speed it ran at, and the
925
+ // segments it closed still carried what they carried.
926
+ this.#host.observeEncodeEnded?.(session);
927
+ // A stretch went back to the map, so what should be running has changed.
928
+ // Said here rather than waited for: this is the moment it became true.
929
+ this.planEncodersSoon();
930
+ // ANYBODY ALREADY WAITING IS TOLD, and it is asked HERE because every
931
+ // ending passes this line while the handlers below return one by one. It
932
+ // used to be the last statement of this method, which the `SHORT` branch —
933
+ // ffmpeg exiting 0 having produced less than the playlist promises —
934
+ // returns before reaching. That ending is terminal (`ENDED_FAILED`), so a
935
+ // request arriving after it is answered with the failure at once while a
936
+ // wait that BEGAN before it sat out its whole deadline, or, where the page
937
+ // states none, until the viewer disconnected.
938
+ //
939
+ // THE CONDITION IS THE OUTPUT'S ACTUAL STATE, not this run's ending.
940
+ // `stateOf` answers from the LIVE runs and falls back to the last ending
941
+ // only when none remain, so one run of several ending in failure wakes
942
+ // nobody — something is still making it — and the last one does. Asked
943
+ // after `noteEnded` has recorded the ending, so the woken request reads the
944
+ // final state; and it re-reads the disk first, because a piece already made
945
+ // is served whatever became of the encoder.
946
+ if (this.hasFailed(session)) {
947
+ this.#host.invalidateWaits(session);
948
+ // A move to this output being prepared for a viewer waits for a segment
949
+ // that nothing will now make, and no publication will ever end that wait.
950
+ // This is the event that does (roadmap item 97, step 12).
951
+ this.#host.productionFailed(session);
952
+ }
953
+ if (!this.isLive(session)) {
954
+ this.forgetEncodingOfGone(session);
955
+ return;
956
+ }
957
+ if (!wasCurrent) {
958
+ // A run the session had already replaced. It has logged its own ending;
959
+ // nothing about the session follows from it.
960
+ return;
961
+ }
962
+ const lastError = this.lastErrorOf(session) || ended.because;
963
+ if (ended.ending === ENCODE_EXIT.STOPPED || ended.ending === ENCODE_EXIT.GONE) {
964
+ return;
965
+ }
966
+ if (ended.ending === ENCODE_EXIT.COMPLETE) {
967
+ this.#host.logger.info(`transcode ${session.id} encode-run complete "${session.file.name}"`);
968
+ return;
969
+ }
970
+ if (ended.ending === ENCODE_EXIT.SHORT) {
971
+ // ffmpeg exits 0 both when it reaches the end of the file and when its
972
+ // input simply stops producing bytes — over HTTP the two look identical
973
+ // to it. Field 2026-08-05: the torrent's download died, the read ended,
974
+ // and a run that had made 188 segments of 624 reported itself complete;
975
+ // the player then consumed what was on disk and froze on the first
976
+ // segment nobody was making. So the claim is checked against the playlist
977
+ // we published, and a run that stopped short is a FAILURE that can be
978
+ // restarted, not a finished file.
979
+ this.#host.logger.error(
980
+ `transcode ${session.id} encode-run #${ended.from}..#${ended.to} ended early: ` +
981
+ `${ended.because} "${session.file.name}"`
982
+ );
983
+ return;
984
+ }
985
+ // Runtime safety net: if a hardware encode fails, downgrade this proxy to
986
+ // software encoding for all sessions and restart this one, so playback is
987
+ // never permanently broken by a hardware/driver issue.
988
+ //
989
+ // Asked only of a genuine encoder failure. It used to be asked of every
990
+ // non-zero exit, so a run whose TORRENT DATA went away — which says nothing
991
+ // whatever about the encoder — condemned a working NVENC or QuickSync to
992
+ // software for the life of the process, and started an extra run at the old
993
+ // index while it was at it.
994
+ if (ended.ending === ENCODE_EXIT.FAILED && session.spec.transcodesVideo && this.#host.videoEncoder.kind !== "software") {
995
+ const failed = this.#host.encoders.useSoftware();
996
+ const failedEncoder = failed?.name ?? this.#host.videoEncoder.name;
997
+ this.#host.logger.warn(
998
+ `transcode ${session.id} hardware encoder ${failedEncoder} failed ` +
999
+ `(${lastError}); falling back to software libx264, and every output named by ${failedEncoder} is closed`
1000
+ );
1001
+ // An output IS its format, and the encoder is part of it: pieces from
1002
+ // another encoder cannot be decoded with the header this output's viewers
1003
+ // already hold. So the outputs made by the failed encoder end here, and
1004
+ // the next request for that picture names a format built on the new one.
1005
+ //
1006
+ // THE ONE DISPOSAL THAT DOES NOT ASK WHETHER ASSIGNMENTS STILL HOLD IT, and
1007
+ // deliberately. Every other one asks, because a repeat of a request must be
1008
+ // answered by what answered it first. Here what answered it first has no
1009
+ // encoder that can go on making it: holding the output keeps only a name
1010
+ // whose next segment nobody will produce. A response already being sent is
1011
+ // not cut short by this — it reads from a file already opened.
1012
+ for (const other of [...this.#host.outputs.values()]) {
1013
+ if (other.spec.video?.encode?.encoder === failedEncoder) {
1014
+ void this.#host.disposeSession(other.id);
1015
+ }
1016
+ }
1017
+ this.planEncodersSoon();
1018
+ return;
1019
+ }
1020
+ // Losing the INPUT is not the session failing — it is the data not being
1021
+ // there YET. The torrent can be re-added and re-downloaded, so the honest
1022
+ // answer to the viewer is "still working", not an error screen. Field
1023
+ // 2026-08-06: a torrent evicted mid-seek took the film with it, the run
1024
+ // died on `File 0 not found`, the session went terminal and answered 500 to
1025
+ // every request from then on — although the swarm was there and the data
1026
+ // would have come back in seconds. The circuit breaker below stays for what
1027
+ // it was built for, a target that genuinely cannot be encoded; it must not
1028
+ // condemn a session whose data merely went away.
1029
+ if (ended.ending === ENCODE_EXIT.INPUT_LOST) {
1030
+ const inputState = this.#inputStateFor(session);
1031
+ inputState.inputRetryCount += 1;
1032
+ this.#host.logger.warn(
1033
+ `transcode ${session.id} encode-run #${ended.from}..#${ended.to} lost its input ` +
1034
+ `(${lastError}) (attempt ${inputState.inputRetryCount})`
1035
+ );
1036
+ // HOW LONG TO WAIT IS THE PLAN'S, and this says only what happened. The
1037
+ // delay used to be timed here, against the dead run, which the plan never
1038
+ // consults — so it placed a fresh run at the same spot as fast as ffmpeg
1039
+ // could fail there: 2432 starts in 23 minutes in the field 2026-09-12,
1040
+ // against a delay that had reached its 15 s ceiling long before. The
1041
+ // orchestrator holds it now, beside the decision it governs.
1042
+ if (run.state === ENCODE_RUN_STATE.RETRY_WAIT) {
1043
+ run.retryDue();
1044
+ }
1045
+ return;
1046
+ }
1047
+ // A run that exits THIS fast never did real work: it failed at the start
1048
+ // itself — opening the input, spawning the process — rather than
1049
+ // mid-stream. Consecutive fast failures at the SAME position are counted so
1050
+ // that whoever commands a start can stop commanding one that keeps failing.
1051
+ //
1052
+ // COUNTED AT EVERY POSITION, #0 included. It used to be counted only past
1053
+ // #0, because it was written for seek restarts and a seek is never to the
1054
+ // beginning. That left the one position the plan commands FIRST with no
1055
+ // count at all, and the plan re-commanding a start that cannot succeed is
1056
+ // an unbounded loop: measured 2026-09-05, ffmpeg failing to spawn produced
1057
+ // fifty passes of the plan before a probe stopped it, as fast as the
1058
+ // failures arrived.
1059
+ const failure = this.#host.encodeOrchestrator.noteStartFailure(
1060
+ session.outputKey,
1061
+ ended.from,
1062
+ ended.livedMs
1063
+ );
1064
+ if (failure.count > 0) {
1065
+ this.#host.logger.warn(
1066
+ `transcode ${session.id} fast failure at segment #${ended.from} ` +
1067
+ `(${ended.livedMs}ms) — ${failure.count}/${failure.limit} consecutive`
1068
+ );
1069
+ if (failure.blocked) {
1070
+ this.#host.logger.error(
1071
+ `transcode ${session.id} will not be started at #${ended.from} again: ` +
1072
+ `${failure.count} starts there failed within ${failure.fastMs}ms each ` +
1073
+ `(${lastError}) "${session.file.name}"`
1074
+ );
1075
+ }
1076
+ }
1077
+ this.#host.logger.error(
1078
+ `transcode ${session.id} encode-run #${ended.from}..#${ended.to} failed: ${lastError}` +
1079
+ ` — ${this.#describeTrackSelection(session)}` +
1080
+ `\n ffmpeg ${run.argsDescribed || "(command not recorded)"}`
1081
+ );
1082
+ }
1083
+
1084
+ /**
1085
+ * What this run asked the source for, against what the source said it has.
1086
+ *
1087
+ * Written for one failure in particular: every `-map` this proxy builds ends
1088
+ * in `?`, so ffmpeg drops a mapping for an absent stream without complaint,
1089
+ * and a run whose mappings ALL drop produces a file with nothing in it —
1090
+ * reported as `Output file does not contain any stream`, exit 255. Read from
1091
+ * the exit code alone that is indistinguishable from any other refusal. Read
1092
+ * beside the tracks the file actually holds it is unmistakable, and it names
1093
+ * which side is wrong: an audio index past the end of the list is ours, no
1094
+ * streams at all is the source's.
1095
+ *
1096
+ * @param {HlsSession} session
1097
+ * @returns {string}
1098
+ */
1099
+ #describeTrackSelection(session) {
1100
+ const wanted = [];
1101
+ // Named exactly as the command line names them, second input included: a
1102
+ // refusal whose message describes a different mapping than the one that was
1103
+ // refused is the reading that cost a wrong diagnosis before.
1104
+ const inputs = this.#host.inputOf(session);
1105
+ const audioInput = inputs.audioInputUrl.length > 0 ? 1 : 0;
1106
+ const audioTrack = session.spec.audioSourceTrackIndex;
1107
+ if (session.spec.carries === "audio-only") {
1108
+ wanted.push(`audio 0:a:${audioTrack}`);
1109
+ } else if (this.#host.servesAudioSeparately(session)) {
1110
+ wanted.push("video 0:v:0");
1111
+ } else {
1112
+ wanted.push("video 0:v:0", `audio ${audioInput}:a:${audioTrack}`);
1113
+ }
1114
+ const counts = session.file.streamCounts;
1115
+ const held = counts
1116
+ ? `the source holds ${counts.video} video, ${counts.audio} audio, ` +
1117
+ `${counts.subtitle} subtitle` +
1118
+ (counts.other > 0 ? `, ${counts.other} other` : "")
1119
+ : "what the source holds was not recorded";
1120
+ return `this run asked for ${wanted.join(" + ")}, and ${held}`;
1121
+ }
1122
+
1123
+ /**
1124
+ * Stop this session's encoder without replacing it.
1125
+ *
1126
+ * A variant nobody is watching must not go on encoding: the host has one
1127
+ * encoder's worth of capacity, and the whole point of switching quality
1128
+ * seamlessly is that the new rung gets it. The session itself stays — its
1129
+ * produced segments remain servable, and switching back restarts it from
1130
+ * where the viewer then is.
1131
+ *
1132
+ * @param {HlsSession} session
1133
+ * @param {string} reason - Named in the log; a stopped encoder is otherwise
1134
+ * indistinguishable from one that died.
1135
+ * @returns {void}
1136
+ */
1137
+ stopEncodeRun(session, reason) {
1138
+ const running = this.liveRunsOf(session);
1139
+ if (running.length === 0) {
1140
+ return;
1141
+ }
1142
+ // Every run this session has going. The callers all mean the session's
1143
+ // encoding as a whole: a rung nobody is watching any more, a session being
1144
+ // disposed. A run that had already finished or failed is not among them,
1145
+ // which is what keeps a stop from erasing how it actually ended.
1146
+ for (const run of running) {
1147
+ // The run resumes itself if it was suspended — a stopped process does not
1148
+ // act on SIGTERM until it is continued — records the cause, and answers
1149
+ // its own exit. Nothing here has to null a field so that the exit is read
1150
+ // correctly, because there is no shared field left to misread.
1151
+ run.stop(reason);
1152
+ // Nothing is cleared up from here. What this run left open is under its
1153
+ // own working name, and the encoding layer removes it when the run's
1154
+ // ending reaches it — one place, and it needs neither the stretch nor the
1155
+ // init bytes this method used to fetch to judge a file by its contents.
1156
+ }
1157
+ this.#host.logger.info(`transcode ${session.id} ${running.length} encoder(s) stopped: ${reason}`);
1158
+ }
1159
+
1160
+ /**
1161
+ * What moving the encoder BACKWARDS costs, said out loud when it happens.
1162
+ *
1163
+ * Nothing already written is lost — every run keeps its own directory and
1164
+ * {@link SegmentStore#pathOfName} serves the union of all of them
1165
+ * — so the price of a restart is not the files. It is two other things, and
1166
+ * neither was ever counted:
1167
+ *
1168
+ * - **work done twice.** The new run begins at the target and encodes
1169
+ * forward through segments the old run had already finished. ffmpeg cannot
1170
+ * know they exist, so it makes them again.
1171
+ * - **the viewer in front.** While the run walks back up to where it already
1172
+ * was, nothing new is being made ahead of them, and their cushion drains.
1173
+ *
1174
+ * Both are what decides whether a session should be allowed a SECOND
1175
+ * concurrent run instead — roadmap item 64. That question cannot be answered
1176
+ * from taste, and this is the reading it needs: how often it happens at all,
1177
+ * how far back, and how much of the walk is a repeat.
1178
+ *
1179
+ * Nothing here is awaited by the caller. Everything below the call site is
1180
+ * the restart path, which is measured in milliseconds and has been worked on
1181
+ * twice to keep it that way; a reading that delays the thing it is reading
1182
+ * about is not a reading. The figures that MUST be taken before the new run
1183
+ * exists are taken synchronously, and only the file counting is left to run
1184
+ * on its own — against the directories that existed at this instant, so what
1185
+ * the new run is about to write cannot be counted as already there.
1186
+ *
1187
+ * @param {HlsSession} session
1188
+ * @param {number} startIndex - Where the new run will begin.
1189
+ * @returns {void}
1190
+ */
1191
+ #accountBackwardRestart(session, startIndex) {
1192
+ const previousStart = earliestRunStart(this.runsOf(session));
1193
+ if (!Number.isInteger(previousStart) || !Number.isInteger(startIndex) || startIndex >= previousStart) {
1194
+ // A first run, or one moving forward. Neither costs anything here: a
1195
+ // forward restart skips material it never made.
1196
+ return;
1197
+ }
1198
+ const processed = Number(this.progressOf(session)?.processedSeconds);
1199
+ const head = Number.isFinite(processed)
1200
+ ? Math.max(previousStart, this.#host.outputTimes.segmentIndexForTime(session, processed))
1201
+ : previousStart;
1202
+ // Bounded: a session an hour in has thousands of segments, and the count is
1203
+ // for a comparison, not an inventory.
1204
+ const last = Math.min(head, startIndex + BACKWARD_RESTART_SCAN_SEGMENTS);
1205
+ const accounting = this.#inputStateFor(session).backwardRestarts;
1206
+ accounting.count += 1;
1207
+ accounting.segmentsBack += previousStart - startIndex;
1208
+ accounting.worstBack = Math.max(accounting.worstBack, previousStart - startIndex);
1209
+
1210
+ void (async () => {
1211
+ let alreadyOnDisk = 0;
1212
+ for (const index of this.#host.producedNumbers(session)) {
1213
+ if (index >= startIndex && index <= last) {
1214
+ alreadyOnDisk += 1;
1215
+ }
1216
+ }
1217
+ accounting.remade += alreadyOnDisk;
1218
+ this.#host.logger.info(
1219
+ `transcode ${session.id} moving the encoder BACK from #${previousStart} to #${startIndex} ` +
1220
+ `(head was #${head}): ${alreadyOnDisk} of the ${last - startIndex + 1} segment(s) it will walk through ` +
1221
+ `are already on disk and will be made again, and nothing is produced ahead of #${head} until it gets ` +
1222
+ `back there — ${accounting.count} backward restart(s) this session, worst ${accounting.worstBack} ` +
1223
+ `segment(s) back, ${accounting.remade} segment(s) remade in total (roadmap 64)`
1224
+ );
1225
+ })().catch(() => {
1226
+ // silent-ok: a reading that fails is not worth ending a restart over.
1227
+ });
1228
+ }
1229
+ }