@torrent-tv/proxy 2.80.13 → 2.80.14
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.
- package/CHANGELOG.md +10 -0
- package/docs/download-architecture.md +42 -0
- package/docs/encode-architecture.md +22 -0
- package/package.json +1 -1
- package/routes/api/transcode-sessions/net-report/post.js +64 -53
- package/services/download/SwarmSelection.js +20 -4
- package/services/encode/SegmentStore.js +743 -718
- package/services/hls-session-manager.js +8 -13
- package/services/output/LiveOutputs.js +36 -0
- package/services/output/playlists.js +86 -11
- package/services/output/rates.js +95 -0
- package/services/piece-store/piece-lru.js +9 -1
- package/services/priority/PriorityOrchestrator.js +284 -278
- package/services/supply-margin.js +40 -1
- package/services/torrent-worker/piece-reader.js +9 -4
- package/services/torrent-worker/worker.js +5 -0
- package/services/viewer/Viewer.js +345 -297
- package/test/declared-rates.test.js +116 -0
- package/test/piece-lru.test.js +5 -1
- package/test/supply-margin.test.js +25 -8
|
@@ -1,278 +1,284 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file The priority map, built once and handed to everybody who acts on it.
|
|
3
|
-
*
|
|
4
|
-
* One map per film, in seconds of film against a number. It is built from where
|
|
5
|
-
* the viewers are and nothing else, and both of the things that do work — the
|
|
6
|
-
* encoding and the downloading — read it and decide for themselves. They do not
|
|
7
|
-
* talk to each other, and neither of them tells this class anything.
|
|
8
|
-
*
|
|
9
|
-
* **Why it has to be published rather than asked for.** The downloading lives
|
|
10
|
-
* in another thread. Until now it took its orders from the reads themselves:
|
|
11
|
-
* every read declared a window around its own head, so fifteen reads declared
|
|
12
|
-
* fifteen windows on a piece store that holds sixteen pieces. Half of all
|
|
13
|
-
* evictions then took a piece a reader had said it wanted, two thirds of reads
|
|
14
|
-
* came back from disk, and what `/stream` handed out stopped being the file's
|
|
15
|
-
* bytes — twenty-two source-parse errors, a segment the player could not
|
|
16
|
-
* append, and an empty picture for six minutes (field 2026-09-05).
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { emptyMap, mapForViewer, mergeMaps, runsOf } from "./PriorityMap.js";
|
|
20
|
-
|
|
21
|
-
export class PriorityOrchestrator {
|
|
22
|
-
/** Where the map goes once it is built. @type {(published: object) => void} */
|
|
23
|
-
#publish;
|
|
24
|
-
|
|
25
|
-
/** The last map published per film and file, so an unchanged one is not resent. */
|
|
26
|
-
#last = new Map();
|
|
27
|
-
|
|
28
|
-
/** The last map BUILT per film and file, for whoever reads instead of being
|
|
29
|
-
* handed it. @type {Map<string, import("./PriorityMap.js").PriorityMap>} */
|
|
30
|
-
#maps = new Map();
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The last map built per OUTPUT, from the viewers of that output alone.
|
|
34
|
-
*
|
|
35
|
-
* The same fact answered at two scopes, because the two things that act on it
|
|
36
|
-
* ask at two scopes and both are right. The swarm is asked for bytes of a
|
|
37
|
-
* FILE, and the picture, a quality step and a soundtrack of one film read the
|
|
38
|
-
* same bytes — so every viewer of any of them wants that file's bytes.
|
|
39
|
-
* Encoders are placed per OUTPUT, and a person watching 480p wants nothing of
|
|
40
|
-
* the 1080p output at all.
|
|
41
|
-
*
|
|
42
|
-
* One map for both was the second authority over encoders. Every output of a
|
|
43
|
-
* film was handed the whole film's map, so the plan wanted an encoder on every
|
|
44
|
-
* one of them; what actually stopped the ones nobody was watching was the
|
|
45
|
-
* session manager killing them by its own judgement — and since a viewer
|
|
46
|
-
* moving between steps also announces itself, the plan started them again on
|
|
47
|
-
* the next pass. Two parties answering "should this encoder exist" by different
|
|
48
|
-
* rules, several times a second.
|
|
49
|
-
*
|
|
50
|
-
* @type {Map<string, import("./PriorityMap.js").PriorityMap>}
|
|
51
|
-
*/
|
|
52
|
-
#byOutput = new Map();
|
|
53
|
-
|
|
54
|
-
/** Who is watching one session. @type {(session: object) => Map<string, object>} */
|
|
55
|
-
#viewersOf;
|
|
56
|
-
|
|
57
|
-
/** How wide the first band of one session's file is. @type {(session: object) => number} */
|
|
58
|
-
#allowanceFor;
|
|
59
|
-
|
|
60
|
-
/** Whether this output is what that person is consuming, as opposed to one
|
|
61
|
-
* they merely hold a record on. @type {(session: object, viewer: object) => boolean} */
|
|
62
|
-
#watchedBy;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* This layer states facts and imports nothing above itself, so what it needs
|
|
66
|
-
* of a session — who is watching it, and how wide an interruption this file
|
|
67
|
-
* has shown on this swarm — is passed in.
|
|
68
|
-
*
|
|
69
|
-
* @param {object} params
|
|
70
|
-
* @param {(published: { sourceKey: string, fileIndex: number, durationSeconds: number,
|
|
71
|
-
* zones: { from: number, to: number, priority: number }[] }) => void} params.publish
|
|
72
|
-
* @param {(session: object) => Map<string, object>} [params.viewersOf]
|
|
73
|
-
* @param {(session: object) => number} [params.allowanceFor]
|
|
74
|
-
* @param {(session: object, viewer: object) => boolean} [params.watchedBy] -
|
|
75
|
-
* Whether this output is the one that person is consuming. Which of a film's
|
|
76
|
-
* outputs a person has on screen is a fact about the film's shape, which
|
|
77
|
-
* this layer does not know; absent, every registered viewer counts, and then
|
|
78
|
-
* the per-output map says the same as the per-file one.
|
|
79
|
-
*/
|
|
80
|
-
constructor({ publish, viewersOf, allowanceFor, watchedBy }) {
|
|
81
|
-
this.#publish = typeof publish === "function" ? publish : () => {};
|
|
82
|
-
this.#viewersOf = typeof viewersOf === "function" ? viewersOf : () => new Map();
|
|
83
|
-
this.#allowanceFor = typeof allowanceFor === "function" ? allowanceFor : () => 0;
|
|
84
|
-
this.#watchedBy = typeof watchedBy === "function" ? watchedBy : () => true;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* A map from a set of viewers, and the ONE statement of how one is built.
|
|
89
|
-
*
|
|
90
|
-
* Asked at both scopes — once per film for the swarm, once per output for the
|
|
91
|
-
* encoders — and written once, because two copies of how a viewer's map is
|
|
92
|
-
* built is the same two-owners fault this class was split for.
|
|
93
|
-
*
|
|
94
|
-
* @param {object} params
|
|
95
|
-
* @param {number} params.durationSeconds
|
|
96
|
-
* @param {number} params.allowanceSeconds
|
|
97
|
-
* @param {{ atSeconds: number, playing: boolean }[]} params.viewers
|
|
98
|
-
* @returns {import("./PriorityMap.js").PriorityMap} A map of no length where
|
|
99
|
-
* nobody is watching or the film's length is unknown, which says the same as
|
|
100
|
-
* a map with nothing in it.
|
|
101
|
-
*/
|
|
102
|
-
#mapFrom({ durationSeconds, allowanceSeconds, viewers }) {
|
|
103
|
-
if (!(durationSeconds > 0) || !(viewers?.length > 0)) {
|
|
104
|
-
return emptyMap(0);
|
|
105
|
-
}
|
|
106
|
-
return mergeMaps(
|
|
107
|
-
viewers.map((viewer) =>
|
|
108
|
-
mapForViewer({
|
|
109
|
-
atSeconds: viewer.atSeconds,
|
|
110
|
-
durationSeconds,
|
|
111
|
-
allowanceSeconds,
|
|
112
|
-
playing: viewer.playing !== false
|
|
113
|
-
})
|
|
114
|
-
)
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The map for one film, from everyone watching it.
|
|
120
|
-
*
|
|
121
|
-
* @param {object} params
|
|
122
|
-
* @param {string} params.sourceKey
|
|
123
|
-
* @param {number} params.fileIndex
|
|
124
|
-
* @param {number} params.durationSeconds
|
|
125
|
-
* @param {number} params.allowanceSeconds - The measured depth below which an
|
|
126
|
-
* interruption reaches a viewer of this file.
|
|
127
|
-
* @param {{ atSeconds: number, playing: boolean }[]} params.viewers
|
|
128
|
-
* @returns {import("./PriorityMap.js").PriorityMap} One number per second of
|
|
129
|
-
* film, merged over everyone watching it.
|
|
130
|
-
*/
|
|
131
|
-
build({ sourceKey, fileIndex, durationSeconds, allowanceSeconds, viewers }) {
|
|
132
|
-
const map = this.#mapFrom({ durationSeconds, allowanceSeconds, viewers });
|
|
133
|
-
const key = `${sourceKey}:${fileIndex}`;
|
|
134
|
-
this.#maps.set(key, map);
|
|
135
|
-
// Unchanged maps are not republished: the downloading rebuilds what it asks
|
|
136
|
-
// the swarm for on every one, and a viewer sitting still would otherwise
|
|
137
|
-
// make it do that several times a second. Compared as stretches rather than
|
|
138
|
-
// second by second, which is the same comparison over far fewer values.
|
|
139
|
-
const zones = runsOf(map);
|
|
140
|
-
const shape = JSON.stringify(zones);
|
|
141
|
-
if (this.#last.get(key) !== shape) {
|
|
142
|
-
this.#last.set(key, shape);
|
|
143
|
-
this.#publish({ sourceKey, fileIndex, durationSeconds, zones });
|
|
144
|
-
}
|
|
145
|
-
return map;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Build and publish the map for every file anybody is watching.
|
|
150
|
-
*
|
|
151
|
-
* One map per FILE, not per output: the picture, a quality step and a
|
|
152
|
-
* soundtrack of one film are three outputs reading the same bytes, and the
|
|
153
|
-
* swarm is asked for bytes. Viewers of all of them merge into one map.
|
|
154
|
-
*
|
|
155
|
-
* @param {object} params
|
|
156
|
-
* @param {Iterable<object[]>} params.sessionGroups - The live sessions, in
|
|
157
|
-
* whatever grouping the caller holds them; they are regrouped by file here.
|
|
158
|
-
* @param {number} params.staleAfterMs - How long a viewer may be silent and
|
|
159
|
-
* still count as watching.
|
|
160
|
-
* @param {number} [params.now]
|
|
161
|
-
* @returns {void}
|
|
162
|
-
*/
|
|
163
|
-
publishFor({ sessionGroups, staleAfterMs, now = Date.now() }) {
|
|
164
|
-
/** @type {Map<string, { sourceKey: string, fileIndex: number, durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
|
|
165
|
-
const byFile = new Map();
|
|
166
|
-
/** @type {Map<string, { durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
|
|
167
|
-
const byOutput = new Map();
|
|
168
|
-
for (const sessions of sessionGroups) {
|
|
169
|
-
for (const session of sessions) {
|
|
170
|
-
const key = `${session.sourceKey}:${session.fileIndex}`;
|
|
171
|
-
const durationSeconds = Number(session.file?.durationSeconds) || 0;
|
|
172
|
-
// The first band is as wide as an interruption this file has actually
|
|
173
|
-
// shown on this swarm, never a chosen number.
|
|
174
|
-
const allowanceSeconds = this.#allowanceFor(session);
|
|
175
|
-
let held = byFile.get(key);
|
|
176
|
-
if (!held) {
|
|
177
|
-
held = {
|
|
178
|
-
sourceKey: session.sourceKey,
|
|
179
|
-
fileIndex: session.fileIndex,
|
|
180
|
-
durationSeconds,
|
|
181
|
-
allowanceSeconds,
|
|
182
|
-
viewers: []
|
|
183
|
-
};
|
|
184
|
-
byFile.set(key, held);
|
|
185
|
-
}
|
|
186
|
-
// Every output anybody holds a session for, whether or not a viewer is
|
|
187
|
-
// consuming it — an output with nobody on it must get a map with
|
|
188
|
-
// nothing in it, which is how the plan is told to stop its encoders.
|
|
189
|
-
// Left out, it would keep the map it had when somebody was watching.
|
|
190
|
-
const address = session.outputKey ?? "";
|
|
191
|
-
let mine = byOutput.get(address);
|
|
192
|
-
if (!mine) {
|
|
193
|
-
mine = { durationSeconds, allowanceSeconds, viewers: [] };
|
|
194
|
-
byOutput.set(address, mine);
|
|
195
|
-
}
|
|
196
|
-
for (const viewer of this.#viewersOf(session).values()) {
|
|
197
|
-
if (!viewer.isPresent(now, staleAfterMs)) {
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
const stated = {
|
|
201
|
-
atSeconds: viewer.positionSeconds() ?? 0,
|
|
202
|
-
playing
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
for (const
|
|
224
|
-
if (!
|
|
225
|
-
this.#
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @file The priority map, built once and handed to everybody who acts on it.
|
|
3
|
+
*
|
|
4
|
+
* One map per film, in seconds of film against a number. It is built from where
|
|
5
|
+
* the viewers are and nothing else, and both of the things that do work — the
|
|
6
|
+
* encoding and the downloading — read it and decide for themselves. They do not
|
|
7
|
+
* talk to each other, and neither of them tells this class anything.
|
|
8
|
+
*
|
|
9
|
+
* **Why it has to be published rather than asked for.** The downloading lives
|
|
10
|
+
* in another thread. Until now it took its orders from the reads themselves:
|
|
11
|
+
* every read declared a window around its own head, so fifteen reads declared
|
|
12
|
+
* fifteen windows on a piece store that holds sixteen pieces. Half of all
|
|
13
|
+
* evictions then took a piece a reader had said it wanted, two thirds of reads
|
|
14
|
+
* came back from disk, and what `/stream` handed out stopped being the file's
|
|
15
|
+
* bytes — twenty-two source-parse errors, a segment the player could not
|
|
16
|
+
* append, and an empty picture for six minutes (field 2026-09-05).
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { emptyMap, mapForViewer, mergeMaps, runsOf } from "./PriorityMap.js";
|
|
20
|
+
|
|
21
|
+
export class PriorityOrchestrator {
|
|
22
|
+
/** Where the map goes once it is built. @type {(published: object) => void} */
|
|
23
|
+
#publish;
|
|
24
|
+
|
|
25
|
+
/** The last map published per film and file, so an unchanged one is not resent. */
|
|
26
|
+
#last = new Map();
|
|
27
|
+
|
|
28
|
+
/** The last map BUILT per film and file, for whoever reads instead of being
|
|
29
|
+
* handed it. @type {Map<string, import("./PriorityMap.js").PriorityMap>} */
|
|
30
|
+
#maps = new Map();
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The last map built per OUTPUT, from the viewers of that output alone.
|
|
34
|
+
*
|
|
35
|
+
* The same fact answered at two scopes, because the two things that act on it
|
|
36
|
+
* ask at two scopes and both are right. The swarm is asked for bytes of a
|
|
37
|
+
* FILE, and the picture, a quality step and a soundtrack of one film read the
|
|
38
|
+
* same bytes — so every viewer of any of them wants that file's bytes.
|
|
39
|
+
* Encoders are placed per OUTPUT, and a person watching 480p wants nothing of
|
|
40
|
+
* the 1080p output at all.
|
|
41
|
+
*
|
|
42
|
+
* One map for both was the second authority over encoders. Every output of a
|
|
43
|
+
* film was handed the whole film's map, so the plan wanted an encoder on every
|
|
44
|
+
* one of them; what actually stopped the ones nobody was watching was the
|
|
45
|
+
* session manager killing them by its own judgement — and since a viewer
|
|
46
|
+
* moving between steps also announces itself, the plan started them again on
|
|
47
|
+
* the next pass. Two parties answering "should this encoder exist" by different
|
|
48
|
+
* rules, several times a second.
|
|
49
|
+
*
|
|
50
|
+
* @type {Map<string, import("./PriorityMap.js").PriorityMap>}
|
|
51
|
+
*/
|
|
52
|
+
#byOutput = new Map();
|
|
53
|
+
|
|
54
|
+
/** Who is watching one session. @type {(session: object) => Map<string, object>} */
|
|
55
|
+
#viewersOf;
|
|
56
|
+
|
|
57
|
+
/** How wide the first band of one session's file is. @type {(session: object) => number} */
|
|
58
|
+
#allowanceFor;
|
|
59
|
+
|
|
60
|
+
/** Whether this output is what that person is consuming, as opposed to one
|
|
61
|
+
* they merely hold a record on. @type {(session: object, viewer: object) => boolean} */
|
|
62
|
+
#watchedBy;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* This layer states facts and imports nothing above itself, so what it needs
|
|
66
|
+
* of a session — who is watching it, and how wide an interruption this file
|
|
67
|
+
* has shown on this swarm — is passed in.
|
|
68
|
+
*
|
|
69
|
+
* @param {object} params
|
|
70
|
+
* @param {(published: { sourceKey: string, fileIndex: number, durationSeconds: number,
|
|
71
|
+
* zones: { from: number, to: number, priority: number }[] }) => void} params.publish
|
|
72
|
+
* @param {(session: object) => Map<string, object>} [params.viewersOf]
|
|
73
|
+
* @param {(session: object) => number} [params.allowanceFor]
|
|
74
|
+
* @param {(session: object, viewer: object) => boolean} [params.watchedBy] -
|
|
75
|
+
* Whether this output is the one that person is consuming. Which of a film's
|
|
76
|
+
* outputs a person has on screen is a fact about the film's shape, which
|
|
77
|
+
* this layer does not know; absent, every registered viewer counts, and then
|
|
78
|
+
* the per-output map says the same as the per-file one.
|
|
79
|
+
*/
|
|
80
|
+
constructor({ publish, viewersOf, allowanceFor, watchedBy }) {
|
|
81
|
+
this.#publish = typeof publish === "function" ? publish : () => {};
|
|
82
|
+
this.#viewersOf = typeof viewersOf === "function" ? viewersOf : () => new Map();
|
|
83
|
+
this.#allowanceFor = typeof allowanceFor === "function" ? allowanceFor : () => 0;
|
|
84
|
+
this.#watchedBy = typeof watchedBy === "function" ? watchedBy : () => true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A map from a set of viewers, and the ONE statement of how one is built.
|
|
89
|
+
*
|
|
90
|
+
* Asked at both scopes — once per film for the swarm, once per output for the
|
|
91
|
+
* encoders — and written once, because two copies of how a viewer's map is
|
|
92
|
+
* built is the same two-owners fault this class was split for.
|
|
93
|
+
*
|
|
94
|
+
* @param {object} params
|
|
95
|
+
* @param {number} params.durationSeconds
|
|
96
|
+
* @param {number} params.allowanceSeconds
|
|
97
|
+
* @param {{ atSeconds: number, playing: boolean }[]} params.viewers
|
|
98
|
+
* @returns {import("./PriorityMap.js").PriorityMap} A map of no length where
|
|
99
|
+
* nobody is watching or the film's length is unknown, which says the same as
|
|
100
|
+
* a map with nothing in it.
|
|
101
|
+
*/
|
|
102
|
+
#mapFrom({ durationSeconds, allowanceSeconds, viewers }) {
|
|
103
|
+
if (!(durationSeconds > 0) || !(viewers?.length > 0)) {
|
|
104
|
+
return emptyMap(0);
|
|
105
|
+
}
|
|
106
|
+
return mergeMaps(
|
|
107
|
+
viewers.map((viewer) =>
|
|
108
|
+
mapForViewer({
|
|
109
|
+
atSeconds: viewer.atSeconds,
|
|
110
|
+
durationSeconds,
|
|
111
|
+
allowanceSeconds,
|
|
112
|
+
playing: viewer.playing !== false
|
|
113
|
+
})
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The map for one film, from everyone watching it.
|
|
120
|
+
*
|
|
121
|
+
* @param {object} params
|
|
122
|
+
* @param {string} params.sourceKey
|
|
123
|
+
* @param {number} params.fileIndex
|
|
124
|
+
* @param {number} params.durationSeconds
|
|
125
|
+
* @param {number} params.allowanceSeconds - The measured depth below which an
|
|
126
|
+
* interruption reaches a viewer of this file.
|
|
127
|
+
* @param {{ atSeconds: number, playing: boolean }[]} params.viewers
|
|
128
|
+
* @returns {import("./PriorityMap.js").PriorityMap} One number per second of
|
|
129
|
+
* film, merged over everyone watching it.
|
|
130
|
+
*/
|
|
131
|
+
build({ sourceKey, fileIndex, durationSeconds, allowanceSeconds, viewers }) {
|
|
132
|
+
const map = this.#mapFrom({ durationSeconds, allowanceSeconds, viewers });
|
|
133
|
+
const key = `${sourceKey}:${fileIndex}`;
|
|
134
|
+
this.#maps.set(key, map);
|
|
135
|
+
// Unchanged maps are not republished: the downloading rebuilds what it asks
|
|
136
|
+
// the swarm for on every one, and a viewer sitting still would otherwise
|
|
137
|
+
// make it do that several times a second. Compared as stretches rather than
|
|
138
|
+
// second by second, which is the same comparison over far fewer values.
|
|
139
|
+
const zones = runsOf(map);
|
|
140
|
+
const shape = JSON.stringify(zones);
|
|
141
|
+
if (this.#last.get(key) !== shape) {
|
|
142
|
+
this.#last.set(key, shape);
|
|
143
|
+
this.#publish({ sourceKey, fileIndex, durationSeconds, zones });
|
|
144
|
+
}
|
|
145
|
+
return map;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Build and publish the map for every file anybody is watching.
|
|
150
|
+
*
|
|
151
|
+
* One map per FILE, not per output: the picture, a quality step and a
|
|
152
|
+
* soundtrack of one film are three outputs reading the same bytes, and the
|
|
153
|
+
* swarm is asked for bytes. Viewers of all of them merge into one map.
|
|
154
|
+
*
|
|
155
|
+
* @param {object} params
|
|
156
|
+
* @param {Iterable<object[]>} params.sessionGroups - The live sessions, in
|
|
157
|
+
* whatever grouping the caller holds them; they are regrouped by file here.
|
|
158
|
+
* @param {number} params.staleAfterMs - How long a viewer may be silent and
|
|
159
|
+
* still count as watching.
|
|
160
|
+
* @param {number} [params.now]
|
|
161
|
+
* @returns {void}
|
|
162
|
+
*/
|
|
163
|
+
publishFor({ sessionGroups, staleAfterMs, now = Date.now() }) {
|
|
164
|
+
/** @type {Map<string, { sourceKey: string, fileIndex: number, durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
|
|
165
|
+
const byFile = new Map();
|
|
166
|
+
/** @type {Map<string, { durationSeconds: number, allowanceSeconds: number, viewers: object[] }>} */
|
|
167
|
+
const byOutput = new Map();
|
|
168
|
+
for (const sessions of sessionGroups) {
|
|
169
|
+
for (const session of sessions) {
|
|
170
|
+
const key = `${session.sourceKey}:${session.fileIndex}`;
|
|
171
|
+
const durationSeconds = Number(session.file?.durationSeconds) || 0;
|
|
172
|
+
// The first band is as wide as an interruption this file has actually
|
|
173
|
+
// shown on this swarm, never a chosen number.
|
|
174
|
+
const allowanceSeconds = this.#allowanceFor(session);
|
|
175
|
+
let held = byFile.get(key);
|
|
176
|
+
if (!held) {
|
|
177
|
+
held = {
|
|
178
|
+
sourceKey: session.sourceKey,
|
|
179
|
+
fileIndex: session.fileIndex,
|
|
180
|
+
durationSeconds,
|
|
181
|
+
allowanceSeconds,
|
|
182
|
+
viewers: []
|
|
183
|
+
};
|
|
184
|
+
byFile.set(key, held);
|
|
185
|
+
}
|
|
186
|
+
// Every output anybody holds a session for, whether or not a viewer is
|
|
187
|
+
// consuming it — an output with nobody on it must get a map with
|
|
188
|
+
// nothing in it, which is how the plan is told to stop its encoders.
|
|
189
|
+
// Left out, it would keep the map it had when somebody was watching.
|
|
190
|
+
const address = session.outputKey ?? "";
|
|
191
|
+
let mine = byOutput.get(address);
|
|
192
|
+
if (!mine) {
|
|
193
|
+
mine = { durationSeconds, allowanceSeconds, viewers: [] };
|
|
194
|
+
byOutput.set(address, mine);
|
|
195
|
+
}
|
|
196
|
+
for (const viewer of this.#viewersOf(session).values()) {
|
|
197
|
+
if (!viewer.isPresent(now, staleAfterMs)) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const stated = {
|
|
201
|
+
atSeconds: viewer.positionSeconds() ?? 0,
|
|
202
|
+
// CONSUMING, not merely playing. A page that is not on screen has
|
|
203
|
+
// its timers throttled and asks for nothing, which is exactly what
|
|
204
|
+
// a viewer holding a full cushion looks like; the two are told
|
|
205
|
+
// apart on the page and folded into one question here.
|
|
206
|
+
playing: typeof viewer.consumesFilm === "function"
|
|
207
|
+
? viewer.consumesFilm()
|
|
208
|
+
: viewer.playing !== false
|
|
209
|
+
};
|
|
210
|
+
held.viewers.push(stated);
|
|
211
|
+
if (this.#watchedBy(session, viewer)) {
|
|
212
|
+
mine.viewers.push(stated);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// WHAT THIS PASS SAW IS ALL THERE IS. Everything below is derived from the
|
|
218
|
+
// live sessions, so a file or an output that is not among them is gone —
|
|
219
|
+
// and these maps are the projection of that, never a memory of it. Left to
|
|
220
|
+
// accumulate they were three maps that only grew, which is the shape of half
|
|
221
|
+
// the memory faults recorded in this repository, and `forget` was written
|
|
222
|
+
// for it and called from nowhere.
|
|
223
|
+
for (const key of [...this.#maps.keys()]) {
|
|
224
|
+
if (!byFile.has(key)) {
|
|
225
|
+
this.#maps.delete(key);
|
|
226
|
+
this.#last.delete(key);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
for (const address of [...this.#byOutput.keys()]) {
|
|
230
|
+
if (!byOutput.has(address)) {
|
|
231
|
+
this.#byOutput.delete(address);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
for (const one of byFile.values()) {
|
|
235
|
+
// A file of unknown length cannot be divided into zones, and a file
|
|
236
|
+
// nobody is watching has nothing to be urgent about.
|
|
237
|
+
if (one.durationSeconds > 0 && one.viewers.length > 0) {
|
|
238
|
+
this.build(one);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
for (const [address, one] of byOutput) {
|
|
242
|
+
this.#byOutput.set(address, this.#mapFrom(one));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Nobody is watching this file any more.
|
|
248
|
+
*
|
|
249
|
+
* @param {string} sourceKey
|
|
250
|
+
* @param {number} fileIndex
|
|
251
|
+
*/
|
|
252
|
+
/**
|
|
253
|
+
* The map this class last built for one file.
|
|
254
|
+
*
|
|
255
|
+
* Read by whoever acts on it and cannot be handed it at the moment it is
|
|
256
|
+
* made — the encoding decides per output, and one file has several. It is the
|
|
257
|
+
* SAME map: built once here, from where the viewers are, and neither read
|
|
258
|
+
* changes it.
|
|
259
|
+
*
|
|
260
|
+
* @param {string} sourceKey
|
|
261
|
+
* @param {number} fileIndex
|
|
262
|
+
* @returns {import("./PriorityMap.js").PriorityMap} A map of no length where
|
|
263
|
+
* none was built, which says the same as a map with nothing in it.
|
|
264
|
+
*/
|
|
265
|
+
mapFor(sourceKey, fileIndex) {
|
|
266
|
+
return this.#maps.get(`${sourceKey}:${fileIndex}`) ?? emptyMap(0);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The map for ONE output, from the viewers consuming that output.
|
|
271
|
+
*
|
|
272
|
+
* What the encoding reads. A map of no length says nobody is on this output,
|
|
273
|
+
* which is what makes an encoder on it unwanted — and it is a statement, not
|
|
274
|
+
* an absence: the walk above writes one for every output a session exists
|
|
275
|
+
* for, including the ones everybody has left.
|
|
276
|
+
*
|
|
277
|
+
* @param {string} address
|
|
278
|
+
* @returns {import("./PriorityMap.js").PriorityMap}
|
|
279
|
+
*/
|
|
280
|
+
mapForOutput(address) {
|
|
281
|
+
return this.#byOutput.get(address) ?? emptyMap(0);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
}
|
|
@@ -110,8 +110,47 @@ export function requiredSpeedFrom(waits) {
|
|
|
110
110
|
if (!(medianIntervalSec > 0)) {
|
|
111
111
|
return null;
|
|
112
112
|
}
|
|
113
|
+
// THE SHARE OF ITS TIME THE READING LOST, over the stretch actually observed.
|
|
114
|
+
//
|
|
115
|
+
// The model is unchanged and it was always right: if a fraction `f` of the
|
|
116
|
+
// time is spent not delivering, then producing one second of film takes
|
|
117
|
+
// `1/(1 - f)` seconds, so a step must run that much faster than realtime.
|
|
118
|
+
// What was wrong was the two quantities fed into it — the WORST single
|
|
119
|
+
// interruption divided by the MEDIAN gap between interruptions, a maximum
|
|
120
|
+
// over a median, from two populations that need not be the same events at
|
|
121
|
+
// all. It asks what would happen if the worst interruption recurred at the
|
|
122
|
+
// typical rate, which is a compound case that never occurs, and it divides by
|
|
123
|
+
// a gap that goes to zero whenever interruptions arrive in a burst.
|
|
124
|
+
//
|
|
125
|
+
// Field 2026-09-08: 0.79 s (one jump, on a file already downloaded whole)
|
|
126
|
+
// over 0.01 s (the gaps inside a burst of microsecond waits) gave 158.60x,
|
|
127
|
+
// and the quality budget refused every step against it forty times in one
|
|
128
|
+
// session. The same measurements as a share of time lost give 1.00x, because
|
|
129
|
+
// that is what a reading of a complete file loses.
|
|
130
|
+
//
|
|
131
|
+
// The denominator here cannot vanish: it is the span the interruptions are
|
|
132
|
+
// spread over, which contains them.
|
|
133
|
+
// OVER WHOLE CYCLES, from the first interruption's start to the last one's
|
|
134
|
+
// start. That window holds exactly one running stretch per interruption in
|
|
135
|
+
// it, so the share does not depend on where the window happens to be cut —
|
|
136
|
+
// measured to the last interruption's END it counts one interruption more
|
|
137
|
+
// than it counts running stretches, and the same supply then reads 1.54x or
|
|
138
|
+
// 1.45x according to nothing but the moment the log line was printed.
|
|
139
|
+
const last = interruptions.length - 1;
|
|
140
|
+
const spanSec = (interruptions[last].start - interruptions[0].start) / 1000;
|
|
141
|
+
const lostSec = interruptions
|
|
142
|
+
.slice(0, last)
|
|
143
|
+
.reduce((total, one) => total + (one.end - one.start), 0) / 1000;
|
|
144
|
+
// A span that is all interruption says the supply delivered nothing at all
|
|
145
|
+
// while it was watched. There is no speed that survives that, and saying a
|
|
146
|
+
// huge number is less honest than saying it is not a speed question — so the
|
|
147
|
+
// largest figure any step is ever compared against is stated and named.
|
|
148
|
+
const lostShare = spanSec > 0 ? Math.min(0.99, lostSec / spanSec) : 0;
|
|
113
149
|
return {
|
|
114
|
-
requiredSpeed: 1
|
|
150
|
+
requiredSpeed: 1 / (1 - lostShare),
|
|
151
|
+
lostShare,
|
|
152
|
+
spanSec,
|
|
153
|
+
lostSec,
|
|
115
154
|
worstWaitSec,
|
|
116
155
|
medianIntervalSec,
|
|
117
156
|
// Interruptions, not waits: what the figure is derived from. The two differ
|
|
@@ -243,7 +243,7 @@ const supplyReportedAt = new Map();
|
|
|
243
243
|
* Record one interruption and, at most twice a minute, say what it implies.
|
|
244
244
|
*
|
|
245
245
|
* The two figures are the whole of roadmap item 3: the speed a step must
|
|
246
|
-
* sustain to survive this supply (`1
|
|
246
|
+
* sustain to survive this supply (`1 / (1 - the share of time lost)`), and the
|
|
247
247
|
* smallest buffer that hides an interruption from the viewer. Both are printed
|
|
248
248
|
* before either is USED, so the field says whether the arithmetic describes
|
|
249
249
|
* reality before anything is decided by it.
|
|
@@ -265,7 +265,7 @@ const supplyReportedAt = new Map();
|
|
|
265
265
|
* @param {string} infoHash
|
|
266
266
|
* @param {string} fileName
|
|
267
267
|
* @param {number} segmentSeconds - The session's own segment duration.
|
|
268
|
-
* @returns {{ requiredSpeed: number, worstWaitSec: number, medianIntervalSec: number, samples: number, minimumBufferSec: number } | null}
|
|
268
|
+
* @returns {{ requiredSpeed: number, worstWaitSec: number, medianIntervalSec: number, lostShare: number, spanSec: number, lostSec: number, samples: number, minimumBufferSec: number } | null}
|
|
269
269
|
*/
|
|
270
270
|
export function supplyFiguresFor(infoHash, fileName, segmentSeconds) {
|
|
271
271
|
const history = supplyWaits.get(`${infoHash ?? "?"}/${fileName ?? "?"}`);
|
|
@@ -502,8 +502,13 @@ function noteSupplyWait(key, label, waitedMs) {
|
|
|
502
502
|
// several waits. Saying how many of each is what makes the figure readable;
|
|
503
503
|
// reporting the waits alone made `2 measured` look like two interruptions
|
|
504
504
|
// 3 ms apart, and the demanded speed came out at 4422x.
|
|
505
|
-
|
|
506
|
-
|
|
505
|
+
// THE NUMBERS THE FIGURE IS MADE OF, so a wrong one can be seen to be wrong.
|
|
506
|
+
// The share of time lost is what the speed now comes from; the worst stall
|
|
507
|
+
// and the typical gap are printed beside it because they are what the
|
|
508
|
+
// cushion is sized by and what the old formula divided one by the other.
|
|
509
|
+
`to survive this swarm (lost ${demand.lostSec.toFixed(2)}s of ${demand.spanSec.toFixed(2)}s ` +
|
|
510
|
+
`= ${(demand.lostShare * 100).toFixed(1)}%, worst stall ${demand.worstWaitSec.toFixed(2)}s, ` +
|
|
511
|
+
`one every ${demand.medianIntervalSec.toFixed(2)}s of running, ${demand.samples} stall(s) ` +
|
|
507
512
|
`from ${demand.waits} wait(s)) — ` +
|
|
508
513
|
`and the smallest buffer that hides it is ${buffer ? buffer.seconds.toFixed(1) : "?"}s` +
|
|
509
514
|
// What steering the blocked piece onto another peer bought, as the
|