@torrent-tv/proxy 2.80.12 → 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 +19 -0
- package/docs/download-architecture.md +65 -0
- package/docs/encode-architecture.md +52 -0
- package/package.json +1 -1
- package/routes/api/transcode-sessions/net-report/post.js +64 -53
- package/routes/stream/get.js +324 -300
- package/routes/transcode/session-file/get.js +22 -2
- package/services/download/SwarmSelection.js +20 -4
- package/services/encode/SegmentDemand.js +34 -0
- package/services/encode/SegmentStore.js +743 -718
- package/services/hls-session-manager.js +22 -29
- package/services/orchestrators/EncodeOrchestrator.js +735 -726
- 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/priority/WaitLedger.js +142 -0
- package/services/supply-margin.js +40 -1
- package/services/torrent-pool.js +38 -1
- package/services/torrent-worker/client.js +27 -12
- 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
- package/test/wait-ledger.test.js +100 -0
|
@@ -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
|
+
}
|