@torrent-tv/proxy 2.14.2 → 2.14.3

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,9 @@
1
+ ## 2.14.3
2
+
3
+ - **Fix**: A separately published audio track begins where the PICTURE is, measured rather than guessed. The position this class keeps is the read head, and the viewer sits behind it by whatever the player has buffered — a figure the browser already reports with every link report, so the playhead is one subtraction away (less one segment of margin, since the report can be ten seconds old). 2.14.2 subtracted the whole look-ahead instead, which was safe but made the encoder produce up to two minutes of audio nobody would hear before reaching the part that was wanted. A report older than fifteen seconds is ignored — a viewer may have seeked since — and then the whole look-ahead is subtracted as before.
4
+ - **Fix**: A request behind the encode run is acted on when the player ASKS AGAIN, not after three seconds of waiting. Repetition is the player saying it still needs that exact segment; a delay only says time has passed, and those three seconds were part of the twenty a track change cost. A scan is told apart by what else is being asked for — more than three distinct segments behind the run within two seconds is the player sweeping the playlist, and moving the encoder to one of them would be moving it to a number picked at random.
5
+ - **Fix**: That scan count is taken over a two-second window rather than over the life of the run. Accumulated, it would have crossed the threshold on any long session and disabled the repair for good — silently, since nothing about a repair that never fires is logged.
6
+
1
7
  ## 2.14.2
2
8
 
3
9
  - **Fix**: A separately published audio track starts BEHIND the picture's read head, and a request behind its run is answered as before. Two mistakes compounded in 2.14.1 and left the viewer on a spinner that never ended. The position this class keeps is written by the segments a session serves — the READ head — while the viewer's picture sits behind it by everything they have buffered, so the track was started AHEAD of them: field 2026-08-15, the run placed at segment #16 while the player asked for #10. On top of that, 2.14.1 had begun answering such a request "not found" at once instead of holding it, which turned a condition the encoder used to correct in twenty seconds into a permanent refusal: hls.js retried #10 for a minute and a half, raised a fatal network error, recovered, and retried it again. The prompt refusal is withdrawn — it was written for a probe and met a real request — and the track now starts a whole look-ahead behind the read head, which is exactly how far apart the two can be. The price is audio the player already holds: at ten to twenty times realtime and 75 KB a piece, a second or two of work.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torrent-tv/proxy",
