@torrent-tv/proxy 2.89.1 → 2.89.2

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 (442) 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 +6 -1
  13. package/CHANGELOG.md +2299 -2288
  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/docs/download-architecture.md +261 -261
  22. package/docs/encode-architecture.md +729 -729
  23. package/docs/encode-run-state.md +96 -96
  24. package/docs/logs.md +97 -97
  25. package/docs/playback-start-forecast.md +42 -8
  26. package/docs/socks5-egress-research.md +114 -114
  27. package/docs/storage-architecture.md +267 -267
  28. package/knip.json +14 -14
  29. package/openspec/changes/adaptive-bitrate/design.md +92 -92
  30. package/openspec/changes/adaptive-bitrate/proposal.md +56 -56
  31. package/openspec/changes/adaptive-bitrate/specs/adaptive-bitrate/spec.md +51 -51
  32. package/openspec/changes/adaptive-bitrate/tasks.md +40 -40
  33. package/openspec/changes/binary-distribution/.openspec.yaml +2 -2
  34. package/openspec/changes/binary-distribution/proposal.md +53 -53
  35. package/openspec/changes/chunked-request-bodies/design.md +81 -81
  36. package/openspec/changes/chunked-request-bodies/proposal.md +53 -53
  37. package/openspec/changes/chunked-request-bodies/specs/chunked-request-bodies/spec.md +30 -30
  38. package/openspec/changes/chunked-request-bodies/tasks.md +40 -40
  39. package/openspec/changes/cold-start/design.md +98 -98
  40. package/openspec/changes/cold-start/proposal.md +62 -62
  41. package/openspec/changes/cold-start/specs/cold-start/spec.md +47 -47
  42. package/openspec/changes/cold-start/tasks.md +48 -48
  43. package/openspec/changes/disk-cap/.openspec.yaml +2 -2
  44. package/openspec/changes/disk-cap/proposal.md +37 -37
  45. package/openspec/changes/disk-cap/specs/disk-cap/spec.md +25 -25
  46. package/openspec/changes/disk-cap/tasks.md +19 -19
  47. package/openspec/changes/proxy-observability/.openspec.yaml +2 -2
  48. package/openspec/changes/proxy-observability/design.md +38 -38
  49. package/openspec/changes/proxy-observability/proposal.md +49 -49
  50. package/openspec/changes/proxy-observability/specs/observability/spec.md +33 -33
  51. package/openspec/changes/proxy-observability/tasks.md +21 -21
  52. package/openspec/changes/seek-debounce/design.md +118 -118
  53. package/openspec/changes/seek-debounce/proposal.md +59 -59
  54. package/openspec/changes/seek-debounce/specs/seek-debounce/spec.md +41 -41
  55. package/openspec/changes/seek-debounce/tasks.md +33 -33
  56. package/openspec/changes/subtitle-language/.openspec.yaml +2 -2
  57. package/openspec/changes/subtitle-language/proposal.md +50 -50
  58. package/openspec/changes/subtitle-language/specs/subtitle-language/spec.md +34 -34
  59. package/openspec/changes/subtitle-language/tasks.md +26 -26
  60. package/openspec/changes/track-selection/.openspec.yaml +2 -2
  61. package/openspec/changes/track-selection/design.md +47 -47
  62. package/openspec/changes/track-selection/proposal.md +45 -45
  63. package/openspec/changes/track-selection/specs/track-selection/spec.md +41 -41
  64. package/openspec/changes/track-selection/tasks.md +20 -20
  65. package/openspec/changes/transcode-quality/.openspec.yaml +2 -2
  66. package/openspec/changes/transcode-quality/proposal.md +51 -51
  67. package/openspec/changes/transcode-quality/specs/transcode-quality/spec.md +139 -139
  68. package/openspec/changes/transcode-quality/tasks.md +63 -63
  69. package/openspec/config.yaml +38 -38
  70. package/package.json +1 -1
  71. package/public/.well-known/appspecific/com.chrome.devtools.json +1 -1
  72. package/research/delivery-wedge-stand-ladder-2026-09-13.md +329 -329
  73. package/research/double-spawn-2026-09-10.md +171 -171
  74. package/research/handover-2026-09-13.md +209 -209
  75. package/research/handover-reader-claims-removal-2026-09-12.md +166 -166
  76. package/research/piece-withdrawn-but-still-claimed-2026-09-12.md +175 -175
  77. package/research/preset-benchmark-size-scaling-2026-08-22.md +55 -55
  78. package/research/priority-map-is-the-truth-2026-09-12.md +225 -225
  79. package/routes/api/client-logs/post.js +86 -86
  80. package/routes/api/delivery-sink/get.js +72 -72
  81. package/routes/api/link-probe/get.js +51 -51
  82. package/routes/api/playback-plan/audio-tracks/post.js +29 -29
  83. package/routes/api/playback-plan/post.js +53 -53
  84. package/routes/api/sources/post.js +36 -36
  85. package/routes/api/sources/stats/get.js +132 -132
  86. package/routes/api/sources/warm/post.js +217 -217
  87. package/routes/api/subtitles/get.js +230 -230
  88. package/routes/api/transcode-sessions/fragment-far/post.js +60 -60
  89. package/routes/api/transcode-sessions/net-report/post.js +138 -138
  90. package/routes/api/transcode-sessions/post.js +178 -178
  91. package/routes/api/transcode-sessions/progress/get.js +27 -27
  92. package/routes/api/transcode-sessions/release/post.js +41 -41
  93. package/routes/api/transcode-sessions/seek/post.js +63 -63
  94. package/routes/health/get.js +13 -13
  95. package/routes/healthz/get.js +13 -13
  96. package/routes/stream/get.js +380 -380
  97. package/routes/transcode/audio-file/get.js +64 -64
  98. package/routes/transcode/audio-warm/get.js +95 -95
  99. package/routes/transcode/session-file/get.js +52 -52
  100. package/routes/transcode/variant-file/get.js +95 -95
  101. package/routes/transcode/variant-warm/get.js +84 -84
  102. package/scripts/render-run-graph.js +173 -173
  103. package/services/encode/CoverageMap.js +428 -428
  104. package/services/encode/CushionReport.js +345 -345
  105. package/services/encode/EncodeAdmission.js +376 -376
  106. package/services/encode/EncodeOrchestrator.js +1217 -1217
  107. package/services/encode/EncodePlan.js +1366 -1366
  108. package/services/encode/EncodeRun.js +813 -813
  109. package/services/encode/Encoder.js +142 -142
  110. package/services/encode/EncoderSelection.js +38 -38
  111. package/services/encode/LocalObservations.js +359 -359
  112. package/services/encode/NvencEncoder.js +50 -50
  113. package/services/encode/OutputTimes.js +579 -579
  114. package/services/encode/QsvEncoder.js +58 -58
  115. package/services/encode/Renditions.js +2282 -2282
  116. package/services/encode/RunProgress.js +84 -84
  117. package/services/encode/SegmentDemand.js +95 -95
  118. package/services/encode/SoftwareEncoder.js +121 -121
  119. package/services/encode/V4l2m2mEncoder.js +53 -53
  120. package/services/encode/VaapiEncoder.js +66 -66
  121. package/services/encode/args.js +418 -418
  122. package/services/encode/available-share.js +89 -89
  123. package/services/encode/calibration.js +361 -361
  124. package/services/encode/contention.js +128 -128
  125. package/services/encode/decode-cost-fit.js +363 -363
  126. package/services/encode/encode-exit.js +114 -114
  127. package/services/encode/encode-run-state.js +551 -551
  128. package/services/encode/encoder-readings.js +45 -45
  129. package/services/encode/fingerprint.js +322 -322
  130. package/services/encode/host-load.js +174 -174
  131. package/services/encode/index.js +9 -9
  132. package/services/encode/learned-median.js +90 -90
  133. package/services/encode/output/EncodedOutput.js +57 -57
  134. package/services/encode/output/Output.js +131 -131
  135. package/services/encode/output/OutputCatalog.js +414 -414
  136. package/services/encode/output/OutputSpec.js +390 -390
  137. package/services/encode/output/Timeline.js +308 -308
  138. package/services/encode/output/cut-grid.js +107 -107
  139. package/services/encode/output/ladder.js +32 -32
  140. package/services/encode/output/playlists.js +284 -284
  141. package/services/encode/output/rates.js +125 -125
  142. package/services/encode/output-key-format.js +21 -21
  143. package/services/encode/quality/HostLoad.js +525 -525
  144. package/services/encode/quality/HostTimings.js +249 -249
  145. package/services/encode/quality/OutputQualityState.js +28 -28
  146. package/services/encode/quality/QualityController.js +935 -935
  147. package/services/encode/quality/QualityOffer.js +416 -416
  148. package/services/encode/quality/drain-threat.js +47 -47
  149. package/services/encode/quality/link-budget.js +349 -349
  150. package/services/encode/quality/output-format.js +319 -319
  151. package/services/encode/quality/serving-output.js +216 -216
  152. package/services/encode/quality/visible-rung.js +57 -57
  153. package/services/encode/run-budget.js +97 -97
  154. package/services/encode/run-costs.js +155 -155
  155. package/services/encode/run-inputs.js +42 -42
  156. package/services/encode/segment-formats/fmp4.js +476 -458
  157. package/services/encode/segment-formats/index.js +103 -85
  158. package/services/encode/segment-formats/init-compat.js +324 -324
  159. package/services/encode/segment-formats/mp4-boxes.js +604 -541
  160. package/services/encode/segment-formats/mpegts.js +134 -134
  161. package/services/encode/start-stop-cost.js +178 -178
  162. package/services/encode/throughput.js +97 -97
  163. package/services/encode/torrent-cost.js +88 -88
  164. package/services/identity/installation-id.js +38 -38
  165. package/services/media/SourceFile.js +298 -298
  166. package/services/media/SubtitleOrchestrator.js +167 -167
  167. package/services/media/container/KeyframeTable.js +272 -272
  168. package/services/media/container/ebml-reader.js +159 -159
  169. package/services/media/ffmpeg-banner.js +260 -260
  170. package/services/media/keyframe-probe.js +157 -157
  171. package/services/media/media-info-probe.js +93 -93
  172. package/services/media/playback-planner.js +931 -931
  173. package/services/media/tracks/AudioTrack.js +143 -143
  174. package/services/media/tracks/ImageSubtitleTrack.js +19 -19
  175. package/services/media/tracks/SubtitleTrack.js +38 -38
  176. package/services/media/tracks/VideoTrack.js +33 -33
  177. package/services/media/tracks/audio-bound.js +233 -233
  178. package/services/media/tracks/index.js +15 -15
  179. package/services/media/tracks/language-detect.js +174 -174
  180. package/services/server/OutputLifecycle.js +418 -418
  181. package/services/server/SegmentServing.js +1150 -1150
  182. package/services/server/ViewerRequests.js +790 -786
  183. package/services/server/WaitLedger.js +142 -142
  184. package/services/server/controllers/PlaybackController.js +40 -40
  185. package/services/server/controllers/SubtitleController.js +109 -109
  186. package/services/server/http-responses.js +60 -60
  187. package/services/server/transcode-session-files.js +325 -325
  188. package/services/server/wire-outputs.js +1024 -1020
  189. package/services/storage/Diagnostics.js +202 -202
  190. package/services/storage/MachineBudget.js +235 -235
  191. package/services/storage/allowance.js +107 -107
  192. package/services/storage/core-dumps.js +106 -106
  193. package/services/storage/files/CompletedFiles.js +391 -391
  194. package/services/storage/files/piece-from-whole-file.js +118 -118
  195. package/services/storage/free.js +71 -71
  196. package/services/storage/keep.js +48 -48
  197. package/services/storage/machine-memory.js +56 -56
  198. package/services/storage/memory-report.js +575 -575
  199. package/services/storage/piece-store/piece-disk-store.js +539 -539
  200. package/services/storage/piece-store/piece-lru.js +495 -495
  201. package/services/storage/returns.js +103 -103
  202. package/services/storage/segment-store/SegmentStore.js +1255 -1255
  203. package/services/storage/wire.js +175 -175
  204. package/services/torrent/demand/DemandRegister.js +202 -202
  205. package/services/torrent/demand/Urgency.js +137 -137
  206. package/services/torrent/demand/Window.js +118 -118
  207. package/services/torrent/demand/index.js +8 -8
  208. package/services/torrent/demand/pieces.js +140 -140
  209. package/services/torrent/download/SwarmSelection.js +386 -386
  210. package/services/torrent/download/registry.js +102 -102
  211. package/services/torrent/download/withdraw-claim.js +80 -80
  212. package/services/torrent/files.js +455 -455
  213. package/services/torrent/naming.js +619 -619
  214. package/services/torrent/piece-store-of.js +29 -29
  215. package/services/torrent/supply-margin.js +262 -262
  216. package/services/torrent/worker/background-fill.js +213 -213
  217. package/services/torrent/worker/channel.js +271 -271
  218. package/services/torrent/worker/client.js +836 -836
  219. package/services/torrent/worker/fastest-wires.js +342 -342
  220. package/services/torrent/worker/handle-state.js +23 -23
  221. package/services/torrent/worker/install-webrtc-shim.js +32 -32
  222. package/services/torrent/worker/piece-reader.js +1078 -1078
  223. package/services/torrent/worker/pool-adapter.js +371 -371
  224. package/services/torrent/worker/protocol.js +184 -184
  225. package/services/torrent/worker/resume-warm.js +107 -107
  226. package/services/torrent/worker/webrtc-shim.js +219 -219
  227. package/services/transport/delivery-probe.js +842 -842
  228. package/services/transport/health-collector.js +53 -53
  229. package/services/transport/nat-classifier.js +231 -231
  230. package/services/transport/packet-witness.js +808 -808
  231. package/services/transport/port-mapper.js +235 -235
  232. package/services/transport/registry-api.js +81 -81
  233. package/services/transport/tunnel-client.js +447 -447
  234. package/services/transport/usrsctp-state.js +175 -175
  235. package/services/transport/webrtc-manager.js +484 -484
  236. package/services/viewer/Assignments.js +340 -340
  237. package/services/viewer/PriorityOrchestrator.js +325 -325
  238. package/services/viewer/Viewer.js +691 -691
  239. package/services/viewer/Viewers.js +368 -368
  240. package/services/viewer/active-output.js +37 -37
  241. package/services/viewer/audio-start.js +46 -46
  242. package/services/viewer/buffer-trend.js +81 -81
  243. package/services/viewer/choices.js +647 -647
  244. package/services/viewer/media-time.js +355 -0
  245. package/services/{server → viewer}/playback-readiness.js +635 -516
  246. package/services/viewer/positions.js +126 -126
  247. package/services/viewer/report-intake.js +46 -46
  248. package/store/source-registry.js +71 -71
  249. package/test/asset-paths.test.js +28 -28
  250. package/test/audio-buffer-continuity.test.js +77 -70
  251. package/test/audio-inventory.test.js +212 -212
  252. package/test/audio-track-bitrate.test.js +40 -40
  253. package/test/auto-quality-step.test.js +707 -707
  254. package/test/available-share.test.js +76 -76
  255. package/test/background-fill.test.js +190 -190
  256. package/test/client-log-file.test.js +110 -110
  257. package/test/completed-files.test.js +115 -115
  258. package/test/concurrent-cost.test.js +148 -148
  259. package/test/container-media-info.test.js +228 -228
  260. package/test/contention.test.js +76 -76
  261. package/test/core-dumps.test.js +57 -57
  262. package/test/coverage-follows-the-disk.test.js +228 -228
  263. package/test/coverage-map.test.js +195 -195
  264. package/test/cue-push-carries-what-the-walk-found.test.js +58 -58
  265. package/test/cut-times-timeline.test.js +65 -65
  266. package/test/cuts-follow-published-grid.test.js +109 -109
  267. package/test/data-channel-frame.test.js +63 -63
  268. package/test/data-channel-viewer-lifecycle.test.js +92 -92
  269. package/test/dead-torrent-handle.test.js +52 -52
  270. package/test/declared-rates.test.js +116 -116
  271. package/test/declared-tracks.test.js +43 -43
  272. package/test/decode-cost-fit.test.js +202 -202
  273. package/test/decode-cost.test.js +462 -462
  274. package/test/delivery-probe.test.js +393 -393
  275. package/test/demand-register.test.js +206 -206
  276. package/test/dht-bootstrap.test.js +72 -72
  277. package/test/drain-threat.test.js +65 -65
  278. package/test/duplicate-add.test.js +48 -48
  279. package/test/encode-admission.test.js +146 -146
  280. package/test/encode-cost.test.js +283 -283
  281. package/test/encode-exit.test.js +111 -111
  282. package/test/encode-orchestrator.test.js +419 -419
  283. package/test/encode-plan-viewers.test.js +718 -718
  284. package/test/encode-plan.test.js +545 -545
  285. package/test/encode-run-state.test.js +314 -314
  286. package/test/encode-run.test.js +162 -162
  287. package/test/encode-slope.test.js +46 -46
  288. package/test/encoded-output.test.js +39 -39
  289. package/test/encoder-kinds.test.js +122 -122
  290. package/test/encoder-readings.test.js +46 -46
  291. package/test/every-claimant-is-told-its-share.test.js +152 -152
  292. package/test/failed-encoder-still-serves-what-exists.test.js +354 -354
  293. package/test/failed-start-breaker.test.js +202 -202
  294. package/test/fastest-wires.test.js +143 -143
  295. package/test/ffmpeg-stream-counts.test.js +78 -78
  296. package/test/file-edges.test.js +191 -191
  297. package/test/fmp4-presentation-clock.test.js +146 -162
  298. package/test/fmp4-self-contained.test.js +80 -80
  299. package/test/fmp4-timestamps.test.js +140 -140
  300. package/test/fmp4-track-completeness.test.js +41 -41
  301. package/test/format-keyed-outputs.test.js +78 -78
  302. package/test/health-metrics.test.js +38 -38
  303. package/test/held-request-width.test.js +157 -157
  304. package/test/helpers/encode-run.js +136 -136
  305. package/test/helpers/manager.js +57 -57
  306. package/test/helpers/output-spec.js +63 -63
  307. package/test/host-load.test.js +68 -68
  308. package/test/index-accuracy-is-about-the-file.test.js +128 -128
  309. package/test/init-compat.test.js +219 -219
  310. package/test/input-lost-quiets-the-plan.test.js +261 -261
  311. package/test/input-unavailable.test.js +48 -48
  312. package/test/installation-id.test.js +39 -39
  313. package/test/keeping-period.test.js +83 -83
  314. package/test/keyframe-index-accuracy.test.js +188 -188
  315. package/test/keyframes-belong-to-the-file.test.js +249 -249
  316. package/test/learned-median.test.js +37 -37
  317. package/test/letting-go-asks-who-is-watching.test.js +191 -191
  318. package/test/limit-row-by-area.test.js +60 -60
  319. package/test/link-budget.test.js +112 -112
  320. package/test/link-probe.test.js +56 -56
  321. package/test/local-encoding-data.test.js +273 -273
  322. package/test/logger-repeats.test.js +134 -134
  323. package/test/look-ahead-coverage.test.js +56 -56
  324. package/test/manager-surface.test.js +292 -292
  325. package/test/matroska-cues-track.test.js +192 -192
  326. package/test/media-time.test.js +62 -0
  327. package/test/memory-budget.test.js +390 -390
  328. package/test/move-cost.test.js +216 -216
  329. package/test/mp4-subtitles.test.js +173 -173
  330. package/test/one-authority.test.js +285 -285
  331. package/test/one-budget.test.js +149 -149
  332. package/test/one-resource-divided.test.js +185 -185
  333. package/test/one-serving-output-per-height.test.js +458 -458
  334. package/test/orchestrator-wired.test.js +206 -206
  335. package/test/output-catalog.test.js +39 -39
  336. package/test/output-shape.test.js +21 -21
  337. package/test/output-spec.test.js +251 -251
  338. package/test/output-speed.test.js +88 -88
  339. package/test/outputs-being-prepared.test.js +40 -40
  340. package/test/packet-witness-ring.test.js +236 -236
  341. package/test/packet-witness.test.js +148 -148
  342. package/test/piece-disk-store.test.js +401 -401
  343. package/test/piece-from-whole-file.test.js +129 -129
  344. package/test/piece-lru.test.js +319 -319
  345. package/test/piece-reader.test.js +162 -162
  346. package/test/piece-selection-offset.test.js +85 -85
  347. package/test/piece-store-eviction.test.js +595 -595
  348. package/test/piece-store-never-refuses.test.js +153 -153
  349. package/test/piece-store-reservations.test.js +403 -403
  350. package/test/piece-store-slow-disk.test.js +159 -159
  351. package/test/piece-tail.test.js +97 -97
  352. package/test/plan-host-timings.test.js +68 -68
  353. package/test/plan-is-steady.test.js +225 -225
  354. package/test/playback-readiness.test.js +518 -483
  355. package/test/playback-start-regressions.test.js +101 -99
  356. package/test/priority-map-download.test.js +174 -174
  357. package/test/priority-map-emptied.test.js +204 -204
  358. package/test/priority-map-per-output.test.js +215 -215
  359. package/test/priority-map.test.js +163 -163
  360. package/test/probe-wedge-certainty.test.js +60 -60
  361. package/test/produced-copy-choice.test.js +213 -213
  362. package/test/published-timeline.test.js +83 -83
  363. package/test/quality-offer.test.js +162 -162
  364. package/test/quality-variants.test.js +1257 -1257
  365. package/test/rate-control-in-the-output.test.js +180 -180
  366. package/test/read-survives-withdrawal.test.js +164 -164
  367. package/test/read-window.test.js +365 -365
  368. package/test/resume-warm.test.js +39 -39
  369. package/test/run-graph-drift.test.js +37 -37
  370. package/test/run-inputs.test.js +80 -80
  371. package/test/run-intervals.test.js +100 -100
  372. package/test/run-position-follows-published-grid.test.js +89 -89
  373. package/test/run-progress.test.js +65 -65
  374. package/test/same-height-switch.test.js +664 -664
  375. package/test/seek-frees-the-viewers-requests.test.js +217 -217
  376. package/test/seek-landing.test.js +109 -109
  377. package/test/seek-target-not-superseded.test.js +195 -195
  378. package/test/segment-closed-on-both-branches.test.js +96 -96
  379. package/test/segment-cut-times.test.js +63 -63
  380. package/test/segment-demand.test.js +80 -80
  381. package/test/segment-hold-supersede.test.js +91 -91
  382. package/test/segment-judged-by-its-own-header.test.js +221 -221
  383. package/test/segment-store-eviction.test.js +247 -247
  384. package/test/segment-store.test.js +352 -352
  385. package/test/segment-true-start.test.js +86 -86
  386. package/test/segments-are-shared.test.js +176 -176
  387. package/test/send-chunk-bytes.test.js +86 -86
  388. package/test/serving-existing-output.test.js +105 -105
  389. package/test/serving-output.test.js +102 -102
  390. package/test/shared-piece-store.test.js +310 -310
  391. package/test/sidecar-naming.test.js +142 -142
  392. package/test/soundtrack-rate-bound.test.js +289 -289
  393. package/test/source-file.test.js +133 -133
  394. package/test/source-registry.test.js +18 -18
  395. package/test/split-a-stretch.test.js +139 -139
  396. package/test/stale-request-after-seek.test.js +158 -158
  397. package/test/startup-readings.test.js +124 -124
  398. package/test/stream-route.test.js +209 -209
  399. package/test/subtitle-cue-framing.test.js +200 -200
  400. package/test/subtitle-cue-source.test.js +102 -102
  401. package/test/subtitle-cursor.test.js +63 -63
  402. package/test/subtitle-defaults.test.js +118 -118
  403. package/test/subtitle-language.test.js +252 -252
  404. package/test/supply-margin.test.js +177 -177
  405. package/test/swarm-follows-readers.test.js +174 -174
  406. package/test/swarm-reach.test.js +130 -130
  407. package/test/swarm-selection.test.js +220 -220
  408. package/test/tail-duplication.test.js +167 -167
  409. package/test/the-channel-has-both-ends.test.js +91 -91
  410. package/test/the-torrent-thread-serves-bytes.test.js +107 -107
  411. package/test/thread-pool.test.js +53 -53
  412. package/test/timeline.test.js +84 -84
  413. package/test/torrent-contents.test.js +235 -235
  414. package/test/torrent-cost.test.js +71 -71
  415. package/test/torrent-files.test.js +221 -221
  416. package/test/torrent-naming.test.js +255 -255
  417. package/test/tracks-begin-together.test.js +209 -209
  418. package/test/tunnel-renewal.test.js +167 -167
  419. package/test/two-viewers-one-picture.test.js +504 -504
  420. package/test/upload-hurry.test.js +186 -186
  421. package/test/usrsctp-state.test.js +131 -131
  422. package/test/video-facts.test.js +102 -102
  423. package/test/viewer-assignments.test.js +143 -143
  424. package/test/viewer-outputs.test.js +285 -285
  425. package/test/viewer-presence.test.js +116 -116
  426. package/test/viewer-requests-admission.test.js +89 -89
  427. package/test/viewer-states.test.js +189 -189
  428. package/test/viewer-subtitle-subscription.test.js +96 -96
  429. package/test/viewer.test.js +139 -139
  430. package/test/viewing-generations.test.js +390 -390
  431. package/test/visible-rung.test.js +37 -37
  432. package/test/wait-ledger.test.js +100 -100
  433. package/test/webrtc-shim.test.js +149 -149
  434. package/test/wedge-certainty.test.js +131 -131
  435. package/test/whole-file-held-read.test.js +30 -30
  436. package/test/withdraw-piece-claim.test.js +212 -212
  437. package/test/worker-channel.test.js +158 -158
  438. package/utils/client-log-file.js +151 -151
  439. package/utils/logger.js +312 -312
  440. package/utils/parse-range.js +25 -25
  441. package/utils/perf.js +59 -59
  442. package/utils/torrent-source-key.js +38 -38
