@torrent-tv/proxy 2.88.3 → 2.88.5

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 (61) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/assets/calibration/audio/NOTICE.md +19 -0
  3. package/assets/calibration/audio/aac.m4a +0 -0
  4. package/assets/calibration/audio/ac3.ac3 +0 -0
  5. package/assets/calibration/audio/alac.m4a +0 -0
  6. package/assets/calibration/audio/dts.dts +0 -0
  7. package/assets/calibration/audio/eac3.eac3 +0 -0
  8. package/assets/calibration/audio/flac.flac +0 -0
  9. package/assets/calibration/audio/manifest.json +98 -0
  10. package/assets/calibration/audio/mp2.mp2 +0 -0
  11. package/assets/calibration/audio/mp3.mp3 +0 -0
  12. package/assets/calibration/audio/opus.opus +0 -0
  13. package/assets/calibration/audio/pcm.wav +0 -0
  14. package/assets/calibration/audio/truehd.thd +0 -0
  15. package/assets/calibration/audio/vorbis.ogg +0 -0
  16. package/biome.json +1 -0
  17. package/docs/browser-proxy-contract.md +155 -133
  18. package/docs/playback-start-forecast.md +56 -0
  19. package/package.json +1 -1
  20. package/routes/api/transcode-sessions/post.js +5 -0
  21. package/routes/transcode/audio-warm/get.js +14 -3
  22. package/scripts/build-audio-samples.mjs +31 -0
  23. package/server.js +5 -0
  24. package/services/encode/EncodeRuns.js +3 -1
  25. package/services/encode/OutputOpening.js +40 -15
  26. package/services/encode/Renditions.js +129 -31
  27. package/services/encode/args.js +5 -3
  28. package/services/encode/audio-calibration.js +67 -0
  29. package/services/encode/audio-work-rate.js +15 -0
  30. package/services/encode/hwaccel.js +1 -1
  31. package/services/encode/quality/EncodeCost.js +29 -3
  32. package/services/encode/quality/link-budget.js +110 -15
  33. package/services/encode/quality/output-format.js +10 -6
  34. package/services/encode/segment-formats/fmp4.js +2 -0
  35. package/services/encode/segment-formats/mp4-boxes.js +12 -1
  36. package/services/media/audio-inventory.js +60 -6
  37. package/services/media/container/Container.js +7 -1
  38. package/services/media/container/Mp4Container.js +103 -3
  39. package/services/media/tracks/AudioTrack.js +12 -0
  40. package/services/media/tracks/audio-bound.js +233 -0
  41. package/services/server/ViewerRequests.js +30 -5
  42. package/services/server/playback-readiness.js +516 -708
  43. package/services/server/wire-outputs.js +31 -3
  44. package/services/storage/piece-store/shared-piece-store.js +6 -0
  45. package/services/storage/segment-store/SegmentStore.js +19 -4
  46. package/services/torrent/episode-naming.js +182 -182
  47. package/services/torrent/torrent-pool.js +13 -0
  48. package/test/audio-buffer-continuity.test.js +70 -70
  49. package/test/audio-calibration.test.js +23 -0
  50. package/test/episode-naming.test.js +131 -131
  51. package/test/fixtures/audio-frame-boundary.json +1 -1
  52. package/test/fmp4-presentation-clock.test.js +17 -2
  53. package/test/link-budget.test.js +7 -6
  54. package/test/one-output-one-session.test.js +77 -8
  55. package/test/playback-readiness.test.js +36 -6
  56. package/test/quality-variants.test.js +36 -8
  57. package/test/segment-store.test.js +15 -0
  58. package/test/serving-existing-output.test.js +3 -0
  59. package/test/soundtrack-rate-bound.test.js +289 -0
  60. package/test/source-files-route.test.js +113 -113
  61. package/test/two-viewers-one-picture.test.js +97 -0
