@torrent-tv/proxy 2.89.6 → 2.89.8

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.
Files changed (92) hide show
  1. package/AGENTS.md +32 -12
  2. package/CHANGELOG.md +2340 -2330
  3. package/assets/calibration/audio/NOTICE.md +19 -19
  4. package/assets/calibration/audio/manifest.json +98 -98
  5. package/biome.json +412 -412
  6. package/docs/browser-proxy-contract.md +155 -155
  7. package/docs/container-architecture.md +273 -273
  8. package/docs/encode-run-state.md +1 -1
  9. package/docs/playback-start-forecast.md +113 -98
  10. package/package.json +51 -50
  11. package/routes/api/sources/files/get.js +106 -106
  12. package/scripts/build-audio-samples.mjs +31 -31
  13. package/services/encode/EncodeRun.js +67 -15
  14. package/services/encode/EncodeRuns.js +19 -7
  15. package/services/encode/OutputOpening.js +784 -784
  16. package/services/encode/audio-calibration.js +67 -67
  17. package/services/encode/audio-work-rate.js +15 -15
  18. package/services/encode/encode-exit.js +2 -0
  19. package/services/encode/hwaccel.js +1894 -1894
  20. package/services/encode/output/index.js +24 -24
  21. package/services/encode/piece-completeness.js +60 -2
  22. package/services/encode/run-command.js +757 -755
  23. package/services/encode/segment-formats/index.js +1 -0
  24. package/services/encode/segment-formats/mp4-boxes.js +8 -1
  25. package/services/media/ContainerOrchestrator.js +202 -202
  26. package/services/media/KeyframeTables.js +356 -356
  27. package/services/media/SubtitleCues.js +592 -592
  28. package/services/media/audio-inventory.js +299 -299
  29. package/services/media/container/AviContainer.js +268 -268
  30. package/services/media/container/Container.js +632 -632
  31. package/services/media/container/ContainerFactory.js +38 -38
  32. package/services/media/container/MatroskaContainer.js +1031 -1031
  33. package/services/media/container/Mp4Container.js +1354 -1354
  34. package/services/media/container/SubtitleFileContainer.js +339 -339
  35. package/services/media/container/ebml-stream.js +174 -174
  36. package/services/media/container/matroska-clusters.js +1165 -1165
  37. package/services/media/container/unavailable.js +77 -77
  38. package/services/media/tracks/ContainerTrack.js +84 -84
  39. package/services/media/tracks/TextSubtitleTrack.js +295 -295
  40. package/services/storage/piece-store/shared-piece-store.js +1937 -1937
  41. package/services/storage/segment-store/SegmentStore.js +1294 -1294
  42. package/services/torrent/Contents.js +382 -382
  43. package/services/torrent/episode-naming.js +182 -182
  44. package/services/torrent/torrent-pool.js +2688 -2688
  45. package/services/torrent/worker/client.js +836 -836
  46. package/services/torrent/worker/held-bytes.js +231 -231
  47. package/services/torrent/worker/worker.js +1137 -1137
  48. package/services/transport/data-channel-handler.js +1373 -1373
  49. package/services/viewer/PriorityMap.js +421 -421
  50. package/test/audio-calibration.test.js +23 -23
  51. package/test/auto-quality-step.test.js +762 -762
  52. package/test/coalesce.test.js +39 -39
  53. package/test/coverage-follows-the-disk.test.js +5 -0
  54. package/test/decode-cost.test.js +475 -475
  55. package/test/encode-run.test.js +58 -0
  56. package/test/episode-naming.test.js +131 -131
  57. package/test/failed-encoder-still-serves-what-exists.test.js +1 -1
  58. package/test/fixtures/audio-frame-boundary.json +1 -1
  59. package/test/fixtures/refused-audio-20261002.json +10 -0
  60. package/test/fmp4-presentation-clock.test.js +4 -2
  61. package/test/format-keyed-outputs.test.js +78 -78
  62. package/test/health-metrics.test.js +11 -13
  63. package/test/held-read-ownership.test.js +77 -77
  64. package/test/helpers/encode-run.js +14 -6
  65. package/test/helpers/held-file.js +77 -77
  66. package/test/helpers/manager.js +62 -62
  67. package/test/helpers/matroska-file.js +296 -296
  68. package/test/keyframe-table-budget.test.js +123 -123
  69. package/test/matroska-cluster-discovery.test.js +323 -323
  70. package/test/one-output-one-session.test.js +273 -273
  71. package/test/output-speed.test.js +6 -1
  72. package/test/piece-completeness.test.js +37 -1
  73. package/test/quality-variants.test.js +1274 -1270
  74. package/test/rate-control-in-the-output.test.js +4 -3
  75. package/test/reads-not-yet-arrived.test.js +218 -218
  76. package/test/seek-frees-the-viewers-requests.test.js +1 -1
  77. package/test/segment-closed-on-both-branches.test.js +114 -114
  78. package/test/segment-judged-by-its-own-header.test.js +222 -222
  79. package/test/segment-serve-wiring.test.js +481 -481
  80. package/test/serving-existing-output.test.js +111 -111
  81. package/test/source-files-route.test.js +113 -113
  82. package/test/subtitle-cue-walk.test.js +385 -385
  83. package/test/subtitle-metadata.test.js +15 -15
  84. package/test/subtitle-plan-waits.test.js +125 -125
  85. package/test/subtitle-track-numbering.test.js +391 -391
  86. package/test/two-viewers-one-picture.test.js +508 -508
  87. package/test/viewer-requests-admission.test.js +92 -92
  88. package/test/viewer-starts-on-map.test.js +27 -27
  89. package/test/viewer-states.test.js +5 -2
  90. package/test/viewing-generations.test.js +390 -390
  91. package/test/whole-file-held-read.test.js +30 -30
  92. package/utils/coalesce.js +41 -41