@@ -1,1020 +1,1024 @@
1
- /**
2
- * @file Wiring of the outputs, the viewers and the encoding.
3
- *
4
- * Builds each component and hands it the narrow host it reads, and nothing
5
- * else. It holds no behaviour: every member that used to be a method of the
6
- * session manager is a method of the component that owns it now. What it
7
- * returns is the set of components and the settings they share, for
8
- * `server.js` to give each route the ones that route asks.
9
- */
10
-
11
- import { logger } from "../../utils/logger.js";
12
- import { KeyframeTables } from "../media/KeyframeTables.js";
13
- import { waits } from "./WaitLedger.js";
14
- import { readMachineState, readProcessCpuSeconds, readProxyCpuSeconds, readSystemCpu, shareOfMachine } from "../encode/host-load.js";
15
- import { minimumBufferFrom } from "../torrent/supply-margin.js";
16
- import { PriorityOrchestrator } from "../viewer/PriorityOrchestrator.js";
17
- import { softwareDescriptor } from "../encode/hwaccel.js";
18
- import { resolveSegmentFormat } from "../encode/segment-formats/index.js";
19
- import { Timelines } from "../encode/output/Timeline.js";
20
- import { SourceFiles } from "../media/SourceFile.js";
21
- import { SegmentStore } from "../storage/segment-store/SegmentStore.js";
22
- import { EncodeCost } from "../encode/quality/EncodeCost.js";
23
- import { QualityOffer } from "../encode/quality/QualityOffer.js";
24
- import { activeOutputFor } from "../viewer/active-output.js";
25
- import {
26
- acceptsGeneration,
27
- askQualityOf,
28
- audioBeingWarmedOf,
29
- audioChoiceOf,
30
- chooseAudioTrack,
31
- chooseOutput,
32
- chosenOutputOf,
33
- consumersOn,
34
- dropAskOf,
35
- generationOfRequest,
36
- givenOutputOf,
37
- heightsChosenAs,
38
- highestGivenSegmentOf,
39
- holdForResponse,
40
- linkMbpsOf,
41
- linkReportOf,
42
- bufferOf,
43
- bufferedSecondsOf,
44
- visiblePictureOf,
45
- linkReportsOn,
46
- noteAudioBeingWarmed,
47
- noteGivenOutput,
48
- noteSameHeightSwitch,
49
- noteServingVerdict,
50
- noteStepBeingWarmed,
51
- noteStepOnScreen,
52
- outputsBeingPrepared,
53
- placeOn,
54
- presentOn,
55
- qualityModeOf,
56
- sameHeightSwitchOf,
57
- servingVerdictOf,
58
- standingAskOf,
59
- stepBeingWarmedOf,
60
- stepBeingWarmedSinceOf,
61
- stepOnScreenOf,
62
- switchingOnto,
63
- watches
64
- } from "../viewer/choices.js";
65
- import { viewerSecondsOn, viewerSegmentsOn } from "../viewer/positions.js";
66
- import { Viewers } from "../viewer/Viewers.js";
67
- import { OutputCatalog } from "../encode/output/OutputCatalog.js";
68
- import { ViewerRequests } from "./ViewerRequests.js";
69
- import { OutputLifecycle } from "./OutputLifecycle.js";
70
- import { SegmentServing } from "./SegmentServing.js";
71
- import { audioStartSecondsFor } from "../viewer/audio-start.js";
72
- import { audioRenditionName } from "../media/audio-inventory.js";
73
- import { Renditions } from "../encode/Renditions.js";
74
- import { CushionReport, LOOKAHEAD_PAUSE_SECONDS } from "../encode/CushionReport.js";
75
- import { EncodeRuns } from "../encode/EncodeRuns.js";
76
- import { OutputTimes } from "../encode/OutputTimes.js";
77
- import { HostLoad } from "../encode/quality/HostLoad.js";
78
- import { HostTimings } from "../encode/quality/HostTimings.js";
79
- import { BUDGET_CHECK_INTERVAL_MS, QualityController } from "../encode/quality/QualityController.js";
80
- import { EncodeOrchestrator } from "../encode/EncodeOrchestrator.js";
81
- import { EncodeAdmission } from "../encode/EncodeAdmission.js";
82
- import { EncoderSelection } from "../encode/EncoderSelection.js";
83
- import { OutputOpening } from "../encode/OutputOpening.js";
84
- import { segmentFormatOfKey } from "../encode/output-key-format.js";
85
- import { OutputSpec } from "../encode/output/OutputSpec.js";
86
- import { compareInits } from "../encode/segment-formats/init-compat.js";
87
- import { nominalKbpsFor, AUDIO_TRANSCODE_KBPS } from "../encode/args.js";
88
- import { audioReadingFor } from "../encode/audio-work-rate.js";
89
- import { probeInputMediaInfo } from "../media/media-info-probe.js";
90
- import { probeVideoKeyframeTimes } from "../media/keyframe-probe.js";
91
- import { wireMachineBudget } from "../storage/wire.js";
92
- import { Returns } from "../storage/returns.js";
93
- import { freeBytesFor } from "../storage/free.js";
94
- import path from "node:path";
95
- import { contentOf, LocalObservations } from "../encode/LocalObservations.js";
96
- import { configurationKeyOf } from "../encode/fingerprint.js";
97
- import { PROXY_ROOT } from "../encode/quality/HostTimings.js";
98
- import { qualityStateOf } from "../encode/quality/OutputQualityState.js";
99
-
100
- // Where a variant and an audio rendition live under a session — `v/<height>/…`
101
- // and `a/<track>/…` — is stated in `encode/output/playlists.js`, beside the lines that
102
- // write those addresses into a master playlist. The routes that parse them back
103
- // are in `server.js`.
104
-
105
- const CLEANUP_INTERVAL_MS = 30_000;
106
- const DEFAULT_SEGMENT_DURATION_SEC = 4;
107
- // Idle TTL: a session is disposed this long after the last segment/playlist
108
- // access. Long enough that a viewer who pauses, backgrounds the tab, or briefly
109
- // turns the phone off can resume WITHOUT a cold ffmpeg restart (the warm session
110
- // also backs the seamless auto-reconnect). ffmpeg stops producing at the
111
- // look-ahead cap when idle, so a lingering session costs retained segments on
112
- // disk, not sustained CPU. Active playback refreshes the timer on every segment
113
- // fetch, so it never expires mid-watch.
114
- // How long a session outlives the BROWSER, not the viewing. Since server
115
- // 0.8.103 a browser that holds a session re-asserts it every 30 s, so an open
116
- // tab never consumes this at all — not while paused, not across a three-hour
117
- // film. What is left is the case where the browser has genuinely gone: the tab
118
- // was killed without releasing, the phone slept, the network dropped. Keeping
119
- // the session means such a viewer comes back to a warm encoder instead of
120
- // waiting out a cold start; the cost while nobody is there is disk for the
121
- // produced segments, since the encoder is suspended and burns no CPU, and that
122
- // disk is already bounded by the pool's 10 GB cap with eviction. Thirty minutes
123
- // covers a meal, a phone call or a lift ride.
124
- const DEFAULT_SESSION_TTL_MS = 30 * 60 * 1000;
125
- const DEFAULT_STARTUP_WAIT_MS = 5_000;
126
-
127
- /**
128
- * Convert a bind-all host address to the loopback address so that
129
- * the HLS input URL is always reachable from the same machine.
130
- *
131
- * @param {string} host
132
- * @returns {string}
133
- */
134
- function toLoopbackHost(host) {
135
- if (host === "0.0.0.0" || host === "::") {
136
- return "127.0.0.1";
137
- }
138
- return host;
139
- }
140
-
141
- /**
142
- * Build the HTTP base URL (scheme + host + port) for the local proxy server.
143
- *
144
- * @param {string} host - Bind host (may be "0.0.0.0" or "::").
145
- * @param {number} port
146
- * @returns {string} e.g. "http://127.0.0.1:9090"
147
- */
148
- function buildHttpBaseUrl(host, port) {
149
- const url = new URL("http://localhost");
150
- url.hostname = toLoopbackHost(host);
151
- url.port = String(port);
152
- return url.origin;
153
- }
154
-
155
- /**
156
- * @typedef {Object} WireOutputsOptions
157
- * @property {boolean} enabled - Whether HLS transcoding is enabled.
158
- * @property {string} ffmpegBin - Path to the ffmpeg executable.
159
- * @property {string} localBindHost - Host the proxy HTTP server is bound to.
160
- * @property {number} localPort - Port the proxy HTTP server is listening on.
161
- * @property {number} [segmentDurationSec] - HLS segment length in seconds.
162
- * @property {number} [sessionTtlMs] - Session idle TTL in milliseconds.
163
- * @property {number} [startupWaitMs] - Max time to wait for the first playlist file.
164
- * @property {string} [segmentFormatId] - Output container: "fmp4" (default)
165
- * or "mpegts". See `../segment-formats/index.js`.
166
- */
167
-
168
- /**
169
- * @typedef {Object} HlsSession
170
- * @property {string} id - The name of the output it produces.
171
- * @property {string} fileName - Display name of the file being transcoded.
172
- * @property {import("../media/container/KeyframeTable.js").KeyframeTable} keyframes -
173
- * Where this file's keyframes are. One object per file, held by every session
174
- * of it, so a table read late still reaches them. Used to snap a source seek
175
- * onto a known-valid position (see #startEncodeRun).
176
- */
177
-
178
- /**
179
- * Build the components and connect them.
180
- *
181
- * @param {WireOutputsOptions} options
182
- * @returns {object} The components, and the settings they read.
183
- */
184
- export function wireOutputs({
185
- enabled,
186
- ffmpegBin,
187
- localBindHost,
188
- localPort,
189
- segmentDurationSec = DEFAULT_SEGMENT_DURATION_SEC,
190
- sessionTtlMs = DEFAULT_SESSION_TTL_MS,
191
- startupWaitMs = DEFAULT_STARTUP_WAIT_MS,
192
- // Where every file's keyframe table lives, and the only thing that reads
193
- // one. A session asks for its file's table and is handed the object every
194
- // other reader of that file holds, so a table that arrives late reaches the
195
- // sessions created before it. Built here when nothing supplied one, which
196
- // is a registry with no reader: it answers "not read" about every file, and
197
- // every picture is then re-encoded — the correct behaviour for a proxy
198
- // wired without a container reader, and what every unit test gets.
199
- keyframeTables = new KeyframeTables(),
200
- videoEncoder = null,
201
- softwarePresetBenchmark = null,
202
- // What every encoder this host may use was qualified and measured to do at
203
- // startup, by kind (`encode/calibration.js`). Takes the place of the one
204
- // array above, which a test may still hand in on its own.
205
- calibration = null,
206
- decodeCostModel = null,
207
- getSourceStats = null,
208
- setPriorityMap = null,
209
- // What a second job costs on this host, measured at startup. Null when it
210
- // could not be measured, and then nothing is corrected — the alternative
211
- // is inventing a penalty, which is the same fault as inventing a fill rate.
212
- contentionPenalties = null,
213
- copySpeedX = null,
214
- audioCalibration = [],
215
- tonemapSupported = false,
216
- getCachedMediaInfo = null,
217
- getCachedAudioTracks = null,
218
- getContainerMediaInfo = null,
219
- fetchWholeFile = null,
220
- segmentFormatId = undefined,
221
- stateDir = "",
222
- segmentStore = null,
223
- getTorrentTotals,
224
- startStopCost = null,
225
- spillDisk = null,
226
- wholeFiles = null,
227
- diagnostics = null,
228
- diagnosticsRoot = "",
229
- memoryClaimant = null,
230
- budgetPolicy = null}) {
231
- const parts = {};
232
- const audioDescriptionOf = (output) => (getCachedAudioTracks?.({
233
- sourceKey: output.file.sourceKey, fileIndex: output.spec.grid?.fileIndex ?? output.file.fileIndex
234
- }) ?? []).find((track) => track.fileIndex === output.spec.audio?.fileIndex &&
235
- track.sourceTrackIndex === output.spec.audio?.trackIndex) ?? null;
236
- // Encode components receive only the storage operations they use. The
237
- // concrete SegmentStore remains owned by this assembly and is never handed
238
- // to the encode layer.
239
- const segmentCoverage = {
240
- provenNumbers: (address) => parts.segmentStore.provenNumbers(address),
241
- announce: (address, index) => parts.segmentStore.announce(address, index),
242
- filesHeld: (address) => parts.segmentStore.filesHeld(address),
243
- clearUpAfter: (address, startedAt) => parts.segmentStore.clearUpAfter(address, startedAt)
244
- };
245
- const segmentFiles = {
246
- pathFor: (address) => parts.segmentStore.pathFor(address),
247
- initOf: (address) => parts.segmentStore.initOf(address),
248
- directoryFor: (address) => parts.segmentStore.directoryFor(address),
249
- publish: (address, makingName, format, coverage) => parts.segmentStore.publish(address, makingName, format, coverage)
250
- };
251
- const segmentOutputFiles = {
252
- addresses: () => parts.segmentStore.addresses(),
253
- isClosed: (address, index) => parts.segmentStore.isClosed(address, index),
254
- lastReadAt: (address) => parts.segmentStore.lastReadAt(address),
255
- directoryFor: (address) => parts.segmentStore.directoryFor(address),
256
- useFormat: (address, format) => parts.segmentStore.useFormat(address, format)
257
- };
258
- const segmentPaths = {
259
- pathOf: (address, index) => parts.segmentStore.pathOf(address, index)
260
- };
261
- /**
262
- * Whether everything this output will ever serve is made: every segment of
263
- * its timeline closed in the store. Such an output needs no encoder and
264
- * holds no place, however many watch it.
265
- *
266
- * @param {object} session
267
- * @returns {boolean}
268
- */
269
- const finishedOutput = (session) => {
270
- const count = Number(session?.timeline?.segmentCount) || 0;
271
- return count > 0 && parts.segmentStore.provenNumbers(session.outputKey).length >= count;
272
- };
273
- // The nominal limits a size may be produced at, highest first. ONE list, read
274
- // by the opening of an output and by the move between limits of a height, so
275
- // the two cannot disagree about which limits exist. Only the size's own until
276
- // a set of lower limits is decided (roadmap item 97, step 14): a lower limit
277
- // handed to a viewer must come with the page saying so. Asked with the whole
278
- // frame, because the row is chosen by area (`limitRowFor`).
279
- parts.limitsFor = (frame) => [nominalKbpsFor(frame)];
280
- // What the torrent and the proxy itself spend on this host, and how fast each watched torrent moves.
281
- parts.hostLoad = new HostLoad({
282
- readMachineState,
283
- readProcessCpuSeconds,
284
- readProxyCpuSeconds,
285
- readSystemCpu,
286
- shareOfMachine,
287
- liveRunsOf: (...args) => parts.encodeRuns.liveRunsOf(...args),
288
- runStateOf: (...args) => parts.encodeRuns.runStateOf(...args),
289
- get getSourceStats() { return parts.getSourceStats; },
290
- get getTorrentTotals() { return parts.getTorrentTotals; },
291
- get outputs() { return parts.outputs; },
292
- });
293
- // Where each segment of an output begins, and how that table is corrected from what the encoder produced.
294
- parts.outputTimes = new OutputTimes({
295
- logger,
296
- runsOf: (...args) => parts.encodeRuns.runsOf(...args),
297
- stopEncodeRun: (...args) => parts.encodeRuns.stopEncodeRun(...args),
298
- planEncodersSoon: (...args) => parts.encodeRuns.planEncodersSoon(...args),
299
- get outputs() { return parts.outputs; },
300
- get segmentDurationSec() { return parts.segmentDurationSec; },
301
- });
302
- // The encoders of this proxy: built where the plan places them, followed while they run, accounted when they end.
303
- parts.encodeRuns = new EncodeRuns({
304
- logger,
305
- // What an admitted encode was seen to do, filed under its configuration.
306
- observeEncodeEnded: (session) => {
307
- if (!parts.localObservations || !session?.spec?.video?.encode) {
308
- return;
309
- }
310
- const alone = Number(qualityStateOf(session).lastAloneSpeed);
311
- parts.localObservations.noteEncode({
312
- spec: session.spec,
313
- content: contentOf(session.file),
314
- aloneSpeedX: Number.isFinite(alone) && alone > 0 ? alone : null,
315
- segmentKbps: segmentRatesOf(session)
316
- });
317
- },
318
- viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
319
- noteRunStarting: (output) => parts.quality.noteRunStarting(output),
320
- inputOf: (...args) => parts.renditions.inputOf(...args),
321
- producedNumbers: (...args) => parts.serving.producedNumbers(...args),
322
- servesAudioSeparately: (...args) => parts.renditions.servesAudioSeparately(...args),
323
- disposeSession: (...args) => parts.lifecycle.disposeSession(...args),
324
- get contentionPenalties() { return parts.contentionPenalties; },
325
- get encodeCost() { return parts.encodeCost; },
326
- get encodeOrchestrator() { return parts.encodeOrchestrator; },
327
- get ffmpegBin() { return parts.ffmpegBin; },
328
- get outputTimes() { return parts.outputTimes; },
329
- get outputs() { return parts.outputs; },
330
- get priority() { return parts.priority; },
331
- get segmentDurationSec() { return parts.segmentDurationSec; },
332
- segmentFiles,
333
- get videoEncoder() { return parts.encoders.current; },
334
- get encoders() { return parts.encoders; },
335
- invalidateWaits: (output) => parts.serving.invalidateWaits(output),
336
- productionFailed: (output) => parts.renditions.noteProductionFailed(output),
337
- });
338
- // How much film is ready in front of the viewers, said; and the spare soundtracks fetched once it is full.
339
- parts.cushion = new CushionReport({
340
- viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
341
- linkReportsOn: (output) => linkReportsOn(parts.viewers, output),
342
- SourceFiles,
343
- logger,
344
- producedNumbers: (...args) => parts.serving.producedNumbers(...args),
345
- get encodeRuns() { return parts.encodeRuns; },
346
- get fetchWholeFile() { return parts.fetchWholeFile; },
347
- get getCachedAudioTracks() { return parts.getCachedAudioTracks; },
348
- get hostLoad() { return parts.hostLoad; },
349
- get lookaheadSeconds() { return parts.lookaheadSeconds; },
350
- get outputTimes() { return parts.outputTimes; },
351
- get outputs() { return parts.outputs; },
352
- });
353
- // The steps of a picture and its soundtracks: which output answers each, made when first asked for, and the master playlist listing them.
354
- // The cushion this file needs, from its segment length and the worst
355
- // interruption its supply has shown — one statement read by every operation
356
- // that asks it.
357
- const minimumBufferSecondsOf = (output) => minimumBufferFrom({
358
- segmentSeconds: parts.segmentDurationSec,
359
- worstSupplyWaitSec: parts.hostLoad.supplyFor(output.file)?.worstWaitSec
360
- })?.seconds ?? null;
361
- parts.renditions = new Renditions({
362
- // Whether this proxy re-encodes at all: a soundtrack with no stated rate is
363
- // re-encoded where it may be, and copied where it may not.
364
- get transcodeEnabled() { return parts.enabled; },
365
- observedPeakMbps: (spec) => parts.localObservations?.peakMbps(spec) ?? null,
366
- // A viewer has been moved onto an output prepared for them: how long it
367
- // took, and what they held.
368
- notePreparation: (output, seconds, bufferedSec) =>
369
- parts.localObservations?.notePreparation({ spec: output?.spec, seconds, bufferedSec }),
370
- viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
371
- audioStartSecondsFor: (args) => audioStartSecondsFor({ ...args, viewers: parts.viewers }),
372
- activeOutputFor: (args) => activeOutputFor({ ...args, viewers: parts.viewers }),
373
- // WHAT ENCODING IS TOLD ABOUT A PERSON, and what it may tell the viewer
374
- // layer about them: values and names, never the viewer itself. Steps and
375
- // soundtracks are chosen, warmed and left per person, so this is the widest
376
- // of the three sets — and every one of them is a call on the owner, which
377
- // is what keeps one writer per fact.
378
- viewerCountOn: (output) => parts.viewers.forOutput(output).size,
379
- // Whether anything still stands on it — somebody watching, or an
380
- // assignment not yet released. See `Viewers.stillNeeded`.
381
- outputStillNeeded: (output) => parts.viewers.stillNeeded(output),
382
- consumersOn: (output) => consumersOn(parts.viewers, output),
383
- presentOn: (output) => presentOn(parts.viewers, output),
384
- qualityModeOf: (output, consumerId) => qualityModeOf(parts.viewers, output, consumerId),
385
- linkMbpsOf: (output, consumerId) => linkMbpsOf(parts.viewers, output, consumerId),
386
- audioChoiceOf: (output, consumerId) => audioChoiceOf(parts.viewers, output, consumerId),
387
- chooseAudioTrack: (output, consumerId, choice) =>
388
- chooseAudioTrack(parts.viewers, output, consumerId, choice),
389
- stepOnScreenOf: (output, consumerId) => stepOnScreenOf(parts.viewers, output, consumerId),
390
- noteStepOnScreen: (output, consumerId, stepId) =>
391
- noteStepOnScreen(parts.viewers, output, consumerId, stepId),
392
- stepBeingWarmedOf: (output, consumerId) => stepBeingWarmedOf(parts.viewers, output, consumerId),
393
- stepBeingWarmedSinceOf: (output, consumerId) => stepBeingWarmedSinceOf(parts.viewers, output, consumerId),
394
- noteStepBeingWarmed: (output, consumerId, stepId) =>
395
- noteStepBeingWarmed(parts.viewers, output, consumerId, stepId),
396
- audioBeingWarmedOf: (output, consumerId) => audioBeingWarmedOf(parts.viewers, output, consumerId),
397
- noteAudioBeingWarmed: (output, consumerId, renditionId) =>
398
- noteAudioBeingWarmed(parts.viewers, output, consumerId, renditionId),
399
- watches: (output, consumerId) => watches(parts.viewers, output, consumerId),
400
- placeOn: (output, consumerId, seconds) => placeOn(parts.viewers, output, consumerId, seconds),
401
- // A viewer's own assignments: which output answered a height and segment in
402
- // one generation of their viewing, so a repeat is answered the same way.
403
- // Values in and out, like everything above.
404
- generationOfRequest: (consumerId, stated) => generationOfRequest(parts.viewers, consumerId, stated),
405
- givenOutputOf: (consumerId, generation, askedHeight, segmentIndex) =>
406
- givenOutputOf(parts.viewers, consumerId, generation, askedHeight, segmentIndex),
407
- noteGivenOutput: (consumerId, generation, askedHeight, segmentIndex, outputKey) =>
408
- noteGivenOutput(parts.viewers, consumerId, generation, askedHeight, segmentIndex, outputKey),
409
- // The output this viewer was chosen at a height, and the rule's choice of
410
- // it — the viewer's own record, never one per file.
411
- chosenOutputOf: (consumerId, askedHeight) => chosenOutputOf(parts.viewers, consumerId, askedHeight),
412
- chooseOutput: (consumerId, askedHeight, outputKey) => chooseOutput(parts.viewers, consumerId, askedHeight, outputKey),
413
- noteServingVerdict: (consumerId, verdict) => noteServingVerdict(parts.viewers, consumerId, verdict),
414
- // A move between two limits of the height on a viewer's screen (roadmap
415
- // item 97, step 12): which heights their choice is the output on screen
416
- // under, the segment they were last given, the move being prepared, and
417
- // whether a segment is closed on the output being prepared.
418
- limitsFor: (frame) => parts.limitsFor(frame),
419
- heightsChosenAs: (consumerId, outputKey) => heightsChosenAs(parts.viewers, consumerId, outputKey),
420
- highestGivenSegmentOf: (consumerId, askedHeight) => highestGivenSegmentOf(parts.viewers, consumerId, askedHeight),
421
- sameHeightSwitchOf: (consumerId) => sameHeightSwitchOf(parts.viewers, consumerId),
422
- noteSameHeightSwitch: (consumerId, value) => noteSameHeightSwitch(parts.viewers, consumerId, value),
423
- switchingOnto: (output) => switchingOnto(parts.viewers, output),
424
- // What a viewer holds, and the cushion this file needs before a move that
425
- // is not urgent is made (roadmap item 98).
426
- bufferedSecondsOf: (consumerId) => bufferedSecondsOf(parts.viewers, consumerId),
427
- minimumBufferSecondsFor: (output) => minimumBufferSecondsOf(output),
428
- // Whether the whole machine has a place for one more encoder on this output,
429
- // asked before a preparation is recorded.
430
- admitsPreparation: (output) => parts.admission.admitsPreparation(output?.outputKey ?? ""),
431
- segmentClosed: (key, index) => parts.segmentStore.isClosed(key, index),
432
- // What a gone output left in the store, and whether another output's header
433
- // may stand in for its own.
434
- storedPieceReady: (key, fileName) => parts.serving.hasStoredPiece(key, fileName),
435
- headersCompatible: (goneKey, liveKey) => parts.serving.headersCompatible(goneKey, liveKey),
436
- placeViewerOn: (...args) => parts.viewerRequests.placeViewerOn(...args),
437
- audioRenditionName,
438
- logger,
439
- viewerLeaves: (...args) => parts.lifecycle.viewerLeaves(...args),
440
- createOrGetSession: (...args) => parts.viewerRequests.createOrGetSession(...args),
441
- planEncodersSoon: (...args) => parts.encodeRuns.planEncodersSoon(...args),
442
- disposeSession: (...args) => parts.lifecycle.disposeSession(...args),
443
- viewerPositionOf: (...args) => parts.viewerRequests.viewerPositionOf(...args),
444
- get encodeRuns() { return parts.encodeRuns; },
445
- get fileStartTimeReads() { return parts.fileStartTimeReads; },
446
- set fileStartTimeReads(value) { parts.fileStartTimeReads = value; },
447
- get getCachedAudioTracks() { return parts.getCachedAudioTracks; },
448
- get getCachedMediaInfo() { return parts.getCachedMediaInfo; },
449
- get getContainerMediaInfo() { return parts.getContainerMediaInfo; },
450
- get localBaseUrl() { return parts.localBaseUrl; },
451
- get outputTimes() { return parts.outputTimes; },
452
- get outputs() { return parts.outputs; },
453
- get quality() { return parts.quality; },
454
- get qualityOffer() { return parts.qualityOffer; },
455
- get segmentDurationSec() { return parts.segmentDurationSec; },
456
- get sourceFiles() { return parts.sourceFiles; },
457
- invalidateWaits: (output) => parts.serving.invalidateWaits(output),
458
- });
459
- // Answering a request for a playlist, an init segment or a segment: from the store when the piece is made and whole, otherwise held until it is.
460
- parts.serving = new SegmentServing({
461
- // What a key names, read back from the key: how to find a gone output's
462
- // stored pieces and whether they are cut at given times.
463
- segmentFormatOfKey,
464
- specOfKey: (key) => OutputSpec.fromKey(key),
465
- compareInits,
466
- activeOutputFor: (args) => activeOutputFor({ ...args, viewers: parts.viewers }),
467
- viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
468
- buildMasterPlaylist: (...args) => parts.renditions.buildMasterPlaylist(...args),
469
- declaredTracks: (...args) => parts.renditions.declaredTracks(...args),
470
- publishedGridFor: (...args) => parts.outputTimes.publishedGridFor(...args),
471
- runStartTimeFor: (...args) => parts.outputTimes.runStartTimeFor(...args),
472
- get cushion() { return parts.cushion; },
473
- get encodeOrchestrator() { return parts.encodeOrchestrator; },
474
- get encodeRuns() { return parts.encodeRuns; },
475
- get hostTimings() { return parts.hostTimings; },
476
- get lookaheadSeconds() { return parts.lookaheadSeconds; },
477
- get outputTimes() { return parts.outputTimes; },
478
- get outputs() { return parts.outputs; },
479
- get segmentStore() { return parts.segmentStore; },
480
- get startupWaitMs() { return parts.startupWaitMs; },
481
- get viewers() { return parts.viewers; },
482
- });
483
- // How an output ends: disposed when nobody is left on it and it has been idle, or all at once on shutdown; and the segments an earlier process left behind adopted at startup.
484
- parts.lifecycle = new OutputLifecycle({
485
- invalidateWaits: (output) => parts.serving.invalidateWaits(output),
486
- segmentFormatOfKey,
487
- // Where the viewers of one output stand, in its segments: what the store
488
- // reads to give disk back in the right order.
489
- viewerSegmentsOn: (key) => viewerSegmentsOn({
490
- outputs: parts.outputs.values(),
491
- outputKey: key,
492
- segmentAt: (output, seconds) => parts.outputTimes.segmentIndexForTime(output, seconds),
493
- now: Date.now(),
494
- viewers: parts.viewers
495
- }),
496
- get budgetTimer() { return parts.budgetTimer; },
497
- get cleanupTimer() { return parts.cleanupTimer; },
498
- get encodeRuns() { return parts.encodeRuns; },
499
- get keyframeTables() { return parts.keyframeTables; },
500
- get machineBudget() { return parts.machineBudget; },
501
- get outputTimes() { return parts.outputTimes; },
502
- get outputs() { return parts.outputs; },
503
- get returns() { return parts.returns; },
504
- get segmentStore() { return parts.segmentStore; },
505
- get sessionTtlMs() { return parts.sessionTtlMs; },
506
- get sourceFiles() { return parts.sourceFiles; },
507
- get timelines() { return parts.timelines; },
508
- get viewers() { return parts.viewers; },
509
- });
510
- // Which output answers a request for a file, made if it is not here yet: the encoding component's decision, made without knowing who asked.
511
- parts.opening = new OutputOpening({
512
- logger,
513
- // The largest peak an output of this mode and rate control has been seen
514
- // carrying here: the only figure an encoder with no bound of its own has.
515
- observedPeakMbps: (spec) => parts.localObservations?.peakMbps(spec) ?? null,
516
- limitsFor: (frame) => parts.limitsFor(frame),
517
- probeMediaInfo: (url) => probeInputMediaInfo(parts.ffmpegBin, url),
518
- probeKeyframeTimes: (url, budgetMs) => probeVideoKeyframeTimes(parts.ffmpegBin, url, budgetMs),
519
- get decodeCostModel() { return parts.decodeCostModel; },
520
- get enabled() { return parts.enabled; },
521
- get encodeCost() { return parts.encodeCost; },
522
- get encodeRuns() { return parts.encodeRuns; },
523
- get admission() { return parts.admission; },
524
- get getCachedMediaInfo() { return parts.getCachedMediaInfo; },
525
- get hostLoad() { return parts.hostLoad; },
526
- get hostTimings() { return parts.hostTimings; },
527
- get keyframeTables() { return parts.keyframeTables; },
528
- get localBaseUrl() { return parts.localBaseUrl; },
529
- get outputs() { return parts.outputs; },
530
- get renditions() { return parts.renditions; },
531
- get returns() { return parts.returns; },
532
- get segmentDurationSec() { return parts.segmentDurationSec; },
533
- get segmentFormat() { return parts.segmentFormat; },
534
- segmentOutputFiles,
535
- get softwarePresetBenchmark() { return parts.softwarePresetBenchmark; },
536
- get sourceFiles() { return parts.sourceFiles; },
537
- get timelines() { return parts.timelines; },
538
- get tonemapSupported() { return parts.tonemapSupported; },
539
- get videoEncoder() { return parts.encoders.current; },
540
- });
541
- // What a viewer asks for: an output of a file, a position, a report of progress.
542
- parts.viewerRequests = new ViewerRequests({
543
- // Whether this machine has a place for one more viewer on this output —
544
- // asked in the same synchronous stretch as they are put on it.
545
- admitsWatching: (output) => parts.admission.admitsWatching(output?.outputKey ?? ""),
546
- activeOutputFor: (args) => activeOutputFor({ ...args, viewers: parts.viewers }),
547
- viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
548
- minimumBufferSecondsFor: (output) => minimumBufferSecondsOf(output),
549
- getSourceStats: (...args) => parts.getSourceStats?.(...args) ?? null,
550
- disposeSession: (...args) => parts.lifecycle.disposeSession(...args),
551
- expectedFirstSegmentMs: (...args) => parts.hostTimings.expectedFirstSegmentMs(...args),
552
- expectedSessionCreateMs: (...args) => parts.hostTimings.expectedSessionCreateMs(...args),
553
- planEncodersSoon: (...args) => parts.encodeRuns.planEncodersSoon(...args),
554
- waitUntilReady: (...args) => parts.serving.waitUntilReady(...args),
555
- get encodeRuns() { return parts.encodeRuns; },
556
- encodeSpeedReadingOf: (output) => parts.encodeCost.latestSpeedReadingOf(output),
557
- projectedEncodeSpeedOf: (output) => parts.encodeCost.projectedSpeedOf(output),
558
- audioDescriptionOf,
559
- processingStartupSecondsOf: (output) => output.spec.carries === "audio-only" ?
560
- audioReadingFor(audioCalibration, { ...audioDescriptionOf(output),
561
- codec: audioDescriptionOf(output)?.codec ?? output.file.media?.audioCodec,
562
- transcode: output.spec.audio?.transcode })?.startupSeconds ?? 0 :
563
- Math.max(0, (startStopCost?.firstByteWaitSec ?? 0) - parts.segmentDurationSec /
564
- parts.encodeCost.projectedSpeedOf(output)),
565
- initialOutputBitsPerMediaSecondOf: (output) => {
566
- const description = audioDescriptionOf(output);
567
- const reference = audioReadingFor(audioCalibration, { codec: description?.codec, transcode: false });
568
- const audio = output.spec.audio ? output.spec.audio.transcode ? AUDIO_TRANSCODE_KBPS * 1000 :
569
- description?.bitrateKbps > 0 ? description.bitrateKbps * 1000 :
570
- reference ? reference.bytes * 8 / reference.durationSeconds : 0 : 0;
571
- const video = output.spec.video ? (output.spec.video.encode?.rateControl?.maxrateKbps ??
572
- output.file.media?.bitrateKbps ?? 0) * 1000 : 0;
573
- return video + audio;
574
- },
575
- get opening() { return parts.opening; },
576
- get outputTimes() { return parts.outputTimes; },
577
- get outputs() { return parts.outputs; },
578
- get lookaheadSeconds() { return parts.lookaheadSeconds; },
579
- get quality() { return parts.quality; },
580
- get qualityOffer() { return parts.qualityOffer; },
581
- get renditions() { return parts.renditions; },
582
- get segmentDurationSec() { return parts.segmentDurationSec; },
583
- get segmentStore() { return parts.segmentStore; },
584
- get sourceFiles() { return parts.sourceFiles; },
585
- get startupWaitMs() { return parts.startupWaitMs; },
586
- get viewers() { return parts.viewers; },
587
- invalidateWaits: (output) => parts.serving.invalidateWaits(output),
588
- // Whether a request of this generation is still taken, what it belongs to,
589
- // what answered it, and the hold a response keeps while it is being sent.
590
- acceptsGeneration: (consumerId, stated) => acceptsGeneration(parts.viewers, consumerId, stated),
591
- generationOfRequest: (consumerId, stated) => generationOfRequest(parts.viewers, consumerId, stated),
592
- noteGivenOutput: (consumerId, generation, askedHeight, segmentIndex, outputKey) =>
593
- noteGivenOutput(parts.viewers, consumerId, generation, askedHeight, segmentIndex, outputKey),
594
- holdForResponse: (consumerId, outputKey) => holdForResponse(parts.viewers, consumerId, outputKey),
595
- noteServingVerdict: (consumerId, verdict) => noteServingVerdict(parts.viewers, consumerId, verdict),
596
- servingVerdictOf: (consumerId) => servingVerdictOf(parts.viewers, consumerId),
597
- });
598
- // What this host takes to create an output and to produce its first segment, kept across restarts until the synthetic figure can replace it (CLAUDE.md, host timings).
599
- parts.hostTimings = new HostTimings({
600
- get segmentDurationSec() { return parts.segmentDurationSec; },
601
- get softwarePresetBenchmark() { return parts.softwarePresetBenchmark; },
602
- get stateDir() { return parts.stateDir; },
603
- });
604
- parts.enabled = Boolean(enabled);
605
- parts.ffmpegBin = ffmpegBin;
606
- parts.keyframeTables = keyframeTables;
607
- // Where measurements about this host are kept between runs. Empty means
608
- // beside the installed proxy; a deployment with somewhere persistent to
609
- // write names it (--state-dir).
610
- parts.stateDir = typeof stateDir === "string" ? stateDir : "";
611
- // Output container (fMP4/CMAF or MPEG-TS). Everything container-specific —
612
- // muxer args, file naming, playlist header, per-segment correction — lives
613
- // in this module; nothing here branches on the format.
614
- parts.segmentFormat = resolveSegmentFormat(segmentFormatId);
615
- // Optional accessor for media info the playback planner already probed for
616
- // (sourceKey, fileIndex), so session create can skip its own ffmpeg scan.
617
- parts.getCachedMediaInfo = typeof getCachedMediaInfo === "function" ? getCachedMediaInfo : null;
618
- // The file's audio tracks, for the master playlist's rendition group. Same
619
- // inventory the browser's audio menu is built from.
620
- parts.getCachedAudioTracks = typeof getCachedAudioTracks === "function" ? getCachedAudioTracks : null;
621
- // What a file declares about itself, read by the container layer from the
622
- // same header its track table comes from: format, duration, and where its
623
- // own timeline begins. The last of those is why this exists — a soundtrack
624
- // shipped as its own file has a timeline of its own, and asking ffmpeg for
625
- // it meant reading a header this proxy had already read.
626
- parts.getContainerMediaInfo =
627
- typeof getContainerMediaInfo === "function" ? getContainerMediaInfo : null;
628
- // Fetch one whole file of a source, as a bounded read rather than a
629
- // selection. Used to pull a soundtrack that ships beside the picture onto
630
- // the disk while the swarm has capacity to spare — see
631
- // `#fetchSpareSoundtracks`. Optional: a proxy wired without it simply reads
632
- // such a soundtrack when it is played.
633
- parts.fetchWholeFile = typeof fetchWholeFile === "function" ? fetchWholeFile : null;
634
- // Optional async accessor for a source's live download stats, used by the
635
- // realtime budget to tell a CPU limit from a download-starved input:
636
- // (sourceKey, fileIndex) => Promise<{ downloadSpeed, fileLength, fileProgress } | null>.
637
- parts.getSourceStats = typeof getSourceStats === "function" ? getSourceStats : null;
638
- parts.setPriorityMap = typeof setPriorityMap === "function" ? setPriorityMap : null;
639
- parts.contentionPenalties = contentionPenalties instanceof Map ? contentionPenalties : null;
640
- // Seconds of film per second when the picture is COPIED, measured at
641
- // startup. Nothing else prices that branch: the other benchmarks measure
642
- // encoding and decoding, and a copy does neither.
643
- parts.copySpeedX = Number.isFinite(copySpeedX) && copySpeedX > 0 ? copySpeedX : null;
644
- // Totals across every torrent this proxy holds, used to price what the
645
- // torrent itself costs the machine (item 7). Optional: a proxy wired
646
- // without it simply never learns that figure.
647
- parts.getTorrentTotals = typeof getTorrentTotals === "function" ? getTorrentTotals : null;
648
- // The spilled pieces, as a pair of closures over the torrent thread: what
649
- // they weigh and how to tell them their share. Not the pool — the pool is
650
- // on the other side of the thread boundary and this side holds none of it.
651
- parts.spillDisk = spillDisk;
652
- parts.wholeFiles = wholeFiles;
653
- parts.diagnostics = diagnostics;
654
- parts.diagnosticsRoot = diagnosticsRoot;
655
- parts.memoryClaimant = memoryClaimant;
656
- parts.budgetPolicy = budgetPolicy;
657
- // Detected H.264 encoder descriptor (hardware or software). Defaults to
658
- // software libx264 when no detection result is supplied. May be downgraded
659
- // to software at runtime if a hardware encode fails.
660
- parts.encoders = new EncoderSelection({ detected: videoEncoder, softwareDescriptor });
661
- // WHAT THIS HOST HAS SEEN ITS OWN ADMITTED ENCODES DO, kept between runs and
662
- // used only on the configuration it was seen on (roadmap item 97, step 14).
663
- // A refinement of figures that exist without it: nothing is selected,
664
- // admitted or served differently when it is empty. Absent where the startup
665
- // calibration was not run, which is only a wiring made without it.
666
- parts.localObservations = null;
667
- if (calibration?.fingerprint) {
668
- parts.localObservations = new LocalObservations({
669
- fingerprint: calibration.fingerprint,
670
- filePath: path.join(typeof stateDir === "string" && stateDir.length > 0 ? stateDir : PROXY_ROOT, "local-observations.json"),
671
- logger
672
- });
673
- parts.localObservations.load(calibration.kinds().map((kind) => configurationKeyOf(calibration.fingerprint, {
674
- kind,
675
- name: kind === "software" ? softwareDescriptor().name : (videoEncoder?.kind === kind ? videoEncoder.name : kind)
676
- })));
677
- }
678
- /**
679
- * What the segments an output has closed carried, from their sizes and the
680
- * spans its timeline gives them: the average over all of them and the
681
- * largest one.
682
- *
683
- * @param {object} session
684
- * @returns {{ averageKbps: number, peakKbps: number } | null}
685
- */
686
- const segmentRatesOf = (session) => {
687
- const sizes = parts.segmentStore.sizesOf(session.outputKey);
688
- const timeline = session.timeline;
689
- if (!timeline || sizes.size === 0) {
690
- return null;
691
- }
692
- let bits = 0;
693
- let seconds = 0;
694
- let peakKbps = 0;
695
- for (const [index, size] of sizes) {
696
- const start = timeline.publishedStartOf(index);
697
- const end = index < timeline.segmentCount ? timeline.publishedStartOf(index + 1) : null;
698
- if (!(Number.isFinite(start) && Number.isFinite(end) && end > start) || !(size > 0)) {
699
- continue;
700
- }
701
- bits += size * 8;
702
- seconds += end - start;
703
- peakKbps = Math.max(peakKbps, (size * 8) / (end - start) / 1000);
704
- }
705
- return seconds > 0 ? { averageKbps: bits / seconds / 1000, peakKbps } : null;
706
- };
707
- // The modes of the encoder IN USE NOW, as measured at startup: slowest
708
- // first, each with its throughput by size. Read through the selection rather
709
- // than copied, because the encoder in use can change — a failing device
710
- // moves this host to software for the rest of the process, and from then on
711
- // it is software's own modes that price and choose, not the device's.
712
- // Null where nothing was measured, and then nothing is re-encoded here.
713
- parts.calibration = calibration ?? null;
714
- const givenModes = Array.isArray(softwarePresetBenchmark) ? softwarePresetBenchmark : null;
715
- Object.defineProperty(parts, "softwarePresetBenchmark", {
716
- get: () => (parts.calibration
717
- ? parts.calibration.modesFor(parts.encoders.current?.kind)
718
- : givenModes),
719
- enumerable: true
720
- });
721
- // Host decode cost solved at startup from the calibration clips:
722
- // `a × Mpixel/s + b × Mbit/s + c` seconds of decoding per second of video.
723
- // A re-encode pays for this as well as for the encoder, and leaving it out
724
- // is what made the budget offer rungs this host ran at a third of realtime.
725
- // Null when the clips are missing or the fit was rejected — the budget then
726
- // prices the encoder alone, as it did before.
727
- parts.decodeCostModel = decodeCostModel ?? null;
728
- // Whether this ffmpeg build can tone-map HDR→SDR (zscale + tonemap filters).
729
- // Gates the tonemap chain for HDR sources on the software path.
730
- parts.tonemapSupported = Boolean(tonemapSupported);
731
- parts.segmentDurationSec = segmentDurationSec;
732
- // How far ahead of the viewer this proxy lets an encoder run, in seconds of
733
- // playback. Stated rather than kept private, because the browser's forward
734
- // buffer is bounded by the same quantity and used to carry a copy of its
735
- // own: a hand-written 30 s, justified in a comment by a DIFFERENT constant
736
- // (the eight segments that bound a request ahead of the ENCODE HEAD), so
737
- // three quarters of what the encoder had already produced was thrown away.
738
- // One figure, said by the side that owns it (roadmap item 4).
739
- parts.lookaheadSeconds = LOOKAHEAD_PAUSE_SECONDS;
740
- parts.sessionTtlMs = sessionTtlMs;
741
- parts.startupWaitMs = startupWaitMs;
742
- parts.localBaseUrl = buildHttpBaseUrl(localBindHost, localPort);
743
- // Everyone watching anything, one object per person rather than one per
744
- // person per output. What a viewer chose, where they are and which outputs
745
- // they are watching are facts about the person; kept per output they were
746
- // three copies of which two were always stale.
747
- // Every change to who is watching what re-decides which encoders should
748
- // exist, because that decision reads nothing else about viewers. It used to
749
- // be re-taken on a five-second timer instead, which made a just-created
750
- // output wait up to five seconds before anything noticed it had a viewer.
751
- parts.viewers = new Viewers({ onChange: () => parts.encodeRuns.planEncodersSoon() });
752
- // The outputs that exist, and every question about them: the picture a step
753
- // belongs to, the steps, the soundtracks, the height a step is named by.
754
- parts.outputs = new OutputCatalog({
755
- fileLengthOf: (session) => parts.hostLoad.fileLengthByKey.get(session.file.key) ?? 0,
756
- largestPieceOf: (address) => parts.segmentStore.largestPiece(address)
757
- });
758
- // What this host learned last time it ran. Without it every restart shows
759
- // the first viewer a figure with no measurement behind it.
760
- parts.hostTimings.loadHostTimings();
761
- // Where produced segments live, addressed by WHAT they are rather than by
762
- // which session's encoder wrote them. Two sessions of one output — two
763
- // viewers who opened the same film at different places — write into one
764
- // directory and each serves what the other has already made. Injectable so
765
- // a test can give it a root of its own.
766
- parts.segmentStore = segmentStore instanceof SegmentStore
767
- ? segmentStore
768
- : new SegmentStore({ logger });
769
- // The store has grown: whether it still fits its share is asked now rather
770
- // than on the next cleanup pass.
771
- parts.segmentStore.onPublished(() => parts.lifecycle.keepWithinRoomSoon());
772
- // A segment closed may be the one a viewer's move between two limits of a
773
- // height is waiting for.
774
- parts.segmentStore.onPublished((key, index) => parts.renditions.noteSegmentPublished(key, index));
775
- // What encoders there should be on each output, and where. Given the two
776
- // things only this class can answer: how many this machine can afford, and
777
- // how to make one.
778
- // What encoding costs this machine, and which heights follow from it. Given
779
- // what it cannot work out for itself: which sessions belong to one file, the
780
- // host's own readings AT THE MOMENT OF THE QUESTION rather than copied, how
781
- // a soundtrack is keyed, how many encoders are running, and what a file
782
- // costs merely by being fetched.
783
- parts.encodeCost = new EncodeCost({
784
- outputs: parts.outputs,
785
- host: () => ({
786
- benchmark: parts.softwarePresetBenchmark,
787
- decodeModel: parts.decodeCostModel,
788
- contentionPenalties: parts.contentionPenalties,
789
- copySpeedX: parts.copySpeedX,
790
- audioCalibration,
791
- audioDescriptionOf,
792
- availability: parts.hostLoad.hostAvailability,
793
- // WHICH encoder this host settled on. Only the software ladder is
794
- // benchmarked, so a reading taken off a hardware encoder cannot be
795
- // split into its decode and encode halves and is not filed as one.
796
- encoderKind: parts.encoders.current?.kind ?? null,
797
- // The slowest this output's mode has been seen running alone on
798
- // comparable material on this configuration, or null.
799
- observedAloneSpeed: (session) => parts.localObservations?.slowestAloneSpeed(session.spec, contentOf(session.file)) ?? null
800
- }),
801
- // HOW MANY ENCODER PROCESSES ARE RUNNING, asked of the one thing that
802
- // makes and unmakes them. This used to be two counts of one fact, taken
803
- // by walking the session registry — one per session, one per run — and
804
- // they agreed only while a session held at most one run.
805
- runningEncoders: () => parts.encodeOrchestrator.runningCount(),
806
- encodersRunningNow: () => parts.encodeOrchestrator.runningCount(),
807
- torrentCostSecFor: (session) => parts.hostLoad.torrentCostSecFor(session),
808
- boundBy: (session) => parts.quality.classifyTranscodeBound(session),
809
- runsFor: (session) => parts.encodeRuns.runsOf(session),
810
- stateFor: (session) => parts.encodeRuns.runStateOf(session),
811
- // The run's OWN progress: a speed is a pair of readings of one process.
812
- progressFor: (_session, run) => run?.progress?.snapshot?.() ?? null
813
- });
814
- // WHICH HEIGHTS ARE ON THE MENU, which is the arithmetic above plus three
815
- // things that are nothing to do with it: whose answer it is, what may never
816
- // be withdrawn, and when the answer may be reused.
817
- parts.qualityOffer = new QualityOffer({
818
- encodeCost: parts.encodeCost,
819
- occupiedCostSec: (fileKey) => parts.admission?.occupiedCost({ exceptFileKey: fileKey }).costSec ?? 0,
820
- occupancyKnownFor: (fileKey) => (parts.admission?.occupiedCost({ exceptFileKey: fileKey }).unpriced ?? 0) === 0,
821
- outputs: parts.outputs,
822
- stateFor: (session) => parts.encodeRuns.runStateOf(session),
823
- // WHICH HEIGHTS A LIVE VIEWER HAS ON SCREEN, as numbers. Who is watching
824
- // what is the viewer layer's, and which session is which height is the
825
- // film's shape; neither travels — what crosses is the list of heights.
826
- heightsOnScreen: (owner) => parts.quality.heightsOnScreen(owner),
827
- // WHAT THE SWARM IS DOING WITH THIS FILE. Three readings, taken by
828
- // whoever reads it and handed over as three numbers: the speed this
829
- // file's own interruptions demand, the megabytes a second a viewer draws
830
- // through it, and what a megabyte costs this process.
831
- supplyFor: (file) => ({
832
- requiredSpeed: parts.hostLoad.requiredSpeedFor(file.sourceKey, file.fileIndex),
833
- megabytesPerSecond: parts.hostLoad.torrentMegabytesPerSecond(
834
- file.sourceKey,
835
- file.fileIndex,
836
- file.lengthBytes ?? parts.hostLoad.fileLengthByKey.get(file.key) ?? null,
837
- file.durationSeconds
838
- ),
839
- costPerMegabyte: parts.hostLoad.observedTorrentCostPerMegabyte
840
- })
841
- });
842
- // The priority map, built from where the viewers are and handed to both
843
- // sides that act on it. The downloading lives in another thread, so its
844
- // copy travels over the worker channel.
845
- parts.priority = new PriorityOrchestrator({
846
- publish: ({ sourceKey, fileIndex, durationSeconds, zones }) => {
847
- void Promise.resolve(
848
- parts.setPriorityMap?.({ sourceKey, fileIndex, durationSeconds, zones })
849
- ).catch(() => {});
850
- },
851
- get viewers() { return parts.viewers; },
852
- // WHERE THE TWO FACTS MEET, and this is the only place that holds both.
853
- // Which step is on somebody's screen belongs to the person; which output
854
- // a step supersedes belongs to the film's shape. Neither layer is handed
855
- // the other — one gets a plain id, the other is read for one field.
856
- watchedBy: (session, viewer) => !parts.outputs.supersededBy(session, viewer.activeVariantId ?? null),
857
- allowanceFor: (session) => minimumBufferFrom({
858
- segmentSeconds: parts.segmentDurationSec,
859
- worstSupplyWaitSec: parts.hostLoad.supplyFor(session.file)?.worstWaitSec
860
- })?.seconds ?? parts.segmentDurationSec
861
- });
862
- // WHETHER THE WHOLE MACHINE HAS A PLACE for one more encoder, across outputs.
863
- // Given what it cannot hold itself: what runs (the orchestrator's), what is
864
- // being prepared (the viewers' records), what an output costs (the encoding
865
- // cost) and how much of the machine nobody has priced (the host's reading).
866
- parts.admission = new EncodeAdmission({
867
- liveRunsByAddress: () => parts.encodeOrchestrator.liveRunsByAddress(),
868
- preparedAddresses: () => {
869
- const addresses = new Set();
870
- for (const id of outputsBeingPrepared(parts.viewers)) {
871
- const key = parts.outputs.get(id)?.outputKey;
872
- if (key) {
873
- addresses.add(key);
874
- }
875
- }
876
- return addresses;
877
- },
878
- // Every output a present viewer RECEIVES — by the same rule the per-output
879
- // priority map is built by — that still has something left to make. The
880
- // place an output is opened on is held from the moment its viewer is put
881
- // on it, not from when the plan gets round to its first encoder.
882
- watchedAddresses: () => {
883
- const addresses = new Set();
884
- for (const session of parts.outputs.values()) {
885
- const address = session.outputKey;
886
- if (!address || addresses.has(address) || finishedOutput(session)) {
887
- continue;
888
- }
889
- for (const viewer of parts.viewers.forOutput(session).values()) {
890
- if (viewer.isPresent() && !parts.outputs.supersededBy(session, viewer.activeVariantId ?? null)) {
891
- addresses.add(address);
892
- break;
893
- }
894
- }
895
- }
896
- return addresses;
897
- },
898
- finished: (address) => parts.outputs.outputsOn(address).some((session) => finishedOutput(session)),
899
- loadOf: (address) => parts.encodeCost.loadOfOutput(address),
900
- loadForCandidate: (spec, file) => parts.encodeCost.loadForCandidate(spec, file),
901
- availability: () => parts.hostLoad.hostAvailability ?? null
902
- });
903
- parts.encodeOrchestrator = new EncodeOrchestrator({
904
- admission: parts.admission,
905
- // What the viewers actually waited for, by band. The ledger is the
906
- // priority layer's; the encoding is handed a way to ask it.
907
- describeWaits: (address) => waits.describe(address),
908
- maxRunsFor: (address) => parts.encodeRuns.maxRunsForOutput(address),
909
- makeRun: ({ address, from, to, because }) => parts.encodeRuns.makeRunAt(address, from, to, because),
910
- segmentSeconds: parts.segmentDurationSec,
911
- contentionPenalties: parts.contentionPenalties,
912
- startingSpeedFor: (address) => parts.encodeCost.speedForOutput(address),
913
- segmentCoverage,
914
- // HOW IT ASKS TO DECIDE AGAIN. A plan that refuses to place anything
915
- // because an output's input is away needs something to bring it back:
916
- // nothing about the state changes while the data is missing, so no event
917
- // arrives on its own.
918
- planSoon: () => parts.encodeRuns.planEncodersSoon(),
919
- logger
920
- });
921
- // What a start and a stop were measured to cost here, before any viewer
922
- // existed. Without it both read zero at a cold open, and zero is not
923
- // "unmeasured" — it is "free", which is what moved an encoder between two
924
- // adjacent numbers every half second in the field.
925
- parts.encodeOrchestrator.noteStartupCosts(startStopCost);
926
- // Where each file is cut, held once per file and grid rather than once per
927
- // session. Two sessions of one film MUST agree about this to the
928
- // millisecond — a segment made by either has to be appendable where the
929
- // other's would have gone — and until now they agreed by copying, which is
930
- // a thing somebody has to remember to do and which drifted twice in the
931
- // field. They share the table now.
932
- parts.timelines = new Timelines();
933
- // The files this proxy is serving, one object per file however many
934
- // sessions are of it. It holds the file's key — which every cache about a
935
- // file is keyed by — its name, and the facts a probe returned.
936
- parts.sourceFiles = new SourceFiles();
937
- parts.cleanupTimer = setInterval(() => {
938
- void parts.lifecycle.cleanupExpired();
939
- }, CLEANUP_INTERVAL_MS);
940
- parts.cleanupTimer.unref();
941
- // How long after material stops being read somebody asks for it again — the
942
- // one term of the keeping period that is guessed rather than measured, and
943
- // the only place it can be measured from.
944
- parts.returns = new Returns();
945
- // One owner of the disk, and the list of what takes it lives with the owner.
946
- parts.machineBudget = wireMachineBudget({
947
- segmentStore: parts.segmentStore,
948
- spill: parts.spillDisk,
949
- wholeFiles: parts.wholeFiles,
950
- diagnostics: parts.diagnostics,
951
- diagnosticsRoot: parts.diagnosticsRoot,
952
- memory: parts.memoryClaimant,
953
- policy: parts.budgetPolicy ?? undefined,
954
- readFree: freeBytesFor,
955
- logger
956
- });
957
- // Realtime-budget monitor: only meaningful for the software encoder with a
958
- // benchmark (the only path that can pick/step resolution). Cheap no-op scan
959
- // otherwise.
960
- // The quality budget: what each output is asked to step to, and the bitrate ceiling a viewer's link sets. Everything it reads of the rest of the proxy is listed here.
961
- parts.quality = new QualityController({
962
- // The longest an output of this mode has been seen taking to be ready for
963
- // a viewer moving onto it, in milliseconds, or null.
964
- observedPreparationMs: (output) => {
965
- const seconds = parts.localObservations?.longestPreparationSec(output?.spec) ?? null;
966
- return seconds === null ? null : seconds * 1000;
967
- },
968
- // WHAT ENCODING IS TOLD ABOUT A PERSON: values, by name. Not the viewer,
969
- // whose record belongs to the viewer layer and is written only there.
970
- consumersOn: (output) => consumersOn(parts.viewers, output),
971
- qualityModeOf: (output, consumerId) => qualityModeOf(parts.viewers, output, consumerId),
972
- stepOnScreenOf: (output, consumerId) => stepOnScreenOf(parts.viewers, output, consumerId),
973
- standingAskOf: (output, consumerId) => standingAskOf(parts.viewers, output, consumerId),
974
- askQualityOf: (output, consumerId, height, reason, now, urgent) =>
975
- askQualityOf(parts.viewers, output, consumerId, height, reason, now, urgent),
976
- // What a viewer's page said about their buffer and the picture they see
977
- // (roadmap item 98), as values.
978
- bufferOf: (output, consumerId, spanSec) => bufferOf(parts.viewers, output, consumerId, spanSec),
979
- visiblePictureOf: (output, consumerId) => visiblePictureOf(parts.viewers, output, consumerId),
980
- // How long this host takes to close a first segment: how soon another
981
- // output could have the piece a viewer needs.
982
- expectedFirstSegmentMs: () => parts.hostTimings.expectedFirstSegmentMs(),
983
- dropAskOf: (output, consumerId) => dropAskOf(parts.viewers, output, consumerId),
984
- // EACH viewer's own link and who is present: a thin link decides for the
985
- // person on it and for nobody else (roadmap item 97, step 11), so the
986
- // budget asks per viewer instead of taking the worst over all of them.
987
- presentOn: (output) => presentOn(parts.viewers, output),
988
- linkReportOf: (output, consumerId) => linkReportOf(parts.viewers, output, consumerId),
989
- // The soundtrack a viewer receives, as part of the load their link carries.
990
- viewerAudioLoadOf: (base, consumerId) => parts.renditions.viewerAudioLoadOf(base, consumerId),
991
- // The first lever on a viewer's link: another limit of the height on their
992
- // screen, prepared and moved to without their player being told.
993
- prepareSameHeightSwitch: (output, consumerId, direction, reason) =>
994
- parts.renditions.prepareSameHeightSwitch(output, consumerId, direction, reason),
995
- sameHeightSwitchPending: (consumerId) => parts.renditions.sameHeightSwitchPending(consumerId),
996
- sameHeightSwitchDirection: (consumerId) => parts.renditions.sameHeightSwitchDirection(consumerId),
997
- cancelSameHeightSwitch: (consumerId, reason) => parts.renditions.cancelSameHeightSwitch(consumerId, reason),
998
- heightReadyFor: (base, consumerId, height) => parts.renditions.heightReadyFor(base, consumerId, height),
999
- isLive: (...args) => parts.encodeRuns.isLive(...args),
1000
- liveConsumers: (...args) => parts.renditions.liveConsumers(...args),
1001
- liveRunsOf: (...args) => parts.encodeRuns.liveRunsOf(...args),
1002
- producedNumbers: (...args) => parts.serving.producedNumbers(...args),
1003
- reportHostLoad: (...args) => parts.hostLoad.reportHostLoad(...args),
1004
- runStateOf: (...args) => parts.encodeRuns.runStateOf(...args),
1005
- sampleDownloadRates: (...args) => parts.hostLoad.sampleDownloadRates(...args),
1006
- get encodeCost() { return parts.encodeCost; },
1007
- get getSourceStats() { return parts.getSourceStats; },
1008
- get outputs() { return parts.outputs; },
1009
- get qualityOffer() { return parts.qualityOffer; },
1010
- get segmentDurationSec() { return parts.segmentDurationSec; },
1011
- segmentPaths,
1012
- get videoEncoder() { return parts.encoders.current; },
1013
- });
1014
- parts.budgetTimer = setInterval(() => {
1015
- parts.cushion.reportCushions();
1016
- void parts.quality.runQualityBudgetOnce();
1017
- }, BUDGET_CHECK_INTERVAL_MS);
1018
- parts.budgetTimer.unref();
1019
- return parts;
1020
- }
1
+ /**
2
+ * @file Wiring of the outputs, the viewers and the encoding.
3
+ *
4
+ * Builds each component and hands it the narrow host it reads, and nothing
5
+ * else. It holds no behaviour: every member that used to be a method of the
6
+ * session manager is a method of the component that owns it now. What it
7
+ * returns is the set of components and the settings they share, for
8
+ * `server.js` to give each route the ones that route asks.
9
+ */
10
+
11
+ import { logger } from "../../utils/logger.js";
12
+ import { KeyframeTables } from "../media/KeyframeTables.js";
13
+ import { waits } from "./WaitLedger.js";
14
+ import { readMachineState, readProcessCpuSeconds, readProxyCpuSeconds, readSystemCpu, shareOfMachine } from "../encode/host-load.js";
15
+ import { minimumBufferFrom } from "../torrent/supply-margin.js";
16
+ import { PriorityOrchestrator } from "../viewer/PriorityOrchestrator.js";
17
+ import { softwareDescriptor } from "../encode/hwaccel.js";
18
+ import { resolveSegmentFormat } from "../encode/segment-formats/index.js";
19
+ import { Timelines } from "../encode/output/Timeline.js";
20
+ import { SourceFiles } from "../media/SourceFile.js";
21
+ import { SegmentStore } from "../storage/segment-store/SegmentStore.js";
22
+ import { EncodeCost } from "../encode/quality/EncodeCost.js";
23
+ import { QualityOffer } from "../encode/quality/QualityOffer.js";
24
+ import { activeOutputFor } from "../viewer/active-output.js";
25
+ import {
26
+ acceptsGeneration,
27
+ askQualityOf,
28
+ audioBeingWarmedOf,
29
+ audioChoiceOf,
30
+ chooseAudioTrack,
31
+ chooseOutput,
32
+ chosenOutputOf,
33
+ consumersOn,
34
+ dropAskOf,
35
+ generationOfRequest,
36
+ givenOutputOf,
37
+ heightsChosenAs,
38
+ highestGivenSegmentOf,
39
+ holdForResponse,
40
+ linkMbpsOf,
41
+ linkReportOf,
42
+ bufferOf,
43
+ bufferedSecondsOf,
44
+ visiblePictureOf,
45
+ linkReportsOn,
46
+ noteAudioBeingWarmed,
47
+ noteGivenOutput,
48
+ noteSameHeightSwitch,
49
+ noteServingVerdict,
50
+ noteStepBeingWarmed,
51
+ noteStepOnScreen,
52
+ outputsBeingPrepared,
53
+ placeOn,
54
+ presentOn,
55
+ qualityModeOf,
56
+ sameHeightSwitchOf,
57
+ servingVerdictOf,
58
+ standingAskOf,
59
+ stepBeingWarmedOf,
60
+ stepBeingWarmedSinceOf,
61
+ stepOnScreenOf,
62
+ switchingOnto,
63
+ watches
64
+ } from "../viewer/choices.js";
65
+ import { viewerSecondsOn, viewerSegmentsOn } from "../viewer/positions.js";
66
+ import { Viewers } from "../viewer/Viewers.js";
67
+ import { OutputCatalog } from "../encode/output/OutputCatalog.js";
68
+ import { ViewerRequests } from "./ViewerRequests.js";
69
+ import { OutputLifecycle } from "./OutputLifecycle.js";
70
+ import { SegmentServing } from "./SegmentServing.js";
71
+ import { audioStartSecondsFor } from "../viewer/audio-start.js";
72
+ import { forecastRate, predictPlaybackReadiness, RateTrend } from "../viewer/playback-readiness.js";
73
+ import { audioRenditionName } from "../media/audio-inventory.js";
74
+ import { Renditions } from "../encode/Renditions.js";
75
+ import { CushionReport, LOOKAHEAD_PAUSE_SECONDS } from "../encode/CushionReport.js";
76
+ import { EncodeRuns } from "../encode/EncodeRuns.js";
77
+ import { OutputTimes } from "../encode/OutputTimes.js";
78
+ import { HostLoad } from "../encode/quality/HostLoad.js";
79
+ import { HostTimings } from "../encode/quality/HostTimings.js";
80
+ import { BUDGET_CHECK_INTERVAL_MS, QualityController } from "../encode/quality/QualityController.js";
81
+ import { EncodeOrchestrator } from "../encode/EncodeOrchestrator.js";
82
+ import { EncodeAdmission } from "../encode/EncodeAdmission.js";
83
+ import { EncoderSelection } from "../encode/EncoderSelection.js";
84
+ import { OutputOpening } from "../encode/OutputOpening.js";
85
+ import { segmentFormatOfKey } from "../encode/output-key-format.js";
86
+ import { OutputSpec } from "../encode/output/OutputSpec.js";
87
+ import { compareInits } from "../encode/segment-formats/init-compat.js";
88
+ import { nominalKbpsFor, AUDIO_TRANSCODE_KBPS } from "../encode/args.js";
89
+ import { audioReadingFor } from "../encode/audio-work-rate.js";
90
+ import { probeInputMediaInfo } from "../media/media-info-probe.js";
91
+ import { probeVideoKeyframeTimes } from "../media/keyframe-probe.js";
92
+ import { wireMachineBudget } from "../storage/wire.js";
93
+ import { Returns } from "../storage/returns.js";
94
+ import { freeBytesFor } from "../storage/free.js";
95
+ import path from "node:path";
96
+ import { contentOf, LocalObservations } from "../encode/LocalObservations.js";
97
+ import { configurationKeyOf } from "../encode/fingerprint.js";
98
+ import { PROXY_ROOT } from "../encode/quality/HostTimings.js";
99
+ import { qualityStateOf } from "../encode/quality/OutputQualityState.js";
100
+
101
+ // Where a variant and an audio rendition live under a session — `v/<height>/…`
102
+ // and `a/<track>/…` — is stated in `encode/output/playlists.js`, beside the lines that
103
+ // write those addresses into a master playlist. The routes that parse them back
104
+ // are in `server.js`.
105
+
106
+ const CLEANUP_INTERVAL_MS = 30_000;
107
+ const DEFAULT_SEGMENT_DURATION_SEC = 4;
108
+ // Idle TTL: a session is disposed this long after the last segment/playlist
109
+ // access. Long enough that a viewer who pauses, backgrounds the tab, or briefly
110
+ // turns the phone off can resume WITHOUT a cold ffmpeg restart (the warm session
111
+ // also backs the seamless auto-reconnect). ffmpeg stops producing at the
112
+ // look-ahead cap when idle, so a lingering session costs retained segments on
113
+ // disk, not sustained CPU. Active playback refreshes the timer on every segment
114
+ // fetch, so it never expires mid-watch.
115
+ // How long a session outlives the BROWSER, not the viewing. Since server
116
+ // 0.8.103 a browser that holds a session re-asserts it every 30 s, so an open
117
+ // tab never consumes this at all — not while paused, not across a three-hour
118
+ // film. What is left is the case where the browser has genuinely gone: the tab
119
+ // was killed without releasing, the phone slept, the network dropped. Keeping
120
+ // the session means such a viewer comes back to a warm encoder instead of
121
+ // waiting out a cold start; the cost while nobody is there is disk for the
122
+ // produced segments, since the encoder is suspended and burns no CPU, and that
123
+ // disk is already bounded by the pool's 10 GB cap with eviction. Thirty minutes
124
+ // covers a meal, a phone call or a lift ride.
125
+ const DEFAULT_SESSION_TTL_MS = 30 * 60 * 1000;
126
+ const DEFAULT_STARTUP_WAIT_MS = 5_000;
127
+
128
+ /**
129
+ * Convert a bind-all host address to the loopback address so that
130
+ * the HLS input URL is always reachable from the same machine.
131
+ *
132
+ * @param {string} host
133
+ * @returns {string}
134
+ */
135
+ function toLoopbackHost(host) {
136
+ if (host === "0.0.0.0" || host === "::") {
137
+ return "127.0.0.1";
138
+ }
139
+ return host;
140
+ }
141
+
142
+ /**
143
+ * Build the HTTP base URL (scheme + host + port) for the local proxy server.
144
+ *
145
+ * @param {string} host - Bind host (may be "0.0.0.0" or "::").
146
+ * @param {number} port
147
+ * @returns {string} e.g. "http://127.0.0.1:9090"
148
+ */
149
+ function buildHttpBaseUrl(host, port) {
150
+ const url = new URL("http://localhost");
151
+ url.hostname = toLoopbackHost(host);
152
+ url.port = String(port);
153
+ return url.origin;
154
+ }
155
+
156
+ /**
157
+ * @typedef {Object} WireOutputsOptions
158
+ * @property {boolean} enabled - Whether HLS transcoding is enabled.
159
+ * @property {string} ffmpegBin - Path to the ffmpeg executable.
160
+ * @property {string} localBindHost - Host the proxy HTTP server is bound to.
161
+ * @property {number} localPort - Port the proxy HTTP server is listening on.
162
+ * @property {number} [segmentDurationSec] - HLS segment length in seconds.
163
+ * @property {number} [sessionTtlMs] - Session idle TTL in milliseconds.
164
+ * @property {number} [startupWaitMs] - Max time to wait for the first playlist file.
165
+ * @property {string} [segmentFormatId] - Output container: "fmp4" (default)
166
+ * or "mpegts". See `../segment-formats/index.js`.
167
+ */
168
+
169
+ /**
170
+ * @typedef {Object} HlsSession
171
+ * @property {string} id - The name of the output it produces.
172
+ * @property {string} fileName - Display name of the file being transcoded.
173
+ * @property {import("../media/container/KeyframeTable.js").KeyframeTable} keyframes -
174
+ * Where this file's keyframes are. One object per file, held by every session
175
+ * of it, so a table read late still reaches them. Used to snap a source seek
176
+ * onto a known-valid position (see #startEncodeRun).
177
+ */
178
+
179
+ /**
180
+ * Build the components and connect them.
181
+ *
182
+ * @param {WireOutputsOptions} options
183
+ * @returns {object} The components, and the settings they read.
184
+ */
185
+ export function wireOutputs({
186
+ enabled,
187
+ ffmpegBin,
188
+ localBindHost,
189
+ localPort,
190
+ segmentDurationSec = DEFAULT_SEGMENT_DURATION_SEC,
191
+ sessionTtlMs = DEFAULT_SESSION_TTL_MS,
192
+ startupWaitMs = DEFAULT_STARTUP_WAIT_MS,
193
+ // Where every file's keyframe table lives, and the only thing that reads
194
+ // one. A session asks for its file's table and is handed the object every
195
+ // other reader of that file holds, so a table that arrives late reaches the
196
+ // sessions created before it. Built here when nothing supplied one, which
197
+ // is a registry with no reader: it answers "not read" about every file, and
198
+ // every picture is then re-encoded — the correct behaviour for a proxy
199
+ // wired without a container reader, and what every unit test gets.
200
+ keyframeTables = new KeyframeTables(),
201
+ videoEncoder = null,
202
+ softwarePresetBenchmark = null,
203
+ // What every encoder this host may use was qualified and measured to do at
204
+ // startup, by kind (`encode/calibration.js`). Takes the place of the one
205
+ // array above, which a test may still hand in on its own.
206
+ calibration = null,
207
+ decodeCostModel = null,
208
+ getSourceStats = null,
209
+ setPriorityMap = null,
210
+ // What a second job costs on this host, measured at startup. Null when it
211
+ // could not be measured, and then nothing is corrected — the alternative
212
+ // is inventing a penalty, which is the same fault as inventing a fill rate.
213
+ contentionPenalties = null,
214
+ copySpeedX = null,
215
+ audioCalibration = [],
216
+ tonemapSupported = false,
217
+ getCachedMediaInfo = null,
218
+ getCachedAudioTracks = null,
219
+ getContainerMediaInfo = null,
220
+ fetchWholeFile = null,
221
+ segmentFormatId = undefined,
222
+ stateDir = "",
223
+ segmentStore = null,
224
+ getTorrentTotals,
225
+ startStopCost = null,
226
+ spillDisk = null,
227
+ wholeFiles = null,
228
+ diagnostics = null,
229
+ diagnosticsRoot = "",
230
+ memoryClaimant = null,
231
+ budgetPolicy = null}) {
232
+ const parts = {};
233
+ const audioDescriptionOf = (output) => (getCachedAudioTracks?.({
234
+ sourceKey: output.file.sourceKey, fileIndex: output.spec.grid?.fileIndex ?? output.file.fileIndex
235
+ }) ?? []).find((track) => track.fileIndex === output.spec.audio?.fileIndex &&
236
+ track.sourceTrackIndex === output.spec.audio?.trackIndex) ?? null;
237
+ // Encode components receive only the storage operations they use. The
238
+ // concrete SegmentStore remains owned by this assembly and is never handed
239
+ // to the encode layer.
240
+ const segmentCoverage = {
241
+ provenNumbers: (address) => parts.segmentStore.provenNumbers(address),
242
+ announce: (address, index) => parts.segmentStore.announce(address, index),
243
+ filesHeld: (address) => parts.segmentStore.filesHeld(address),
244
+ clearUpAfter: (address, startedAt) => parts.segmentStore.clearUpAfter(address, startedAt)
245
+ };
246
+ const segmentFiles = {
247
+ pathFor: (address) => parts.segmentStore.pathFor(address),
248
+ initOf: (address) => parts.segmentStore.initOf(address),
249
+ directoryFor: (address) => parts.segmentStore.directoryFor(address),
250
+ publish: (address, makingName, format, coverage) => parts.segmentStore.publish(address, makingName, format, coverage)
251
+ };
252
+ const segmentOutputFiles = {
253
+ addresses: () => parts.segmentStore.addresses(),
254
+ isClosed: (address, index) => parts.segmentStore.isClosed(address, index),
255
+ lastReadAt: (address) => parts.segmentStore.lastReadAt(address),
256
+ directoryFor: (address) => parts.segmentStore.directoryFor(address),
257
+ useFormat: (address, format) => parts.segmentStore.useFormat(address, format)
258
+ };
259
+ const segmentPaths = {
260
+ pathOf: (address, index) => parts.segmentStore.pathOf(address, index)
261
+ };
262
+ /**
263
+ * Whether everything this output will ever serve is made: every segment of
264
+ * its timeline closed in the store. Such an output needs no encoder and
265
+ * holds no place, however many watch it.
266
+ *
267
+ * @param {object} session
268
+ * @returns {boolean}
269
+ */
270
+ const finishedOutput = (session) => {
271
+ const count = Number(session?.timeline?.segmentCount) || 0;
272
+ return count > 0 && parts.segmentStore.provenNumbers(session.outputKey).length >= count;
273
+ };
274
+ // The nominal limits a size may be produced at, highest first. ONE list, read
275
+ // by the opening of an output and by the move between limits of a height, so
276
+ // the two cannot disagree about which limits exist. Only the size's own until
277
+ // a set of lower limits is decided (roadmap item 97, step 14): a lower limit
278
+ // handed to a viewer must come with the page saying so. Asked with the whole
279
+ // frame, because the row is chosen by area (`limitRowFor`).
280
+ parts.limitsFor = (frame) => [nominalKbpsFor(frame)];
281
+ // What the torrent and the proxy itself spend on this host, and how fast each watched torrent moves.
282
+ parts.hostLoad = new HostLoad({
283
+ readMachineState,
284
+ readProcessCpuSeconds,
285
+ readProxyCpuSeconds,
286
+ readSystemCpu,
287
+ shareOfMachine,
288
+ liveRunsOf: (...args) => parts.encodeRuns.liveRunsOf(...args),
289
+ runStateOf: (...args) => parts.encodeRuns.runStateOf(...args),
290
+ get getSourceStats() { return parts.getSourceStats; },
291
+ get getTorrentTotals() { return parts.getTorrentTotals; },
292
+ get outputs() { return parts.outputs; },
293
+ });
294
+ // Where each segment of an output begins, and how that table is corrected from what the encoder produced.
295
+ parts.outputTimes = new OutputTimes({
296
+ logger,
297
+ runsOf: (...args) => parts.encodeRuns.runsOf(...args),
298
+ stopEncodeRun: (...args) => parts.encodeRuns.stopEncodeRun(...args),
299
+ planEncodersSoon: (...args) => parts.encodeRuns.planEncodersSoon(...args),
300
+ get outputs() { return parts.outputs; },
301
+ get segmentDurationSec() { return parts.segmentDurationSec; },
302
+ });
303
+ // The encoders of this proxy: built where the plan places them, followed while they run, accounted when they end.
304
+ parts.encodeRuns = new EncodeRuns({
305
+ logger,
306
+ // What an admitted encode was seen to do, filed under its configuration.
307
+ observeEncodeEnded: (session) => {
308
+ if (!parts.localObservations || !session?.spec?.video?.encode) {
309
+ return;
310
+ }
311
+ const alone = Number(qualityStateOf(session).lastAloneSpeed);
312
+ parts.localObservations.noteEncode({
313
+ spec: session.spec,
314
+ content: contentOf(session.file),
315
+ aloneSpeedX: Number.isFinite(alone) && alone > 0 ? alone : null,
316
+ segmentKbps: segmentRatesOf(session)
317
+ });
318
+ },
319
+ viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
320
+ noteRunStarting: (output) => parts.quality.noteRunStarting(output),
321
+ inputOf: (...args) => parts.renditions.inputOf(...args),
322
+ producedNumbers: (...args) => parts.serving.producedNumbers(...args),
323
+ servesAudioSeparately: (...args) => parts.renditions.servesAudioSeparately(...args),
324
+ disposeSession: (...args) => parts.lifecycle.disposeSession(...args),
325
+ get contentionPenalties() { return parts.contentionPenalties; },
326
+ get encodeCost() { return parts.encodeCost; },
327
+ get encodeOrchestrator() { return parts.encodeOrchestrator; },
328
+ get ffmpegBin() { return parts.ffmpegBin; },
329
+ get outputTimes() { return parts.outputTimes; },
330
+ get outputs() { return parts.outputs; },
331
+ get priority() { return parts.priority; },
332
+ get segmentDurationSec() { return parts.segmentDurationSec; },
333
+ segmentFiles,
334
+ get videoEncoder() { return parts.encoders.current; },
335
+ get encoders() { return parts.encoders; },
336
+ invalidateWaits: (output) => parts.serving.invalidateWaits(output),
337
+ productionFailed: (output) => parts.renditions.noteProductionFailed(output),
338
+ });
339
+ // How much film is ready in front of the viewers, said; and the spare soundtracks fetched once it is full.
340
+ parts.cushion = new CushionReport({
341
+ viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
342
+ linkReportsOn: (output) => linkReportsOn(parts.viewers, output),
343
+ SourceFiles,
344
+ logger,
345
+ producedNumbers: (...args) => parts.serving.producedNumbers(...args),
346
+ get encodeRuns() { return parts.encodeRuns; },
347
+ get fetchWholeFile() { return parts.fetchWholeFile; },
348
+ get getCachedAudioTracks() { return parts.getCachedAudioTracks; },
349
+ get hostLoad() { return parts.hostLoad; },
350
+ get lookaheadSeconds() { return parts.lookaheadSeconds; },
351
+ get outputTimes() { return parts.outputTimes; },
352
+ get outputs() { return parts.outputs; },
353
+ });
354
+ // The steps of a picture and its soundtracks: which output answers each, made when first asked for, and the master playlist listing them.
355
+ // The cushion this file needs, from its segment length and the worst
356
+ // interruption its supply has shown — one statement read by every operation
357
+ // that asks it.
358
+ const minimumBufferSecondsOf = (output) => minimumBufferFrom({
359
+ segmentSeconds: parts.segmentDurationSec,
360
+ worstSupplyWaitSec: parts.hostLoad.supplyFor(output.file)?.worstWaitSec
361
+ })?.seconds ?? null;
362
+ parts.renditions = new Renditions({
363
+ // Whether this proxy re-encodes at all: a soundtrack with no stated rate is
364
+ // re-encoded where it may be, and copied where it may not.
365
+ get transcodeEnabled() { return parts.enabled; },
366
+ observedPeakMbps: (spec) => parts.localObservations?.peakMbps(spec) ?? null,
367
+ // A viewer has been moved onto an output prepared for them: how long it
368
+ // took, and what they held.
369
+ notePreparation: (output, seconds, bufferedSec) =>
370
+ parts.localObservations?.notePreparation({ spec: output?.spec, seconds, bufferedSec }),
371
+ viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
372
+ audioStartSecondsFor: (args) => audioStartSecondsFor({ ...args, viewers: parts.viewers }),
373
+ activeOutputFor: (args) => activeOutputFor({ ...args, viewers: parts.viewers }),
374
+ // WHAT ENCODING IS TOLD ABOUT A PERSON, and what it may tell the viewer
375
+ // layer about them: values and names, never the viewer itself. Steps and
376
+ // soundtracks are chosen, warmed and left per person, so this is the widest
377
+ // of the three sets — and every one of them is a call on the owner, which
378
+ // is what keeps one writer per fact.
379
+ viewerCountOn: (output) => parts.viewers.forOutput(output).size,
380
+ // Whether anything still stands on it — somebody watching, or an
381
+ // assignment not yet released. See `Viewers.stillNeeded`.
382
+ outputStillNeeded: (output) => parts.viewers.stillNeeded(output),
383
+ consumersOn: (output) => consumersOn(parts.viewers, output),
384
+ presentOn: (output) => presentOn(parts.viewers, output),
385
+ qualityModeOf: (output, consumerId) => qualityModeOf(parts.viewers, output, consumerId),
386
+ linkMbpsOf: (output, consumerId) => linkMbpsOf(parts.viewers, output, consumerId),
387
+ audioChoiceOf: (output, consumerId) => audioChoiceOf(parts.viewers, output, consumerId),
388
+ chooseAudioTrack: (output, consumerId, choice) =>
389
+ chooseAudioTrack(parts.viewers, output, consumerId, choice),
390
+ stepOnScreenOf: (output, consumerId) => stepOnScreenOf(parts.viewers, output, consumerId),
391
+ noteStepOnScreen: (output, consumerId, stepId) =>
392
+ noteStepOnScreen(parts.viewers, output, consumerId, stepId),
393
+ stepBeingWarmedOf: (output, consumerId) => stepBeingWarmedOf(parts.viewers, output, consumerId),
394
+ stepBeingWarmedSinceOf: (output, consumerId) => stepBeingWarmedSinceOf(parts.viewers, output, consumerId),
395
+ noteStepBeingWarmed: (output, consumerId, stepId) =>
396
+ noteStepBeingWarmed(parts.viewers, output, consumerId, stepId),
397
+ audioBeingWarmedOf: (output, consumerId) => audioBeingWarmedOf(parts.viewers, output, consumerId),
398
+ noteAudioBeingWarmed: (output, consumerId, renditionId) =>
399
+ noteAudioBeingWarmed(parts.viewers, output, consumerId, renditionId),
400
+ watches: (output, consumerId) => watches(parts.viewers, output, consumerId),
401
+ placeOn: (output, consumerId, seconds) => placeOn(parts.viewers, output, consumerId, seconds),
402
+ // A viewer's own assignments: which output answered a height and segment in
403
+ // one generation of their viewing, so a repeat is answered the same way.
404
+ // Values in and out, like everything above.
405
+ generationOfRequest: (consumerId, stated) => generationOfRequest(parts.viewers, consumerId, stated),
406
+ givenOutputOf: (consumerId, generation, askedHeight, segmentIndex) =>
407
+ givenOutputOf(parts.viewers, consumerId, generation, askedHeight, segmentIndex),
408
+ noteGivenOutput: (consumerId, generation, askedHeight, segmentIndex, outputKey) =>
409
+ noteGivenOutput(parts.viewers, consumerId, generation, askedHeight, segmentIndex, outputKey),
410
+ // The output this viewer was chosen at a height, and the rule's choice of
411
+ // it — the viewer's own record, never one per file.
412
+ chosenOutputOf: (consumerId, askedHeight) => chosenOutputOf(parts.viewers, consumerId, askedHeight),
413
+ chooseOutput: (consumerId, askedHeight, outputKey) => chooseOutput(parts.viewers, consumerId, askedHeight, outputKey),
414
+ noteServingVerdict: (consumerId, verdict) => noteServingVerdict(parts.viewers, consumerId, verdict),
415
+ // A move between two limits of the height on a viewer's screen (roadmap
416
+ // item 97, step 12): which heights their choice is the output on screen
417
+ // under, the segment they were last given, the move being prepared, and
418
+ // whether a segment is closed on the output being prepared.
419
+ limitsFor: (frame) => parts.limitsFor(frame),
420
+ heightsChosenAs: (consumerId, outputKey) => heightsChosenAs(parts.viewers, consumerId, outputKey),
421
+ highestGivenSegmentOf: (consumerId, askedHeight) => highestGivenSegmentOf(parts.viewers, consumerId, askedHeight),
422
+ sameHeightSwitchOf: (consumerId) => sameHeightSwitchOf(parts.viewers, consumerId),
423
+ noteSameHeightSwitch: (consumerId, value) => noteSameHeightSwitch(parts.viewers, consumerId, value),
424
+ switchingOnto: (output) => switchingOnto(parts.viewers, output),
425
+ // What a viewer holds, and the cushion this file needs before a move that
426
+ // is not urgent is made (roadmap item 98).
427
+ bufferedSecondsOf: (consumerId) => bufferedSecondsOf(parts.viewers, consumerId),
428
+ minimumBufferSecondsFor: (output) => minimumBufferSecondsOf(output),
429
+ // Whether the whole machine has a place for one more encoder on this output,
430
+ // asked before a preparation is recorded.
431
+ admitsPreparation: (output) => parts.admission.admitsPreparation(output?.outputKey ?? ""),
432
+ segmentClosed: (key, index) => parts.segmentStore.isClosed(key, index),
433
+ // What a gone output left in the store, and whether another output's header
434
+ // may stand in for its own.
435
+ storedPieceReady: (key, fileName) => parts.serving.hasStoredPiece(key, fileName),
436
+ headersCompatible: (goneKey, liveKey) => parts.serving.headersCompatible(goneKey, liveKey),
437
+ placeViewerOn: (...args) => parts.viewerRequests.placeViewerOn(...args),
438
+ audioRenditionName,
439
+ logger,
440
+ viewerLeaves: (...args) => parts.lifecycle.viewerLeaves(...args),
441
+ createOrGetSession: (...args) => parts.viewerRequests.createOrGetSession(...args),
442
+ planEncodersSoon: (...args) => parts.encodeRuns.planEncodersSoon(...args),
443
+ disposeSession: (...args) => parts.lifecycle.disposeSession(...args),
444
+ viewerPositionOf: (...args) => parts.viewerRequests.viewerPositionOf(...args),
445
+ get encodeRuns() { return parts.encodeRuns; },
446
+ get fileStartTimeReads() { return parts.fileStartTimeReads; },
447
+ set fileStartTimeReads(value) { parts.fileStartTimeReads = value; },
448
+ get getCachedAudioTracks() { return parts.getCachedAudioTracks; },
449
+ get getCachedMediaInfo() { return parts.getCachedMediaInfo; },
450
+ get getContainerMediaInfo() { return parts.getContainerMediaInfo; },
451
+ get localBaseUrl() { return parts.localBaseUrl; },
452
+ get outputTimes() { return parts.outputTimes; },
453
+ get outputs() { return parts.outputs; },
454
+ get quality() { return parts.quality; },
455
+ get qualityOffer() { return parts.qualityOffer; },
456
+ get segmentDurationSec() { return parts.segmentDurationSec; },
457
+ get sourceFiles() { return parts.sourceFiles; },
458
+ invalidateWaits: (output) => parts.serving.invalidateWaits(output),
459
+ });
460
+ // Answering a request for a playlist, an init segment or a segment: from the store when the piece is made and whole, otherwise held until it is.
461
+ parts.serving = new SegmentServing({
462
+ // What a key names, read back from the key: how to find a gone output's
463
+ // stored pieces and whether they are cut at given times.
464
+ segmentFormatOfKey,
465
+ specOfKey: (key) => OutputSpec.fromKey(key),
466
+ compareInits,
467
+ activeOutputFor: (args) => activeOutputFor({ ...args, viewers: parts.viewers }),
468
+ viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
469
+ buildMasterPlaylist: (...args) => parts.renditions.buildMasterPlaylist(...args),
470
+ declaredTracks: (...args) => parts.renditions.declaredTracks(...args),
471
+ publishedGridFor: (...args) => parts.outputTimes.publishedGridFor(...args),
472
+ runStartTimeFor: (...args) => parts.outputTimes.runStartTimeFor(...args),
473
+ get cushion() { return parts.cushion; },
474
+ get encodeOrchestrator() { return parts.encodeOrchestrator; },
475
+ get encodeRuns() { return parts.encodeRuns; },
476
+ get hostTimings() { return parts.hostTimings; },
477
+ get lookaheadSeconds() { return parts.lookaheadSeconds; },
478
+ get outputTimes() { return parts.outputTimes; },
479
+ get outputs() { return parts.outputs; },
480
+ get segmentStore() { return parts.segmentStore; },
481
+ get startupWaitMs() { return parts.startupWaitMs; },
482
+ get viewers() { return parts.viewers; },
483
+ });
484
+ // How an output ends: disposed when nobody is left on it and it has been idle, or all at once on shutdown; and the segments an earlier process left behind adopted at startup.
485
+ parts.lifecycle = new OutputLifecycle({
486
+ invalidateWaits: (output) => parts.serving.invalidateWaits(output),
487
+ segmentFormatOfKey,
488
+ // Where the viewers of one output stand, in its segments: what the store
489
+ // reads to give disk back in the right order.
490
+ viewerSegmentsOn: (key) => viewerSegmentsOn({
491
+ outputs: parts.outputs.values(),
492
+ outputKey: key,
493
+ segmentAt: (output, seconds) => parts.outputTimes.segmentIndexForTime(output, seconds),
494
+ now: Date.now(),
495
+ viewers: parts.viewers
496
+ }),
497
+ get budgetTimer() { return parts.budgetTimer; },
498
+ get cleanupTimer() { return parts.cleanupTimer; },
499
+ get encodeRuns() { return parts.encodeRuns; },
500
+ get keyframeTables() { return parts.keyframeTables; },
501
+ get machineBudget() { return parts.machineBudget; },
502
+ get outputTimes() { return parts.outputTimes; },
503
+ get outputs() { return parts.outputs; },
504
+ get returns() { return parts.returns; },
505
+ get segmentStore() { return parts.segmentStore; },
506
+ get sessionTtlMs() { return parts.sessionTtlMs; },
507
+ get sourceFiles() { return parts.sourceFiles; },
508
+ get timelines() { return parts.timelines; },
509
+ get viewers() { return parts.viewers; },
510
+ });
511
+ // Which output answers a request for a file, made if it is not here yet: the encoding component's decision, made without knowing who asked.
512
+ parts.opening = new OutputOpening({
513
+ logger,
514
+ // The largest peak an output of this mode and rate control has been seen
515
+ // carrying here: the only figure an encoder with no bound of its own has.
516
+ observedPeakMbps: (spec) => parts.localObservations?.peakMbps(spec) ?? null,
517
+ limitsFor: (frame) => parts.limitsFor(frame),
518
+ probeMediaInfo: (url) => probeInputMediaInfo(parts.ffmpegBin, url),
519
+ probeKeyframeTimes: (url, budgetMs) => probeVideoKeyframeTimes(parts.ffmpegBin, url, budgetMs),
520
+ get decodeCostModel() { return parts.decodeCostModel; },
521
+ get enabled() { return parts.enabled; },
522
+ get encodeCost() { return parts.encodeCost; },
523
+ get encodeRuns() { return parts.encodeRuns; },
524
+ get admission() { return parts.admission; },
525
+ get getCachedMediaInfo() { return parts.getCachedMediaInfo; },
526
+ get hostLoad() { return parts.hostLoad; },
527
+ get hostTimings() { return parts.hostTimings; },
528
+ get keyframeTables() { return parts.keyframeTables; },
529
+ get localBaseUrl() { return parts.localBaseUrl; },
530
+ get outputs() { return parts.outputs; },
531
+ get renditions() { return parts.renditions; },
532
+ get returns() { return parts.returns; },
533
+ get segmentDurationSec() { return parts.segmentDurationSec; },
534
+ get segmentFormat() { return parts.segmentFormat; },
535
+ segmentOutputFiles,
536
+ get softwarePresetBenchmark() { return parts.softwarePresetBenchmark; },
537
+ get sourceFiles() { return parts.sourceFiles; },
538
+ get timelines() { return parts.timelines; },
539
+ get tonemapSupported() { return parts.tonemapSupported; },
540
+ get videoEncoder() { return parts.encoders.current; },
541
+ });
542
+ // What a viewer asks for: an output of a file, a position, a report of progress.
543
+ parts.viewerRequests = new ViewerRequests({
544
+ // Whether this machine has a place for one more viewer on this output —
545
+ // asked in the same synchronous stretch as they are put on it.
546
+ admitsWatching: (output) => parts.admission.admitsWatching(output?.outputKey ?? ""),
547
+ activeOutputFor: (args) => activeOutputFor({ ...args, viewers: parts.viewers }),
548
+ viewerSecondsOn: (output, consumerId, now) => viewerSecondsOn(parts.viewers, output, consumerId, now),
549
+ minimumBufferSecondsFor: (output) => minimumBufferSecondsOf(output),
550
+ getSourceStats: (...args) => parts.getSourceStats?.(...args) ?? null,
551
+ disposeSession: (...args) => parts.lifecycle.disposeSession(...args),
552
+ expectedFirstSegmentMs: (...args) => parts.hostTimings.expectedFirstSegmentMs(...args),
553
+ expectedSessionCreateMs: (...args) => parts.hostTimings.expectedSessionCreateMs(...args),
554
+ planEncodersSoon: (...args) => parts.encodeRuns.planEncodersSoon(...args),
555
+ waitUntilReady: (...args) => parts.serving.waitUntilReady(...args),
556
+ get encodeRuns() { return parts.encodeRuns; },
557
+ // When this viewer's playback can start and run to the end: the viewer
558
+ // component's forecast, which models the viewer's browser.
559
+ playbackReadiness: { predict: predictPlaybackReadiness, forecastRate, RateTrend },
560
+ encodeSpeedReadingOf: (output) => parts.encodeCost.latestSpeedReadingOf(output),
561
+ projectedEncodeSpeedOf: (output) => parts.encodeCost.projectedSpeedOf(output),
562
+ audioDescriptionOf,
563
+ processingStartupSecondsOf: (output) => output.spec.carries === "audio-only" ?
564
+ audioReadingFor(audioCalibration, { ...audioDescriptionOf(output),
565
+ codec: audioDescriptionOf(output)?.codec ?? output.file.media?.audioCodec,
566
+ transcode: output.spec.audio?.transcode })?.startupSeconds ?? 0 :
567
+ Math.max(0, (startStopCost?.firstByteWaitSec ?? 0) - parts.segmentDurationSec /
568
+ parts.encodeCost.projectedSpeedOf(output)),
569
+ initialOutputBitsPerMediaSecondOf: (output) => {
570
+ const description = audioDescriptionOf(output);
571
+ const reference = audioReadingFor(audioCalibration, { codec: description?.codec, transcode: false });
572
+ const audio = output.spec.audio ? output.spec.audio.transcode ? AUDIO_TRANSCODE_KBPS * 1000 :
573
+ description?.bitrateKbps > 0 ? description.bitrateKbps * 1000 :
574
+ reference ? reference.bytes * 8 / reference.durationSeconds : 0 : 0;
575
+ const video = output.spec.video ? (output.spec.video.encode?.rateControl?.maxrateKbps ??
576
+ output.file.media?.bitrateKbps ?? 0) * 1000 : 0;
577
+ return video + audio;
578
+ },
579
+ get opening() { return parts.opening; },
580
+ get outputTimes() { return parts.outputTimes; },
581
+ get outputs() { return parts.outputs; },
582
+ get lookaheadSeconds() { return parts.lookaheadSeconds; },
583
+ get quality() { return parts.quality; },
584
+ get qualityOffer() { return parts.qualityOffer; },
585
+ get renditions() { return parts.renditions; },
586
+ get segmentDurationSec() { return parts.segmentDurationSec; },
587
+ get segmentStore() { return parts.segmentStore; },
588
+ get sourceFiles() { return parts.sourceFiles; },
589
+ get startupWaitMs() { return parts.startupWaitMs; },
590
+ get viewers() { return parts.viewers; },
591
+ invalidateWaits: (output) => parts.serving.invalidateWaits(output),
592
+ // Whether a request of this generation is still taken, what it belongs to,
593
+ // what answered it, and the hold a response keeps while it is being sent.
594
+ acceptsGeneration: (consumerId, stated) => acceptsGeneration(parts.viewers, consumerId, stated),
595
+ generationOfRequest: (consumerId, stated) => generationOfRequest(parts.viewers, consumerId, stated),
596
+ noteGivenOutput: (consumerId, generation, askedHeight, segmentIndex, outputKey) =>
597
+ noteGivenOutput(parts.viewers, consumerId, generation, askedHeight, segmentIndex, outputKey),
598
+ holdForResponse: (consumerId, outputKey) => holdForResponse(parts.viewers, consumerId, outputKey),
599
+ noteServingVerdict: (consumerId, verdict) => noteServingVerdict(parts.viewers, consumerId, verdict),
600
+ servingVerdictOf: (consumerId) => servingVerdictOf(parts.viewers, consumerId),
601
+ });
602
+ // What this host takes to create an output and to produce its first segment, kept across restarts until the synthetic figure can replace it (CLAUDE.md, host timings).
603
+ parts.hostTimings = new HostTimings({
604
+ get segmentDurationSec() { return parts.segmentDurationSec; },
605
+ get softwarePresetBenchmark() { return parts.softwarePresetBenchmark; },
606
+ get stateDir() { return parts.stateDir; },
607
+ });
608
+ parts.enabled = Boolean(enabled);
609
+ parts.ffmpegBin = ffmpegBin;
610
+ parts.keyframeTables = keyframeTables;
611
+ // Where measurements about this host are kept between runs. Empty means
612
+ // beside the installed proxy; a deployment with somewhere persistent to
613
+ // write names it (--state-dir).
614
+ parts.stateDir = typeof stateDir === "string" ? stateDir : "";
615
+ // Output container (fMP4/CMAF or MPEG-TS). Everything container-specific —
616
+ // muxer args, file naming, playlist header, per-segment correction — lives
617
+ // in this module; nothing here branches on the format.
618
+ parts.segmentFormat = resolveSegmentFormat(segmentFormatId);
619
+ // Optional accessor for media info the playback planner already probed for
620
+ // (sourceKey, fileIndex), so session create can skip its own ffmpeg scan.
621
+ parts.getCachedMediaInfo = typeof getCachedMediaInfo === "function" ? getCachedMediaInfo : null;
622
+ // The file's audio tracks, for the master playlist's rendition group. Same
623
+ // inventory the browser's audio menu is built from.
624
+ parts.getCachedAudioTracks = typeof getCachedAudioTracks === "function" ? getCachedAudioTracks : null;
625
+ // What a file declares about itself, read by the container layer from the
626
+ // same header its track table comes from: format, duration, and where its
627
+ // own timeline begins. The last of those is why this exists — a soundtrack
628
+ // shipped as its own file has a timeline of its own, and asking ffmpeg for
629
+ // it meant reading a header this proxy had already read.
630
+ parts.getContainerMediaInfo =
631
+ typeof getContainerMediaInfo === "function" ? getContainerMediaInfo : null;
632
+ // Fetch one whole file of a source, as a bounded read rather than a
633
+ // selection. Used to pull a soundtrack that ships beside the picture onto
634
+ // the disk while the swarm has capacity to spare — see
635
+ // `#fetchSpareSoundtracks`. Optional: a proxy wired without it simply reads
636
+ // such a soundtrack when it is played.
637
+ parts.fetchWholeFile = typeof fetchWholeFile === "function" ? fetchWholeFile : null;
638
+ // Optional async accessor for a source's live download stats, used by the
639
+ // realtime budget to tell a CPU limit from a download-starved input:
640
+ // (sourceKey, fileIndex) => Promise<{ downloadSpeed, fileLength, fileProgress } | null>.
641
+ parts.getSourceStats = typeof getSourceStats === "function" ? getSourceStats : null;
642
+ parts.setPriorityMap = typeof setPriorityMap === "function" ? setPriorityMap : null;
643
+ parts.contentionPenalties = contentionPenalties instanceof Map ? contentionPenalties : null;
644
+ // Seconds of film per second when the picture is COPIED, measured at
645
+ // startup. Nothing else prices that branch: the other benchmarks measure
646
+ // encoding and decoding, and a copy does neither.
647
+ parts.copySpeedX = Number.isFinite(copySpeedX) && copySpeedX > 0 ? copySpeedX : null;
648
+ // Totals across every torrent this proxy holds, used to price what the
649
+ // torrent itself costs the machine (item 7). Optional: a proxy wired
650
+ // without it simply never learns that figure.
651
+ parts.getTorrentTotals = typeof getTorrentTotals === "function" ? getTorrentTotals : null;
652
+ // The spilled pieces, as a pair of closures over the torrent thread: what
653
+ // they weigh and how to tell them their share. Not the pool — the pool is
654
+ // on the other side of the thread boundary and this side holds none of it.
655
+ parts.spillDisk = spillDisk;
656
+ parts.wholeFiles = wholeFiles;
657
+ parts.diagnostics = diagnostics;
658
+ parts.diagnosticsRoot = diagnosticsRoot;
659
+ parts.memoryClaimant = memoryClaimant;
660
+ parts.budgetPolicy = budgetPolicy;
661
+ // Detected H.264 encoder descriptor (hardware or software). Defaults to
662
+ // software libx264 when no detection result is supplied. May be downgraded
663
+ // to software at runtime if a hardware encode fails.
664
+ parts.encoders = new EncoderSelection({ detected: videoEncoder, softwareDescriptor });
665
+ // WHAT THIS HOST HAS SEEN ITS OWN ADMITTED ENCODES DO, kept between runs and
666
+ // used only on the configuration it was seen on (roadmap item 97, step 14).
667
+ // A refinement of figures that exist without it: nothing is selected,
668
+ // admitted or served differently when it is empty. Absent where the startup
669
+ // calibration was not run, which is only a wiring made without it.
670
+ parts.localObservations = null;
671
+ if (calibration?.fingerprint) {
672
+ parts.localObservations = new LocalObservations({
673
+ fingerprint: calibration.fingerprint,
674
+ filePath: path.join(typeof stateDir === "string" && stateDir.length > 0 ? stateDir : PROXY_ROOT, "local-observations.json"),
675
+ logger
676
+ });
677
+ parts.localObservations.load(calibration.kinds().map((kind) => configurationKeyOf(calibration.fingerprint, {
678
+ kind,
679
+ name: kind === "software" ? softwareDescriptor().name : (videoEncoder?.kind === kind ? videoEncoder.name : kind)
680
+ })));
681
+ }
682
+ /**
683
+ * What the segments an output has closed carried, from their sizes and the
684
+ * spans its timeline gives them: the average over all of them and the
685
+ * largest one.
686
+ *
687
+ * @param {object} session
688
+ * @returns {{ averageKbps: number, peakKbps: number } | null}
689
+ */
690
+ const segmentRatesOf = (session) => {
691
+ const sizes = parts.segmentStore.sizesOf(session.outputKey);
692
+ const timeline = session.timeline;
693
+ if (!timeline || sizes.size === 0) {
694
+ return null;
695
+ }
696
+ let bits = 0;
697
+ let seconds = 0;
698
+ let peakKbps = 0;
699
+ for (const [index, size] of sizes) {
700
+ const start = timeline.publishedStartOf(index);
701
+ const end = index < timeline.segmentCount ? timeline.publishedStartOf(index + 1) : null;
702
+ if (!(Number.isFinite(start) && Number.isFinite(end) && end > start) || !(size > 0)) {
703
+ continue;
704
+ }
705
+ bits += size * 8;
706
+ seconds += end - start;
707
+ peakKbps = Math.max(peakKbps, (size * 8) / (end - start) / 1000);
708
+ }
709
+ return seconds > 0 ? { averageKbps: bits / seconds / 1000, peakKbps } : null;
710
+ };
711
+ // The modes of the encoder IN USE NOW, as measured at startup: slowest
712
+ // first, each with its throughput by size. Read through the selection rather
713
+ // than copied, because the encoder in use can change — a failing device
714
+ // moves this host to software for the rest of the process, and from then on
715
+ // it is software's own modes that price and choose, not the device's.
716
+ // Null where nothing was measured, and then nothing is re-encoded here.
717
+ parts.calibration = calibration ?? null;
718
+ const givenModes = Array.isArray(softwarePresetBenchmark) ? softwarePresetBenchmark : null;
719
+ Object.defineProperty(parts, "softwarePresetBenchmark", {
720
+ get: () => (parts.calibration
721
+ ? parts.calibration.modesFor(parts.encoders.current?.kind)
722
+ : givenModes),
723
+ enumerable: true
724
+ });
725
+ // Host decode cost solved at startup from the calibration clips:
726
+ // `a × Mpixel/s + b × Mbit/s + c` seconds of decoding per second of video.
727
+ // A re-encode pays for this as well as for the encoder, and leaving it out
728
+ // is what made the budget offer rungs this host ran at a third of realtime.
729
+ // Null when the clips are missing or the fit was rejected — the budget then
730
+ // prices the encoder alone, as it did before.
731
+ parts.decodeCostModel = decodeCostModel ?? null;
732
+ // Whether this ffmpeg build can tone-map HDR→SDR (zscale + tonemap filters).
733
+ // Gates the tonemap chain for HDR sources on the software path.
734
+ parts.tonemapSupported = Boolean(tonemapSupported);
735
+ parts.segmentDurationSec = segmentDurationSec;
736
+ // How far ahead of the viewer this proxy lets an encoder run, in seconds of
737
+ // playback. Stated rather than kept private, because the browser's forward
738
+ // buffer is bounded by the same quantity and used to carry a copy of its
739
+ // own: a hand-written 30 s, justified in a comment by a DIFFERENT constant
740
+ // (the eight segments that bound a request ahead of the ENCODE HEAD), so
741
+ // three quarters of what the encoder had already produced was thrown away.
742
+ // One figure, said by the side that owns it (roadmap item 4).
743
+ parts.lookaheadSeconds = LOOKAHEAD_PAUSE_SECONDS;
744
+ parts.sessionTtlMs = sessionTtlMs;
745
+ parts.startupWaitMs = startupWaitMs;
746
+ parts.localBaseUrl = buildHttpBaseUrl(localBindHost, localPort);
747
+ // Everyone watching anything, one object per person rather than one per
748
+ // person per output. What a viewer chose, where they are and which outputs
749
+ // they are watching are facts about the person; kept per output they were
750
+ // three copies of which two were always stale.
751
+ // Every change to who is watching what re-decides which encoders should
752
+ // exist, because that decision reads nothing else about viewers. It used to
753
+ // be re-taken on a five-second timer instead, which made a just-created
754
+ // output wait up to five seconds before anything noticed it had a viewer.
755
+ parts.viewers = new Viewers({ onChange: () => parts.encodeRuns.planEncodersSoon() });
756
+ // The outputs that exist, and every question about them: the picture a step
757
+ // belongs to, the steps, the soundtracks, the height a step is named by.
758
+ parts.outputs = new OutputCatalog({
759
+ fileLengthOf: (session) => parts.hostLoad.fileLengthByKey.get(session.file.key) ?? 0,
760
+ largestPieceOf: (address) => parts.segmentStore.largestPiece(address)
761
+ });
762
+ // What this host learned last time it ran. Without it every restart shows
763
+ // the first viewer a figure with no measurement behind it.
764
+ parts.hostTimings.loadHostTimings();
765
+ // Where produced segments live, addressed by WHAT they are rather than by
766
+ // which session's encoder wrote them. Two sessions of one output — two
767
+ // viewers who opened the same film at different places — write into one
768
+ // directory and each serves what the other has already made. Injectable so
769
+ // a test can give it a root of its own.
770
+ parts.segmentStore = segmentStore instanceof SegmentStore
771
+ ? segmentStore
772
+ : new SegmentStore({ logger });
773
+ // The store has grown: whether it still fits its share is asked now rather
774
+ // than on the next cleanup pass.
775
+ parts.segmentStore.onPublished(() => parts.lifecycle.keepWithinRoomSoon());
776
+ // A segment closed may be the one a viewer's move between two limits of a
777
+ // height is waiting for.
778
+ parts.segmentStore.onPublished((key, index) => parts.renditions.noteSegmentPublished(key, index));
779
+ // What encoders there should be on each output, and where. Given the two
780
+ // things only this class can answer: how many this machine can afford, and
781
+ // how to make one.
782
+ // What encoding costs this machine, and which heights follow from it. Given
783
+ // what it cannot work out for itself: which sessions belong to one file, the
784
+ // host's own readings AT THE MOMENT OF THE QUESTION rather than copied, how
785
+ // a soundtrack is keyed, how many encoders are running, and what a file
786
+ // costs merely by being fetched.
787
+ parts.encodeCost = new EncodeCost({
788
+ outputs: parts.outputs,
789
+ host: () => ({
790
+ benchmark: parts.softwarePresetBenchmark,
791
+ decodeModel: parts.decodeCostModel,
792
+ contentionPenalties: parts.contentionPenalties,
793
+ copySpeedX: parts.copySpeedX,
794
+ audioCalibration,
795
+ audioDescriptionOf,
796
+ availability: parts.hostLoad.hostAvailability,
797
+ // WHICH encoder this host settled on. Only the software ladder is
798
+ // benchmarked, so a reading taken off a hardware encoder cannot be
799
+ // split into its decode and encode halves and is not filed as one.
800
+ encoderKind: parts.encoders.current?.kind ?? null,
801
+ // The slowest this output's mode has been seen running alone on
802
+ // comparable material on this configuration, or null.
803
+ observedAloneSpeed: (session) => parts.localObservations?.slowestAloneSpeed(session.spec, contentOf(session.file)) ?? null
804
+ }),
805
+ // HOW MANY ENCODER PROCESSES ARE RUNNING, asked of the one thing that
806
+ // makes and unmakes them. This used to be two counts of one fact, taken
807
+ // by walking the session registry — one per session, one per run — and
808
+ // they agreed only while a session held at most one run.
809
+ runningEncoders: () => parts.encodeOrchestrator.runningCount(),
810
+ encodersRunningNow: () => parts.encodeOrchestrator.runningCount(),
811
+ torrentCostSecFor: (session) => parts.hostLoad.torrentCostSecFor(session),
812
+ boundBy: (session) => parts.quality.classifyTranscodeBound(session),
813
+ runsFor: (session) => parts.encodeRuns.runsOf(session),
814
+ stateFor: (session) => parts.encodeRuns.runStateOf(session),
815
+ // The run's OWN progress: a speed is a pair of readings of one process.
816
+ progressFor: (_session, run) => run?.progress?.snapshot?.() ?? null
817
+ });
818
+ // WHICH HEIGHTS ARE ON THE MENU, which is the arithmetic above plus three
819
+ // things that are nothing to do with it: whose answer it is, what may never
820
+ // be withdrawn, and when the answer may be reused.
821
+ parts.qualityOffer = new QualityOffer({
822
+ encodeCost: parts.encodeCost,
823
+ occupiedCostSec: (fileKey) => parts.admission?.occupiedCost({ exceptFileKey: fileKey }).costSec ?? 0,
824
+ occupancyKnownFor: (fileKey) => (parts.admission?.occupiedCost({ exceptFileKey: fileKey }).unpriced ?? 0) === 0,
825
+ outputs: parts.outputs,
826
+ stateFor: (session) => parts.encodeRuns.runStateOf(session),
827
+ // WHICH HEIGHTS A LIVE VIEWER HAS ON SCREEN, as numbers. Who is watching
828
+ // what is the viewer layer's, and which session is which height is the
829
+ // film's shape; neither travels — what crosses is the list of heights.
830
+ heightsOnScreen: (owner) => parts.quality.heightsOnScreen(owner),
831
+ // WHAT THE SWARM IS DOING WITH THIS FILE. Three readings, taken by
832
+ // whoever reads it and handed over as three numbers: the speed this
833
+ // file's own interruptions demand, the megabytes a second a viewer draws
834
+ // through it, and what a megabyte costs this process.
835
+ supplyFor: (file) => ({
836
+ requiredSpeed: parts.hostLoad.requiredSpeedFor(file.sourceKey, file.fileIndex),
837
+ megabytesPerSecond: parts.hostLoad.torrentMegabytesPerSecond(
838
+ file.sourceKey,
839
+ file.fileIndex,
840
+ file.lengthBytes ?? parts.hostLoad.fileLengthByKey.get(file.key) ?? null,
841
+ file.durationSeconds
842
+ ),
843
+ costPerMegabyte: parts.hostLoad.observedTorrentCostPerMegabyte
844
+ })
845
+ });
846
+ // The priority map, built from where the viewers are and handed to both
847
+ // sides that act on it. The downloading lives in another thread, so its
848
+ // copy travels over the worker channel.
849
+ parts.priority = new PriorityOrchestrator({
850
+ publish: ({ sourceKey, fileIndex, durationSeconds, zones }) => {
851
+ void Promise.resolve(
852
+ parts.setPriorityMap?.({ sourceKey, fileIndex, durationSeconds, zones })
853
+ ).catch(() => {});
854
+ },
855
+ get viewers() { return parts.viewers; },
856
+ // WHERE THE TWO FACTS MEET, and this is the only place that holds both.
857
+ // Which step is on somebody's screen belongs to the person; which output
858
+ // a step supersedes belongs to the film's shape. Neither layer is handed
859
+ // the other — one gets a plain id, the other is read for one field.
860
+ watchedBy: (session, viewer) => !parts.outputs.supersededBy(session, viewer.activeVariantId ?? null),
861
+ allowanceFor: (session) => minimumBufferFrom({
862
+ segmentSeconds: parts.segmentDurationSec,
863
+ worstSupplyWaitSec: parts.hostLoad.supplyFor(session.file)?.worstWaitSec
864
+ })?.seconds ?? parts.segmentDurationSec
865
+ });
866
+ // WHETHER THE WHOLE MACHINE HAS A PLACE for one more encoder, across outputs.
867
+ // Given what it cannot hold itself: what runs (the orchestrator's), what is
868
+ // being prepared (the viewers' records), what an output costs (the encoding
869
+ // cost) and how much of the machine nobody has priced (the host's reading).
870
+ parts.admission = new EncodeAdmission({
871
+ liveRunsByAddress: () => parts.encodeOrchestrator.liveRunsByAddress(),
872
+ preparedAddresses: () => {
873
+ const addresses = new Set();
874
+ for (const id of outputsBeingPrepared(parts.viewers)) {
875
+ const key = parts.outputs.get(id)?.outputKey;
876
+ if (key) {
877
+ addresses.add(key);
878
+ }
879
+ }
880
+ return addresses;
881
+ },
882
+ // Every output a present viewer RECEIVES — by the same rule the per-output
883
+ // priority map is built by — that still has something left to make. The
884
+ // place an output is opened on is held from the moment its viewer is put
885
+ // on it, not from when the plan gets round to its first encoder.
886
+ watchedAddresses: () => {
887
+ const addresses = new Set();
888
+ for (const session of parts.outputs.values()) {
889
+ const address = session.outputKey;
890
+ if (!address || addresses.has(address) || finishedOutput(session)) {
891
+ continue;
892
+ }
893
+ for (const viewer of parts.viewers.forOutput(session).values()) {
894
+ if (viewer.isPresent() && !parts.outputs.supersededBy(session, viewer.activeVariantId ?? null)) {
895
+ addresses.add(address);
896
+ break;
897
+ }
898
+ }
899
+ }
900
+ return addresses;
901
+ },
902
+ finished: (address) => parts.outputs.outputsOn(address).some((session) => finishedOutput(session)),
903
+ loadOf: (address) => parts.encodeCost.loadOfOutput(address),
904
+ loadForCandidate: (spec, file) => parts.encodeCost.loadForCandidate(spec, file),
905
+ availability: () => parts.hostLoad.hostAvailability ?? null
906
+ });
907
+ parts.encodeOrchestrator = new EncodeOrchestrator({
908
+ admission: parts.admission,
909
+ // What the viewers actually waited for, by band. The ledger is the
910
+ // priority layer's; the encoding is handed a way to ask it.
911
+ describeWaits: (address) => waits.describe(address),
912
+ maxRunsFor: (address) => parts.encodeRuns.maxRunsForOutput(address),
913
+ makeRun: ({ address, from, to, because }) => parts.encodeRuns.makeRunAt(address, from, to, because),
914
+ segmentSeconds: parts.segmentDurationSec,
915
+ contentionPenalties: parts.contentionPenalties,
916
+ startingSpeedFor: (address) => parts.encodeCost.speedForOutput(address),
917
+ segmentCoverage,
918
+ // HOW IT ASKS TO DECIDE AGAIN. A plan that refuses to place anything
919
+ // because an output's input is away needs something to bring it back:
920
+ // nothing about the state changes while the data is missing, so no event
921
+ // arrives on its own.
922
+ planSoon: () => parts.encodeRuns.planEncodersSoon(),
923
+ logger
924
+ });
925
+ // What a start and a stop were measured to cost here, before any viewer
926
+ // existed. Without it both read zero at a cold open, and zero is not
927
+ // "unmeasured" — it is "free", which is what moved an encoder between two
928
+ // adjacent numbers every half second in the field.
929
+ parts.encodeOrchestrator.noteStartupCosts(startStopCost);
930
+ // Where each file is cut, held once per file and grid rather than once per
931
+ // session. Two sessions of one film MUST agree about this to the
932
+ // millisecond — a segment made by either has to be appendable where the
933
+ // other's would have gone — and until now they agreed by copying, which is
934
+ // a thing somebody has to remember to do and which drifted twice in the
935
+ // field. They share the table now.
936
+ parts.timelines = new Timelines();
937
+ // The files this proxy is serving, one object per file however many
938
+ // sessions are of it. It holds the file's key — which every cache about a
939
+ // file is keyed by — its name, and the facts a probe returned.
940
+ parts.sourceFiles = new SourceFiles();
941
+ parts.cleanupTimer = setInterval(() => {
942
+ void parts.lifecycle.cleanupExpired();
943
+ }, CLEANUP_INTERVAL_MS);
944
+ parts.cleanupTimer.unref();
945
+ // How long after material stops being read somebody asks for it again — the
946
+ // one term of the keeping period that is guessed rather than measured, and
947
+ // the only place it can be measured from.
948
+ parts.returns = new Returns();
949
+ // One owner of the disk, and the list of what takes it lives with the owner.
950
+ parts.machineBudget = wireMachineBudget({
951
+ segmentStore: parts.segmentStore,
952
+ spill: parts.spillDisk,
953
+ wholeFiles: parts.wholeFiles,
954
+ diagnostics: parts.diagnostics,
955
+ diagnosticsRoot: parts.diagnosticsRoot,
956
+ memory: parts.memoryClaimant,
957
+ policy: parts.budgetPolicy ?? undefined,
958
+ readFree: freeBytesFor,
959
+ logger
960
+ });
961
+ // Realtime-budget monitor: only meaningful for the software encoder with a
962
+ // benchmark (the only path that can pick/step resolution). Cheap no-op scan
963
+ // otherwise.
964
+ // The quality budget: what each output is asked to step to, and the bitrate ceiling a viewer's link sets. Everything it reads of the rest of the proxy is listed here.
965
+ parts.quality = new QualityController({
966
+ // The longest an output of this mode has been seen taking to be ready for
967
+ // a viewer moving onto it, in milliseconds, or null.
968
+ observedPreparationMs: (output) => {
969
+ const seconds = parts.localObservations?.longestPreparationSec(output?.spec) ?? null;
970
+ return seconds === null ? null : seconds * 1000;
971
+ },
972
+ // WHAT ENCODING IS TOLD ABOUT A PERSON: values, by name. Not the viewer,
973
+ // whose record belongs to the viewer layer and is written only there.
974
+ consumersOn: (output) => consumersOn(parts.viewers, output),
975
+ qualityModeOf: (output, consumerId) => qualityModeOf(parts.viewers, output, consumerId),
976
+ stepOnScreenOf: (output, consumerId) => stepOnScreenOf(parts.viewers, output, consumerId),
977
+ standingAskOf: (output, consumerId) => standingAskOf(parts.viewers, output, consumerId),
978
+ askQualityOf: (output, consumerId, height, reason, now, urgent) =>
979
+ askQualityOf(parts.viewers, output, consumerId, height, reason, now, urgent),
980
+ // What a viewer's page said about their buffer and the picture they see
981
+ // (roadmap item 98), as values.
982
+ bufferOf: (output, consumerId, spanSec) => bufferOf(parts.viewers, output, consumerId, spanSec),
983
+ visiblePictureOf: (output, consumerId) => visiblePictureOf(parts.viewers, output, consumerId),
984
+ // How long this host takes to close a first segment: how soon another
985
+ // output could have the piece a viewer needs.
986
+ expectedFirstSegmentMs: () => parts.hostTimings.expectedFirstSegmentMs(),
987
+ dropAskOf: (output, consumerId) => dropAskOf(parts.viewers, output, consumerId),
988
+ // EACH viewer's own link and who is present: a thin link decides for the
989
+ // person on it and for nobody else (roadmap item 97, step 11), so the
990
+ // budget asks per viewer instead of taking the worst over all of them.
991
+ presentOn: (output) => presentOn(parts.viewers, output),
992
+ linkReportOf: (output, consumerId) => linkReportOf(parts.viewers, output, consumerId),
993
+ // The soundtrack a viewer receives, as part of the load their link carries.
994
+ viewerAudioLoadOf: (base, consumerId) => parts.renditions.viewerAudioLoadOf(base, consumerId),
995
+ // The first lever on a viewer's link: another limit of the height on their
996
+ // screen, prepared and moved to without their player being told.
997
+ prepareSameHeightSwitch: (output, consumerId, direction, reason) =>
998
+ parts.renditions.prepareSameHeightSwitch(output, consumerId, direction, reason),
999
+ sameHeightSwitchPending: (consumerId) => parts.renditions.sameHeightSwitchPending(consumerId),
1000
+ sameHeightSwitchDirection: (consumerId) => parts.renditions.sameHeightSwitchDirection(consumerId),
1001
+ cancelSameHeightSwitch: (consumerId, reason) => parts.renditions.cancelSameHeightSwitch(consumerId, reason),
1002
+ heightReadyFor: (base, consumerId, height) => parts.renditions.heightReadyFor(base, consumerId, height),
1003
+ isLive: (...args) => parts.encodeRuns.isLive(...args),
1004
+ liveConsumers: (...args) => parts.renditions.liveConsumers(...args),
1005
+ liveRunsOf: (...args) => parts.encodeRuns.liveRunsOf(...args),
1006
+ producedNumbers: (...args) => parts.serving.producedNumbers(...args),
1007
+ reportHostLoad: (...args) => parts.hostLoad.reportHostLoad(...args),
1008
+ runStateOf: (...args) => parts.encodeRuns.runStateOf(...args),
1009
+ sampleDownloadRates: (...args) => parts.hostLoad.sampleDownloadRates(...args),
1010
+ get encodeCost() { return parts.encodeCost; },
1011
+ get getSourceStats() { return parts.getSourceStats; },
1012
+ get outputs() { return parts.outputs; },
1013
+ get qualityOffer() { return parts.qualityOffer; },
1014
+ get segmentDurationSec() { return parts.segmentDurationSec; },
1015
+ segmentPaths,
1016
+ get videoEncoder() { return parts.encoders.current; },
1017
+ });
1018
+ parts.budgetTimer = setInterval(() => {
1019
+ parts.cushion.reportCushions();
1020
+ void parts.quality.runQualityBudgetOnce();
1021
+ }, BUDGET_CHECK_INTERVAL_MS);
1022
+ parts.budgetTimer.unref();
1023
+ return parts;
1024
+ }