@@ -0,0 +1,233 @@
1
+ /**
2
+ * @file The most a soundtrack's codec allows it to carry, from what its
3
+ * container declares about it.
4
+ *
5
+ * A copied soundtrack is sent to the viewer as the file has it, so the most it
6
+ * can weigh on their link is a property of its CODEC as configured: a bound
7
+ * the stream is held to by its own specification, not an average a file
8
+ * happens to state. Only bounds confirmed against a published source are
9
+ * given, for exactly the configurations those sources describe; any other
10
+ * combination answers null, and the caller falls back to what the file states
11
+ * or to re-encoding the track. Sources, and why each other codec has none:
12
+ * `research/soundtrack-rate-bound-2026-10-01.md`.
13
+ *
14
+ * 1. AAC-LC (`audioObjectType` 2) with 1024-sample frames: at most 6144 bits
15
+ * per channel in one frame, whatever the bit reservoir holds (ISO/IEC
16
+ * 14496-3 §4.5.3.1; Fraunhofer's FDK AAC encoder states the same: "For
17
+ * AAC-LC, the bitrate is only limited by the maximum AAC frame length. It
18
+ * is, regardless of the available bit reservoir, defined as 6144 bits per
19
+ * channel"). One frame lasts 1024 / samplingFrequency seconds, so the rate
20
+ * over any stretch of whole frames is at most
21
+ * 6144 x channels x samplingFrequency / 1024 bit/s. The channel count and
22
+ * the sampling frequency are the ones the AudioSpecificConfig states — for
23
+ * a stream with implicit SBR that is the core's, which is what one frame
24
+ * lasts. The count includes an LFE channel the buffer rule does not, so the
25
+ * bound errs high.
26
+ * 2. AC-3: the largest syncframe ATSC A/52:2018 Table 5.18 allows at each
27
+ * sampling frequency (`frmsizecod` 37), over the 1536 samples it carries.
28
+ * That is 640 kbit/s at 32 and 48 kHz and 640.369 kbit/s at 44.1 kHz, whose
29
+ * frames alternate in length; with the frequency unknown the largest of the
30
+ * three is the bound.
31
+ *
32
+ * PURE: plain values in, plain values out. No container, no torrent.
33
+ */
34
+
35
+ /** ISO/IEC 14496-3 Table 1.18: samplingFrequencyIndex to Hz. 0xF is explicit. */
36
+ const AAC_SAMPLING_FREQUENCIES = Object.freeze([
37
+ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350
38
+ ]);
39
+
40
+ /** ISO/IEC 14496-3 Table 1.19: channelConfiguration to channels; 0 is a PCE. */
41
+ const AAC_CHANNELS = Object.freeze([null, 1, 2, 3, 4, 5, 6, 8]);
42
+
43
+ /** The AAC-LC object type, and the one frame length the bound is stated for. */
44
+ const AAC_LC = 2;
45
+ const AAC_LC_FRAME_LENGTH = 1024;
46
+ const AAC_MAX_BITS_PER_CHANNEL_FRAME = 6144;
47
+
48
+ /**
49
+ * ATSC A/52:2018 Table 5.18, `frmsizecod` 37 (640 kbps nominal): 16-bit words
50
+ * in the largest syncframe at each sampling frequency. A syncframe carries
51
+ * 1536 samples (§5.1).
52
+ */
53
+ const AC3_LARGEST_WORDS = Object.freeze(new Map([[48000, 1280], [44100, 1394], [32000, 1920]]));
54
+ const AC3_SAMPLES_PER_FRAME = 1536;
55
+
56
+ /**
57
+ * @typedef {object} CodecParameters
58
+ * @property {string} codec - The ffmpeg-side name: "aac", "ac3", ...
59
+ * @property {number | null} objectType - AAC `audioObjectType`.
60
+ * @property {number | null} frameLength - Samples per frame.
61
+ * @property {number | null} channels
62
+ * @property {number | null} sampleRate - Hz.
63
+ */
64
+
65
+ /**
66
+ * A minimal MSB-first bit reader over a buffer.
67
+ *
68
+ * @param {Buffer} bytes
69
+ */
70
+ function bitReader(bytes) {
71
+ let position = 0;
72
+ return {
73
+ read(count) {
74
+ let value = 0;
75
+ for (let i = 0; i < count; i += 1) {
76
+ const byte = bytes[position >> 3];
77
+ if (byte === undefined) {
78
+ throw new RangeError("AudioSpecificConfig ended early");
79
+ }
80
+ value = (value * 2) + ((byte >> (7 - (position & 7))) & 1);
81
+ position += 1;
82
+ }
83
+ return value;
84
+ }
85
+ };
86
+ }
87
+
88
+ /**
89
+ * The fields of an AudioSpecificConfig the bound is computed from (ISO/IEC
90
+ * 14496-3 §1.6.2.1 and the GASpecificConfig that follows it for AAC-LC).
91
+ *
92
+ * @param {Buffer | null} bytes
93
+ * @returns {{ objectType: number, sampleRate: number | null, channels: number | null, frameLength: number | null } | null}
94
+ * Null when the bytes are not a readable config.
95
+ */
96
+ export function parseAudioSpecificConfig(bytes) {
97
+ if (!Buffer.isBuffer(bytes) || bytes.length < 2) {
98
+ return null;
99
+ }
100
+ try {
101
+ const bits = bitReader(bytes);
102
+ let objectType = bits.read(5);
103
+ if (objectType === 31) {
104
+ objectType = 32 + bits.read(6);
105
+ }
106
+ const frequencyIndex = bits.read(4);
107
+ const sampleRate = frequencyIndex === 0xf
108
+ ? bits.read(24)
109
+ : (AAC_SAMPLING_FREQUENCIES[frequencyIndex] ?? null);
110
+ const channelConfiguration = bits.read(4);
111
+ const channels = AAC_CHANNELS[channelConfiguration] ?? null;
112
+ // GASpecificConfig begins with frameLengthFlag for the object types that
113
+ // carry one; for any other the frame length is not read here.
114
+ const frameLength = objectType === AAC_LC
115
+ ? (bits.read(1) === 1 ? 960 : AAC_LC_FRAME_LENGTH)
116
+ : null;
117
+ return { objectType, sampleRate: sampleRate > 0 ? sampleRate : null, channels, frameLength };
118
+ } catch {
119
+ return null;
120
+ }
121
+ }
122
+
123
+ /**
124
+ * What a container declares about one soundtrack's codec configuration, as
125
+ * plain values.
126
+ *
127
+ * For AAC the AudioSpecificConfig is the statement, and the container's own
128
+ * channel count and frequency are not used in its place: for HE-AAC the
129
+ * container may state the output frequency while a frame lasts the core's.
130
+ *
131
+ * @param {object} params
132
+ * @param {string} params.codec - The ffmpeg-side name.
133
+ * @param {string} [params.codecPrivateB64] - The AudioSpecificConfig for AAC.
134
+ * @param {number | null} [params.channels] - As the container states it.
135
+ * @param {number | null} [params.samplingFrequency] - As the container states it.
136
+ * @returns {CodecParameters | null} Null when nothing names the codec.
137
+ */
138
+ export function codecParametersOf({ codec, codecPrivateB64 = "", channels = null, samplingFrequency = null }) {
139
+ const name = typeof codec === "string" ? codec.trim().toLowerCase() : "";
140
+ if (!name) {
141
+ return null;
142
+ }
143
+ if (name === "aac") {
144
+ const config = typeof codecPrivateB64 === "string" && codecPrivateB64.length > 0
145
+ ? parseAudioSpecificConfig(Buffer.from(codecPrivateB64, "base64"))
146
+ : null;
147
+ return {
148
+ codec: name,
149
+ objectType: config?.objectType ?? null,
150
+ frameLength: config?.frameLength ?? null,
151
+ channels: config?.channels ?? null,
152
+ sampleRate: config?.sampleRate ?? null
153
+ };
154
+ }
155
+ return {
156
+ codec: name,
157
+ objectType: null,
158
+ frameLength: null,
159
+ channels: Number.isFinite(channels) && channels > 0 ? channels : null,
160
+ sampleRate: Number.isFinite(samplingFrequency) && samplingFrequency > 0 ? samplingFrequency : null
161
+ };
162
+ }
163
+
164
+ /**
165
+ * Whether a set of parameters holds everything the bound for its codec is
166
+ * computed from. A set that is complete can replace another whole; one that is
167
+ * not adds nothing to a set already known.
168
+ *
169
+ * @param {CodecParameters | null} params
170
+ * @returns {boolean}
171
+ */
172
+ export function codecParametersComplete(params) {
173
+ if (!params?.codec) {
174
+ return false;
175
+ }
176
+ if (params.codec === "aac") {
177
+ return [params.objectType, params.frameLength, params.channels, params.sampleRate]
178
+ .every((value) => Number.isFinite(value) && value > 0);
179
+ }
180
+ // Every other codec's bound — where it has one — needs no more than its name.
181
+ return true;
182
+ }
183
+
184
+ /**
185
+ * Whether two sets contradict each other: a field both state, stated
186
+ * differently.
187
+ *
188
+ * @param {CodecParameters | null} left
189
+ * @param {CodecParameters | null} right
190
+ * @returns {boolean}
191
+ */
192
+ export function codecParametersConflict(left, right) {
193
+ if (!left || !right) {
194
+ return false;
195
+ }
196
+ return ["codec", "objectType", "frameLength", "channels", "sampleRate"].some(
197
+ (field) => left[field] !== null && left[field] !== undefined &&
198
+ right[field] !== null && right[field] !== undefined &&
199
+ left[field] !== right[field]
200
+ );
201
+ }
202
+
203
+ /**
204
+ * The confirmed bound for these parameters, in kbit/s, or null.
205
+ *
206
+ * @param {CodecParameters | null} params
207
+ * @returns {number | null}
208
+ */
209
+ export function peakKbpsOf(params) {
210
+ if (!params?.codec) {
211
+ return null;
212
+ }
213
+ if (params.codec === "aac") {
214
+ if (
215
+ params.objectType !== AAC_LC ||
216
+ params.frameLength !== AAC_LC_FRAME_LENGTH ||
217
+ !(params.channels > 0) ||
218
+ !(params.sampleRate > 0)
219
+ ) {
220
+ return null;
221
+ }
222
+ return (AAC_MAX_BITS_PER_CHANNEL_FRAME * params.channels * params.sampleRate) / AAC_LC_FRAME_LENGTH / 1000;
223
+ }
224
+ if (params.codec === "ac3") {
225
+ const rateOf = (sampleRate, words) => (words * 16 * sampleRate) / AC3_SAMPLES_PER_FRAME / 1000;
226
+ const words = AC3_LARGEST_WORDS.get(params.sampleRate);
227
+ if (words !== undefined) {
228
+ return rateOf(params.sampleRate, words);
229
+ }
230
+ return Math.max(...[...AC3_LARGEST_WORDS].map(([sampleRate, largest]) => rateOf(sampleRate, largest)));
231
+ }
232
+ return null;
233
+ }
@@ -9,7 +9,7 @@
9
9
 
