@torrent-tv/proxy 2.107.30 → 2.107.31
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/package.json
CHANGED
|
@@ -157,16 +157,17 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
157
157
|
// starts), the read piece nearest before them (what an unread piece is placed
|
|
158
158
|
// by) and read pieces inside what the page reports holding (their frames).
|
|
159
159
|
//
|
|
160
|
-
// THE NEAR WINDOW
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
//
|
|
164
|
-
//
|
|
160
|
+
// THE NEAR WINDOW. The look-ahead is scheduled with exact segment times and
|
|
161
|
+
// source ranges when known. Missing range metadata is priced from aggregate
|
|
162
|
+
// file density and measured download service without claiming an address or
|
|
163
|
+
// byte. This forecast decides whether playback can start from work near the
|
|
164
|
+
// viewer; it does not make the first frame wait on an aggregate estimate of
|
|
165
|
+
// the whole remaining film. Each later poll advances this same window.
|
|
165
166
|
const lookahead = finiteNonNegative(input.lookaheadSeconds);
|
|
166
167
|
const horizonEnd = lookahead > 0 ? finiteNonNegative(input.positionSeconds) + lookahead : Number.POSITIVE_INFINITY;
|
|
167
168
|
const tracks = (Array.isArray(input.tracks) ? input.tracks : []).map((track) =>
|
|
168
|
-
Array.isArray(track.segments)
|
|
169
|
-
segments: piecesThatMatter(track, finiteNonNegative(input.positionSeconds), horizonEnd) } : track);
|
|
169
|
+
Array.isArray(track.segments)
|
|
170
|
+
? { ...track, segments: piecesThatMatter(track, finiteNonNegative(input.positionSeconds), horizonEnd) } : track);
|
|
170
171
|
const duration = finiteNonNegative(input.durationSeconds);
|
|
171
172
|
// Each track's served ticks are placed on the clock the page reports by the
|
|
172
173
|
// offset its player applied (`timestampOffset`).
|
|
@@ -237,25 +238,11 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
237
238
|
.filter((segment) => isPiece(segment) &&
|
|
238
239
|
segment.endSeconds >= position - PLAYER_SEEK_TOLERANCE_SECONDS && segment.startSeconds < horizonEnd)
|
|
239
240
|
.sort((left, right) => left.startSeconds - right.startSeconds || left.index - right.index),
|
|
240
|
-
// The pieces after the near window, as the playlist cuts them, and the
|
|
241
|
-
// offset the page's player placed them by.
|
|
242
|
-
tail: [],
|
|
243
|
-
tailStartSeconds: horizonEnd,
|
|
244
241
|
durationSeconds: Number.isFinite(track.durationSeconds) ? track.durationSeconds : duration,
|
|
245
|
-
segmentCount: Number.isSafeInteger(track.segmentCount) ? track.segmentCount
|
|
246
|
-
: Math.max(0, ...track.segments.map(segment => segment.index + 1)),
|
|
247
242
|
clockSeconds: Number.isFinite(track.clockOffsetSeconds) ? track.clockOffsetSeconds : 0
|
|
248
243
|
}));
|
|
249
244
|
for (const state of trackState) {
|
|
250
|
-
state.
|
|
251
|
-
? Math.min(duration, state.durationSeconds + state.clockSeconds) : null;
|
|
252
|
-
const endIndex = state.segments.reduce((last, segment) => segment.startSeconds < horizonEnd
|
|
253
|
-
? Math.max(last, segment.index) : last, -1);
|
|
254
|
-
state.tailSegmentCount = Math.max(0, state.segmentCount - endIndex - 1);
|
|
255
|
-
const readyTail = [...state.ready].reduce((count, index) => count + Number(index > endIndex), 0);
|
|
256
|
-
state.tailWorkFraction = state.tailSegmentCount > 0
|
|
257
|
-
? Math.max(0, 1 - Math.min(state.tailSegmentCount, readyTail) / state.tailSegmentCount)
|
|
258
|
-
: 0;
|
|
245
|
+
state.trackEndSeconds = Math.min(duration, state.durationSeconds + state.clockSeconds);
|
|
259
246
|
}
|
|
260
247
|
if (trackState.some(({ segments }) => segments.length === 0)) {
|
|
261
248
|
return result(false, null, buffered, reserve, null, "timeline-unavailable", 0);
|
|
@@ -604,21 +591,12 @@ export function predictPlaybackReadiness(input = {}) {
|
|
|
604
591
|
// Supply interruption coverage is stock held by the proxy as well as
|
|
605
592
|
// the browser. It is not a browser buffer-size admission rule.
|
|
606
593
|
// The reserve is bounded by what is left of the track, not of the window.
|
|
607
|
-
const left = state.
|
|
608
|
-
: Math.max(0, state.tailEndSeconds - toSeconds(before));
|
|
594
|
+
const left = Math.max(0, state.trackEndSeconds - toSeconds(before));
|
|
609
595
|
const ahead = Math.max(0, secondsAhead(before, at) - Math.min(reserve, left) * Number(production.requiresSource));
|
|
610
596
|
constrain(production.at - ahead, "production", trackIndex, production.segment.index, production.at, ahead,
|
|
611
597
|
production.requiresSource, production.sourceArrivalAt, production.sourcePlanned);
|
|
612
598
|
}
|
|
613
599
|
}
|
|
614
|
-
if (trackState.some(({ tailEndSeconds }) => Number.isFinite(tailEndSeconds))) {
|
|
615
|
-
const stop = scheduleTail({ trackState, sourceState, sourceByteCoverage, runStarts, trackFinish,
|
|
616
|
-
link, position, buffered, reserve, transferFinish, constrain, duration });
|
|
617
|
-
if (stop) {
|
|
618
|
-
return { ...result(false, null, buffered, reserve, null, stop.reason, preparedSegments),
|
|
619
|
-
...(stop.unavailableSource ? { unavailableSource: stop.unavailableSource } : {}) };
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
600
|
const firstRequiredStages = trackState.slice(0, requiredTracks).map((state, trackIndex) => {
|
|
623
601
|
const first = state.segments.find(segment => segment.endSeconds > position);
|
|
624
602
|
if (!first) return { track: state.track.id ?? trackIndex, segment: null, alreadyBuffered: true,
|
|
@@ -683,93 +661,6 @@ function isPiece(segment) {
|
|
|
683
661
|
Number.isFinite(segment?.endSeconds) && segment.endSeconds > segment.startSeconds;
|
|
684
662
|
}
|
|
685
663
|
|
|
686
|
-
/**
|
|
687
|
-
* A duration-level estimate of the suffix after the exact near window. Source
|
|
688
|
-
* work uses each file's aggregate byte density and measured download service;
|
|
689
|
-
* processing uses each track's measured rate; delivery uses output bitrates and
|
|
690
|
-
* measured link service. Held source bytes and made segments reduce the
|
|
691
|
-
* estimate. This conditional forecast does not assert exact source ranges or
|
|
692
|
-
* individual frame timing. Its work is bounded by sources and tracks, not by
|
|
693
|
-
* the film's segment count.
|
|
694
|
-
*
|
|
695
|
-
* @returns {{ reason: string, unavailableSource?: object } | null} Why there is no answer, or null.
|
|
696
|
-
*/
|
|
697
|
-
function scheduleTail({ trackState, sourceState, sourceByteCoverage, runStarts, trackFinish, link,
|
|
698
|
-
position, buffered, reserve, transferFinish, constrain, duration }) {
|
|
699
|
-
const tailStart = Math.max(position, ...trackState.map(state => state.tailStartSeconds));
|
|
700
|
-
if (!Number.isFinite(tailStart)) return null;
|
|
701
|
-
const sourceDurations = new Map();
|
|
702
|
-
for (const state of trackState) {
|
|
703
|
-
const seconds = Math.max(0, state.tailEndSeconds - tailStart) * state.tailWorkFraction;
|
|
704
|
-
if (!(seconds > 0)) continue;
|
|
705
|
-
for (const sourceId of new Set(state.track.sourceIds ?? [])) {
|
|
706
|
-
sourceDurations.set(sourceId, Math.max(sourceDurations.get(sourceId) ?? 0, seconds));
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
let sourceArrivalAt = 0;
|
|
711
|
-
for (const [sourceId, seconds] of sourceDurations) {
|
|
712
|
-
const source = sourceState.get(sourceId);
|
|
713
|
-
if (!source) return { reason: "source-measurement-unavailable" };
|
|
714
|
-
if (source.source.complete === true) continue;
|
|
715
|
-
const bytes = estimateUnmappedSourceBytes(source.source, seconds, duration,
|
|
716
|
-
sourceByteCoverage.get(sourceId));
|
|
717
|
-
if (bytes === null) return { reason: "source-measurement-unavailable",
|
|
718
|
-
unavailableSource: { sourceId, phase: "tail" } };
|
|
719
|
-
if (!(bytes > 0)) continue;
|
|
720
|
-
const { service } = source;
|
|
721
|
-
if (!service.rate || !(service.rate.rateAt(0) > 0)) {
|
|
722
|
-
return { reason: service.rate ? "service-not-advancing" : "download-rate-unavailable",
|
|
723
|
-
unavailableSource: { sourceId, estimatedBytes: bytes, phase: "tail" } };
|
|
724
|
-
}
|
|
725
|
-
service.finish = service.rate.finish(bytes, service.finish);
|
|
726
|
-
sourceArrivalAt = Math.max(sourceArrivalAt, service.finish);
|
|
727
|
-
const state = trackState.find(candidate => candidate.track.sourceIds?.includes(sourceId));
|
|
728
|
-
const index = state?.segments.at(-1)?.index ?? 0;
|
|
729
|
-
const ahead = Math.max(0, (state?.tailEndSeconds ?? tailStart) - position);
|
|
730
|
-
constrain(service.finish - Math.max(0, ahead - reserve), "source", trackState.indexOf(state), index,
|
|
731
|
-
service.finish, ahead, true, service.finish, false);
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
let outputBits = 0;
|
|
735
|
-
let latestTrackFinish = 0;
|
|
736
|
-
for (let trackIndex = 0; trackIndex < trackState.length; trackIndex += 1) {
|
|
737
|
-
const state = trackState[trackIndex];
|
|
738
|
-
const end = state.tailEndSeconds;
|
|
739
|
-
const seconds = Math.max(0, end - tailStart);
|
|
740
|
-
if (!(seconds > 0)) continue;
|
|
741
|
-
const track = state.track;
|
|
742
|
-
const segment = state.segments.at(-1) ?? { index: 0 };
|
|
743
|
-
const remaining = seconds * state.tailWorkFraction;
|
|
744
|
-
const work = Math.max(0, remaining - numericCoverage((track.processingRanges ?? [])
|
|
745
|
-
.filter(range => Number.isFinite(range?.start) && Number.isFinite(range?.end))
|
|
746
|
-
.map(range => ({ start: Math.max(tailStart, range.start), end: Math.min(end, range.end) }))
|
|
747
|
-
.filter(range => range.end > range.start)).reduce((sum, range) => sum + range.end - range.start, 0));
|
|
748
|
-
const processing = state.curve;
|
|
749
|
-
if (work > 0 && !processing) return { reason: "encode-rate-unavailable" };
|
|
750
|
-
const run = runStarts[trackIndex];
|
|
751
|
-
const localStart = tailStart - state.clockSeconds;
|
|
752
|
-
const inRun = run.spans.some(span => localStart >= span.start && localStart < span.end);
|
|
753
|
-
const open = work > 0 && !inRun ? run.openSeconds : 0;
|
|
754
|
-
const finishedAt = work > 0 ? processing.finish(work, trackFinish[trackIndex] + open) : trackFinish[trackIndex];
|
|
755
|
-
latestTrackFinish = Math.max(latestTrackFinish, finishedAt);
|
|
756
|
-
const deliveryStart = Math.max(tailStart, position + buffered);
|
|
757
|
-
outputBits += Math.max(0, state.bitsPerMediaSecond) * Math.max(0, end - deliveryStart);
|
|
758
|
-
const ahead = Math.max(0, end - position);
|
|
759
|
-
constrain(finishedAt - Math.max(0, ahead - reserve), "production", trackIndex, segment.index,
|
|
760
|
-
finishedAt, ahead, sourceArrivalAt > 0, sourceArrivalAt, false);
|
|
761
|
-
}
|
|
762
|
-
|
|
763
|
-
if (outputBits > 0) {
|
|
764
|
-
const end = Math.max(...trackState.map(state => state.tailEndSeconds));
|
|
765
|
-
const completedAt = link.finish(outputBits, Math.max(transferFinish, latestTrackFinish));
|
|
766
|
-
const ahead = Math.max(0, end - position);
|
|
767
|
-
constrain(completedAt - Math.max(0, ahead - reserve), "delivery", 0,
|
|
768
|
-
trackState[0]?.segments.at(-1)?.index ?? 0, completedAt, ahead, false, sourceArrivalAt, false);
|
|
769
|
-
}
|
|
770
|
-
return null;
|
|
771
|
-
}
|
|
772
|
-
|
|
773
664
|
/**
|
|
774
665
|
* Seconds from `from` to `to`, or zero when `to` is not later.
|
|
775
666
|
*
|
|
@@ -1075,7 +966,7 @@ function numericCoverage(ranges) {
|
|
|
1075
966
|
/**
|
|
1076
967
|
* Estimate download work without claiming an address or held byte. The whole
|
|
1077
968
|
* file's measured density, reduced by the held fraction, prices unresolved
|
|
1078
|
-
* intervals
|
|
969
|
+
* intervals in the near window.
|
|
1079
970
|
*/
|
|
1080
971
|
function estimateUnmappedSourceBytes(source, mediaSeconds, durationSeconds, coverage = null) {
|
|
1081
972
|
const fileLength = Number(source.fileLength);
|