@torrent-tv/proxy 2.91.0 → 2.92.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.
Files changed (145) hide show
  1. package/bin/cli.js +3 -2
  2. package/package.json +18 -2
  3. package/routes/api/playback-plan/post.js +21 -4
  4. package/routes/api/sources/files/get.js +20 -28
  5. package/routes/api/sources/post.js +4 -2
  6. package/routes/api/sources/stats/get.js +4 -11
  7. package/routes/api/sources/viewer/post.js +16 -0
  8. package/routes/api/sources/warm/post.js +6 -215
  9. package/routes/api/subtitles/get.js +22 -125
  10. package/routes/api/subtitles/post.js +26 -0
  11. package/routes/api/transcode-sessions/post.js +24 -2
  12. package/routes/media/get.js +80 -0
  13. package/routes/stream/get.js +26 -94
  14. package/routes/transcode/audio-warm/get.js +9 -21
  15. package/routes/transcode/variant-warm/get.js +8 -14
  16. package/server.js +410 -46
  17. package/services/encode/AdmittedInput.js +136 -0
  18. package/services/encode/CushionReport.js +1 -76
  19. package/services/encode/EncodeInputs.js +142 -0
  20. package/services/encode/EncodeOrchestrator.js +4 -1
  21. package/services/encode/EncodeRun.js +13 -4
  22. package/services/encode/EncodeRuns.js +132 -52
  23. package/services/encode/InputFailures.js +37 -0
  24. package/services/encode/MatroskaInput.js +158 -0
  25. package/services/encode/OutputOpening.js +16 -64
  26. package/services/encode/Renditions.js +1 -17
  27. package/services/encode/admitted-command.js +60 -0
  28. package/services/encode/annex-b-input.js +37 -0
  29. package/services/encode/bit-payload.js +29 -0
  30. package/services/encode/output/OutputCatalog.js +1 -1
  31. package/services/encode/output/Timeline.js +11 -0
  32. package/services/encode/pcm-normalization.js +30 -0
  33. package/services/encode/piece-completeness.js +54 -1
  34. package/services/encode/quality/HostLoad.js +19 -73
  35. package/services/encode/quality/QualityOffer.js +1 -1
  36. package/services/encode/run-command.js +1 -589
  37. package/services/encode/segment-formats/mp4-boxes.js +3 -1
  38. package/services/encode/segment-formats/presentation-segment.js +114 -0
  39. package/services/media/ContainerOrchestrator.js +118 -48
  40. package/services/media/KeyframeTables.js +16 -115
  41. package/services/media/MediaReadRequests.js +120 -0
  42. package/services/media/ProbeRequests.js +135 -0
  43. package/services/media/SegmentInputs.js +48 -0
  44. package/services/media/SourceFile.js +6 -0
  45. package/services/media/SubtitleOrchestrator.js +190 -4
  46. package/services/media/await-plan.js +47 -0
  47. package/services/media/container/AsfContainer.js +217 -0
  48. package/services/media/container/AviContainer.js +343 -34
  49. package/services/media/container/CadencedPictureOrder.js +45 -0
  50. package/services/media/container/Container.js +7 -3
  51. package/services/media/container/ContainerFactory.js +10 -3
  52. package/services/media/container/FfprobeContainer.js +222 -0
  53. package/services/media/container/MatroskaContainer.js +72 -46
  54. package/services/media/container/Mp4Container.js +472 -139
  55. package/services/media/container/MpegPsContainer.js +281 -0
  56. package/services/media/container/MpegTsContainer.js +277 -0
  57. package/services/media/container/PacketIndex.js +270 -0
  58. package/services/media/container/PacketRecords.js +237 -0
  59. package/services/media/container/RetainedBytes.js +39 -0
  60. package/services/media/container/RetainedReads.js +51 -0
  61. package/services/media/container/ac3-frame.js +46 -0
  62. package/services/media/container/asf-packets.js +153 -0
  63. package/services/media/container/avc-picture-order.js +50 -0
  64. package/services/media/container/avi-open-dml.js +47 -0
  65. package/services/media/container/dts-frame.js +51 -0
  66. package/services/media/container/ebml-stream.js +12 -1
  67. package/services/media/container/ffprobe-record.js +45 -0
  68. package/services/media/container/flv-packet-ranges.js +27 -0
  69. package/services/media/container/following-packet-time.js +37 -0
  70. package/services/media/container/h264-configuration.js +183 -0
  71. package/services/media/container/h264-elementary-index.js +245 -0
  72. package/services/media/container/hevc-configuration.js +165 -0
  73. package/services/media/container/hevc-elementary-index.js +132 -0
  74. package/services/media/container/hevc-picture-order.js +66 -0
  75. package/services/media/container/latm-frame.js +99 -0
  76. package/services/media/container/matroska-packets.js +207 -0
  77. package/services/media/container/memory-unavailable.js +8 -0
  78. package/services/media/container/mp3-packet-dependencies.js +41 -0
  79. package/services/media/container/mp4-fragments.js +148 -0
  80. package/services/media/container/mpeg-audio-frame.js +19 -0
  81. package/services/media/container/mpeg-audio-reservoir.js +27 -0
  82. package/services/media/container/mpeg-elementary-index.js +196 -0
  83. package/services/media/container/mpeg-sequence-reader.js +69 -0
  84. package/services/media/container/mpeg-ts-packets.js +93 -0
  85. package/services/media/container/mpeg-video-configuration.js +50 -0
  86. package/services/media/container/mpeg4-picture-timing.js +108 -0
  87. package/services/media/container/pes-header.js +34 -0
  88. package/services/media/playback-declarations.js +57 -0
  89. package/services/media/playback-planner.js +72 -230
  90. package/services/media/probe-packets.js +61 -0
  91. package/services/media/read-playback-declarations.js +18 -0
  92. package/services/media/tracks/AudioTrack.js +9 -0
  93. package/services/media/tracks/TextSubtitleTrack.js +4 -1
  94. package/services/server/OutputLifecycle.js +2 -0
  95. package/services/server/SegmentServing.js +40 -33
  96. package/services/server/ViewerRequests.js +22 -41
  97. package/services/server/controllers/PlaybackController.js +4 -0
  98. package/services/server/controllers/SubtitleController.js +8 -29
  99. package/services/server/transcode-session-files.js +58 -99
  100. package/services/server/wire-outputs.js +58 -15
  101. package/services/storage/IndexMemory.js +85 -0
  102. package/services/storage/MachineBudget.js +27 -3
  103. package/services/storage/allowance.js +8 -1
  104. package/services/storage/piece-store/piece-disk-store.js +18 -3
  105. package/services/storage/piece-store/piece-lru.js +8 -4
  106. package/services/storage/piece-store/shared-piece-store.js +26 -11
  107. package/services/storage/segment-store/SegmentStore.js +49 -18
  108. package/services/storage/wire.js +7 -0
  109. package/services/torrent/Contents.js +52 -20
  110. package/services/torrent/demand/DemandRegister.js +13 -4
  111. package/services/torrent/demand/Window.js +13 -2
  112. package/services/torrent/demand-progress.js +25 -0
  113. package/services/torrent/download/DeadlineScheduler.js +167 -0
  114. package/services/torrent/download/FutureDownload.js +166 -0
  115. package/services/torrent/download/SwarmSelection.js +30 -59
  116. package/services/torrent/download/registry.js +45 -5
  117. package/services/torrent/download/withdraw-claim.js +2 -0
  118. package/services/torrent/torrent-pool.js +39 -600
  119. package/services/torrent/worker/channel.js +8 -1
  120. package/services/torrent/worker/client.js +74 -72
  121. package/services/torrent/worker/files-in-use.js +9 -0
  122. package/services/torrent/worker/handle-state.js +12 -0
  123. package/services/torrent/worker/held-bytes.js +82 -99
  124. package/services/torrent/worker/held-file-bytes.js +17 -0
  125. package/services/torrent/worker/piece-reader.js +67 -683
  126. package/services/torrent/worker/pool-adapter.js +37 -123
  127. package/services/torrent/worker/protocol.js +6 -17
  128. package/services/torrent/worker/whole-file-stats.js +2 -0
  129. package/services/torrent/worker/worker.js +86 -156
  130. package/services/transport/data-channel-handler.js +56 -13
  131. package/services/viewer/DownloadMaps.js +168 -0
  132. package/services/viewer/NativeSourceMap.js +33 -0
  133. package/services/viewer/PriorityMap.js +49 -40
  134. package/services/viewer/PriorityOrchestrator.js +42 -13
  135. package/services/viewer/SourcePreparation.js +278 -0
  136. package/services/viewer/Viewer.js +11 -0
  137. package/services/viewer/Viewers.js +109 -2
  138. package/services/viewer/playback-readiness.js +50 -63
  139. package/services/viewer/segment-demands.js +37 -0
  140. package/services/viewer/urgent-output-ready.js +26 -0
  141. package/services/encode/run-inputs.js +0 -53
  142. package/services/media/keyframe-probe.js +0 -157
  143. package/services/media/media-info-probe.js +0 -93
  144. package/services/torrent/worker/background-fill.js +0 -213
  145. package/services/torrent/worker/resume-warm.js +0 -107