10
10
  import { logger } from "../../utils/logger.js";
11
11
  import { isOutputName } from "../encode/output/index.js";
12
- import { predictPlaybackReadiness, RateTrend } from "./playback-readiness.js";
12
+ import { predictPlaybackReadiness, RateTrend, forecastRate as forecastRateOf } from "./playback-readiness.js";
13
13
 
14
14
  function isWarmupTimeoutError(error) {
15
15
  if (!(error instanceof Error)) {
@@ -568,7 +568,8 @@ export class ViewerRequests {
568
568
  const trackName = output === session ? "video" : "audio";
569
569
  const nativeRanges = Array.from({ length: timeline?.segmentCount ?? 0 }, (_, index) =>
570
570
  this.#host.segmentStore.mediaRangesOf(output.outputKey, index, {
571
- startSeconds: timeline.publishedStartOf(index)
571
+ startSeconds: timeline.publishedStartOf(index),
572
+ endSeconds: index < timeline.segmentCount - 1 ? timeline.publishedStartOf(index + 1) : undefined
572
573
  }));
573
574
  const segments = Array.from({ length: timeline?.segmentCount ?? 0 }, (_, index) => {
574
575
  const ranges = nativeRanges[index];
@@ -622,7 +623,8 @@ export class ViewerRequests {
622
623
  Number.isFinite(sourceDuration) && sourceDuration > 0
623
624
  ? fileLength / sourceDuration
624
625
  : 0,
625
- readings: sourceTrend
626
+ readings: sourceTrend,
627
+ residence: stats?.residence,
626
628
  });
627
629
  }
628
630
 
@@ -630,13 +632,22 @@ export class ViewerRequests {
630
632
  const observedMbps = output === session
631
633
  ? outputMbps
632
634
  : await this.#host.quality.observedStreamMbps(output);
635
+ const liveRuns = this.#host.encodeRuns.liveRunsOf?.(output) ?? [];
636
+ const processingStarted = liveRuns.some(({ progress }) => progress.processedSeconds > progress.startPositionSeconds);
637
+ const startedAt = liveRuns.find(({ startedAt }) => startedAt > 0)?.startedAt ?? now;
633
638
  tracks.push({
634
639
  id: output.outputKey,
635
640
  clientRanges: viewerReading?.bufferedRanges?.[output === session ? "video" : "audio"] ??
636
641
  viewerReading?.bufferedRanges?.media,
637
642
  sourceIds,
638
643
  processedSeconds: trackProgress?.processedSeconds,
639
- bitsPerMediaSecond: Number.isFinite(observedMbps) && observedMbps > 0 ? observedMbps * 1_000_000 : 0,
644
+ startupRemainingSeconds: processingStarted ? 0 : Math.max(0,
645
+ (this.#host.processingStartupSecondsOf?.(output) ?? 0) - Math.max(0, (now - startedAt) / 1000)),
646
+ processingRanges: liveRuns.map(({ progress }) => ({
647
+ start: progress.startPositionSeconds, end: progress.processedSeconds
648
+ })),
649
+ bitsPerMediaSecond: Number.isFinite(observedMbps) && observedMbps > 0 ? observedMbps * 1_000_000 :
650
+ this.#host.initialOutputBitsPerMediaSecondOf?.(output) ?? 0,
640
651
  readings: trackRates,
641
652
  segments,
642
653
  readySegmentIndices: [...inventory.keys()],
@@ -693,6 +704,20 @@ export class ViewerRequests {
693
704
  linkReadings: measurement.link.snapshot()
694
705
  };
695
706
  const forecast = predictPlaybackReadiness(input);
707
+ forecast.operations = outputs.flatMap((output, index) => {
708
+ const common = { speed: forecastRateOf(tracks[index].readings), processedSeconds: tracks[index].processedSeconds };
709
+ return [
710
+ ...(output.spec.video ? [{ ...common, track: "video",
711
+ operation: output.spec.transcodesVideo ? "encode" : "copy",
712
+ inputCodec: output.file.media?.videoCodec ?? output.file.media?.codec,
713
+ outputCodec: output.spec.transcodesVideo ? "h264" : null,
714
+ height: output.spec.video.encode?.height ?? output.file.media?.height }] : []),
715
+ ...(output.spec.audio ? [{ ...common, track: "audio",
716
+ operation: output.spec.audio.transcode ? "encode" : "copy",
717
+ inputCodec: this.#host.audioDescriptionOf?.(output)?.codec ?? output.file.media?.audioCodec,
718
+ outputCodec: output.spec.audio.transcode ? "aac" : null }] : [])
719
+ ];
720
+ });
696
721
  if (measurement.forecastReason !== forecast.reason) {
697
722
  measurement.forecastReason = forecast.reason;
698
723
  logger.info(`playback readiness ${session.id} consumer=${consumerId} ${JSON.stringify({
@@ -749,7 +774,7 @@ export class ViewerRequests {
749
774
  const reading = this.#host.encodeSpeedReadingOf?.(output, now) ?? null;
750
775
  if (Number.isFinite(reading?.speed) && reading.speed > 0 && Number.isFinite(reading.at)) {
751
776
  trend.add(reading.at, reading.speed);
752
- } else if (!trend.snapshot()) {
777
+ } else {
753
778
  const projected = Number(this.#host.projectedEncodeSpeedOf?.(output));
754
779
  if (Number.isFinite(projected) && projected > 0) {
755
780
  trend.add(now, projected);