@@ -106,6 +106,8 @@ export class EncodeRun {
106
106
  /** Half a name left over from the last chunk of the encoder's own channel. */
107
107
  #closedTail = "";
108
108
  #pendingClosed = null;
109
+ #closedTimings = new Map();
110
+ #publicationError = null;
109
111
  #inputTruncated = false;
110
112
  #stderrReadTail = "";
111
113
  #processExited = false;
@@ -550,16 +552,24 @@ export class EncodeRun {
550
552
  // The last piece of the chunk may be half a name; it waits for the rest.
551
553
  this.#closedTail = lines.pop() ?? "";
552
554
  for (const line of lines) {
553
- const name = line.trim();
555
+ const entry = line.trim();
556
+ const csv = /^"?([^",]+)"?,(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)$/.exec(entry);
557
+ const name = csv ? csv[1] : entry;
554
558
  if (name.length === 0) {
555
559
  continue;
556
560
  }
557
- if (this.#stopping) continue;
561
+ if (this.#stopping || this.#publicationError) continue;
562
+ if (csv) {
563
+ this.#closedTimings.set(name, {
564
+ endMicros: BigInt(Math.round(Number(csv[3]) * 1_000_000))
565
+ });
566
+ }
558
567
  // A subsequent closed file proves the previous one reached a cut.
559
568
  // The last file can instead have been flushed by an input failure.
560
569
  if (this.#pendingClosed !== null) {
561
570
  this.#publishClosed(this.#pendingClosed);
562
571
  }
572
+ if (this.#publicationError) break;
563
573
  this.#pendingClosed = name;
564
574
  }
565
575
  }
