@torrent-tv/proxy 2.75.0 → 2.76.0

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 (113) hide show
  1. package/CHANGELOG.md +1504 -1461
  2. package/CLAUDE.md +11 -1
  3. package/biome.json +182 -1
  4. package/docs/container-architecture.md +2 -1
  5. package/docs/encode-run-state.md +1 -1
  6. package/knip.json +14 -0
  7. package/package.json +1 -1
  8. package/routes/api/sources/warm/post.js +1 -1
  9. package/routes/api/transcode-sessions/post.js +185 -185
  10. package/routes/api/transcode-sessions/progress/get.js +5 -1
  11. package/routes/transcode/audio-file/get.js +11 -1
  12. package/routes/transcode/audio-warm/get.js +11 -1
  13. package/routes/transcode/session-file/get.js +1 -1
  14. package/routes/transcode/variant-file/get.js +10 -1
  15. package/scripts/render-run-graph.js +2 -2
  16. package/server.js +25 -0
  17. package/services/audio-inventory.js +206 -201
  18. package/services/container/AviContainer.js +1 -1
  19. package/services/container/Container.js +33 -1
  20. package/services/container/MatroskaContainer.js +1 -1
  21. package/services/container/Mp4Container.js +1 -1
  22. package/services/container/SubtitleFileContainer.js +0 -1
  23. package/services/controllers/SubtitleController.js +128 -128
  24. package/services/demand/index.js +7 -10
  25. package/services/download/registry.js +0 -14
  26. package/services/encode/CoverageMap.js +281 -0
  27. package/services/encode/EncodePlan.js +255 -0
  28. package/services/encode/EncodeRun.js +587 -0
  29. package/services/encode/Encoder.js +84 -0
  30. package/services/encode/NvencEncoder.js +45 -0
  31. package/services/encode/QsvEncoder.js +47 -0
  32. package/services/encode/SegmentDemand.js +0 -0
  33. package/services/encode/SegmentStore.js +529 -0
  34. package/services/encode/SoftwareEncoder.js +111 -0
  35. package/services/encode/V4l2m2mEncoder.js +53 -0
  36. package/services/encode/VaapiEncoder.js +53 -0
  37. package/services/encode/args.js +200 -0
  38. package/services/{encode-exit.js → encode/encode-exit.js} +17 -0
  39. package/services/encode/index.js +9 -0
  40. package/services/encode/run-command.js +647 -0
  41. package/services/hls-session-manager.js +11073 -10711
  42. package/services/hwaccel.js +1688 -1992
  43. package/services/orchestrators/EncodeOrchestrator.js +359 -0
  44. package/services/output/LiveOutputs.js +213 -0
  45. package/services/output/Output.js +94 -0
  46. package/services/output/OutputSpec.js +195 -0
  47. package/services/output/Timeline.js +220 -0
  48. package/services/output/index.js +1 -0
  49. package/services/output/ladder.js +26 -0
  50. package/services/playback-planner.js +806 -775
  51. package/services/produced-index.js +222 -300
  52. package/services/source/SourceFile.js +346 -0
  53. package/services/{sidecar-files.js → torrent/files.js} +107 -11
  54. package/services/torrent/naming.js +619 -0
  55. package/services/torrent-worker/client.js +10 -0
  56. package/services/torrent-worker/container-tracks.js +71 -43
  57. package/services/torrent-worker/pool-adapter.js +18 -0
  58. package/services/torrent-worker/protocol.js +7 -0
  59. package/services/torrent-worker/subtitle-cues.js +549 -549
  60. package/services/torrent-worker/worker.js +18 -0
  61. package/services/tracks/TextSubtitleTrack.js +287 -287
  62. package/services/tracks/index.js +15 -14
  63. package/services/viewer/Viewer.js +145 -0
  64. package/services/viewer/Viewers.js +124 -0
  65. package/test/auto-quality-step.test.js +508 -506
  66. package/test/behind-head-repair.test.js +17 -7
  67. package/test/coverage-map.test.js +153 -0
  68. package/test/cut-times-timeline.test.js +6 -5
  69. package/test/cuts-follow-published-grid.test.js +4 -4
  70. package/test/decode-cost.test.js +31 -12
  71. package/test/encode-exit.test.js +1 -1
  72. package/test/encode-orchestrator.test.js +196 -0
  73. package/test/encode-plan.test.js +245 -0
  74. package/test/encode-run-state.test.js +2 -2
  75. package/test/encode-run.test.js +168 -0
  76. package/test/encoder-kinds.test.js +122 -0
  77. package/test/held-request-width.test.js +9 -3
  78. package/test/helpers/encode-run.js +128 -0
  79. package/test/keyframe-index-accuracy.test.js +19 -12
  80. package/test/keyframes-belong-to-the-file.test.js +132 -0
  81. package/test/orchestrator-wired.test.js +164 -0
  82. package/test/output-shape.test.js +68 -0
  83. package/test/output-spec.test.js +157 -0
  84. package/test/produced-copy-choice.test.js +58 -92
  85. package/test/produced-index.test.js +142 -188
  86. package/test/quality-variants.test.js +1079 -1075
  87. package/test/run-graph-drift.test.js +1 -1
  88. package/test/run-intervals.test.js +329 -0
  89. package/test/run-position-follows-published-grid.test.js +4 -4
  90. package/test/seek-landing.test.js +8 -8
  91. package/test/seek-target-not-superseded.test.js +21 -9
  92. package/test/segment-demand.test.js +82 -0
  93. package/test/segment-serve-wiring.test.js +47 -52
  94. package/test/segment-store.test.js +187 -0
  95. package/test/segments-are-shared.test.js +175 -0
  96. package/test/sidecar-naming.test.js +142 -0
  97. package/test/source-file.test.js +133 -0
  98. package/test/stale-request-after-seek.test.js +18 -12
  99. package/test/subtitle-language.test.js +252 -252
  100. package/test/timeline.test.js +95 -0
  101. package/test/{sidecar-files.test.js → torrent-files.test.js} +44 -1
  102. package/test/torrent-naming.test.js +255 -0
  103. package/test/tracks-begin-together.test.js +44 -32
  104. package/test/two-viewers-one-picture.test.js +347 -0
  105. package/test/viewer-outputs.test.js +273 -0
  106. package/test/viewer.test.js +91 -0
  107. package/utils/perf.js +1 -63
  108. package/services/container/index.js +0 -6
  109. package/services/controllers/index.js +0 -2
  110. package/services/download/index.js +0 -8
  111. package/services/orchestrators/index.js +0 -2
  112. /package/services/{encode-run-state.js → encode/encode-run-state.js} +0 -0
  113. /package/services/{language-detect.js → tracks/language-detect.js} +0 -0
