@torrent-tv/proxy 2.27.0 → 2.28.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 CHANGED
@@ -1,3 +1,8 @@
1
+ ## 2.28.0
2
+
3
+ - **Fix**: The playlist and the media agree again, and the container's keyframe table was never at fault. On a file whose first timestamp is 2.002 s, the copied picture was asked to cut at 808.808 s on the 0-based grid and cut at 806.806 s — exactly the container's start time early, because that branch keeps the source's own timestamps and re-labels the output afterwards, so a cut list stated in 0-based terms is applied 2 s away from where it means. The soundtrack, re-encoded and on the other branch, cut where it was asked. The two then wrote different values into the shared boundary table and corrected each other for the whole session (#202: 808.808 → 806.806 → 808.750 → …), the playlist drifted a whole segment from the media, and the player refetched fragments it could not place. The cut list is now stated in the source's terms on that branch, which is the same shift the seek on it already applies.
4
+ - **Chore**: Which timeline a session works on is answered by one exported predicate instead of two expressions that could disagree — and their disagreement is exactly what desynced picture from sound. Pinned by `test/cut-times-timeline.test.js`, with the field numbers in its header.
5
+
1
6
  ## 2.27.0
2
7
 
3
8
  - **Fix**: Picture and sound now begin a run at the same instant. They were asked for the same time and landed in different places: a copied picture may begin only at a real keyframe and may not begin before the time asked for — that content belongs to the previous segment — so it moves FORWARD to the next keyframe, by up to the keyframe spacing (0.58-2.96 s measured on the field file); a soundtrack has no keyframes and begins exactly where asked, to within one audio frame. So after every seek the two runs of one film began up to three seconds apart. The picture's true start is measured from the piece it produces, and that measurement now moves every other member of the family whose run begins at the same boundary. Restarted at the boundary rather than seeked to the time, deliberately: a seek decides by segment index, finds the run already begins there and answers "already within the running encode" — true about the index and false about the instant, which is why the first version of this fix moved nothing at all.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.27.0",
3
+ "version": "2.28.0",
4
4
  "description": "Torrent proxy client that exposes webseed-like HTTP stream endpoint.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "publishConfig": {