@@ -570,7 +580,20 @@ export class EncodeRun {
570
580
  // ffmpeg writes a piece under a working name and reports that; the piece
571
581
  // becomes servable by being renamed, and everything below works in the
572
582
  // name a request can actually ask for.
573
- const served = this.onClosed(name);
583
+ let served;
584
+ try {
585
+ served = this.onClosed(name, this.#closedTimings.get(name) ?? null);
586
+ } catch (error) {
587
+ this.#publicationError = error instanceof Error ? error.message : String(error);
588
+ this.lastError = this.#publicationError;
589
+ // A refused closed piece must release this run's claim now, rather
590
+ // than keep its viewer waiting until the rest of the film is encoded.
591
+ this.#continue();
592
+ this.#signal("SIGTERM");
593
+ return false;
594
+ } finally {
595
+ this.#closedTimings.delete(name);
596
+ }
574
597
  if (!served) return false;
575
598
  if (!this.#stopping) {
576
599
  this.#provenName = served;
@@ -640,10 +663,37 @@ export class EncodeRun {
640
663
  // A suspended process does not act on SIGTERM until it is continued, so the
641
664
  // wait for its exit would never end. Let it run before asking it to stop.
642
665
  this.#continue();
666
+ this.#signal("SIGTERM");
667
+ }
668
+
669
+ /**
670
+ * Send a signal to this run's process, and only to it.
671
+ *
672
+ * ONLY A PROCESS THAT STARTED IS SIGNALLED. When the executable cannot be
673
+ * started, Node reports the failure on a later turn, and until then the child
674
+ * holds a handle whose process id is 0. Signalling it then is `kill(0, …)`,
675
+ * which reaches every process in our own process group: the proxy itself, and
676
+ * under `node --test` the test runner. Measured 2026-10-03: a run stopped in
677
+ * the same turn its `ffmpeg` failed to start ended the whole test run
678
+ * ("Interrupted while running") in 5 of 40 runs of one file.
679
+ *
680
+ * Sent through the child object rather than `process.kill(pid)`, so the
681
+ * signal goes to the process this run spawned and to nothing that happens to
682
+ * hold the same number.
683
+ *
684
+ * @param {NodeJS.Signals} signal
685
+ * @returns {boolean} Whether the signal was delivered.
686
+ */
687
+ #signal(signal) {
688
+ const child = this.#process;
689
+ if (!child || !(Number(child.pid) > 0)) {
690
+ return false;
691
+ }
643
692
  try {
644
- this.#process.kill("SIGTERM");
693
+ return child.kill(signal) !== false;
645
694
  } catch {
646
695
  // Best effort: it may already be gone, and its exit will say so.
696
+ return false;
647
697
  }
648
698
  }
649
699
 
@@ -662,7 +712,9 @@ export class EncodeRun {
662
712
  return false;
663
713
  }
664
714
  try {
665
- globalThis.process.kill(this.#process.pid, "SIGSTOP");
715
+ // A platform without the signal throws here (ENOSYS), which is what
716
+ // marks suspension unsupported below.
717
+ this.#process.kill("SIGSTOP");
666
718
  } catch (error) {
667
719
  this.#pauseUnsupported = true;
668
720
  this.logger.info(
@@ -708,15 +760,10 @@ export class EncodeRun {
708
760
  if (this.#state !== ENCODE_RUN_STATE.SUSPENDED || !this.#process?.pid) {
709
761
  return false;
710
762
  }
711
- let continued = true;
712
- try {
713
- globalThis.process.kill(this.#process.pid, "SIGCONT");
714
- } catch {
715
- // The process is gone; its exit handler will deal with it. The state is
716
- // moved either way — but nothing was resumed, and saying so is what stops
717
- // a dead run being reported as producing again.
718
- continued = false;
719
- }
763
+ // The process may be gone; its exit handler will deal with it. The state
764
+ // is moved either way — but nothing was resumed, and saying so is what
765
+ // stops a dead run being reported as producing again.
766
+ const continued = this.#signal("SIGCONT");
720
767
  this.#transition(ENCODE_RUN_EVENT.RESUMED);
721
768
  this.clock.continued();
722
769
  return continued;
@@ -727,6 +774,10 @@ export class EncodeRun {
727
774
  * @param {string | null} signal
728
775
  */
729
776
  #onExit(code, signal) {
777
+ if (this.#publicationError) {
778
+ this.#finish(ENCODE_EXIT.PUBLICATION_FAILED, this.#publicationError, code, signal);
779
+ return;
780
+ }
730
781
  if (this.#stopping) {
731
782
  this.#finish(ENCODE_EXIT.STOPPED, this.#stopReason, code, signal);
732
783
  return;
@@ -749,7 +800,8 @@ export class EncodeRun {
749
800
  if (outcome === ENCODE_EXIT.COMPLETE) {
750
801
  if (this.#pendingClosed !== null && !this.#inputTruncated) {
751
802
  if (!this.#publishClosed(this.#pendingClosed)) {
752
- this.#finish(ENCODE_EXIT.SHORT, "its final file could not be published", code, signal);
803
+ this.#finish(this.#publicationError ? ENCODE_EXIT.PUBLICATION_FAILED : ENCODE_EXIT.SHORT,
804
+ this.#publicationError ?? "its final file could not be published", code, signal);
753
805
  return;
754
806
  }
755
807
  this.#pendingClosed = null;
@@ -15,7 +15,7 @@ import { ENCODE_EXIT } from "./encode-exit.js";
15
15
  import { EncodeRun } from "./EncodeRun.js";
16
16
  import { computeOutputDimensions } from "./args.js";
17
17
  import { buildRunCommand, trueStartOf } from "./run-command.js";
18
- import { cutOf, judgePiece } from "./piece-completeness.js";
18
+ import { cutOf, judgePiece, originOf, productionOf } from "./piece-completeness.js";
19
19
 
20
20
  /**
21
21
  * @typedef {Object} SegmentFiles
@@ -346,7 +346,7 @@ export class EncodeRuns {
346
346
  * @param {number} index
347
347
  * @returns {object | null | false}
348
348
  */
349
- #wholeClosedPiece(session, name, index) {
349
+ #wholeClosedPiece(session, name, index, production = null) {
350
350
  const format = session.segmentFormat;
351
351
  if (!format?.readMediaRanges || !Number.isInteger(index) || index < 0) {
352
352
  return null;
@@ -354,6 +354,7 @@ export class EncodeRuns {
354
354
  const address = session.outputKey ?? "";
355
355
  try {
356
356
  const mediaRanges = format.readMediaRanges(this.#host.segmentFiles.closedBytesOf(address, name));
357
+ if (mediaRanges && production) mediaRanges.production = production;
357
358
  const cut = cutOf(session.timeline, index);
358
359
  const { whole, throughSeconds } = judgePiece(format, mediaRanges, cut);
359
360
  if (!whole) {
@@ -851,7 +852,7 @@ export class EncodeRuns {
851
852
  this.#lastInputToken += 1;
852
853
  const inputToken = this.#lastInputToken;
853
854
  const inputs = this.#host.inputOf(session, inputToken);
854
- const { args, safeIndex, startSeconds, cutTimes } = buildRunCommand({
855
+ const { args, safeIndex, inputIndex, startSeconds, cutTimes } = buildRunCommand({
855
856
  keyframes: session.keyframes,
856
857
  inputFile: inputs.inputFile,
857
858
  audioFile: inputs.audioFile,
@@ -897,6 +898,8 @@ export class EncodeRuns {
897
898
  // killed mid-piece leaving a partial file — is not a correctness problem
898
899
  // either, because the store serves a segment only once its closure is
899
900
  // proven, and it is cleared up when the run ends.
901
+ let originMicros = null;
902
+ const referenceKind = session.spec.video ? "vide" : "soun";
900
903
  const run = new EncodeRun({
901
904
  address: session.outputKey ?? session.id,
902
905
  encoder: this.#host.videoEncoder,
@@ -929,13 +932,22 @@ export class EncodeRuns {
929
932
  // Why this encoder exists, recorded with its argument list. It used to be
930
933
  // handed to a separate `start` call; there is no separate call now.
931
934
  because,
932
- onClosed: (name) => {
935
+ onClosed: (name, timing) => {
933
936
  const index = session.segmentFormat.segmentIndexFromName(
934
937
  session.segmentFormat.servedNameOf?.(name) ?? name);
938
+ if (timing && originMicros === null && session.segmentFormat.readMediaRanges) {
939
+ const ranges = session.segmentFormat.readMediaRanges(
940
+ this.#host.segmentFiles.closedBytesOf(session.outputKey ?? "", name));
941
+ originMicros = originOf(ranges, timing.endMicros, referenceKind);
942
+ }
935
943
  if (Number.isInteger(index) && index < safeIndex) return null;
936
- const mediaRanges = this.#wholeClosedPiece(session, name, index);
937
- if (mediaRanges === false) return null;
938
- return this.#host.segmentFiles.publish(session.outputKey ?? "", name, session.segmentFormat, { mediaRanges });
944
+ const production = productionOf(timing ? { ...timing, originMicros } : null,
945
+ cutTimes, index - inputIndex, referenceKind);
946
+ const mediaRanges = this.#wholeClosedPiece(session, name, index, production);
947
+ if (mediaRanges === false) throw new Error(`Closed piece #${index} could not be published: its media does not reach its muxer cut or is unreadable.`);
948
+ const published = this.#host.segmentFiles.publish(session.outputKey ?? "", name, session.segmentFormat, { mediaRanges });
949
+ if (!published) throw new Error(`Closed piece #${index} could not be published to the segment store.`);
950
+ return published;
939
951
  },
940
952
  onSpeedMeasured: () => this.#host.noteRunSpeedMeasured?.(session, run),
941
953
  onEnded: (ended) => {