3
- "version": "2.14.2",
3
+ "version": "2.14.3",
4
4
  "description": "Torrent proxy client that exposes webseed-like HTTP stream endpoint.",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "publishConfig": {
@@ -354,6 +354,10 @@ const READ_WINDOW_SECONDS = 30;
354
354
  const READ_WINDOW_MIN_BYTES = 16 * 1024 * 1024;
355
355
  const READ_WINDOW_MAX_BYTES = 96 * 1024 * 1024;
356
356
  const LOOKAHEAD_PAUSE_SECONDS = 120;
357
+ // How old a viewer's link report may be and still describe where they are. It
358
+ // is sent every 10 s, and a seek in between moves them somewhere this cannot
359
+ // predict — so anything older is treated as no report at all.
360
+ const NET_REPORT_FRESH_MS = 15_000;
357
361
  const LOOKAHEAD_RESUME_SECONDS = 60;
358
362
  // Seek debounce. A far (out-of-window) segment request is a server-side seek.
359
363
  // Rather than restart ffmpeg on the first one, wait a short quiet period:
@@ -389,7 +393,18 @@ const SEEK_SETTLE_MS = 300;
389
393
  // seek settles on its own — the seek is what should move the encoder — and
390
394
  // short enough that a session cannot sit on an unanswerable request, which
391
395
  // measured two minutes forty-one before a viewer gave up.
392
- const BEHIND_HEAD_REPAIR_MS = 3_000;
396
+ // A request behind the run is acted on once it has been REPEATED, not once it
397
+ // has waited: repetition is the player saying it still needs this exact
398
+ // segment, while a delay only says time has passed. The floor below stays as a
399
+ // last guard against acting on a single stray poll.
400
+ const BEHIND_HEAD_REPAIR_MIN_ASKS = 2;
401
+ // More distinct indices than this behind the head at once is the player
402
+ // scanning the playlist rather than waiting for a frame.
403
+ const BEHIND_HEAD_SCAN_INDICES = 3;
404
+ // The window the count above is taken over. A player's scan lands inside half a
405
+ // second (field log 2026-08-02); a viewer waiting asks every few seconds.
406
+ const BEHIND_HEAD_SCAN_WINDOW_MS = 2_000;
407
+ const BEHIND_HEAD_REPAIR_MS = 400;
393
408
  // How far behind the run a request may be and still be treated as the encoder
394
409
  // standing in the wrong place rather than as a player scanning the playlist. A
395
410
  // misplaced run is out by at most the buffer the player was holding — measured
@@ -2886,6 +2901,7 @@ export class HlsSessionManager {
2886
2901
  // for the seek that should move the encoder. It also stops the map growing
2887
2902
  // for the life of a session.
2888
2903
  session.firstWantedAt = new Map();
2904
+ session.behindHeadAsks = new Map();
2889
2905
  const generation = ++session.encodeRunGeneration;
2890
2906
  const previousFfmpeg = session.ffmpeg;
2891
2907
  // A suspended process does not act on SIGTERM until it is continued, so the
@@ -3461,6 +3477,16 @@ export class HlsSessionManager {
3461
3477
  if (!session.firstWantedAt.has(index)) {
3462
3478
  session.firstWantedAt.set(index, Date.now());
3463
3479
  }
3480
+ // How often each index behind the run has been asked for, and how many
3481
+ // distinct ones there are. The repair reads both: one index asked twice is
3482
+ // a viewer waiting, a dozen asked once each is the player scanning. Kept
3483
+ // only for what is behind the head — everything ahead is ordinary
3484
+ // read-ahead — and cleared with each run, like the record above.
3485
+ if (index < session.encodeStartIndex) {
3486
+ session.behindHeadAsks ??= new Map();
3487
+ const asked = session.behindHeadAsks.get(index);
3488
+ session.behindHeadAsks.set(index, { count: (asked?.count ?? 0) + 1, at: Date.now() });
3489
+ }
3464
3490
  if (!session || session.state === "disposed" || index < 0) {
3465
3491
  return;
3466
3492
  }
@@ -3580,6 +3606,37 @@ export class HlsSessionManager {
3580
3606
  if (session.seekSettleTimer != null) {
3581
3607
  return;
3582
3608
  }
3609
+ // What separates a request the viewer is waiting for from the player
3610
+ // scanning the playlist is not TIME but what else it is asking for. On a
3611
+ // seek hls.js fires dozens of DIFFERENT indices within half a second (field
3612
+ // log: #178, #681, #725, #807, #74, #245, #387) and abandons them all; a
3613
+ // viewer waiting for audio asks for the SAME one, over and over, because it
3614
+ // is the only thing that will let playback continue.
3615
+ //
3616
+ // So: this index has been asked for at least twice, and it is the only
3617
+ // thing behind the head being asked for. Both are facts about the traffic,
3618
+ // available at once, where a delay is a guess about it — and it was three
3619
+ // seconds of the twenty a track change cost on 2026-08-15.
3620
+ const asked = session.behindHeadAsks?.get(index)?.count ?? 0;
3621
+ if (asked < BEHIND_HEAD_REPAIR_MIN_ASKS) {
3622
+ return;
3623
+ }
3624
+ // Counted over a WINDOW, not over the run: a scan is many indices at once,
3625
+ // while the same map left to accumulate would eventually hold every
3626
+ // behind-head request a long run ever saw and switch the repair off for
3627
+ // good.
3628
+ const scanSince = Date.now() - BEHIND_HEAD_SCAN_WINDOW_MS;
3629
+ let distinctBehind = 0;
3630
+ for (const record of session.behindHeadAsks?.values() ?? []) {
3631
+ if (record.at >= scanSince) {
3632
+ distinctBehind += 1;
3633
+ }
3634
+ }
3635
+ if (distinctBehind > BEHIND_HEAD_SCAN_INDICES) {
3636
+ // A scan, not a wait. Moving the encoder to one of these is moving it to
3637
+ // a number the player picked at random.
3638
+ return;
3639
+ }
3583
3640
  const wantedAt = session.firstWantedAt?.get(index);
3584
3641
  if (!Number.isFinite(wantedAt) || Date.now() - wantedAt < BEHIND_HEAD_REPAIR_MS) {
3585
3642
  return;
@@ -4751,6 +4808,37 @@ export class HlsSessionManager {
4751
4808
  return this.#viewerPositionOf(this.#activeVariant(base));
4752
4809
  }
4753
4810
 
4811
+ /**
4812
+ * Where to start a separately published audio track, in seconds.
4813
+ *
4814
+ * The player, on changing track, discards the audio it holds and refills from
4815
+ * the PICTURE onwards — so that is where the encoder has to begin. What this
4816
+ * class knows directly is the read head, which runs ahead of the picture by
4817
+ * the player's own buffer; the browser reports that buffer with every link
4818
+ * report, so the picture is the one subtraction below.
4819
+ *
4820
+ * One segment of margin, because the report is up to ten seconds old and the
4821
+ * picture has moved on since — a run that begins a little early costs a
4822
+ * segment of audio nobody plays, while one that begins a little late is
4823
+ * behind the viewer and can only be fixed by restarting it.
4824
+ *
4825
+ * With no fresh report, the whole look-ahead is subtracted instead: it is the
4826
+ * furthest the two can be apart, so it cannot leave the run ahead of them.
4827
+ *
4828
+ * @param {HlsSession} base
4829
+ * @returns {number}
4830
+ */
4831
+ #audioStartSecondsFor(base) {
4832
+ const watching = this.#activeVariant(base);
4833
+ const readHead = this.#viewerPositionOf(watching);
4834
+ const report = watching.netReport;
4835
+ const reportAge = Number.isFinite(report?.at) ? Date.now() - report.at : Number.POSITIVE_INFINITY;
4836
+ const buffered = reportAge <= NET_REPORT_FRESH_MS && Number.isFinite(report?.bufferedAheadSec)
4837
+ ? report.bufferedAheadSec
4838
+ : LOOKAHEAD_PAUSE_SECONDS;
4839
+ return Math.max(0, readHead - buffered - this.segmentDurationSec);
4840
+ }
4841
+
4754
4842
  #viewerPositionOf(session) {
4755
4843
  if (Number.isFinite(session.viewerPositionSeconds) && session.viewerPositionSeconds > 0) {
4756
4844
  return session.viewerPositionSeconds;
@@ -5334,26 +5422,23 @@ export class HlsSessionManager {
5334
5422
  // while the player is asking for segment #537 — and the audio would begin
5335
5423
  // at zero and never catch up, since nothing treats a far request as a
5336
5424
  // seek. The accessor falls back to the last segment actually requested.
5337
- // Behind where the picture has been READ to, by the whole look-ahead.
5425
+ // Where the PICTURE is, not where it has been read to.
5338
5426
  //
5339
5427
  // The position this class keeps is written by the segments a session
5340
- // serves, so it is the READ head, and the player's picture sits behind it
5341
- // by everything it has buffered up to the look-ahead cap. Starting the
5342
- // audio at the read head therefore starts it AHEAD of the viewer, and
5343
- // every request they then make is behind a run that only moves forward.
5344
- // Field 2026-08-15: the run was placed at #16, the player asked for #10,
5345
- // and the audio never arrived until the encoder was dragged back.
5428
+ // serves, so it is the READ head, and the viewer's picture sits behind it
5429
+ // by everything the player has buffered. Started at the read head, the
5430
+ // audio run begins AHEAD of the viewer, and every request they then make
5431
+ // is behind a run that only moves forward — field 2026-08-15, placed at
5432
+ // #16 while the player asked for #10, and the audio arrived only after
5433
+ // the encoder was dragged back.
5346
5434
  //
5347
- // The cap is exactly how far apart the two can be, so subtracting it
5348
- // cannot leave the run ahead of the viewer. The price is audio the player
5349
- // already has for a track being encoded at ten to twenty times realtime
5350
- // and served in 75 KB pieces, that is a second or two of work.
5351
- startPositionSeconds: Math.max(
5352
- 0,
5353
- Math.floor(
5354
- (this.#viewerPositionOf(this.#activeVariant(base)) - LOOKAHEAD_PAUSE_SECONDS) / 10
5355
- ) * 10
5356
- ),
5435
+ // The distance is measured, not assumed: the browser reports how many
5436
+ // seconds it holds ahead of the picture with every link report, so the
5437
+ // playhead is one subtraction away. A stale report is no use a viewer
5438
+ // who seeked since then is somewhere else entirely so an old one is
5439
+ // ignored and the whole look-ahead is subtracted instead, which cannot
5440
+ // leave the run ahead of them.
5441
+ startPositionSeconds: this.#audioStartSecondsFor(base),
5357
5442
  segmentFormatId: base.segmentFormat.id,
5358
5443
  // Cut where the picture is cut. Two streams meant to be played together
5359
5444
  // have to be divided at the same times, and the grid is the base's — the
@@ -71,16 +71,23 @@ async function managerWithRunAhead() {
71
71
  return { manager, session, dirPath, restarts };
72
72
  }
73
73
 
74
- test("a request behind the run is repaired once it has waited", async (t) => {
74
+ test("a request behind the run is repaired once the player asks again", async (t) => {
75
75
  const { manager, session, dirPath } = await managerWithRunAhead();
76
76
  t.after(async () => {
77
77
  await manager.disposeAll();
78
78
  await rm(dirPath, { recursive: true, force: true });
79
79
  });
80
- // Asked for four seconds ago and still unanswerable.
81
- session.firstWantedAt.set(WANTED, Date.now() - 4000);
80
+ // Asked for a moment ago and still unanswerable. What decides is that the
81
+ // player comes BACK for the same segment: it is the only one it wants, and no
82
+ // amount of waiting could say that as clearly.
83
+ session.firstWantedAt.set(WANTED, Date.now() - 1000);
84
+ const name = fmp4Format.segmentFileName(WANTED);
82
85
 
83
- await manager.getFileStream(SESSION_ID, fmp4Format.segmentFileName(WANTED), { requestSeq: 1 });
86
+ await manager.getFileStream(SESSION_ID, name, { requestSeq: 1 });
87
+
88
+ assert.equal(session.seekTarget, null, "one poll is not yet evidence");
89
+
90
+ await manager.getFileStream(SESSION_ID, name, { requestSeq: 2 });
84
91
 
85
92
  assert.equal(
86
93
  session.seekTarget,
@@ -89,6 +96,33 @@ test("a request behind the run is repaired once it has waited", async (t) => {
89
96
  );
90
97
  });
91
98
 
99
+ test("a burst of different segments behind the run is a scan, and moves nothing", async (t) => {
100
+ const { manager, session, dirPath } = await managerWithRunAhead();
101
+ t.after(async () => {
102
+ await manager.disposeAll();
103
+ await rm(dirPath, { recursive: true, force: true });
104
+ });
105
+ // What hls.js does on a seek: dozens of indices within half a second, each
106
+ // abandoned. Field log 2026-08-02: #178, #681, #725, #807, #74, #245, #387.
107
+ const scanned = [WANTED, WANTED - 40, WANTED - 120, WANTED - 200, WANTED - 300];
108
+ for (const index of scanned) {
109
+ session.firstWantedAt.set(index, Date.now() - 1000);
110
+ }
111
+ // Interleaved, as they arrive on the wire: the player opens them together
112
+ // rather than finishing with one before opening the next.
113
+ for (const seq of [1, 2]) {
114
+ for (const index of scanned) {
115
+ await manager.getFileStream(SESSION_ID, fmp4Format.segmentFileName(index), { requestSeq: seq });
116
+ }
117
+ }
118
+
119
+ assert.equal(
120
+ session.seekTarget,
121
+ null,
122
+ "moving the encoder to one of these would be moving it to a number the player picked at random"
123
+ );
124
+ });
125
+
92
126
  test("a request behind the run is left alone at first", async (t) => {
93
127
  const { manager, session, dirPath } = await managerWithRunAhead();
94
128
  t.after(async () => {
@@ -638,19 +638,18 @@ test("a rung served by copy stays offered while a re-encoded rung is on screen",
638
638
  );
639
639
  });
640
640
 
641
- test("a separately published audio track starts behind the picture's read head", async (t) => {
641
+ test("a separately published audio track starts where the picture is, from the reported buffer", async (t) => {
642
642
  const { manager, base, dirPath } = await managerWithBase();
643
643
  t.after(async () => {
644
644
  await manager.disposeAll();
645
645
  await rm(dirPath, { recursive: true, force: true });
646
646
  });
647
- // What broke playback on 2026-08-15: the position this class keeps is where
648
- // segments have been SERVED to, and the viewer's picture is behind it by
649
- // everything they have buffered. Started at the read head, the audio run sat
650
- // ahead of the viewer and every request they made was behind a run that only
651
- // moves forward.
652
- base.viewerPositionSeconds = 140;
653
647
  base.audioSeparate = true;
648
+ // Served up to 140 s, and the browser says it holds 40 s ahead of the
649
+ // picture — so the viewer is at 100 s, and that, less a segment of margin,
650
+ // is where the track has to begin.
651
+ base.viewerPositionSeconds = 140;
652
+ base.netReport = { linkMbps: 20, bufferedAheadSec: 40, at: Date.now() };
654
653
  manager.getCachedAudioTracks = () => [
655
654
  { index: 0, language: "rus", title: "", isDefault: true },
656
655
  { index: 1, language: "eng", title: "", isDefault: false }
@@ -663,13 +662,44 @@ test("a separately published audio track starts behind the picture's read head",
663
662
  return { sessionId: VARIANT_ID, session: rendition };
664
663
  };
665
664
 
666
- // A segment, not the playlist: the playlist is answered from the base and
667
- // deliberately starts no encoder.
668
665
  await manager.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4");
669
666
 
670
667
  assert.equal(created.length, 1, "the track's own session was made");
671
- assert.ok(
672
- created[0].startPositionSeconds <= 20,
673
- `started behind the picture, not at the read head — got ${created[0].startPositionSeconds}s for a read head of 140s`
668
+ assert.equal(
669
+ created[0].startPositionSeconds,
670
+ 96,
671
+ "140 s served, less the 40 s the player holds, less one segment of margin"
672
+ );
673
+ });
674
+
675
+ test("a stale buffer report is not used to place an audio track", async (t) => {
676
+ const { manager, base, dirPath } = await managerWithBase();
677
+ t.after(async () => {
678
+ await manager.disposeAll();
679
+ await rm(dirPath, { recursive: true, force: true });
680
+ });
681
+ base.audioSeparate = true;
682
+ base.viewerPositionSeconds = 300;
683
+ // Sent a minute ago: the viewer may have seeked anywhere since, so it says
684
+ // nothing about where they are now.
685
+ base.netReport = { linkMbps: 20, bufferedAheadSec: 5, at: Date.now() - 60_000 };
686
+ manager.getCachedAudioTracks = () => [
687
+ { index: 0, language: "rus", title: "", isDefault: true },
688
+ { index: 1, language: "eng", title: "", isDefault: false }
689
+ ];
690
+ const created = [];
691
+ manager.createOrGetSession = async (params) => {
692
+ created.push(params);
693
+ const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath });
694
+ rendition.audioOnly = true;
695
+ return { sessionId: VARIANT_ID, session: rendition };
696
+ };
697
+
698
+ await manager.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4");
699
+
700
+ assert.equal(
701
+ created[0].startPositionSeconds,
702
+ 176,
703
+ "the whole look-ahead is subtracted instead — it cannot leave the run ahead of the viewer"
674
704
  );
675
705
  });