@torrent-tv/proxy 2.80.5 → 2.80.6

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.
@@ -1,256 +1,333 @@
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
- * @param {number[] | null} [params.sourceTimes] - The same cuts on the FILE's
76
- * own clock — the keyframe a run must seek to for each boundary.
77
- */
78
- constructor({ boundaries, published = null, cutGrid, sourceTimes = null }) {
79
- this.boundaries = Array.isArray(boundaries) ? boundaries : [];
80
- // What the player holds. Taken from the boundaries as they stood when the
81
- // playlist was written, and never touched again. Given outright only when a
82
- // table is being restored with corrections already in it — a live one is
83
- // always published from its own boundaries.
84
- this.published = Array.isArray(published) ? published : [...this.boundaries];
85
- this.cutGrid = cutGrid === "keyframe" ? "keyframe" : "uniform";
86
- // Where each cut is on the FILE's clock, which is what a seek asks for.
87
- // Frozen beside `published` and never corrected: a run must land where the
88
- // player was told the segment begins, and the corrections belong to the
89
- // live table. Empty when the grid was restored without it, and then the
90
- // seek falls back to searching the file's keyframe list — which is the
91
- // lossy path this exists to replace, kept only so a restored table still
92
- // plays.
93
- this.sourceTimes = Array.isArray(sourceTimes) && sourceTimes.length === this.published.length
94
- ? sourceTimes
95
- : [];
96
- // How well this container's keyframe index matches its own file. A fact
97
- // about the FILE and its index: asked per session it would be answered a
98
- // different number of times for one film depending on how many people
99
- // happened to watch it.
100
- this.indexCheck = newIndexCheck();
101
- }
102
-
103
- /** @returns {number} How many segments this file is cut into. */
104
- get segmentCount() {
105
- return Math.max(0, this.boundaries.length - 1);
106
- }
107
-
108
- /**
109
- * Where segment `index` begins, on the timeline the player was given.
110
- *
111
- * @param {number} index
112
- * @returns {number}
113
- */
114
- publishedStartOf(index) {
115
- if (!Number.isInteger(index) || index <= 0) {
116
- return this.published[0] ?? 0;
117
- }
118
- const at = Math.min(index, this.published.length - 1);
119
- return this.published[at] ?? 0;
120
- }
121
-
122
- /**
123
- * Where segment `index` begins on the FILE's own clock: the keyframe a run
124
- * starting there must seek to.
125
- *
126
- * Not `publishedStartOf(index) + startTime`. That round trip is lossy, and a
127
- * residue of two parts in a quadrillion costs a whole keyframe interval when
128
- * the result is looked up in the container's list by value — which is how a
129
- * run came to be numbered from #36 while carrying film 17.4 s earlier
130
- * (2026-09-05). The number the container stated is carried, not rebuilt.
131
- *
132
- * @param {number} index
133
- * @returns {number | null} Null when this table was restored without the
134
- * source clock, and the caller must fall back to searching.
135
- */
136
- sourceStartOf(index) {
137
- if (this.sourceTimes.length === 0) {
138
- return null;
139
- }
140
- if (!Number.isInteger(index) || index <= 0) {
141
- return this.sourceTimes[0] ?? null;
142
- }
143
- return this.sourceTimes[Math.min(index, this.sourceTimes.length - 1)] ?? null;
144
- }
145
-
146
- /**
147
- * Where segment `index` begins on the live table — where a run cutting now
148
- * will really put it.
149
- *
150
- * @param {number} index
151
- * @returns {number}
152
- */
153
- liveStartOf(index) {
154
- if (!Number.isInteger(index) || index <= 0) {
155
- return this.boundaries[0] ?? 0;
156
- }
157
- const at = Math.min(index, this.boundaries.length - 1);
158
- return this.boundaries[at] ?? 0;
159
- }
160
-
161
- /**
162
- * Which segment holds this moment.
163
- *
164
- * @param {number} seconds
165
- * @returns {number}
166
- */
167
- indexForTime(seconds) {
168
- const wanted = Number.isFinite(seconds) && seconds > 0 ? seconds : 0;
169
- for (let index = 0; index < this.segmentCount; index += 1) {
170
- if (wanted < this.boundaries[index + 1]) {
171
- return index;
172
- }
173
- }
174
- return Math.max(0, this.segmentCount - 1);
175
- }
176
-
177
- }
178
-
179
- /**
180
- * The timelines this proxy holds, one per file and grid.
181
- *
182
- * Keyed by the two things that decide where the cuts are: which file, and
183
- * whether the cuts are its own keyframes or an even grid. A quality step of the
184
- * same film is a different OUTPUT and the same timeline, which is exactly the
185
- * agreement `inheritedGrid` used to arrange by copying.
186
- */
187
- export class Timelines {
188
- /** @type {Map<string, Timeline>} */
189
- #byKey = new Map();
190
-
191
- /**
192
- * @param {string} sourceKey
193
- * @param {number} fileIndex
194
- * @param {"keyframe" | "uniform"} cutGrid
195
- * @returns {string}
196
- */
197
- static keyFor(sourceKey, fileIndex, cutGrid) {
198
- return `${sourceKey}:${fileIndex}:${cutGrid === "keyframe" ? "kf" : "even"}`;
199
- }
200
-
201
- /**
202
- * The one for this file and grid, made by `build` if it is not there yet.
203
- *
204
- * @param {string} key
205
- * @param {() => Timeline} build
206
- * @returns {Timeline}
207
- */
208
- get(key, build) {
209
- let timeline = this.#byKey.get(key);
210
- if (!timeline) {
211
- timeline = build();
212
- this.#byKey.set(key, timeline);
213
- }
214
- return timeline;
215
- }
216
-
217
- /**
218
- * @param {string} key
219
- * @returns {Timeline | null}
220
- */
221
- peek(key) {
222
- return this.#byKey.get(key) ?? null;
223
- }
224
-
225
- /** @param {string} key */
226
- forget(key) {
227
- this.#byKey.delete(key);
228
- }
229
-
230
- /**
231
- * Drop every timeline nobody is holding.
232
- *
233
- * A timeline is small — two arrays of a few thousand numbers — and it is kept
234
- * for as long as somebody is reading the file it describes. Nothing else
235
- * removes one, and a map that only ever grows is the shape of half the memory
236
- * faults recorded in this project.
237
- *
238
- * @param {Set<Timeline>} inUse
239
- * @returns {number} How many were dropped.
240
- */
241
- forgetUnused(inUse) {
242
- let dropped = 0;
243
- for (const [key, timeline] of [...this.#byKey]) {
244
- if (!inUse.has(timeline)) {
245
- this.#byKey.delete(key);
246
- dropped += 1;
247
- }
248
- }
249
- return dropped;
250
- }
251
-
252
- /** @returns {number} */
253
- get size() {
254
- return this.#byKey.size;
255
- }
256
- }
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
+ * @param {number[] | null} [params.sourceTimes] - The same cuts on the FILE's
76
+ * own clock — the keyframe a run must seek to for each boundary.
77
+ */
78
+ constructor({ boundaries, published = null, cutGrid, sourceTimes = null }) {
79
+ this.boundaries = Array.isArray(boundaries) ? boundaries : [];
80
+ // What the player holds. Taken from the boundaries as they stood when the
81
+ // playlist was written, and never touched again. Given outright only when a
82
+ // table is being restored with corrections already in it — a live one is
83
+ // always published from its own boundaries.
84
+ this.published = Array.isArray(published) ? published : [...this.boundaries];
85
+ this.cutGrid = cutGrid === "keyframe" ? "keyframe" : "uniform";
86
+ // Where each cut is on the FILE's clock, which is what a seek asks for.
87
+ // Frozen beside `published` and never corrected: a run must land where the
88
+ // player was told the segment begins, and the corrections belong to the
89
+ // live table. Empty when the grid was restored without it, and then the
90
+ // seek falls back to searching the file's keyframe list — which is the
91
+ // lossy path this exists to replace, kept only so a restored table still
92
+ // plays.
93
+ this.sourceTimes = Array.isArray(sourceTimes) && sourceTimes.length === this.published.length
94
+ ? sourceTimes
95
+ : [];
96
+ // How well this container's keyframe index matches its own file. A fact
97
+ // about the FILE and its index: asked per session it would be answered a
98
+ // different number of times for one film depending on how many people
99
+ // happened to watch it.
100
+ this.indexCheck = newIndexCheck();
101
+ }
102
+
103
+ /** @returns {number} How many segments this file is cut into. */
104
+ get segmentCount() {
105
+ return Math.max(0, this.boundaries.length - 1);
106
+ }
107
+
108
+ /**
109
+ * Where segment `index` begins, on the timeline the player was given.
110
+ *
111
+ * @param {number} index
112
+ * @returns {number}
113
+ */
114
+ publishedStartOf(index) {
115
+ if (!Number.isInteger(index) || index <= 0) {
116
+ return this.published[0] ?? 0;
117
+ }
118
+ const at = Math.min(index, this.published.length - 1);
119
+ return this.published[at] ?? 0;
120
+ }
121
+
122
+ /**
123
+ * Where segment `index` begins on the FILE's own clock: the keyframe a run
124
+ * starting there must seek to.
125
+ *
126
+ * Not `publishedStartOf(index) + startTime`. That round trip is lossy, and a
127
+ * residue of two parts in a quadrillion costs a whole keyframe interval when
128
+ * the result is looked up in the container's list by value — which is how a
129
+ * run came to be numbered from #36 while carrying film 17.4 s earlier
130
+ * (2026-09-05). The number the container stated is carried, not rebuilt.
131
+ *
132
+ * @param {number} index
133
+ * @returns {number | null} Null when this table was restored without the
134
+ * source clock, and the caller must fall back to searching.
135
+ */
136
+ sourceStartOf(index) {
137
+ if (this.sourceTimes.length === 0) {
138
+ return null;
139
+ }
140
+ if (!Number.isInteger(index) || index <= 0) {
141
+ return this.sourceTimes[0] ?? null;
142
+ }
143
+ return this.sourceTimes[Math.min(index, this.sourceTimes.length - 1)] ?? null;
144
+ }
145
+
146
+ /**
147
+ * Where segment `index` begins on the live table — where a run cutting now
148
+ * will really put it.
149
+ *
150
+ * @param {number} index
151
+ * @returns {number}
152
+ */
153
+ liveStartOf(index) {
154
+ if (!Number.isInteger(index) || index <= 0) {
155
+ return this.boundaries[0] ?? 0;
156
+ }
157
+ const at = Math.min(index, this.boundaries.length - 1);
158
+ return this.boundaries[at] ?? 0;
159
+ }
160
+
161
+ /**
162
+ * A map stated in seconds of film, in THIS timeline's own numbering.
163
+ *
164
+ * The priority map is one per film, in seconds, because a viewer's position
165
+ * is a moment of film and nothing else. Two outputs of one film are cut
166
+ * independently — 454 pieces against 401 on the field file — so the same
167
+ * second is a different number in each, and the conversion belongs to
168
+ * whichever cut table is being read.
169
+ *
170
+ * It lived in the session manager, which is a place, not a layer. Here it is
171
+ * beside the table it converts against.
172
+ *
173
+ * @param {import("../priority/PriorityMap.js").PriorityMap} map - One number
174
+ * per second of film.
175
+ * @param {number} segmentCount - How many pieces this output has.
176
+ * @returns {import("../priority/PriorityMap.js").DemandZone[]} Runs of pieces
177
+ * that agree. Empty where nothing is stated, which says nobody is coming.
178
+ */
179
+ inSegments(map, segmentCount) {
180
+ if (!(segmentCount > 0) || !map || !(map.durationSeconds > 0)) {
181
+ return [];
182
+ }
183
+ /** @type {import("../priority/PriorityMap.js").DemandZone[]} */
184
+ const runs = [];
185
+ for (let index = 0; index < segmentCount; index += 1) {
186
+ // A PIECE TAKES THE STRONGEST SECOND IT HOLDS. It is made or not made
187
+ // whole, so it is wanted as soon as the soonest second inside it is
188
+ // wanted and the piece a viewer is standing in the middle of is wanted
189
+ // exactly as much as the second under their feet.
190
+ const from = Math.max(0, Math.floor(this.publishedStartOf(index)));
191
+ const until = index + 1 < segmentCount
192
+ ? Math.max(from + 1, Math.ceil(this.publishedStartOf(index + 1)))
193
+ : map.durationSeconds;
194
+ let priority = 0;
195
+ let withinSeconds = Number.POSITIVE_INFINITY;
196
+ let behind = true;
197
+ for (let second = from; second < until && second < map.durationSeconds; second += 1) {
198
+ if (map.priority[second] > priority) {
199
+ priority = map.priority[second];
200
+ }
201
+ if (map.secondsUntilPlayed[second] < withinSeconds) {
202
+ withinSeconds = map.secondsUntilPlayed[second];
203
+ }
204
+ if (map.behind[second] === 0) {
205
+ behind = false;
206
+ }
207
+ }
208
+ if (priority === 0) {
209
+ continue;
210
+ }
211
+ const previous = runs[runs.length - 1];
212
+ if (
213
+ previous
214
+ && previous.to === index - 1
215
+ && previous.priority === priority
216
+ && previous.behind === behind
217
+ ) {
218
+ // The time is not compared, only the rank. A run's time is that of its
219
+ // near edge a stretch is met at its beginning — and whoever needs the
220
+ // time of a piece inside it walks forward from there.
221
+ previous.to = index;
222
+ continue;
223
+ }
224
+ runs.push({
225
+ from: index,
226
+ to: index,
227
+ priority,
228
+ withinSeconds,
229
+ // Which side of the viewers this is. Stated by the map and carried
230
+ // through: converting seconds into piece numbers cannot move a stretch
231
+ // from in front of somebody to behind them.
232
+ behind
233
+ });
234
+ }
235
+ return runs;
236
+ }
237
+
238
+ /**
239
+ * Which segment holds this moment.
240
+ *
241
+ * @param {number} seconds
242
+ * @returns {number}
243
+ */
244
+ indexForTime(seconds) {
245
+ const wanted = Number.isFinite(seconds) && seconds > 0 ? seconds : 0;
246
+ for (let index = 0; index < this.segmentCount; index += 1) {
247
+ if (wanted < this.boundaries[index + 1]) {
248
+ return index;
249
+ }
250
+ }
251
+ return Math.max(0, this.segmentCount - 1);
252
+ }
253
+
254
+ }
255
+
256
+ /**
257
+ * The timelines this proxy holds, one per file and grid.
258
+ *
259
+ * Keyed by the two things that decide where the cuts are: which file, and
260
+ * whether the cuts are its own keyframes or an even grid. A quality step of the
261
+ * same film is a different OUTPUT and the same timeline, which is exactly the
262
+ * agreement `inheritedGrid` used to arrange by copying.
263
+ */
264
+ export class Timelines {
265
+ /** @type {Map<string, Timeline>} */
266
+ #byKey = new Map();
267
+
268
+ /**
269
+ * @param {string} sourceKey
270
+ * @param {number} fileIndex
271
+ * @param {"keyframe" | "uniform"} cutGrid
272
+ * @returns {string}
273
+ */
274
+ static keyFor(sourceKey, fileIndex, cutGrid) {
275
+ return `${sourceKey}:${fileIndex}:${cutGrid === "keyframe" ? "kf" : "even"}`;
276
+ }
277
+
278
+ /**
279
+ * The one for this file and grid, made by `build` if it is not there yet.
280
+ *
281
+ * @param {string} key
282
+ * @param {() => Timeline} build
283
+ * @returns {Timeline}
284
+ */
285
+ get(key, build) {
286
+ let timeline = this.#byKey.get(key);
287
+ if (!timeline) {
288
+ timeline = build();
289
+ this.#byKey.set(key, timeline);
290
+ }
291
+ return timeline;
292
+ }
293
+
294
+ /**
295
+ * @param {string} key
296
+ * @returns {Timeline | null}
297
+ */
298
+ peek(key) {
299
+ return this.#byKey.get(key) ?? null;
300
+ }
301
+
302
+ /** @param {string} key */
303
+ forget(key) {
304
+ this.#byKey.delete(key);
305
+ }
306
+
307
+ /**
308
+ * Drop every timeline nobody is holding.
309
+ *
310
+ * A timeline is small — two arrays of a few thousand numbers — and it is kept
311
+ * for as long as somebody is reading the file it describes. Nothing else
312
+ * removes one, and a map that only ever grows is the shape of half the memory
313
+ * faults recorded in this project.
314
+ *
315
+ * @param {Set<Timeline>} inUse
316
+ * @returns {number} How many were dropped.
317
+ */
318
+ forgetUnused(inUse) {
319
+ let dropped = 0;
320
+ for (const [key, timeline] of [...this.#byKey]) {
321
+ if (!inUse.has(timeline)) {
322
+ this.#byKey.delete(key);
323
+ dropped += 1;
324
+ }
325
+ }
326
+ return dropped;
327
+ }
328
+
329
+ /** @returns {number} */
330
+ get size() {
331
+ return this.#byKey.size;
332
+ }
333
+ }