@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
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* exhausting the continuous media available for the required tracks.
|
|
4
4
|
*
|
|
5
5
|
* This module contains the playback decision's only model. Its inputs are
|
|
6
|
-
* measurements and physical limits:
|
|
6
|
+
* measurements and physical limits: mapped whole-piece arrivals, encoded
|
|
7
7
|
* media and encoder rate, segment sizes and client-link rate, existing
|
|
8
8
|
* continuous media, and measured supply interruptions. Each service integral
|
|
9
9
|
* extends its latest measured rate until the next observation. The forecast
|
|
@@ -99,8 +99,8 @@ export class RateTrend {
|
|
|
99
99
|
* @param {number} input.lookaheadSeconds
|
|
100
100
|
* @param {number} input.now
|
|
101
101
|
* @param {boolean} [input.requiredAudio]
|
|
102
|
-
* @param {Array<{ id: string,
|
|
103
|
-
*
|
|
102
|
+
* @param {Array<{ id: string, complete: boolean,
|
|
103
|
+
* downloadForecast?: { ranges: Array<{ start: number, end: number, availableAt: number | null }> } }>} input.sources
|
|
104
104
|
* @param {Array<{ id: string, sourceIds: string[], processedSeconds: number,
|
|
105
105
|
* bitsPerMediaSecond: number, readings: Array<{ at: number, value: number }>,
|
|
106
106
|
* clockOffsetSeconds?: number, clientRanges?: Array<{ start: number, end: number }>,
|
|
@@ -137,10 +137,10 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
137
137
|
]))
|
|
138
138
|
]);
|
|
139
139
|
const zero = mediaTime(0n, unit);
|
|
140
|
-
const segmentsOf = tracks.map((track, trackIndex) =>
|
|
140
|
+
const segmentsOf = tracks.map((track, trackIndex) => predictedMediaSegments(
|
|
141
141
|
(Array.isArray(track.segments) ? track.segments : []).map((segment) => ({
|
|
142
142
|
...segment, ranges: playerRanges(segment.mediaRanges, rescale(clocks[trackIndex], unit), unit)
|
|
143
|
-
})));
|
|
143
|
+
})), unit));
|
|
144
144
|
const origin = latest(zero, ...segmentsOf.map((segments) =>
|
|
145
145
|
segments.find(({ index }) => index === 0)?.ranges?.[0]?.start ?? zero));
|
|
146
146
|
const at = latest(origin, rescale(fromSeconds(finiteNonNegative(input.positionSeconds)), unit));
|
|
@@ -182,7 +182,7 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
182
182
|
curve: rateCurve(track.readings, now),
|
|
183
183
|
bitsPerMediaSecond: averageBitsPerMediaSecond(track, track.segments,
|
|
184
184
|
new Set(Array.isArray(track.readySegmentIndices) ? track.readySegmentIndices : [])),
|
|
185
|
-
segments:
|
|
185
|
+
segments: segmentsOf[trackIndex]
|
|
186
186
|
.filter((segment) => Number.isInteger(segment?.index) &&
|
|
187
187
|
Number.isFinite(segment?.startSeconds) && Number.isFinite(segment?.endSeconds) &&
|
|
188
188
|
segment.endSeconds > segment.startSeconds && segment.endSeconds > position)
|
|
@@ -192,19 +192,8 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
192
192
|
return result(false, null, buffered, reserve, null, "timeline-unavailable", 0);
|
|
193
193
|
}
|
|
194
194
|
const sourceState = new Map();
|
|
195
|
-
const downloadServices = new Map();
|
|
196
195
|
for (const [id, source] of sources) {
|
|
197
|
-
|
|
198
|
-
const service = downloadServices.get(serviceId);
|
|
199
|
-
if (!service || (!service.curve && source.complete !== true)) {
|
|
200
|
-
downloadServices.set(serviceId, {
|
|
201
|
-
curve: source.complete === true ? null : rateCurve(source.readings, now)
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
sourceState.set(id, {
|
|
205
|
-
source,
|
|
206
|
-
serviceId
|
|
207
|
-
});
|
|
196
|
+
sourceState.set(id, { source });
|
|
208
197
|
}
|
|
209
198
|
const link = rateCurve(input.linkReadings, now);
|
|
210
199
|
let unknownReason = null;
|
|
@@ -246,19 +235,28 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
246
235
|
Number.isFinite(range?.start) && Number.isFinite(range?.end) && range.end > range.start));
|
|
247
236
|
const encodeWork = produced ? 0 : remainingProcessing.reduce((sum, part) => sum + part.end - part.start, 0);
|
|
248
237
|
|
|
249
|
-
const
|
|
238
|
+
const sourceByteIntervals = new Map();
|
|
250
239
|
if (!produced && encodeWork > 0) {
|
|
251
|
-
|
|
240
|
+
const exactInputs = Array.isArray(segment.sourceInputs) ? segment.sourceInputs : null;
|
|
241
|
+
const requiredSources = new Set([
|
|
242
|
+
...(Array.isArray(track.sourceIds) ? track.sourceIds : []),
|
|
243
|
+
...(exactInputs ?? []).map(input => input?.sourceId)
|
|
244
|
+
]);
|
|
245
|
+
for (const sourceId of requiredSources) {
|
|
252
246
|
const source = sourceState.get(sourceId);
|
|
253
247
|
if (!source) {
|
|
254
248
|
unknownReason ??= "source-measurement-unavailable";
|
|
255
249
|
} else if (source.source.complete !== true) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
250
|
+
if (exactInputs) {
|
|
251
|
+
const inputs = exactInputs.filter(input => input?.sourceId === sourceId);
|
|
252
|
+
const ranges = inputs.flatMap(input => Array.isArray(input.ranges) ? input.ranges : []);
|
|
253
|
+
if (!ranges.length || inputs.some(input => !Array.isArray(input.ranges) || !input.ranges.length) ||
|
|
254
|
+
ranges.some(range => !range || !Number.isSafeInteger(range.start) || !Number.isSafeInteger(range.end) || range.start < 0 || range.end <= range.start)) {
|
|
255
|
+
unknownReason ??= "source-input-ranges-unavailable";
|
|
256
|
+
} else sourceByteIntervals.set(sourceId, ranges);
|
|
257
|
+
continue;
|
|
261
258
|
}
|
|
259
|
+
unknownReason ??= "source-input-ranges-unavailable";
|
|
262
260
|
}
|
|
263
261
|
}
|
|
264
262
|
}
|
|
@@ -272,8 +270,7 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
272
270
|
ahead: Math.max(0, segment.startSeconds - position),
|
|
273
271
|
sizeBits,
|
|
274
272
|
encodeWork,
|
|
275
|
-
|
|
276
|
-
sourceIntervals,
|
|
273
|
+
sourceByteIntervals,
|
|
277
274
|
produced
|
|
278
275
|
});
|
|
279
276
|
}
|
|
@@ -295,17 +292,20 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
295
292
|
// scheduled in media order, independently of browser storage capacity.
|
|
296
293
|
// The earliest legal start is the supremum of completion lateness against
|
|
297
294
|
// presentation deadlines, not a search over guessed delay candidates.
|
|
298
|
-
const
|
|
299
|
-
const sourceMediaService = new Map();
|
|
295
|
+
const sourceByteService = new Map();
|
|
300
296
|
for (const [id, { source }] of sourceState) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
.
|
|
297
|
+
sourceByteService.set(id, Array.isArray(source.residence) ? source.residence
|
|
298
|
+
.filter(({ location, start, end }) => location !== "missing" && Number.isFinite(start) && Number.isFinite(end) && end > start)
|
|
299
|
+
.map(({ start, end }) => ({ start, end, begin: 0, finish: 0 })) : []);
|
|
300
|
+
if (source.downloadForecast) {
|
|
301
|
+
for (const range of source.downloadForecast.ranges ?? []) {
|
|
302
|
+
if (!Number.isFinite(range?.availableAt) || !Number.isSafeInteger(range.start) ||
|
|
303
|
+
!Number.isSafeInteger(range.end) || range.start < 0 || range.end <= range.start) continue;
|
|
304
|
+
const arrival = Math.max(0, (range.availableAt - now) / 1000);
|
|
305
|
+
// The entire protocol piece becomes readable after verification;
|
|
306
|
+
// bytes inside it never arrive proportionally to their file address.
|
|
307
|
+
sourceByteService.get(id).push({ start: range.start, end: range.end, begin: arrival, finish: arrival });
|
|
308
|
+
}
|
|
309
309
|
}
|
|
310
310
|
}
|
|
311
311
|
const trackFinish = trackState.map(({ track }) => finiteNonNegative(track.startupRemainingSeconds));
|
|
@@ -313,36 +313,23 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
313
313
|
const completions = [];
|
|
314
314
|
const productions = [];
|
|
315
315
|
for (const job of jobs) {
|
|
316
|
-
const { track, trackIndex, segment, sizeBits, encodeWork,
|
|
316
|
+
const { track, trackIndex, segment, sizeBits, encodeWork, sourceByteIntervals } = job;
|
|
317
317
|
const sourceArrivals = [];
|
|
318
|
-
for (const [sourceId,
|
|
319
|
-
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
const begin = sourceFinish.get(source.serviceId) ?? 0;
|
|
326
|
-
const end = supply.finish(
|
|
327
|
-
(part.end - part.start) * Number(source.source.bytesPerMediaSecond), begin);
|
|
328
|
-
sourceFinish.set(source.serviceId, end);
|
|
329
|
-
served.push({ ...part, begin, finish: end });
|
|
318
|
+
for (const [sourceId, ranges] of sourceByteIntervals) {
|
|
319
|
+
const served = sourceByteService.get(sourceId) ?? [];
|
|
320
|
+
for (const interval of ranges) {
|
|
321
|
+
if (uncoveredIntervals(interval, served).length) return result(false, null, buffered, reserve, null, "download-schedule-unavailable", preparedSegments);
|
|
322
|
+
for (const part of served.filter(part => part.end > interval.start && part.start < interval.end)) {
|
|
323
|
+
sourceArrivals.push({ at: part.finish });
|
|
324
|
+
}
|
|
330
325
|
}
|
|
331
|
-
|
|
332
|
-
sourceArrivals.push(...served.filter((part) => part.end > interval.start && part.start < interval.end)
|
|
333
|
-
.flatMap((part) => [Math.max(part.start, interval.start), Math.min(part.end, interval.end)]
|
|
334
|
-
.map((media) => ({ media, at: media === part.start ? part.begin : part.begin +
|
|
335
|
-
(part.finish - part.begin) * (media - part.start) / (part.end - part.start) }))));
|
|
326
|
+
sourceByteService.set(sourceId, served);
|
|
336
327
|
}
|
|
337
|
-
//
|
|
338
|
-
//
|
|
339
|
-
// Reused input keeps its original arrival times; it is neither downloaded
|
|
340
|
-
// twice nor charged the queue start of a later request.
|
|
328
|
+
// Production accepts a complete held segment input. It cannot overlap its
|
|
329
|
+
// own source download; reused input retains its original arrival time.
|
|
341
330
|
const processing = trackState[trackIndex].curve;
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
...sourceArrivals.map(({ media, at }) => processing.finish(
|
|
345
|
-
remainingProcessing.reduce((sum, part) => sum + Math.max(0, part.end - Math.max(part.start, media)), 0), at)));
|
|
331
|
+
const admittedAt = Math.max(trackFinish[trackIndex], ...sourceArrivals.map(({ at }) => at));
|
|
332
|
+
const producedAt = job.produced || encodeWork === 0 ? 0 : processing.finish(encodeWork, admittedAt);
|
|
346
333
|
trackFinish[trackIndex] = Math.max(trackFinish[trackIndex], producedAt);
|
|
347
334
|
productions.push({ at: producedAt, trackIndex, segment, requiresSource: sourceArrivals.some(({ at }) => at > 0) });
|
|
348
335
|
const deliveryAt = link.finish(sizeBits, Math.max(transferFinish, producedAt));
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Translate map demand to complete output intervals and selected source tracks. */
|
|
2
|
+
export function segmentDemands(output, fileIndex, zones) {
|
|
3
|
+
const tracks = [];
|
|
4
|
+
for (const type of ["video", "audio"]) {
|
|
5
|
+
const spec = output.spec?.[type];
|
|
6
|
+
if (spec?.fileIndex !== fileIndex) continue;
|
|
7
|
+
tracks.push({ type, index: type === "video" ? 0 : spec.trackIndex,
|
|
8
|
+
mode: type === "video" ? spec.encode ? "transcode" : "copy" : spec.transcode ? "transcode" : "copy" });
|
|
9
|
+
}
|
|
10
|
+
if (!tracks.length) return [];
|
|
11
|
+
const grid = output.timeline?.published ?? output.timeline?.boundaries;
|
|
12
|
+
if (!Array.isArray(grid)) return [];
|
|
13
|
+
const demands = [];
|
|
14
|
+
for (let index = 0; index + 1 < grid.length; index++) {
|
|
15
|
+
const from = grid[index], to = grid[index + 1];
|
|
16
|
+
if (!Number.isFinite(from) || !Number.isFinite(to) || !(to > from)) throw new Error("An output segment needs a valid interval.");
|
|
17
|
+
const overlapping = zones.filter(zone => zone.priority > 0 && zone.from < to && zone.to > from);
|
|
18
|
+
if (!overlapping.length) continue;
|
|
19
|
+
const owner = overlapping.reduce((first, zone) => (zone.deadlineAt ?? Infinity) < (first.deadlineAt ?? Infinity) ? zone : first);
|
|
20
|
+
demands.push({ index, from, to, tracks, owner,
|
|
21
|
+
priority: Math.max(...overlapping.map(zone => zone.priority)),
|
|
22
|
+
urgent: overlapping.some(zone => zone.urgent),
|
|
23
|
+
behind: overlapping.every(zone => zone.behind),
|
|
24
|
+
deferred: overlapping.every(zone => zone.deferred),
|
|
25
|
+
deadlineAt: Math.min(...overlapping.map(zone => Number.isFinite(zone.deadlineAt) ? zone.deadlineAt : Infinity)) });
|
|
26
|
+
}
|
|
27
|
+
return demands.sort((left, right) => left.deadlineAt - right.deadlineAt || right.priority - left.priority || left.index - right.index);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Source bytes must arrive before measured processing and delivery consume time. */
|
|
31
|
+
export function sourceDeadline(demand, { encodeSpeed, outputBytes, linkMbps }) {
|
|
32
|
+
const span = demand.to - demand.from;
|
|
33
|
+
const processing = Number.isFinite(encodeSpeed) && encodeSpeed > 0 ? span / encodeSpeed : 0;
|
|
34
|
+
const delivery = Number.isFinite(outputBytes) && outputBytes > 0 && Number.isFinite(linkMbps) && linkMbps > 0
|
|
35
|
+
? outputBytes * 8 / (linkMbps * 1_000_000) : 0;
|
|
36
|
+
return Number.isFinite(demand.deadlineAt) ? demand.deadlineAt - (processing + delivery) * 1000 : demand.deadlineAt;
|
|
37
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** All outputs still consumed by this viewer must cover the urgent interval. */
|
|
2
|
+
export function urgentOutputsReady({ sourceKey, fileIndex, durationSeconds, atSeconds, seconds,
|
|
3
|
+
outputs, consumed, segmentIndex, segmentStart, closed }) {
|
|
4
|
+
if (!Number.isFinite(durationSeconds) || !(durationSeconds > 0) ||
|
|
5
|
+
!Number.isFinite(atSeconds) || atSeconds < 0 ||
|
|
6
|
+
!Number.isFinite(seconds) || !(seconds > 0)) return false;
|
|
7
|
+
const target = Math.min(durationSeconds, atSeconds + seconds);
|
|
8
|
+
let found = false;
|
|
9
|
+
for (const output of outputs) {
|
|
10
|
+
if (output.file?.sourceKey !== sourceKey || output.file?.fileIndex !== fileIndex || !consumed(output)) continue;
|
|
11
|
+
found = true;
|
|
12
|
+
if (!output.timeline || !output.outputKey) return false;
|
|
13
|
+
let index = segmentIndex(output, atSeconds);
|
|
14
|
+
if (!Number.isSafeInteger(index) || index < 0) return false;
|
|
15
|
+
let start = segmentStart(output, index);
|
|
16
|
+
if (!Number.isFinite(start) || start > atSeconds) return false;
|
|
17
|
+
while (start < target) {
|
|
18
|
+
if (!closed(output.outputKey, index)) return false;
|
|
19
|
+
const next = segmentStart(output, index + 1);
|
|
20
|
+
if (!Number.isFinite(next) || !(next > start)) return false;
|
|
21
|
+
start = next;
|
|
22
|
+
index += 1;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return found;
|
|
26
|
+
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file The addresses an encoder reads its source from.
|
|
3
|
-
*
|
|
4
|
-
* Built per run from facts that belong elsewhere — which files the output
|
|
5
|
-
* reads (`OutputSpec`), where the stream route listens, which output the read
|
|
6
|
-
* feeds, and how wide its read window is — and kept nowhere.
|
|
7
|
-
*
|
|
8
|
-
* Two query parameters are what make the encoder's read what it is to the
|
|
9
|
-
* stream route: `reader=playback`, the one read that says where the viewer is,
|
|
10
|
-
* and `windowBytes`, how far ahead of itself that read asks the swarm for,
|
|
11
|
-
* sized in seconds of playback by whoever knows the file's byte rate.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** The query parameter naming which run of an output a read feeds. */
|
|
15
|
-
const RUN_PARAMETER = "run";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @param {object} params
|
|
19
|
-
* @param {{ fileIndex: number, streamUrl: (baseUrl: string, options?: { sessionId?: string }) => URL }} params.picture
|
|
20
|
-
* @param {{ fileIndex: number, streamUrl: (baseUrl: string, options?: { sessionId?: string }) => URL }} params.soundtrack
|
|
21
|
-
* The file the carried soundtrack lives in; the picture's own file for an
|
|
22
|
-
* embedded track.
|
|
23
|
-
* @param {"video-only" | "audio-only" | "muxed" | "empty"} params.carries
|
|
24
|
-
* @param {boolean} params.audioSeparate - The picture's sound travels as its own output.
|
|
25
|
-
* @param {string} params.sessionId - Which output this read feeds.
|
|
26
|
-
* @param {number} [params.runToken] - Which run of that output the read feeds,
|
|
27
|
-
* so the time its input waits is charged to that run and not to another one
|
|
28
|
-
* working on the same output.
|
|
29
|
-
* @param {number} params.readWindowBytes - Zero when no window was measured.
|
|
30
|
-
* @param {string} params.baseUrl
|
|
31
|
-
* @returns {{ inputFile: object, audioFile: object, inputUrl: string, audioInputUrl: string }}
|
|
32
|
-
*/
|
|
33
|
-
export function encoderInputs({ picture, soundtrack, carries, audioSeparate, sessionId, runToken, readWindowBytes, baseUrl }) {
|
|
34
|
-
// A soundtrack shipped as its own file is encoded FROM that file, so a
|
|
35
|
-
// rendition of it reads nothing else.
|
|
36
|
-
const inputFile = carries === "audio-only" && soundtrack !== picture ? soundtrack : picture;
|
|
37
|
-
const input = inputFile.streamUrl(baseUrl, { sessionId });
|
|
38
|
-
const forRun = (url) => {
|
|
39
|
-
if (Number.isInteger(runToken)) url.searchParams.set(RUN_PARAMETER, String(runToken));
|
|
40
|
-
return url;
|
|
41
|
-
};
|
|
42
|
-
forRun(input);
|
|
43
|
-
if (Number.isFinite(readWindowBytes) && readWindowBytes > 0) {
|
|
44
|
-
input.searchParams.set("windowBytes", String(readWindowBytes));
|
|
45
|
-
input.searchParams.set("reader", "playback");
|
|
46
|
-
}
|
|
47
|
-
// The second input exists only for a picture that carries its sound inside
|
|
48
|
-
// itself when that sound is in another file.
|
|
49
|
-
const audioInputUrl = carries !== "audio-only" && !audioSeparate && soundtrack !== inputFile
|
|
50
|
-
? forRun(soundtrack.streamUrl(baseUrl, { sessionId })).toString()
|
|
51
|
-
: "";
|
|
52
|
-
return { inputFile, audioFile: soundtrack, inputUrl: input.toString(), audioInputUrl };
|
|
53
|
-
}
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file Where a file's keyframes are, found by DECODING it rather than by
|
|
3
|
-
* reading what its container states.
|
|
4
|
-
*
|
|
5
|
-
* The SECOND reader of one fact, and the weaker of the two: the container's own
|
|
6
|
-
* index is two point reads of 16 KB and names every keyframe, while this scans
|
|
7
|
-
* the media over a torrent-backed input and on a large file often does not
|
|
8
|
-
* finish — measured 2026-08-02, 77 keyframes in 45 s against all 570 in 0.8 s
|
|
9
|
-
* from the index. So it is never waited for and never displaces an index that
|
|
10
|
-
* answered; `KeyframeTable.learn` keeps the fuller answer whichever arrives
|
|
11
|
-
* second.
|
|
12
|
-
*
|
|
13
|
-
* It exists because some containers state no index at all, and a keyframe is
|
|
14
|
-
* still needed for something both encode branches need: choosing a SOURCE seek
|
|
15
|
-
* position ffmpeg can land on. `-ss` before `-i` trusts the container's
|
|
16
|
-
* on-the-fly seek, which for some containers (observed: AVI with VBR MP3 audio)
|
|
17
|
-
* can point at a position with no valid frame boundary at all — ffmpeg then
|
|
18
|
-
* fails outright ("Seek failed" / "Header missing"), not merely imprecisely.
|
|
19
|
-
*
|
|
20
|
-
* **Why it is here and not in `container/`.** It runs a process and reads the
|
|
21
|
-
* file over the address this proxy serves it at. A container is built from four
|
|
22
|
-
* functions and takes byte ranges; it spawns nothing and knows no URL. Both are
|
|
23
|
-
* readings of the same file, so both are this layer's — and they are separate
|
|
24
|
-
* files because one of them needs a machine and the other needs sixteen
|
|
25
|
-
* kilobytes.
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
import { spawn } from "node:child_process";
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The ffprobe that sits beside a given ffmpeg.
|
|
32
|
-
*
|
|
33
|
-
* @param {string} ffmpegBin
|
|
34
|
-
* @returns {string}
|
|
35
|
-
*/
|
|
36
|
-
export function ffprobeBinFor(ffmpegBin) {
|
|
37
|
-
if (typeof ffmpegBin !== "string" || ffmpegBin.length === 0) {
|
|
38
|
-
return "ffprobe";
|
|
39
|
-
}
|
|
40
|
-
if (/ffmpeg(\.exe)?$/i.test(ffmpegBin)) {
|
|
41
|
-
return ffmpegBin.replace(/ffmpeg(\.exe)?$/i, "ffprobe$1");
|
|
42
|
-
}
|
|
43
|
-
return "ffprobe";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Where this file's keyframes are, found by DECODING it rather than by reading
|
|
48
|
-
* what its container states.
|
|
49
|
-
*
|
|
50
|
-
* The second reader of one fact, and the weaker of the two: the container's own
|
|
51
|
-
* index is two point reads of 16 KB and names every keyframe, while this scans
|
|
52
|
-
* the media over a torrent-backed input and on a large file often does not
|
|
53
|
-
* finish — measured 2026-08-02, 77 keyframes in 45 s against all 570 in 0.8 s
|
|
54
|
-
* from the index. So it is never waited for and never displaces an index that
|
|
55
|
-
* answered; `KeyframeTable.learn` keeps the fuller answer whichever arrives
|
|
56
|
-
* second.
|
|
57
|
-
*
|
|
58
|
-
* It exists because some containers state no index at all, and a keyframe is
|
|
59
|
-
* still needed for something both branches need: choosing a SOURCE seek
|
|
60
|
-
* position ffmpeg can land on. `-ss` before `-i` trusts the container's
|
|
61
|
-
* on-the-fly seek, which for some containers (observed: AVI with VBR MP3 audio)
|
|
62
|
-
* can point at a position with no valid frame boundary at all — ffmpeg then
|
|
63
|
-
* fails outright ("Seek failed" / "Header missing"), not merely imprecisely.
|
|
64
|
-
*
|
|
65
|
-
* It lives here, beside the codec probe, because it is the same kind of thing:
|
|
66
|
-
* a reading of the file made by running ffmpeg's own tools over the address
|
|
67
|
-
* this proxy serves it at. The container layer takes byte ranges and spawns
|
|
68
|
-
* nothing.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} ffmpegBin
|
|
71
|
-
* @param {string | URL} inputUrl
|
|
72
|
-
* @param {number} [timeoutMs]
|
|
73
|
-
* @returns {Promise<number[] | null>} Sorted keyframe times, or null.
|
|
74
|
-
*/
|
|
75
|
-
export async function probeVideoKeyframeTimes(ffmpegBin, inputUrl, timeoutMs = 25_000) {
|
|
76
|
-
return new Promise((resolve) => {
|
|
77
|
-
let proc;
|
|
78
|
-
try {
|
|
79
|
-
proc = spawn(
|
|
80
|
-
ffprobeBinFor(ffmpegBin),
|
|
81
|
-
[
|
|
82
|
-
"-v", "error",
|
|
83
|
-
// `-skip_frame nokey` makes the decoder discard non-keyframes, so the
|
|
84
|
-
// probe reads only what it needs. Without it a full packet scan of a
|
|
85
|
-
// ~5 GB MKV cannot finish inside any sane budget over a torrent-backed
|
|
86
|
-
// input, the probe returns nothing, and the playlist falls back to a
|
|
87
|
-
// uniform grid — which on the COPY path is a lie: cuts land on the
|
|
88
|
-
// source's real keyframes, not on a 4 s ruler. The player then finds
|
|
89
|
-
// the declared times do not match the media, stops trusting the
|
|
90
|
-
// playlist and walks the file from segment #1 to locate the seek
|
|
91
|
-
// position by hand (field 2026-08-02: a seek to 1:30 produced requests
|
|
92
|
-
// #1, #2, #45, #86, #123 … #1187, taking minutes and never arriving).
|
|
93
|
-
"-skip_frame", "nokey",
|
|
94
|
-
"-select_streams", "v:0",
|
|
95
|
-
"-show_entries", "packet=pts_time,flags",
|
|
96
|
-
"-of", "csv=p=0",
|
|
97
|
-
String(inputUrl)
|
|
98
|
-
],
|
|
99
|
-
{ stdio: ["ignore", "pipe", "ignore"], windowsHide: true }
|
|
100
|
-
);
|
|
101
|
-
} catch {
|
|
102
|
-
resolve(null);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
let stdout = "";
|
|
106
|
-
let settled = false;
|
|
107
|
-
const finish = (value) => {
|
|
108
|
-
if (settled) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
settled = true;
|
|
112
|
-
resolve(value);
|
|
113
|
-
};
|
|
114
|
-
const timer = setTimeout(() => {
|
|
115
|
-
try {
|
|
116
|
-
if (!proc.killed) {
|
|
117
|
-
proc.kill("SIGTERM");
|
|
118
|
-
}
|
|
119
|
-
} catch {
|
|
120
|
-
// ignore
|
|
121
|
-
}
|
|
122
|
-
finish(null);
|
|
123
|
-
}, timeoutMs);
|
|
124
|
-
proc.stdout.on("data", (chunk) => {
|
|
125
|
-
stdout += String(chunk);
|
|
126
|
-
});
|
|
127
|
-
proc.on("error", () => {
|
|
128
|
-
clearTimeout(timer);
|
|
129
|
-
finish(null);
|
|
130
|
-
});
|
|
131
|
-
proc.on("exit", (code) => {
|
|
132
|
-
clearTimeout(timer);
|
|
133
|
-
if (code !== 0) {
|
|
134
|
-
finish(null);
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const times = [];
|
|
138
|
-
for (const line of stdout.split("\n")) {
|
|
139
|
-
// Each line: "<pts_time>,<flags>" e.g. "12.345000,K__"
|
|
140
|
-
const comma = line.indexOf(",");
|
|
141
|
-
if (comma < 0) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
const flags = line.slice(comma + 1);
|
|
145
|
-
if (!flags.includes("K")) {
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
const t = Number(line.slice(0, comma));
|
|
149
|
-
if (Number.isFinite(t)) {
|
|
150
|
-
times.push(t);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
times.sort((a, b) => a - b);
|
|
154
|
-
finish(times.length > 0 ? times : null);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
}
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file What a stream's own header says: duration, picture size, frame rate,
|
|
3
|
-
* start time, stream counts and HDR — from one short ffmpeg run that stops as
|
|
4
|
-
* soon as the header has been printed.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { spawn } from "node:child_process";
|
|
8
|
-
import {
|
|
9
|
-
parseFfmpegBitrateKbps,
|
|
10
|
-
parseFfmpegDurationSeconds,
|
|
11
|
-
parseFfmpegHdr,
|
|
12
|
-
parseFfmpegStartTimeSeconds,
|
|
13
|
-
parseFfmpegStreamCounts,
|
|
14
|
-
parseFfmpegVideoDimensions,
|
|
15
|
-
parseFfmpegVideoFps
|
|
16
|
-
} from "./ffmpeg-banner.js";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Run a short ffmpeg probe to extract the total duration AND video resolution
|
|
20
|
-
* of a stream from the container header. Both are printed almost immediately
|
|
21
|
-
* (before any decoding), so this returns as soon as they are seen; an 8 s
|
|
22
|
-
* timeout guards the rest.
|
|
23
|
-
*
|
|
24
|
-
* @param {string} ffmpegBin - Path to the ffmpeg executable.
|
|
25
|
-
* @param {string | URL} inputUrl - URL of the stream to probe.
|
|
26
|
-
* @returns {Promise<{ durationSeconds: number | null, width: number | null, height: number | null, fps: number | null, startTime: number, isHdr: boolean }>}
|
|
27
|
-
*/
|
|
28
|
-
export async function probeInputMediaInfo(ffmpegBin, inputUrl) {
|
|
29
|
-
return new Promise((resolve) => {
|
|
30
|
-
const ffmpeg = spawn(ffmpegBin, ["-hide_banner", "-loglevel", "info", "-i", inputUrl, "-f", "null", "-"], {
|
|
31
|
-
stdio: ["ignore", "ignore", "pipe"],
|
|
32
|
-
windowsHide: true
|
|
33
|
-
});
|
|
34
|
-
let stderr = "";
|
|
35
|
-
let settled = false;
|
|
36
|
-
const finish = () => {
|
|
37
|
-
if (settled) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
settled = true;
|
|
41
|
-
const dims = parseFfmpegVideoDimensions(stderr);
|
|
42
|
-
resolve({
|
|
43
|
-
durationSeconds: parseFfmpegDurationSeconds(stderr),
|
|
44
|
-
bitrateKbps: parseFfmpegBitrateKbps(stderr),
|
|
45
|
-
width: dims.width,
|
|
46
|
-
height: dims.height,
|
|
47
|
-
fps: parseFfmpegVideoFps(stderr),
|
|
48
|
-
startTime: parseFfmpegStartTimeSeconds(stderr),
|
|
49
|
-
// Only ever read when a run fails, and read HERE because by then the
|
|
50
|
-
// banner is long gone: this probe is the one place the source says what
|
|
51
|
-
// it holds.
|
|
52
|
-
streamCounts: parseFfmpegStreamCounts(stderr),
|
|
53
|
-
isHdr: parseFfmpegHdr(stderr)
|
|
54
|
-
});
|
|
55
|
-
};
|
|
56
|
-
const timeoutId = setTimeout(() => {
|
|
57
|
-
if (!ffmpeg.killed) {
|
|
58
|
-
ffmpeg.kill("SIGTERM");
|
|
59
|
-
}
|
|
60
|
-
finish();
|
|
61
|
-
}, 8_000);
|
|
62
|
-
ffmpeg.stderr.on("data", (chunk) => {
|
|
63
|
-
stderr += String(chunk);
|
|
64
|
-
// The header ("Duration:" then the "Video: … WxH" stream line) is printed
|
|
65
|
-
// before any decoding. Bail as soon as both are present instead of letting
|
|
66
|
-
// `-f null -` decode the whole stream until the 8 s timeout.
|
|
67
|
-
//
|
|
68
|
-
// This probe asks about the PICTURE and nothing else now: a file with no
|
|
69
|
-
// video track is read by the container layer, which answers from 64 KB of
|
|
70
|
-
// header. It used to take an `expectVideo: false` for exactly that case,
|
|
71
|
-
// and the branch cost 8121 ms of every cold start (2026-09-03) because
|
|
72
|
-
// the exit still waited for a parsed DURATION, which a partly downloaded
|
|
73
|
-
// file prints as `N/A`.
|
|
74
|
-
const duration = parseFfmpegDurationSeconds(stderr);
|
|
75
|
-
const dims = parseFfmpegVideoDimensions(stderr);
|
|
76
|
-
if (duration != null && dims.width != null) {
|
|
77
|
-
clearTimeout(timeoutId);
|
|
78
|
-
if (!ffmpeg.killed) {
|
|
79
|
-
ffmpeg.kill("SIGTERM");
|
|
80
|
-
}
|
|
81
|
-
finish();
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
ffmpeg.on("error", () => {
|
|
85
|
-
clearTimeout(timeoutId);
|
|
86
|
-
finish();
|
|
87
|
-
});
|
|
88
|
-
ffmpeg.on("exit", () => {
|
|
89
|
-
clearTimeout(timeoutId);
|
|
90
|
-
finish();
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
}
|