@torrent-tv/proxy 2.47.0 → 2.48.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/CHANGELOG.md +6 -0
- package/package.json +1 -1
- package/services/hls-session-manager.js +95 -1
- package/test/seek-landing.test.js +50 -0
- package/test/tunnel-renewal.test.js +13 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
## 2.48.0
|
|
2
|
+
|
|
3
|
+
- **Fix**: A copied picture now begins where it was asked to, so its cuts land on the times its playlist names. ffmpeg's own CLI moves an input seek back by `3*AV_TIME_BASE / 23` — **130.435 ms** — whenever the container does not declare `AVFMT_SEEK_TO_PTS` (Matroska does not) and a stream carries B-frames, which is sound in itself: such containers seek in decode order while the caller asks in presentation order. The consequence for a copy is that asking for a keyframe lands on the one BEFORE it, deterministically; and since `-segment_times` is measured from where the run really began while this code computed those offsets from the time it asked for, every cut of the run inherited one whole keyframe interval. Field 2026-08-20: 119 of 125 segments arriving a uniform 2.002 s early against the 0.5 s hls.js bridges, so every fragment was refused and re-fetched — on 2026-08-17 two of them 1908 times each. The request is now made that much later, bounded by half the distance to the next keyframe. Measured 2026-08-21 on Matroska with keyframes every 2 s: `-ss 10` produced a first segment starting at 8.000, `-ss 10.130435` one starting at 10.000; on MP4, where the heuristic does not fire, 10, 10.130435 and 10.2 all produced 10.000 — right in one case and harmless in the other. Not applied when the picture is re-encoded: a re-encode discards frames up to the requested time and already begins exactly there (`-ss 11` copied starts at 10.000, re-encoded at 11.000).
|
|
4
|
+
- **New**: A run that did not begin where it was asked to says so. The first piece a run produces is the only statement of its real origin that exists, and nothing compared the two — which is why the fault above stayed silent through two releases that touched the same grid. Said once per run, and only past what a player bridges.
|
|
5
|
+
- **Chore**: The tunnel-renewal test shuts its stand-in registry down deterministically. `WebSocketServer.close` waits for every connection to end and a renewal can leave one still closing, so a full suite run could hang for nine minutes on it.
|
|
6
|
+
|
|
1
7
|
## 2.47.0
|
|
2
8
|
|
|
3
9
|
- **Fix**: The tunnel is replaced before anything upstream ends it, so a viewer no longer arrives to find no proxy. Something between the proxy and the server closes the socket after exactly **100 min 15 s** — measured across a day of logs 2026-08-20, three intervals of 100:15 wherever a restart did not reset the clock, `code=1006` each time, and with the 30 s keepalive running throughout, so it is a lifetime cap and not an idle timeout. Reconnecting afterwards takes five seconds during which this proxy does not exist as far as the registry is concerned. The connection is now replaced at ninety minutes and the replacement takes over FIRST: the new socket registers itself, the server atomically supersedes the old one, and only then does the old one close — so there is no instant with nothing registered. A socket that finds itself superseded says so rather than reporting the tunnel as down, and an abrupt close nobody asked for still reconnects as before. Pinned by a test against a real WebSocket server.
|
package/package.json
CHANGED
|
@@ -568,6 +568,36 @@ const SEGMENT_READ_HIGH_WATER_MARK = 4 * 1024 * 1024;
|
|
|
568
568
|
// second is below any drift a viewer could notice, so anything above it is the
|
|
569
569
|
// index being wrong about where a keyframe is rather than rounding.
|
|
570
570
|
const SEGMENT_START_DISAGREEMENT_SEC = 0.25;
|
|
571
|
+
/**
|
|
572
|
+
* What ffmpeg's own CLI subtracts from an input seek, and therefore what has to
|
|
573
|
+
* be added back to land where we asked.
|
|
574
|
+
*
|
|
575
|
+
* `fftools/ffmpeg_demux.c`, in `ifile_open`: when the container does not
|
|
576
|
+
* declare `AVFMT_SEEK_TO_PTS` — Matroska does not — and any stream carries
|
|
577
|
+
* B-frames, the seek target is moved back by `3*AV_TIME_BASE / 23` before
|
|
578
|
+
* `avformat_seek_file` is called. Its purpose is sound: such containers seek in
|
|
579
|
+
* decode order while the caller asks in presentation order, and with B-frames
|
|
580
|
+
* the two differ, so it backs off far enough to be sure of reaching the frame
|
|
581
|
+
* asked for.
|
|
582
|
+
*
|
|
583
|
+
* The consequence for a COPY is that asking for a keyframe lands on the one
|
|
584
|
+
* BEFORE it — deterministically, every time. Measured 2026-08-21 on a Matroska
|
|
585
|
+
* file with keyframes every 2 s: `-ss 10` produced a first segment starting at
|
|
586
|
+
* 8.000; `-ss 10.130435` produced one starting at 10.000. On MP4, where the
|
|
587
|
+
* heuristic does not fire, all of 10, 10.130435 and 10.2 produced 10.000 — so
|
|
588
|
+
* adding this is right in one case and harmless in the other.
|
|
589
|
+
*
|
|
590
|
+
* That landing is what `-segment_times` is measured from, while this code
|
|
591
|
+
* computes those offsets from the time it ASKED for. One keyframe interval
|
|
592
|
+
* apart, inherited by every cut of the run: 119 of 125 segments arriving a
|
|
593
|
+
* uniform 2.002 s early in the field, four times what a player bridges.
|
|
594
|
+
*
|
|
595
|
+
* Not applied when the picture is re-encoded: a re-encode decodes from the
|
|
596
|
+
* keyframe and discards frames up to the requested time, so its output already
|
|
597
|
+
* begins exactly where asked (measured the same day: `-ss 11` copied starts at
|
|
598
|
+
* 10.000, re-encoded at 11.000).
|
|
599
|
+
*/
|
|
600
|
+
const SEEK_LANDING_OFFSET_SEC = 3 / 23;
|
|
571
601
|
|
|
572
602
|
/**
|
|
573
603
|
* How far a fragment may land from where the playlist put it before the player
|
|
@@ -1146,6 +1176,34 @@ export function segmentCutTimesFrom(boundaries, startIndex) {
|
|
|
1146
1176
|
return times;
|
|
1147
1177
|
}
|
|
1148
1178
|
|
|
1179
|
+
/**
|
|
1180
|
+
* How much later than a keyframe to ASK, so that ffmpeg lands on that keyframe.
|
|
1181
|
+
*
|
|
1182
|
+
* Bounded by half the distance to the next keyframe, which matters only where
|
|
1183
|
+
* keyframes stand closer together than twice the offset. There no single value
|
|
1184
|
+
* can satisfy both worlds — asking too little lands a keyframe early when the
|
|
1185
|
+
* heuristic fires, asking too much lands a keyframe late when it does not — and
|
|
1186
|
+
* the bound picks the smaller error, which is then under one keyframe interval
|
|
1187
|
+
* and therefore under what a player bridges.
|
|
1188
|
+
*
|
|
1189
|
+
* @param {HlsSession} session
|
|
1190
|
+
* @param {number} keyframe - A real keyframe time the run is to begin at.
|
|
1191
|
+
* @returns {number} Seconds to add to the request.
|
|
1192
|
+
*/
|
|
1193
|
+
export function seekLandingOffsetFor(session, keyframe) {
|
|
1194
|
+
// A re-encode trims to the requested time itself, so it needs no help and
|
|
1195
|
+
// must not be pushed past what it was asked for.
|
|
1196
|
+
if (session?.transcodeVideo === true) {
|
|
1197
|
+
return 0;
|
|
1198
|
+
}
|
|
1199
|
+
const times = Array.isArray(session?.keyframeTimes) ? session.keyframeTimes : [];
|
|
1200
|
+
const next = times.find((time) => time > keyframe + 0.001);
|
|
1201
|
+
if (next === undefined) {
|
|
1202
|
+
return SEEK_LANDING_OFFSET_SEC;
|
|
1203
|
+
}
|
|
1204
|
+
return Math.min(SEEK_LANDING_OFFSET_SEC, (next - keyframe) / 2);
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1149
1207
|
/**
|
|
1150
1208
|
* The largest keyframe time that does not exceed `target`, from a SORTED
|
|
1151
1209
|
* (ascending) array of keyframe times such as {@link probeVideoKeyframeTimes}
|
|
@@ -3932,7 +3990,7 @@ export class HlsSessionManager {
|
|
|
3932
3990
|
if (snappedKeyframe !== null) {
|
|
3933
3991
|
const residualSeconds = Math.max(0, seekSeconds - snappedKeyframe);
|
|
3934
3992
|
if (snappedKeyframe > 0) {
|
|
3935
|
-
args.push("-ss", ffmpegSeconds(snappedKeyframe));
|
|
3993
|
+
args.push("-ss", ffmpegSeconds(snappedKeyframe + seekLandingOffsetFor(session, snappedKeyframe)));
|
|
3936
3994
|
}
|
|
3937
3995
|
args.push("-i", session.inputUrl);
|
|
3938
3996
|
if (residualSeconds > 0) {
|
|
@@ -5124,6 +5182,41 @@ export class HlsSessionManager {
|
|
|
5124
5182
|
* @param {number} declaredStart - Seconds, from the playlist.
|
|
5125
5183
|
* @returns {void}
|
|
5126
5184
|
*/
|
|
5185
|
+
/**
|
|
5186
|
+
* Where the run REALLY began, against where it was asked to begin.
|
|
5187
|
+
*
|
|
5188
|
+
* The first piece a run produces is the only statement of this that exists,
|
|
5189
|
+
* and until now nothing compared the two. They disagree whenever the seek
|
|
5190
|
+
* lands somewhere other than the time asked for — which, before the landing
|
|
5191
|
+
* offset, was every run on a Matroska source with B-frames, by exactly one
|
|
5192
|
+
* keyframe interval. Every cut of the run then inherits it, because
|
|
5193
|
+
* `-segment_times` is measured from the landing.
|
|
5194
|
+
*
|
|
5195
|
+
* Said once per run, and only when it matters: within what a player bridges
|
|
5196
|
+
* there is nothing to report.
|
|
5197
|
+
*
|
|
5198
|
+
* @param {HlsSession} session
|
|
5199
|
+
* @param {number} index
|
|
5200
|
+
* @param {number} trueStart
|
|
5201
|
+
* @returns {void}
|
|
5202
|
+
*/
|
|
5203
|
+
#noteRunLanding(session, index, trueStart) {
|
|
5204
|
+
if (session.encodeStartIndex !== index || session.landingReportedForRun === index) {
|
|
5205
|
+
return;
|
|
5206
|
+
}
|
|
5207
|
+
session.landingReportedForRun = index;
|
|
5208
|
+
const asked = this.#segmentStartTime(session, index);
|
|
5209
|
+
const drift = trueStart - asked;
|
|
5210
|
+
if (!Number.isFinite(drift) || Math.abs(drift) <= PLAYER_BUFFER_HOLE_SEC) {
|
|
5211
|
+
return;
|
|
5212
|
+
}
|
|
5213
|
+
logger.warn(
|
|
5214
|
+
`transcode ${session.id} run began at ${trueStart.toFixed(3)}s but was asked for ` +
|
|
5215
|
+
`${asked.toFixed(3)}s — ${drift > 0 ? "+" : ""}${drift.toFixed(3)}s, and every cut of this ` +
|
|
5216
|
+
"run is measured from where it began, so the whole run is that far from its playlist"
|
|
5217
|
+
);
|
|
5218
|
+
}
|
|
5219
|
+
|
|
5127
5220
|
#noteIndexAccuracy(session, index, trueStart, declaredStart) {
|
|
5128
5221
|
const deviation = Math.abs(trueStart - declaredStart);
|
|
5129
5222
|
session.indexCheck ??= newIndexCheck();
|
|
@@ -7719,6 +7812,7 @@ export class HlsSessionManager {
|
|
|
7719
7812
|
: null;
|
|
7720
7813
|
const declaredStart = this.#segmentStartTime(session, index);
|
|
7721
7814
|
if (trueStart !== null) {
|
|
7815
|
+
this.#noteRunLanding(session, index, trueStart);
|
|
7722
7816
|
this.#noteIndexAccuracy(session, index, trueStart, declaredStart);
|
|
7723
7817
|
}
|
|
7724
7818
|
// WHERE THE PLAYER WAS TOLD THIS SEGMENT BEGINS, which is the playlist
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Ask ffmpeg late enough that it lands where we meant.
|
|
3
|
+
*
|
|
4
|
+
* `fftools/ffmpeg_demux.c` moves an input seek back by `3*AV_TIME_BASE / 23` —
|
|
5
|
+
* 130.435 ms — whenever the container does not declare `AVFMT_SEEK_TO_PTS` and
|
|
6
|
+
* a stream carries B-frames. So asking for a keyframe lands on the one before
|
|
7
|
+
* it, and since `-segment_times` is measured from where the run really began,
|
|
8
|
+
* every cut of that run inherits the shift.
|
|
9
|
+
*
|
|
10
|
+
* Measured 2026-08-21 on Matroska with keyframes every 2 s: `-ss 10` produced a
|
|
11
|
+
* first segment starting at 8.000, `-ss 10.130435` one starting at 10.000. On
|
|
12
|
+
* MP4, where the heuristic does not fire, 10, 10.130435 and 10.2 all produced
|
|
13
|
+
* 10.000 — right in one case, harmless in the other.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import assert from "node:assert/strict";
|
|
17
|
+
import test from "node:test";
|
|
18
|
+
|
|
19
|
+
import { seekLandingOffsetFor } from "../services/hls-session-manager.js";
|
|
20
|
+
|
|
21
|
+
const OFFSET = 3 / 23;
|
|
22
|
+
|
|
23
|
+
test("a copied picture is asked for one heuristic later than the keyframe", () => {
|
|
24
|
+
const session = { transcodeVideo: false, keyframeTimes: [0, 2.002, 4.004, 6.006] };
|
|
25
|
+
assert.equal(seekLandingOffsetFor(session, 2.002), OFFSET);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("a re-encode is asked for exactly what it should produce", () => {
|
|
29
|
+
// It decodes from the keyframe and discards frames up to the requested time,
|
|
30
|
+
// so pushing the request later would start its output late.
|
|
31
|
+
const session = { transcodeVideo: true, keyframeTimes: [0, 2.002, 4.004] };
|
|
32
|
+
assert.equal(seekLandingOffsetFor(session, 2.002), 0);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("the offset never reaches the next keyframe", () => {
|
|
36
|
+
// Keyframes 0.1 s apart: half of that is the most that can be added without
|
|
37
|
+
// risking a landing on the NEXT one where the heuristic does not fire.
|
|
38
|
+
const session = { transcodeVideo: false, keyframeTimes: [0, 0.1, 0.2, 0.3] };
|
|
39
|
+
assert.equal(seekLandingOffsetFor(session, 0.1), 0.05);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("the last keyframe has nothing after it to collide with", () => {
|
|
43
|
+
const session = { transcodeVideo: false, keyframeTimes: [0, 2.002, 4.004] };
|
|
44
|
+
assert.equal(seekLandingOffsetFor(session, 4.004), OFFSET);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("no keyframe list is still answered", () => {
|
|
48
|
+
assert.equal(seekLandingOffsetFor({ transcodeVideo: false }, 5), OFFSET);
|
|
49
|
+
assert.equal(seekLandingOffsetFor(null, 5), OFFSET);
|
|
50
|
+
});
|
|
@@ -33,8 +33,12 @@ async function startRegistry() {
|
|
|
33
33
|
let current = null;
|
|
34
34
|
let opened = 0;
|
|
35
35
|
let everEmpty = false;
|
|
36
|
+
/** Every socket ever accepted, so the server can be shut without waiting. */
|
|
37
|
+
const accepted = new Set();
|
|
36
38
|
server.on("connection", (socket) => {
|
|
37
39
|
opened += 1;
|
|
40
|
+
accepted.add(socket);
|
|
41
|
+
socket.on("close", () => { accepted.delete(socket); });
|
|
38
42
|
const previous = current;
|
|
39
43
|
current = socket;
|
|
40
44
|
// The replacement is registered BEFORE the old one is closed, so a reader
|
|
@@ -52,7 +56,15 @@ async function startRegistry() {
|
|
|
52
56
|
const { port } = server.address();
|
|
53
57
|
return {
|
|
54
58
|
url: `http://127.0.0.1:${port}`,
|
|
55
|
-
close
|
|
59
|
+
// `close` waits for every connection to end, and a renewal can leave one
|
|
60
|
+
// still closing, so they are ended here rather than waited on.
|
|
61
|
+
close: () => new Promise((resolve) => {
|
|
62
|
+
for (const socket of accepted) {
|
|
63
|
+
socket.terminate();
|
|
64
|
+
}
|
|
65
|
+
accepted.clear();
|
|
66
|
+
server.close(() => resolve());
|
|
67
|
+
}),
|
|
56
68
|
registered: () => (current && current.readyState === 1 ? 1 : 0),
|
|
57
69
|
killCurrent: () => { current?.terminate(); },
|
|
58
70
|
opened: () => opened,
|