@@ -0,0 +1,195 @@
1
+ /**
2
+ * @file What a session PRODUCES, stated once and used as its identity.
3
+ *
4
+ * Three domain axes already have a home in this package: what a file states
5
+ * about itself (`container/`), what a track states about itself (`tracks/`),
6
+ * and what anybody wants off the swarm (`demand/`). This is the fourth — what
7
+ * we make out of them — and it had no home at all: the identity of a session
8
+ * was a `[...].join(":")` inside a 300-line function, with no name, no test and
9
+ * nothing printing it.
10
+ *
11
+ * The rule it exists to express, stated by the user 2026-09-03:
12
+ *
13
+ * > A session belongs to the tracks its output actually carries. Its key holds
14
+ * > those tracks' parameters and nothing else. Two sessions whose parameters
15
+ * > agree ARE the same session, and the encoded result is reused by definition.
16
+ *
17
+ * So reuse between viewers is not a feature built on top of this — it is what a
18
+ * correctly built key already means. A field of the REQUEST that does not
19
+ * change one byte of the output must not appear here; measured 2026-09-03, two
20
+ * viewers of one copied picture got two sessions whose output was identical
21
+ * byte for byte (`research/two-viewers-one-file-2026-09-03.md`).
22
+ *
23
+ * What deliberately does NOT appear:
24
+ *
25
+ * 1. the viewer, in any form. Not the consumer id, not where they started, not
26
+ * their viewport. A viewer is not a property of the material;
27
+ * 2. the bitrate ceiling the viewer's measured link puts on a re-encode. It is
28
+ * a runtime parameter of a SHARED session — the worst link among the live
29
+ * consumers decides — so two viewers on one encode is the design, not a
30
+ * collision. Rate control appears in neither the SPS nor the PPS, which is
31
+ * why it can move under a player that has already cached the init;
32
+ * 3. the video track number. Only `0:v:0` is ever mapped, so the file names the
33
+ * picture. Add it here the day a second video track can be chosen.
34
+ *
35
+ * A limit worth stating: two specs that agree name interchangeable output
36
+ * within ONE proxy. A copied picture's bytes depend only on the source, but a
37
+ * re-encoded one's depend on this host's encoder, its preset and its rate cap —
38
+ * so this is not enough to reuse segments BETWEEN proxies (roadmap item 41).
39
+ */
40
+
41
+ /**
42
+ * The picture an output carries.
43
+ */
44
+ export class VideoOutput {
45
+ /**
46
+ * @param {object} params
47
+ * @param {number} params.fileIndex - The file the picture is read from.
48
+ * @param {{ width: number, height: number, manual: boolean } | null} params.encode
49
+ * Null when the picture is copied — then the output is the source's own
50
+ * size and nothing about a target box can change it. The box when it is
51
+ * re-encoded, with `manual` saying the viewer forced it and the realtime
52
+ * budget must not move it.
53
+ */
54
+ constructor({ fileIndex, encode = null }) {
55
+ this.fileIndex = Number.isInteger(fileIndex) && fileIndex >= 0 ? fileIndex : 0;
56
+ this.encode = encode
57
+ ? {
58
+ width: Number.isInteger(encode.width) && encode.width > 0 ? encode.width : 0,
59
+ height: Number.isInteger(encode.height) && encode.height > 0 ? encode.height : 0,
60
+ manual: encode.manual === true
61
+ }
62
+ : null;
63
+ }
64
+
65
+ /**
66
+ * @returns {string}
67
+ */
68
+ toKey() {
69
+ if (!this.encode) {
70
+ return `v=${this.fileIndex}/copy`;
71
+ }
72
+ const box = `${this.encode.width}x${this.encode.height}`;
73
+ return `v=${this.fileIndex}/enc:${box}:${this.encode.manual ? "manual" : "auto"}`;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * The soundtrack an output carries.
79
+ */
80
+ export class AudioOutput {
81
+ /**
82
+ * @param {object} params
83
+ * @param {number} params.fileIndex - The file the TRACK lives in, which for a
84
+ * dub shipped beside the picture is not the picture's file.
85
+ * @param {number} params.trackIndex - `0:a:N` inside that file. The flat
86
+ * number the browser sends spans the picture's own tracks and the files
87
+ * beside it, and two flat numbers of two different pictures can name one
88
+ * track; this is the number that cannot.
89
+ * @param {boolean} params.transcode - Re-encoded to AAC, or copied.
90
+ */
91
+ constructor({ fileIndex, trackIndex, transcode }) {
92
+ this.fileIndex = Number.isInteger(fileIndex) && fileIndex >= 0 ? fileIndex : 0;
93
+ this.trackIndex = Number.isInteger(trackIndex) && trackIndex >= 0 ? trackIndex : 0;
94
+ this.transcode = transcode === true;
95
+ }
96
+
97
+ /**
98
+ * @returns {string}
99
+ */
100
+ toKey() {
101
+ return `a=${this.fileIndex}/${this.trackIndex}/${this.transcode ? "aac" : "copy"}`;
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Where an output is cut.
107
+ *
108
+ * Both forms belong to a FILE and not to the session: the keyframe grid is that
109
+ * file's own keyframe times, and the uniform grid is derived from that file's
110
+ * duration. A soundtrack takes the grid of the picture it accompanies, so its
111
+ * grid names the picture's file — which is what makes two soundtrack sessions
112
+ * cut for two different pictures tell themselves apart.
113
+ */
114
+ export class CutGrid {
115
+ /**
116
+ * @param {object} params
117
+ * @param {"keyframe" | "uniform"} params.kind
118
+ * @param {number} params.fileIndex - Whose keyframes, or whose duration.
119
+ */
120
+ constructor({ kind, fileIndex }) {
121
+ this.kind = kind === "keyframe" ? "keyframe" : "uniform";
122
+ this.fileIndex = Number.isInteger(fileIndex) && fileIndex >= 0 ? fileIndex : 0;
123
+ }
124
+
125
+ /**
126
+ * @returns {string}
127
+ */
128
+ toKey() {
129
+ return `grid=${this.kind === "keyframe" ? "kf" : "even"}@${this.fileIndex}`;
130
+ }
131
+ }
132
+
133
+ /**
134
+ * One encode of one torrent's material: which tracks, in what form, cut how,
135
+ * packaged how.
136
+ */
137
+ export class OutputSpec {
138
+ /**
139
+ * @param {object} params
140
+ * @param {string} params.sourceKey - `torrent:<infohash>`, the canonical
141
+ * identity of the torrent itself: a magnet and a `.torrent` file for the
142
+ * same content produce the same one (`torrent-source-key.js`). Nothing
143
+ * further is needed to say WHICH film this is.
144
+ * @param {string} params.segmentFormatId - fMP4 or MPEG-TS. Two viewers
145
+ * asking for different containers cannot share one ffmpeg.
146
+ * @param {CutGrid} params.grid
147
+ * @param {VideoOutput | null} params.video
148
+ * @param {AudioOutput | null} params.audio
149
+ */
150
+ constructor({ sourceKey, segmentFormatId, grid, video = null, audio = null }) {
151
+ this.sourceKey = String(sourceKey ?? "");
152
+ this.segmentFormatId = String(segmentFormatId ?? "");
153
+ this.grid = grid;
154
+ this.video = video;
155
+ this.audio = audio;
156
+ }
157
+
158
+ /**
159
+ * What this output carries, in the vocabulary the rest of the class uses.
160
+ *
161
+ * `muxed` is the one case where a session legitimately holds the parameters
162
+ * of two tracks: a browser that does not understand rendition groups must be
163
+ * sent its sound inside the picture's own stream.
164
+ *
165
+ * @returns {"video-only" | "audio-only" | "muxed" | "empty"}
166
+ */
167
+ get carries() {
168
+ if (this.video && this.audio) {
169
+ return "muxed";
170
+ }
171
+ if (this.video) {
172
+ return "video-only";
173
+ }
174
+ if (this.audio) {
175
+ return "audio-only";
176
+ }
177
+ return "empty";
178
+ }
179
+
180
+ /**
181
+ * The identity. Two outputs with the same one are the same output.
182
+ *
183
+ * @returns {string}
184
+ */
185
+ toKey() {
186
+ const parts = [this.sourceKey, `fmt=${this.segmentFormatId}`, this.grid.toKey(), this.carries];
187
+ if (this.video) {
188
+ parts.push(this.video.toKey());
189
+ }
190
+ if (this.audio) {
191
+ parts.push(this.audio.toKey());
192
+ }
193
+ return parts.join(":");
194
+ }
195
+ }
@@ -0,0 +1,220 @@
1
+ /**
2
+ * @file Where a file is cut, and what the player was told about it.
3
+ *
4
+ * This belongs to the FILE and its grid, not to a session and not to a viewer.
5
+ * Every quality step of one film has to be cut at exactly the same times — that
6
+ * is what lets a segment made by one encoder be appended where another's would
7
+ * have gone — and every session serving that film has to publish the same
8
+ * playlist, or two of them stamp the same moment differently and the picture
9
+ * and the sound drift apart.
10
+ *
11
+ * Until now each session computed and kept its own copy, and agreement between
12
+ * them was achieved by COPYING: a variant was handed `inheritedGrid` at
13
+ * creation, and a soundtrack the same. That works while somebody remembers to
14
+ * pass it, and it failed when the two tables drifted — measured 2026-08-17,
15
+ * corrections of 0.6-2.9 s between two sessions of one file, and again
16
+ * 2026-08-20, segments arriving a uniform 2.002 s before the times the playlist
17
+ * named for them, four times what a player will bridge. One table, held once,
18
+ * cannot drift from itself.
19
+ *
20
+ * **What it does NOT hold, and why.** The container's own keyframe table, how
21
+ * exact that table is, which container answered, and how long the file runs:
22
+ * every one of those is a fact of the FILE, and this object is held per file AND
23
+ * grid — so a file cut two ways kept two copies of one immutable list. They live
24
+ * on the source file now, which is one object per file whatever grids are cut
25
+ * from it. What is left here is the two things that really are per grid: where
26
+ * the cuts are, and what the player was told they are.
27
+ *
28
+ * **Two tables, and they are not the same thing.** `boundaries` is where the
29
+ * file is cut NOW, corrections included, and it is what a run is told to cut
30
+ * at. `published` is what the player was given, written once and never changed,
31
+ * and it is what a segment must be stamped to. A player places a fragment by
32
+ * the playlist it holds; the live table keeps moving as produced segments
33
+ * reveal where the file's cuts really are.
34
+ */
35
+
36
+ /**
37
+ * A fresh tally of how well a container's keyframe index matches its file.
38
+ *
39
+ * @returns {{ checked: number, disagreed: number, maxDeviationSec: number, firstDisagreementIndex: number, deviations: number[], landedOnAnotherKeyframe: number, seen: Set<number> }}
40
+ */
41
+ export function newIndexCheck() {
42
+ return {
43
+ checked: 0,
44
+ disagreed: 0,
45
+ maxDeviationSec: 0,
46
+ firstDisagreementIndex: -1,
47
+ // Every deviation, so the summary can report a distribution instead of one
48
+ // extreme. Bounded by the number of distinct boundaries a file produces.
49
+ deviations: [],
50
+ // Of the segments that started away from the playlist, how many began at
51
+ // ANOTHER time in the very list the grid was built from. This is the
52
+ // measurement that separates the two explanations: a table that describes
53
+ // times the file does not have, against a table that lists only SOME
54
+ // keyframes and a grid built over its gaps. Asked 2026-08-17 by the user,
55
+ // who was right that the second is far more likely — every deviation
56
+ // measured that day was positive, 0.58-2.96 s, which is what a cut pushed
57
+ // forward to the next real keyframe looks like.
58
+ landedOnAnotherKeyframe: 0,
59
+ // Which boundaries have been counted. A segment can be requested again, and
60
+ // a repeat is the same boundary, not new evidence.
61
+ seen: new Set()
62
+ };
63
+ }
64
+
65
+ export class Timeline {
66
+ /**
67
+ * @param {object} params
68
+ * @param {number[]} params.boundaries - Cut times in seconds, ascending, one
69
+ * more than there are segments.
70
+ * @param {number[] | null} [params.published] - What the player was told, when
71
+ * that differs from the boundaries because corrections have been made since.
72
+ * @param {"keyframe" | "uniform"} params.cutGrid - Whether those times are
73
+ * the source's own keyframes, which a copied picture has no choice about,
74
+ * or an even grid the encoder is told to place keyframes on.
75
+ */
76
+ constructor({ boundaries, published = null, cutGrid }) {
77
+ this.boundaries = Array.isArray(boundaries) ? boundaries : [];
78
+ // What the player holds. Taken from the boundaries as they stood when the
79
+ // playlist was written, and never touched again. Given outright only when a
80
+ // table is being restored with corrections already in it — a live one is
81
+ // always published from its own boundaries.
82
+ this.published = Array.isArray(published) ? published : [...this.boundaries];
83
+ this.cutGrid = cutGrid === "keyframe" ? "keyframe" : "uniform";
84
+ // How well this container's keyframe index matches its own file. A fact
85
+ // about the FILE and its index: asked per session it would be answered a
86
+ // different number of times for one film depending on how many people
87
+ // happened to watch it.
88
+ this.indexCheck = newIndexCheck();
89
+ }
90
+
91
+ /** @returns {number} How many segments this file is cut into. */
92
+ get segmentCount() {
93
+ return Math.max(0, this.boundaries.length - 1);
94
+ }
95
+
96
+ /**
97
+ * Where segment `index` begins, on the timeline the player was given.
98
+ *
99
+ * @param {number} index
100
+ * @returns {number}
101
+ */
102
+ publishedStartOf(index) {
103
+ if (!Number.isInteger(index) || index <= 0) {
104
+ return this.published[0] ?? 0;
105
+ }
106
+ const at = Math.min(index, this.published.length - 1);
107
+ return this.published[at] ?? 0;
108
+ }
109
+
110
+ /**
111
+ * Where segment `index` begins on the live table — where a run cutting now
112
+ * will really put it.
113
+ *
114
+ * @param {number} index
115
+ * @returns {number}
116
+ */
117
+ liveStartOf(index) {
118
+ if (!Number.isInteger(index) || index <= 0) {
119
+ return this.boundaries[0] ?? 0;
120
+ }
121
+ const at = Math.min(index, this.boundaries.length - 1);
122
+ return this.boundaries[at] ?? 0;
123
+ }
124
+
125
+ /**
126
+ * Which segment holds this moment.
127
+ *
128
+ * @param {number} seconds
129
+ * @returns {number}
130
+ */
131
+ indexForTime(seconds) {
132
+ const wanted = Number.isFinite(seconds) && seconds > 0 ? seconds : 0;
133
+ for (let index = 0; index < this.segmentCount; index += 1) {
134
+ if (wanted < this.boundaries[index + 1]) {
135
+ return index;
136
+ }
137
+ }
138
+ return Math.max(0, this.segmentCount - 1);
139
+ }
140
+
141
+ }
142
+
143
+ /**
144
+ * The timelines this proxy holds, one per file and grid.
145
+ *
146
+ * Keyed by the two things that decide where the cuts are: which file, and
147
+ * whether the cuts are its own keyframes or an even grid. A quality step of the
148
+ * same film is a different OUTPUT and the same timeline, which is exactly the
149
+ * agreement `inheritedGrid` used to arrange by copying.
150
+ */
151
+ export class Timelines {
152
+ /** @type {Map<string, Timeline>} */
153
+ #byKey = new Map();
154
+
155
+ /**
156
+ * @param {string} sourceKey
157
+ * @param {number} fileIndex
158
+ * @param {"keyframe" | "uniform"} cutGrid
159
+ * @returns {string}
160
+ */
161
+ static keyFor(sourceKey, fileIndex, cutGrid) {
162
+ return `${sourceKey}:${fileIndex}:${cutGrid === "keyframe" ? "kf" : "even"}`;
163
+ }
164
+
165
+ /**
166
+ * The one for this file and grid, made by `build` if it is not there yet.
167
+ *
168
+ * @param {string} key
169
+ * @param {() => Timeline} build
170
+ * @returns {Timeline}
171
+ */
172
+ get(key, build) {
173
+ let timeline = this.#byKey.get(key);
174
+ if (!timeline) {
175
+ timeline = build();
176
+ this.#byKey.set(key, timeline);
177
+ }
178
+ return timeline;
179
+ }
180
+
181
+ /**
182
+ * @param {string} key
183
+ * @returns {Timeline | null}
184
+ */
185
+ peek(key) {
186
+ return this.#byKey.get(key) ?? null;
187
+ }
188
+
189
+ /** @param {string} key */
190
+ forget(key) {
191
+ this.#byKey.delete(key);
192
+ }
193
+
194
+ /**
195
+ * Drop every timeline nobody is holding.
196
+ *
197
+ * A timeline is small — two arrays of a few thousand numbers — and it is kept
198
+ * for as long as somebody is reading the file it describes. Nothing else
199
+ * removes one, and a map that only ever grows is the shape of half the memory
200
+ * faults recorded in this project.
201
+ *
202
+ * @param {Set<Timeline>} inUse
203
+ * @returns {number} How many were dropped.
204
+ */
205
+ forgetUnused(inUse) {
206
+ let dropped = 0;
207
+ for (const [key, timeline] of [...this.#byKey]) {
208
+ if (!inUse.has(timeline)) {
209
+ this.#byKey.delete(key);
210
+ dropped += 1;
211
+ }
212
+ }
213
+ return dropped;
214
+ }
215
+
216
+ /** @returns {number} */
217
+ get size() {
218
+ return this.#byKey.size;
219
+ }
220
+ }
@@ -0,0 +1 @@
1
+ export { OutputSpec, VideoOutput, AudioOutput, CutGrid } from "./OutputSpec.js";
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @file The heights a picture may be offered at.
3
+ *
4
+ * A fact about an output and about nothing else: it takes a source height and
5
+ * answers with heights. What the machine can afford of them is the quality
6
+ * budget's question, and what a player may splice between is `LiveOutputs`.
7
+ */
8
+
9
+ // The resolutions a viewer may choose between. Only rungs at or below the
10
+ // source are offered: upscaling invents detail and costs the encoder more than
11
+ // the source itself.
12
+ const VARIANT_LADDER = [2160, 1440, 1080, 720, 540, 480, 360, 240];
13
+
14
+ /**
15
+ * The heights offered for a source of this height, largest first.
16
+ *
17
+ * @param {number} sourceHeight
18
+ * @returns {number[]}
19
+ */
20
+ export function variantHeightsFor(sourceHeight) {
21
+ if (!Number.isFinite(sourceHeight) || sourceHeight <= 0) {
22
+ return [];
23
+ }
24
+ const rungs = VARIANT_LADDER.filter((height) => height < sourceHeight);
25
+ return [Math.round(sourceHeight), ...rungs];
26
+ }