@@ -1019,6 +1019,29 @@ export function ffmpegSeconds(value) {
1019
1019
  * @param {{ useKeyframeGrid: boolean, durationSeconds: number, segDur: number, keyframeTimes: number[] | null, startTime: number }} params
1020
1020
  * @returns {number[]}
1021
1021
  */
1022
+ /**
1023
+ * Which timeline a session's own ffmpeg works on.
1024
+ *
1025
+ * True — the COPY branch: the source's timestamps are kept (`-copyts`) and the
1026
+ * output is re-labelled 0-based. Everything handed to the muxer is therefore
1027
+ * stated in the source's terms, and everything read back out of a produced
1028
+ * piece is 0-based.
1029
+ *
1030
+ * False — the re-encode branch: the output is labelled from the run's start on
1031
+ * the 0-based timeline, and the muxer is addressed in those same terms.
1032
+ *
1033
+ * One predicate for both callers, because the two used to answer it separately
1034
+ * and a disagreement between them is exactly what desynced picture from sound.
1035
+ *
1036
+ * @param {{ audioOnly?: boolean, cutGrid?: string, transcodeVideo?: boolean }} session
1037
+ * @returns {boolean}
1038
+ */
1039
+ export function onKeyframeGridFor(session) {
1040
+ return session?.audioOnly === true
1041
+ ? session?.cutGrid === "keyframe"
1042
+ : session?.transcodeVideo !== true;
1043
+ }
1044
+
1022
1045
  export function computeSegmentBoundaries({ useKeyframeGrid, durationSeconds, segDur, keyframeTimes, startTime }) {
1023
1046
  const total = Number.isFinite(durationSeconds) && durationSeconds > 0 ? durationSeconds : 0;
1024
1047
  const step = Number.isFinite(segDur) && segDur > 0 ? segDur : 4;
@@ -3541,9 +3564,33 @@ export class HlsSessionManager {
3541
3564
  // writes no self-contained pieces, so nothing could read a true start and
3542
3565
  // segments were stamped with times the file does not have — the 4.17 s
3543
3566
  // speech-against-subtitles drift, back again.
3544
- const cutTimes = explicitTimes && (!session.transcodeVideo || session.cutGrid === "keyframe")
3567
+ const gridCutTimes = explicitTimes && (!session.transcodeVideo || session.cutGrid === "keyframe")
3545
3568
  ? segmentCutTimesFrom(session.segmentBoundaries, safeIndex)
3546
3569
  : null;
3570
+ // On the COPY branch the muxer decides its cuts against the source's own
3571
+ // timestamps, not against the labels we ask it to write. That branch keeps
3572
+ // the source's timestamps (`-copyts`) and re-labels the output 0-based with
3573
+ // `-output_ts_offset -sourceStartTime`; the cut list, being applied before
3574
+ // that relabelling, must therefore be stated in the SOURCE's terms.
3575
+ //
3576
+ // Measured 2026-08-17, and this is the whole of the trouble: asked to cut
3577
+ // at 808.808 s on the 0-based grid, ffmpeg cut at 806.806 s — exactly
3578
+ // `sourceStartTime` (2.002 s) early, and 806.806 s is itself a keyframe the
3579
+ // container's table names, which is why every "disagreement" landed on
3580
+ // another real keyframe. The soundtrack, which is re-encoded and takes the
3581
+ // other branch, cut where it was asked. The two then told the shared
3582
+ // boundary table different things and corrected each other back and forth
3583
+ // for the whole session (#202: 808.808 → 806.806 → 808.750 → …), so the
3584
+ // playlist and the media drifted apart by a whole segment and the player
3585
+ // refetched what it could not place.
3586
+ //
3587
+ // Nothing here is a guess about ffmpeg's semantics: the shift is the same
3588
+ // one the seek already applies on this branch (`seekSeconds = startSeconds
3589
+ // + sourceStartTime`), and the field measurement above is what says the
3590
+ // cuts needed it too.
3591
+ const cutTimes = gridCutTimes && onKeyframeGridFor(session) && sourceStartTime !== 0
3592
+ ? gridCutTimes.map((time) => Number((time + sourceStartTime).toFixed(6)))
3593
+ : gridCutTimes;
3547
3594
 
3548
3595
  // A second chance for a predecessor that survived the escalation above —
3549
3596
  // the first block is the one that does the work. Its exit is ignored
@@ -3641,9 +3688,7 @@ export class HlsSessionManager {
3641
3688
  // the copy branch: `-copyts` and a shift by the container's start time,
3642
3689
  // against a picture labelled from zero. The two would be offset by
3643
3690
  // `sourceStartTime` for the whole file.
3644
- const onKeyframeGrid = session.audioOnly === true
3645
- ? session.cutGrid === "keyframe"
3646
- : !session.transcodeVideo;
3691
+ const onKeyframeGrid = onKeyframeGridFor(session);
3647
3692
  if (!onKeyframeGrid) {
3648
3693
  // Branch A (re-encode): fixed GOP makes keyframes land exactly on the
3649
3694
  // segment grid; relabel output onto the original timeline so segment N
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @file The cut list must be stated on the timeline the muxer decides against.
3
+ *
4
+ * Measured 2026-08-17 on a Matroska whose first timestamp is 2.002 s. Asked to
5
+ * cut at 808.808 s on the 0-based grid, the copied picture cut at 806.806 s —
6
+ * exactly the container's start time early, and itself a keyframe the file's
7
+ * own table names, which is why every "disagreement" landed on a real keyframe
8
+ * and the table looked like it was lying. The soundtrack, re-encoded and on the
9
+ * other branch, cut where it was asked. The two then wrote different values
10
+ * into the shared boundary table and corrected each other back and forth
11
+ * (#202: 808.808 → 806.806 → 808.750), so the playlist and the media drifted
12
+ * apart by a whole segment.
13
+ */
14
+
15
+ import assert from "node:assert/strict";
16
+ import test from "node:test";
17
+
18
+ import { onKeyframeGridFor } from "../services/hls-session-manager.js";
19
+
20
+ test("a copied picture works on the source's timeline", () => {
21
+ assert.equal(
22
+ onKeyframeGridFor({ transcodeVideo: false }),
23
+ true,
24
+ "video copied means the source's own timestamps are kept"
25
+ );
26
+ });
27
+
28
+ test("a re-encoded picture works on the 0-based timeline", () => {
29
+ assert.equal(onKeyframeGridFor({ transcodeVideo: true }), false);
30
+ });
31
+
32
+ test("a soundtrack follows the grid it was cut on, not its own encoding", () => {
33
+ // A rendition is re-encoded by definition, so asking `transcodeVideo` about
34
+ // it answers nothing. What decides its timeline is the grid it shares with
35
+ // the picture it plays with.
36
+ assert.equal(onKeyframeGridFor({ audioOnly: true, cutGrid: "keyframe" }), true);
37
+ assert.equal(onKeyframeGridFor({ audioOnly: true, cutGrid: "uniform" }), false);
38
+ assert.equal(
39
+ onKeyframeGridFor({ audioOnly: true, cutGrid: "keyframe", transcodeVideo: true }),
40
+ true,
41
+ "its own encoding must not decide this — that disagreement is the defect"
42
+ );
43
+ });
44
+
45
+ test("one predicate answers for every caller", () => {
46
+ // The two callers used to answer this separately, in two expressions that
47
+ // could drift apart. A session put through both must get one answer.
48
+ for (const session of [
49
+ { transcodeVideo: false },
50
+ { transcodeVideo: true },
51
+ { audioOnly: true, cutGrid: "keyframe" },
52
+ { audioOnly: true, cutGrid: "uniform" },
53
+ {}
54
+ ]) {
55
+ assert.equal(onKeyframeGridFor(session), onKeyframeGridFor({ ...session }));
56
+ }
57
+ });
58
+
59
+ test("a session that says nothing is treated as copying", () => {
60
+ // The default matters: an absent `transcodeVideo` means the picture is
61
+ // copied, which is the branch that needs the shift.
62
+ assert.equal(onKeyframeGridFor({}), true);
63
+ assert.equal(onKeyframeGridFor(null), true);
64
+ });