@torrent-tv/proxy 2.90.0 → 2.92.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.
- package/bin/cli.js +5 -2
- package/package.json +18 -2
- package/routes/api/playback-plan/post.js +21 -4
- package/routes/api/sources/files/get.js +20 -28
- package/routes/api/sources/post.js +4 -2
- package/routes/api/sources/stats/get.js +4 -11
- package/routes/api/sources/viewer/post.js +16 -0
- package/routes/api/sources/warm/post.js +6 -215
- package/routes/api/subtitles/get.js +22 -125
- package/routes/api/subtitles/post.js +26 -0
- package/routes/api/transcode-sessions/post.js +24 -2
- package/routes/media/get.js +80 -0
- package/routes/stream/get.js +26 -94
- package/routes/transcode/audio-warm/get.js +9 -21
- package/routes/transcode/variant-warm/get.js +8 -14
- package/server.js +410 -46
- package/services/encode/AdmittedInput.js +136 -0
- package/services/encode/CushionReport.js +1 -76
- package/services/encode/EncodeInputs.js +142 -0
- package/services/encode/EncodeOrchestrator.js +4 -1
- package/services/encode/EncodeRun.js +13 -4
- package/services/encode/EncodeRuns.js +132 -52
- package/services/encode/InputFailures.js +37 -0
- package/services/encode/MatroskaInput.js +158 -0
- package/services/encode/OutputOpening.js +16 -64
- package/services/encode/Renditions.js +1 -17
- package/services/encode/admitted-command.js +60 -0
- package/services/encode/annex-b-input.js +37 -0
- package/services/encode/bit-payload.js +29 -0
- package/services/encode/output/OutputCatalog.js +1 -1
- package/services/encode/output/Timeline.js +11 -0
- package/services/encode/pcm-normalization.js +30 -0
- package/services/encode/piece-completeness.js +54 -1
- package/services/encode/quality/HostLoad.js +19 -73
- package/services/encode/quality/QualityOffer.js +1 -1
- package/services/encode/run-command.js +1 -589
- package/services/encode/segment-formats/mp4-boxes.js +3 -1
- package/services/encode/segment-formats/presentation-segment.js +114 -0
- package/services/media/ContainerOrchestrator.js +118 -48
- package/services/media/KeyframeTables.js +16 -115
- package/services/media/MediaReadRequests.js +120 -0
- package/services/media/ProbeRequests.js +135 -0
- package/services/media/SegmentInputs.js +48 -0
- package/services/media/SourceFile.js +6 -0
- package/services/media/SubtitleOrchestrator.js +190 -4
- package/services/media/await-plan.js +47 -0
- package/services/media/container/AsfContainer.js +217 -0
- package/services/media/container/AviContainer.js +343 -34
- package/services/media/container/CadencedPictureOrder.js +45 -0
- package/services/media/container/Container.js +7 -3
- package/services/media/container/ContainerFactory.js +10 -3
- package/services/media/container/FfprobeContainer.js +222 -0
- package/services/media/container/MatroskaContainer.js +72 -46
- package/services/media/container/Mp4Container.js +472 -139
- package/services/media/container/MpegPsContainer.js +281 -0
- package/services/media/container/MpegTsContainer.js +277 -0
- package/services/media/container/PacketIndex.js +270 -0
- package/services/media/container/PacketRecords.js +237 -0
- package/services/media/container/RetainedBytes.js +39 -0
- package/services/media/container/RetainedReads.js +51 -0
- package/services/media/container/ac3-frame.js +46 -0
- package/services/media/container/asf-packets.js +153 -0
- package/services/media/container/avc-picture-order.js +50 -0
- package/services/media/container/avi-open-dml.js +47 -0
- package/services/media/container/dts-frame.js +51 -0
- package/services/media/container/ebml-stream.js +12 -1
- package/services/media/container/ffprobe-record.js +45 -0
- package/services/media/container/flv-packet-ranges.js +27 -0
- package/services/media/container/following-packet-time.js +37 -0
- package/services/media/container/h264-configuration.js +183 -0
- package/services/media/container/h264-elementary-index.js +245 -0
- package/services/media/container/hevc-configuration.js +165 -0
- package/services/media/container/hevc-elementary-index.js +132 -0
- package/services/media/container/hevc-picture-order.js +66 -0
- package/services/media/container/latm-frame.js +99 -0
- package/services/media/container/matroska-packets.js +207 -0
- package/services/media/container/memory-unavailable.js +8 -0
- package/services/media/container/mp3-packet-dependencies.js +41 -0
- package/services/media/container/mp4-fragments.js +148 -0
- package/services/media/container/mpeg-audio-frame.js +19 -0
- package/services/media/container/mpeg-audio-reservoir.js +27 -0
- package/services/media/container/mpeg-elementary-index.js +196 -0
- package/services/media/container/mpeg-sequence-reader.js +69 -0
- package/services/media/container/mpeg-ts-packets.js +93 -0
- package/services/media/container/mpeg-video-configuration.js +50 -0
- package/services/media/container/mpeg4-picture-timing.js +108 -0
- package/services/media/container/pes-header.js +34 -0
- package/services/media/playback-declarations.js +57 -0
- package/services/media/playback-planner.js +72 -230
- package/services/media/probe-packets.js +61 -0
- package/services/media/read-playback-declarations.js +18 -0
- package/services/media/tracks/AudioTrack.js +9 -0
- package/services/media/tracks/TextSubtitleTrack.js +4 -1
- package/services/server/OutputLifecycle.js +2 -0
- package/services/server/SegmentServing.js +40 -33
- package/services/server/ViewerRequests.js +22 -41
- package/services/server/controllers/PlaybackController.js +4 -0
- package/services/server/controllers/SubtitleController.js +8 -29
- package/services/server/transcode-session-files.js +58 -99
- package/services/server/wire-outputs.js +58 -15
- package/services/storage/IndexMemory.js +85 -0
- package/services/storage/MachineBudget.js +27 -3
- package/services/storage/allowance.js +8 -1
- package/services/storage/piece-store/piece-disk-store.js +18 -3
- package/services/storage/piece-store/piece-lru.js +8 -4
- package/services/storage/piece-store/shared-piece-store.js +26 -11
- package/services/storage/segment-store/SegmentStore.js +49 -18
- package/services/storage/wire.js +7 -0
- package/services/torrent/Contents.js +52 -20
- package/services/torrent/demand/DemandRegister.js +13 -4
- package/services/torrent/demand/Window.js +13 -2
- package/services/torrent/demand-progress.js +25 -0
- package/services/torrent/download/DeadlineScheduler.js +167 -0
- package/services/torrent/download/FutureDownload.js +166 -0
- package/services/torrent/download/SwarmSelection.js +30 -59
- package/services/torrent/download/registry.js +45 -5
- package/services/torrent/download/withdraw-claim.js +2 -0
- package/services/torrent/torrent-pool.js +39 -600
- package/services/torrent/worker/channel.js +8 -1
- package/services/torrent/worker/client.js +74 -72
- package/services/torrent/worker/files-in-use.js +9 -0
- package/services/torrent/worker/handle-state.js +12 -0
- package/services/torrent/worker/held-bytes.js +82 -99
- package/services/torrent/worker/held-file-bytes.js +17 -0
- package/services/torrent/worker/piece-reader.js +67 -683
- package/services/torrent/worker/pool-adapter.js +37 -123
- package/services/torrent/worker/protocol.js +6 -17
- package/services/torrent/worker/whole-file-stats.js +2 -0
- package/services/torrent/worker/worker.js +86 -156
- package/services/transport/data-channel-handler.js +56 -13
- package/services/transport/tunnel-client.js +111 -20
- package/services/viewer/DownloadMaps.js +168 -0
- package/services/viewer/NativeSourceMap.js +33 -0
- package/services/viewer/PriorityMap.js +49 -40
- package/services/viewer/PriorityOrchestrator.js +42 -13
- package/services/viewer/SourcePreparation.js +278 -0
- package/services/viewer/Viewer.js +11 -0
- package/services/viewer/Viewers.js +109 -2
- package/services/viewer/playback-readiness.js +50 -63
- package/services/viewer/segment-demands.js +37 -0
- package/services/viewer/urgent-output-ready.js +26 -0
- package/services/encode/run-inputs.js +0 -53
- package/services/media/keyframe-probe.js +0 -157
- package/services/media/media-info-probe.js +0 -93
- package/services/torrent/worker/background-fill.js +0 -213
- package/services/torrent/worker/resume-warm.js +0 -107
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Fetch a whole file that nobody is playing yet, using only the room the viewer
|
|
3
|
-
* is not using.
|
|
4
|
-
*
|
|
5
|
-
* WHAT IT IS FOR. A release ships its dub and its subtitles as separate files,
|
|
6
|
-
* and a viewer who switches to one waits for the swarm to deliver its first
|
|
7
|
-
* pieces — 27.7 s in the field on 2026-08-31, which is longer than the switch is
|
|
8
|
-
* willing to wait. These files are small beside the picture (30 MB against 566
|
|
9
|
-
* MB, about a twentieth), so having them on disk before anyone asks turns every
|
|
10
|
-
* later switch into a local read.
|
|
11
|
-
*
|
|
12
|
-
* THE ORDERING, WHICH IS THE WHOLE DESIGN. What plays now comes first: the
|
|
13
|
-
* picture at the playhead, the soundtrack being heard, the subtitles being
|
|
14
|
-
* shown. The other soundtracks and subtitle files come next. Reading the film
|
|
15
|
-
* far ahead comes last. This module implements the middle tier, and it stays
|
|
16
|
-
* below the first by a condition that is measured rather than chosen: it fetches
|
|
17
|
-
* only while NO reader on the torrent is inside a wait. A reader blocked on a
|
|
18
|
-
* piece is the viewer's own reading starving, and that is exactly the moment
|
|
19
|
-
* this must not be asking the swarm for anything.
|
|
20
|
-
*
|
|
21
|
-
* WHY IT IS A READ AND NOT A SELECTION. `file.select()` claims every piece of a
|
|
22
|
-
* file at once. `#syncSelections` in `torrent/torrent-pool.js` records what that cost
|
|
23
|
-
* when it was done alongside the readers' own moving windows: a claim covering
|
|
24
|
-
* everything always outranked the window, and a seek to 89.1 % of a 4.7 GB film
|
|
25
|
-
* waited 93 s while the swarm fetched 2.47 GB in file order. So this walks the
|
|
26
|
-
* file a piece at a time through an ordinary bounded read, which claims what it
|
|
27
|
-
* is reading and gives it back.
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import { logger } from "../../../utils/logger.js";
|
|
31
|
-
import { readersAreBlockedOn, stallsSeenOn } from "./piece-reader.js";
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* How long to stand aside after finding the viewer's own reading blocked.
|
|
35
|
-
*
|
|
36
|
-
* Not a measurement of anything: it is how often the question "is the viewer
|
|
37
|
-
* still starving?" is asked, and it is answered by the reader count, which is
|
|
38
|
-
* exact. Short enough that room is used soon after it appears, long enough that
|
|
39
|
-
* asking costs nothing.
|
|
40
|
-
*/
|
|
41
|
-
const STAND_ASIDE_MS = 1_000;
|
|
42
|
-
|
|
43
|
-
/** Files being filled, by `sourceKey:fileIndex`, so one runs per file. */
|
|
44
|
-
const running = new Map();
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @param {number} ms
|
|
48
|
-
* @returns {Promise<void>}
|
|
49
|
-
*/
|
|
50
|
-
function pause(ms) {
|
|
51
|
-
return new Promise((resolve) => {
|
|
52
|
-
const timer = setTimeout(resolve, ms);
|
|
53
|
-
timer.unref?.();
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Read one byte range, letting the torrent fetch what is missing.
|
|
59
|
-
*
|
|
60
|
-
* @param {object} file
|
|
61
|
-
* @param {number} start
|
|
62
|
-
* @param {number} end - Inclusive.
|
|
63
|
-
* @returns {Promise<number>} Bytes read; 0 on failure.
|
|
64
|
-
*/
|
|
65
|
-
function readRange(file, start, end) {
|
|
66
|
-
return new Promise((resolve) => {
|
|
67
|
-
let stream;
|
|
68
|
-
try {
|
|
69
|
-
stream = file.createReadStream({ start, end });
|
|
70
|
-
} catch {
|
|
71
|
-
resolve(0);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
let bytes = 0;
|
|
75
|
-
let settled = false;
|
|
76
|
-
const settle = (value) => {
|
|
77
|
-
if (settled) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
settled = true;
|
|
81
|
-
resolve(value);
|
|
82
|
-
};
|
|
83
|
-
stream.on("data", (chunk) => {
|
|
84
|
-
bytes += chunk.length;
|
|
85
|
-
});
|
|
86
|
-
stream.on("end", () => settle(bytes));
|
|
87
|
-
stream.on("error", () => {
|
|
88
|
-
stream.destroy?.();
|
|
89
|
-
settle(0);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Fetch a file whole, in the room the viewer leaves.
|
|
96
|
-
*
|
|
97
|
-
* Returns as soon as the work is under way; the caller is not waiting for it.
|
|
98
|
-
* One fill per file — a second request while one is running is ignored rather
|
|
99
|
-
* than doubling the reads.
|
|
100
|
-
*
|
|
101
|
-
* @param {object} torrent
|
|
102
|
-
* @param {number} fileIndex
|
|
103
|
-
* @param {string} sourceKey
|
|
104
|
-
* @param {{ chunkBytes?: number, isBlocked?: (infoHash: string) => boolean, stallsSeen?: (infoHash: string) => number }} [options]
|
|
105
|
-
* `isBlocked` answers "is the viewer's own reading starving right now"; it is
|
|
106
|
-
* the tier boundary, and it is a parameter so it can be exercised without a
|
|
107
|
-
* swarm.
|
|
108
|
-
* @returns {boolean} Whether a fill was started by this call.
|
|
109
|
-
*/
|
|
110
|
-
export function fillFileInBackground(torrent, fileIndex, sourceKey, options = {}) {
|
|
111
|
-
const key = `${sourceKey}:${fileIndex}`;
|
|
112
|
-
if (running.has(key)) {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
const file = torrent?.files?.[fileIndex];
|
|
116
|
-
if (!file || !Number.isFinite(file.length) || file.length <= 0) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
// One piece at a time: the smallest unit the swarm actually delivers, so the
|
|
120
|
-
// gap between two checks of "is the viewer starving?" is as short as it can
|
|
121
|
-
// usefully be.
|
|
122
|
-
const chunkBytes = Number.isFinite(options.chunkBytes) && options.chunkBytes > 0
|
|
123
|
-
? options.chunkBytes
|
|
124
|
-
: (Number(torrent?.pieceLength) || 4 * 1024 * 1024);
|
|
125
|
-
const isBlocked = typeof options.isBlocked === "function" ? options.isBlocked : readersAreBlockedOn;
|
|
126
|
-
const stallsSeen = typeof options.stallsSeen === "function" ? options.stallsSeen : stallsSeenOn;
|
|
127
|
-
const work = fill(torrent, file, fileIndex, chunkBytes, isBlocked, stallsSeen).finally(() => {
|
|
128
|
-
running.delete(key);
|
|
129
|
-
});
|
|
130
|
-
running.set(key, work);
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* @param {object} torrent
|
|
136
|
-
* @param {object} file
|
|
137
|
-
* @param {number} fileIndex
|
|
138
|
-
* @param {number} chunkBytes
|
|
139
|
-
* @param {(infoHash: string) => boolean} isBlocked
|
|
140
|
-
* @param {(infoHash: string) => number} stallsSeen
|
|
141
|
-
* @returns {Promise<void>}
|
|
142
|
-
*/
|
|
143
|
-
async function fill(torrent, file, fileIndex, chunkBytes, isBlocked, stallsSeen) {
|
|
144
|
-
const startedAt = Date.now();
|
|
145
|
-
const infoHash = String(torrent?.infoHash ?? "");
|
|
146
|
-
let read = 0;
|
|
147
|
-
let stoodAsideMs = 0;
|
|
148
|
-
// The stall count this fill last saw. A chunk is fetched only when it has not
|
|
149
|
-
// moved since the previous one.
|
|
150
|
-
let quietSince = stallsSeen(infoHash);
|
|
151
|
-
logger.info(
|
|
152
|
-
`background-fill: "${String(file.name).slice(0, 40)}" (${(file.length / 1e6).toFixed(1)}MB) will be ` +
|
|
153
|
-
"fetched whole while the viewer's own reading leaves room"
|
|
154
|
-
);
|
|
155
|
-
for (let start = 0; start < file.length; start += chunkBytes) {
|
|
156
|
-
// Stand aside while anything the viewer is watching is waiting on the
|
|
157
|
-
// swarm — AND for as long after it as it takes for a quiet stretch to
|
|
158
|
-
// pass. Pausing only DURING a stall is not enough: on a swarm delivering
|
|
159
|
-
// exactly what the film needs, this still takes bandwidth between stalls,
|
|
160
|
-
// and the stalls themselves are the proof there was none to spare. Field
|
|
161
|
-
// 2026-08-31, the case that forced this: 200-600 KB/s delivered against
|
|
162
|
-
// the 399 KB/s the film eats, one piece waited 101 s, and the picture
|
|
163
|
-
// stood still 145.6 s.
|
|
164
|
-
//
|
|
165
|
-
// "A quiet stretch" is measured, not chosen: the stall counter must not
|
|
166
|
-
// have moved while the previous chunk was being fetched. On a starving
|
|
167
|
-
// swarm it moves constantly and this stops altogether, which is the right
|
|
168
|
-
// answer — there is no spare room to use.
|
|
169
|
-
while (isBlocked(infoHash) || stallsSeen(infoHash) !== quietSince) {
|
|
170
|
-
stoodAsideMs += STAND_ASIDE_MS;
|
|
171
|
-
await pause(STAND_ASIDE_MS);
|
|
172
|
-
// Re-baselined after the pause, so a stretch that passes without a new
|
|
173
|
-
// stall lets the fill go on. Without this it could never resume.
|
|
174
|
-
quietSince = stallsSeen(infoHash);
|
|
175
|
-
}
|
|
176
|
-
// The torrent may have been destroyed under us — a viewer who left, the
|
|
177
|
-
// disk sweep, a restart. Reading a destroyed file throws, and there is
|
|
178
|
-
// nothing here worth an error.
|
|
179
|
-
if (!torrent?.files?.[fileIndex]) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
// Taken BEFORE the read, and deliberately not refreshed after it: a stall
|
|
183
|
-
// that happens while this chunk is in flight must still be visible to the
|
|
184
|
-
// next iteration. Refreshing afterwards erased exactly that evidence, which
|
|
185
|
-
// is the defect a test caught here.
|
|
186
|
-
quietSince = stallsSeen(infoHash);
|
|
187
|
-
const bytes = await readRange(file, start, Math.min(start + chunkBytes, file.length) - 1);
|
|
188
|
-
if (bytes === 0) {
|
|
189
|
-
logger.info(
|
|
190
|
-
`background-fill: "${String(file.name).slice(0, 40)}" stopped at ` +
|
|
191
|
-
`${(start / 1e6).toFixed(1)}MB — the read returned nothing`
|
|
192
|
-
);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
read += bytes;
|
|
196
|
-
}
|
|
197
|
-
logger.info(
|
|
198
|
-
`background-fill: "${String(file.name).slice(0, 40)}" is on disk — ${(read / 1e6).toFixed(1)}MB in ` +
|
|
199
|
-
`${((Date.now() - startedAt) / 1000).toFixed(0)}s, of which ${(stoodAsideMs / 1000).toFixed(0)}s ` +
|
|
200
|
-
"was spent standing aside for the viewer; a switch to it will not wait for the swarm"
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Whether a file is being filled right now. For tests and for the log.
|
|
206
|
-
*
|
|
207
|
-
* @param {string} sourceKey
|
|
208
|
-
* @param {number} fileIndex
|
|
209
|
-
* @returns {boolean}
|
|
210
|
-
*/
|
|
211
|
-
export function fillIsRunning(sourceKey, fileIndex) {
|
|
212
|
-
return running.has(`${sourceKey}:${fileIndex}`);
|
|
213
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Fetching the region under a viewer's resume position.
|
|
3
|
-
*
|
|
4
|
-
* All that is left of a file that used to read containers here as well. The
|
|
5
|
-
* parsing moved to the main thread on 2026-09-15: a container is built from one
|
|
6
|
-
* function, `readRange(start, end)`, the pieces behind it live in shared memory,
|
|
7
|
-
* and the reason it had been put here — "the main thread cannot open a read
|
|
8
|
-
* stream on one of its files" — was true of WebTorrent's own API and not of the
|
|
9
|
-
* bytes. Three commands, a second `ContainerOrchestrator` in this thread, and
|
|
10
|
-
* every answer carried back over the channel went with it.
|
|
11
|
-
*
|
|
12
|
-
* What stays is the part that really is the torrent's: turning a position in
|
|
13
|
-
* seconds into a byte offset and pulling that region off the swarm. How long
|
|
14
|
-
* the file runs is TOLD to it, because that is what the file states about
|
|
15
|
-
* itself and this thread no longer reads such things.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { logger } from "../../../utils/logger.js";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* How much of the file to pull in under the viewer's resume position.
|
|
31
|
-
*
|
|
32
|
-
* One piece of a video torrent is 4-16 MB and a resume lands anywhere inside
|
|
33
|
-
* one, so anything smaller would still leave the encoder waiting for the piece
|
|
34
|
-
* it starts in. Eight megabytes covers that piece and usually the next.
|
|
35
|
-
*/
|
|
36
|
-
const RESUME_REGION_BYTES = 8 * 1024 * 1024;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Where a position in seconds falls in a file, in bytes.
|
|
40
|
-
*
|
|
41
|
-
* Proportional, and therefore approximate on a variable bitrate — which is what
|
|
42
|
-
* it is for: a prefetch that puts the swarm to work on roughly the right place
|
|
43
|
-
* while the plan and the session are still being built. The encoder's own read
|
|
44
|
-
* asks for the exact bytes a moment later and corrects it.
|
|
45
|
-
*
|
|
46
|
-
* A position past the end is clamped to the end rather than refused: a resume
|
|
47
|
-
* position can outlive the file it was recorded against, and reading the last
|
|
48
|
-
* bytes is harmless where reading past them is an error.
|
|
49
|
-
*
|
|
50
|
-
* @param {number} fileLength
|
|
51
|
-
* @param {number} durationSeconds
|
|
52
|
-
* @param {number} positionSeconds
|
|
53
|
-
* @returns {number}
|
|
54
|
-
*/
|
|
55
|
-
export function resumeByteOffset(fileLength, durationSeconds, positionSeconds) {
|
|
56
|
-
if (!(fileLength > 0) || !(durationSeconds > 0) || !(positionSeconds > 0)) {
|
|
57
|
-
return 0;
|
|
58
|
-
}
|
|
59
|
-
const within = Math.min(positionSeconds, durationSeconds);
|
|
60
|
-
return Math.min(fileLength - 1, Math.floor((fileLength * within) / durationSeconds));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Start fetching the region a viewer is about to resume at.
|
|
65
|
-
*
|
|
66
|
-
* Where that region IS can only be worked out from two numbers the file itself
|
|
67
|
-
* holds — its length and its duration — so this belongs beside the container
|
|
68
|
-
* read rather than in the route: the route knows a position in seconds and
|
|
69
|
-
* nothing else. The conversion is proportional and therefore approximate on a
|
|
70
|
-
* variable bitrate; it is a prefetch, and the encoder's own read corrects it.
|
|
71
|
-
*
|
|
72
|
-
* @param {object} torrent
|
|
73
|
-
* @param {number} fileIndex
|
|
74
|
-
* @param {string} sourceKey
|
|
75
|
-
* @param {number} positionSeconds
|
|
76
|
-
* @param {{ prefetchEdges?: () => Promise<unknown>, fetchRegion?: (start: number, bytes: number) => Promise<unknown> }} options
|
|
77
|
-
* @returns {Promise<boolean>} Whether a region was asked for.
|
|
78
|
-
*/
|
|
79
|
-
export async function warmResumePosition(torrent, fileIndex, sourceKey, positionSeconds, options = {}) {
|
|
80
|
-
const file = torrent?.files?.[fileIndex];
|
|
81
|
-
if (!file || !(positionSeconds > 0) || typeof options.fetchRegion !== "function") {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
// TOLD, not worked out here. How long a file runs is what the file states
|
|
85
|
-
// about itself, and that is read on the main thread now; this one turns a
|
|
86
|
-
// position into a byte offset and fetches the region under it, which is all
|
|
87
|
-
// of the job that is actually the torrent's.
|
|
88
|
-
const duration = Number(options.durationSeconds);
|
|
89
|
-
if (!Number.isFinite(duration) || duration <= 0) {
|
|
90
|
-
logger.info(
|
|
91
|
-
`warm ${sourceKey.slice(0, 8)}: "${String(file.name).slice(0, 40)}" does not declare its ` +
|
|
92
|
-
"duration, so where the viewer's position falls in it cannot be worked out — " +
|
|
93
|
-
"the region under it is left to the encoder's own read"
|
|
94
|
-
);
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
const at = resumeByteOffset(file.length, duration, positionSeconds);
|
|
98
|
-
logger.info(
|
|
99
|
-
`warm ${sourceKey.slice(0, 8)}: fetching ${(RESUME_REGION_BYTES / (1024 * 1024)).toFixed(0)}MB under the ` +
|
|
100
|
-
`viewer's position ${positionSeconds.toFixed(1)}s of ${duration.toFixed(1)}s, which is ` +
|
|
101
|
-
`${(at / (1024 * 1024)).toFixed(1)}MB into "${String(file.name).slice(0, 40)}"`
|
|
102
|
-
);
|
|
103
|
-
await options.fetchRegion(at, RESUME_REGION_BYTES);
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|