@@ -1,107 +0,0 @@
1
- /**
2
- * @file Fetching the region under a viewer's resume position.
3
- *
4
- * All that is left of a file that used to read containers here as well. The
5
- * parsing moved to the main thread on 2026-09-15: a container is built from one
6
- * function, `readRange(start, end)`, the pieces behind it live in shared memory,
7
- * and the reason it had been put here — "the main thread cannot open a read
8
- * stream on one of its files" — was true of WebTorrent's own API and not of the
9
- * bytes. Three commands, a second `ContainerOrchestrator` in this thread, and
10
- * every answer carried back over the channel went with it.
11
- *
12
- * What stays is the part that really is the torrent's: turning a position in
13
- * seconds into a byte offset and pulling that region off the swarm. How long
14
- * the file runs is TOLD to it, because that is what the file states about
15
- * itself and this thread no longer reads such things.
16
- */
17
-
18
- import { logger } from "../../../utils/logger.js";
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
- /**
30
- * How much of the file to pull in under the viewer's resume position.
31
- *
32
- * One piece of a video torrent is 4-16 MB and a resume lands anywhere inside
33
- * one, so anything smaller would still leave the encoder waiting for the piece
34
- * it starts in. Eight megabytes covers that piece and usually the next.
35
- */
36
- const RESUME_REGION_BYTES = 8 * 1024 * 1024;
37
-
38
- /**
39
- * Where a position in seconds falls in a file, in bytes.
40
- *
41
- * Proportional, and therefore approximate on a variable bitrate — which is what
42
- * it is for: a prefetch that puts the swarm to work on roughly the right place
43
- * while the plan and the session are still being built. The encoder's own read
44
- * asks for the exact bytes a moment later and corrects it.
45
- *
46
- * A position past the end is clamped to the end rather than refused: a resume
47
- * position can outlive the file it was recorded against, and reading the last
48
- * bytes is harmless where reading past them is an error.
49
- *
50
- * @param {number} fileLength
51
- * @param {number} durationSeconds
52
- * @param {number} positionSeconds
53
- * @returns {number}
54
- */
55
- export function resumeByteOffset(fileLength, durationSeconds, positionSeconds) {
56
- if (!(fileLength > 0) || !(durationSeconds > 0) || !(positionSeconds > 0)) {
57
- return 0;
58
- }
59
- const within = Math.min(positionSeconds, durationSeconds);
60
- return Math.min(fileLength - 1, Math.floor((fileLength * within) / durationSeconds));
61
- }
62
-
63
- /**
64
- * Start fetching the region a viewer is about to resume at.
65
- *
66
- * Where that region IS can only be worked out from two numbers the file itself
67
- * holds — its length and its duration — so this belongs beside the container
68
- * read rather than in the route: the route knows a position in seconds and
69
- * nothing else. The conversion is proportional and therefore approximate on a
70
- * variable bitrate; it is a prefetch, and the encoder's own read corrects it.
71
- *
72
- * @param {object} torrent
73
- * @param {number} fileIndex
74
- * @param {string} sourceKey
75
- * @param {number} positionSeconds
76
- * @param {{ prefetchEdges?: () => Promise<unknown>, fetchRegion?: (start: number, bytes: number) => Promise<unknown> }} options
77
- * @returns {Promise<boolean>} Whether a region was asked for.
78
- */
79
- export async function warmResumePosition(torrent, fileIndex, sourceKey, positionSeconds, options = {}) {
80
- const file = torrent?.files?.[fileIndex];
81
- if (!file || !(positionSeconds > 0) || typeof options.fetchRegion !== "function") {
82
- return false;
83
- }
84
- // TOLD, not worked out here. How long a file runs is what the file states
85
- // about itself, and that is read on the main thread now; this one turns a
86
- // position into a byte offset and fetches the region under it, which is all
87
- // of the job that is actually the torrent's.
88
- const duration = Number(options.durationSeconds);
89
- if (!Number.isFinite(duration) || duration <= 0) {
90
- logger.info(
91
- `warm ${sourceKey.slice(0, 8)}: "${String(file.name).slice(0, 40)}" does not declare its ` +
92
- "duration, so where the viewer's position falls in it cannot be worked out — " +
93
- "the region under it is left to the encoder's own read"
94
- );
95
- return false;
96
- }
97
- const at = resumeByteOffset(file.length, duration, positionSeconds);
98
- logger.info(
99
- `warm ${sourceKey.slice(0, 8)}: fetching ${(RESUME_REGION_BYTES / (1024 * 1024)).toFixed(0)}MB under the ` +
100
- `viewer's position ${positionSeconds.toFixed(1)}s of ${duration.toFixed(1)}s, which is ` +
101
- `${(at / (1024 * 1024)).toFixed(1)}MB into "${String(file.name).slice(0, 40)}"`
102
- );
103
- await options.fetchRegion(at, RESUME_REGION_BYTES);
104
- return true;
105
- }
106
-
107
-