@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
@@ -50,30 +50,33 @@ function request(over = {}) {
50
50
  *
51
51
  * @returns {string}
52
52
  */
53
- function specOfThatOutput() {
53
+ function specOfThatOutput({ trackIndex = 0, transcode = false } = {}) {
54
54
  return new OutputSpec({
55
55
  sourceKey: TORRENT,
56
56
  segmentFormatId: "fmp4",
57
57
  grid: new CutGrid({ kind: "keyframe", fileIndex: 0 }),
58
58
  video: new VideoOutput({ fileIndex: 0, encode: null }),
59
- audio: new AudioOutput({ fileIndex: 0, trackIndex: 0, transcode: false })
59
+ audio: new AudioOutput({ fileIndex: 0, trackIndex, transcode })
60
60
  });
61
61
  }
62
62
 
63
63
  /** @returns {string} */
64
- function nameOfThatOutput() {
65
- return specOfThatOutput().toName();
64
+ function nameOfThatOutput(sound) {
65
+ return specOfThatOutput(sound).toName();
66
66
  }
67
67
 
68
68
  /**
69
69
  * @param {object} manager
70
+ * @param {{ trackIndex?: number, transcode?: boolean }} [sound] - The soundtrack
71
+ * the seeded output carries; the file's own, copied, when left out.
70
72
  * @returns {object} The session it is seeded with.
71
73
  */
72
- function seedOneSession(manager) {
74
+ function seedOneSession(manager, sound = {}) {
73
75
  const session = {
74
- id: nameOfThatOutput(),
75
- spec: specOfThatOutput(),
76
- outputKey: "seeded",
76
+ id: nameOfThatOutput(sound),
77
+ spec: specOfThatOutput(sound),
78
+ // Its own address, so two seeded outputs are two places for viewers.
79
+ outputKey: specOfThatOutput(sound).toKey(),
77
80
  state: "ready",
78
81
  file: new SourceFile({ sourceKey: TORRENT, fileIndex: 0, name: "film.mkv" }),
79
82
  timeline: new Timeline({ boundaries: [0, 4, 8], cutGrid: "keyframe" }),
@@ -98,7 +101,17 @@ function seedOneSession(manager) {
98
101
  * @param {object} manager
99
102
  */
100
103
  function knownFile(manager) {
104
+ // This check exercises output identity. Machine admission has its own tests.
105
+ manager.admission.admitsWatching = () => ({ admitted: true, reason: "", speedX: 8 });
106
+ manager.admission.previewCandidate = () => ({ admitted: true, reason: "", speedX: 8 });
101
107
  manager.getCachedMediaInfo = () => ({ durationSeconds: 8, width: 1920, height: 1080, fps: 24 });
108
+ // The soundtracks the plan listed: the file's own, at the rate it states, and
109
+ // a dub beside it that states nothing — which no link can be asked about as a
110
+ // copy, so it is sent re-encoded.
111
+ manager.getCachedAudioTracks = () => [
112
+ { index: 0, fileIndex: 0, sourceTrackIndex: 0, bitrateKbps: 128 },
113
+ { index: 1, fileIndex: 0, sourceTrackIndex: 1, bitrateKbps: null, peakKbps: null }
114
+ ];
102
115
  manager.keyframeTables.learn({ sourceKey: TORRENT, fileIndex: 0 }, { times: [0, 4], format: "test" });
103
116
  }
104
117
 
@@ -202,3 +215,59 @@ test("two screens that come to the same format share one output, and a different
202
215
  assert.notEqual(other, seeded, "a smaller picture is another output");
203
216
  assert.ok(!manager.viewers.forOutput(seeded).has("small-window"));
204
217
  });
218
+
219
+ test("a remembered dub with no stated rate opens on the output that sends it as AAC, not on its copy", async (t) => {
220
+ // The field case of 2026-10-01: the next episode opened on a dub the page
221
+ // remembered, which its browser plays as it stands and nothing states a rate
222
+ // for. Opened as a copy, no link could be asked about it and the episode was
223
+ // refused; it is the AAC output that answers.
224
+ const manager = wireOutputs({
225
+ enabled: true,
226
+ ffmpegBin: "ffmpeg",
227
+ localBindHost: "127.0.0.1",
228
+ localPort: 9090,
229
+ // A measured copy speed, so this machine can price the output and the
230
+ // admission answers on room rather than on knowing nothing.
231
+ copySpeedX: 20
232
+ });
233
+ t.after(() => manager.lifecycle.disposeAll());
234
+ manager.encodeRuns.planEncodersNow = () => {};
235
+ manager.encodeRuns.planEncodersSoon = () => {};
236
+ knownFile(manager);
237
+ const copied = seedOneSession(manager, { trackIndex: 1, transcode: false });
238
+ const encoded = seedOneSession(manager, { trackIndex: 1, transcode: true });
239
+
240
+ const answered = await manager.viewerRequests.createOrGetSession(
241
+ request({ consumerId: "viewer-two", audioTrackIndex: 1, transcodeAudio: false })
242
+ );
243
+
244
+ assert.equal(answered, encoded, "the dub is re-encoded");
245
+ assert.notEqual(answered, copied);
246
+ assert.deepEqual(
247
+ manager.renditions.soundtrackOf(answered, "viewer-two"),
248
+ { trackIndex: 1, transcode: true },
249
+ "and the viewer is recorded as receiving AAC"
250
+ );
251
+ });
252
+
253
+ test("a track whose rate is stated is still copied for a browser that plays it", async (t) => {
254
+ const manager = wireOutputs({
255
+ enabled: true,
256
+ ffmpegBin: "ffmpeg",
257
+ localBindHost: "127.0.0.1",
258
+ localPort: 9090,
259
+ // A measured copy speed, so this machine can price the output and the
260
+ // admission answers on room rather than on knowing nothing.
261
+ copySpeedX: 20
262
+ });
263
+ t.after(() => manager.lifecycle.disposeAll());
264
+ manager.encodeRuns.planEncodersNow = () => {};
265
+ manager.encodeRuns.planEncodersSoon = () => {};
266
+ knownFile(manager);
267
+ const copied = seedOneSession(manager);
268
+ seedOneSession(manager, { trackIndex: 0, transcode: true });
269
+
270
+ const answered = await manager.viewerRequests.createOrGetSession(request({ consumerId: "viewer-two" }));
271
+
272
+ assert.equal(answered, copied);
273
+ });
@@ -39,6 +39,36 @@ test("finds the first safe start from segments delivered before playback", () =>
39
39
  assert.ok(Math.abs(forecast.delaySeconds - 1) < 1e-8);
40
40
  });
41
41
 
42
+ test("startup delay is independent of browser capacity and proxy lookahead", () => {
43
+ for (const bufferLimitSeconds of [undefined, null, 0, 1, 120]) {
44
+ for (const lookaheadSeconds of [undefined, 0, 4, 120]) {
45
+ assert.equal(predictPlaybackReadiness(input({ bufferLimitSeconds, lookaheadSeconds })).delaySeconds, 1);
46
+ }
47
+ }
48
+ });
49
+
50
+ test("missing duration or required audio cannot be reported as fully buffered", () => {
51
+ assert.equal(predictPlaybackReadiness(input({ durationSeconds: 0 })).reason, "incomplete-state");
52
+ assert.equal(predictPlaybackReadiness(input({ requiredAudio: true, bufferedAheadSeconds: 8 })).ready, false);
53
+ });
54
+
55
+ test("held input does not require a second download or a download rate", () => {
56
+ const state = input();
57
+ state.sources[0] = { id: "source", complete: false, bytesPerMediaSecond: 1, readings: [],
58
+ residence: [{ start: 0, end: 8, location: "disk" }] };
59
+ assert.equal(predictPlaybackReadiness(state).delaySeconds, 1);
60
+ });
61
+
62
+ test("subtracts progress only inside the actual processing run interval", () => {
63
+ const state = input({ reserveSeconds: 0 });
64
+ state.tracks[0].readySegmentIndices = [];
65
+ state.tracks[0].readings = [{ at: state.now, value: 1 }];
66
+ state.tracks[0].processingRanges = [{ start: 0, end: 3 }];
67
+ assert.equal(predictPlaybackReadiness(state).delaySeconds, 2);
68
+ state.tracks[0].processingRanges = [{ start: 40, end: 50 }];
69
+ assert.equal(predictPlaybackReadiness(state).delaySeconds, 5);
70
+ });
71
+
42
72
  test("future cuts follow measured track time without inventing a permanent clock gap", () => {
43
73
  const state = input({ bufferedAheadSeconds: 3.9, reserveSeconds: 0 });
44
74
  state.tracks[0].clientRanges = [{ start: 0, end: 3.9 }];
@@ -47,7 +77,7 @@ test("future cuts follow measured track time without inventing a permanent clock
47
77
  assert.notEqual(forecast.reason, "no-safe-start-found");
48
78
  state.tracks[0].segments[1].mediaRanges = [{ start: 4, end: 8 }];
49
79
  state.tracks[0].readySegmentIndices = [0, 1];
50
- assert.equal(predictPlaybackReadiness(state).reason, "no-safe-start-found");
80
+ assert.equal(predictPlaybackReadiness(state).reason, "media-continuity-unavailable");
51
81
  });
52
82
 
53
83
  test("keeps source-stall reserve in the proxy's prepared timeline, not the capped browser buffer", () => {
@@ -124,8 +154,8 @@ test("rate forecast does not extrapolate acceleration beyond observed service",
124
154
  trend.add(10_000, 4);
125
155
 
126
156
  const predicted = forecastRate(trend.snapshot(), 10_000, 2);
127
- assert.ok(predicted > 3 && predicted < 4);
128
- assert.ok(forecastRate(trend.snapshot(), 100_000, 2) >= 3);
157
+ assert.equal(predicted, 4);
158
+ assert.equal(forecastRate(trend.snapshot(), 100_000, 2), 4);
129
159
  });
130
160
 
131
161
  test("uses each track's own segment boundaries and requires continuous coverage on both tracks", () => {
@@ -181,7 +211,7 @@ test("does not start when only one required track has continuous coverage", () =
181
211
 
182
212
  assert.equal(forecast.ready, false);
183
213
  assert.equal(forecast.delaySeconds, null);
184
- assert.equal(forecast.reason, "no-safe-start-found");
214
+ assert.equal(forecast.reason, "media-continuity-unavailable");
185
215
  });
186
216
 
187
217
  test("downloads an overlapping source interval once when two tracks use the same file", () => {
@@ -373,8 +403,8 @@ test("does not invent future encode acceleration to start an unsustainable outpu
373
403
  });
374
404
 
375
405
  assert.equal(forecast.ready, false);
376
- assert.equal(forecast.reason, "no-safe-start-found");
377
- assert.equal(forecast.delaySeconds, null);
406
+ assert.equal(forecast.reason, "minimum-safe-delay");
407
+ assert.ok(Math.abs(forecast.delaySeconds - (4 / 0.15 + 8 / 100 - 6)) < 1e-8);
378
408
  });
379
409
 
380
410
  test("a faster old probe cannot turn positive delivery into a permanent zero rate", () => {
@@ -843,8 +843,15 @@ test("a separately published audio track starts where the picture is, from the r
843
843
  const created = [];
844
844
  manager.viewerRequests.createOrGetSession = async (params) => {
845
845
  created.push(params);
846
- const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath, audioOnly: true });
847
- return { sessionId: VARIANT_ID, session: rendition };
846
+ // The output itself, which is what `createOrGetSession` answers, carrying
847
+ // the track it was asked for.
848
+ return fakeSession({
849
+ id: VARIANT_ID,
850
+ encodeHeight: 0,
851
+ dirPath,
852
+ audioOnly: true,
853
+ audioSourceTrackIndex: params.audioTrackIndex
854
+ });
848
855
  };
849
856
 
850
857
  await manager.renditions.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4", VIEWER);
@@ -877,8 +884,15 @@ test("with two viewers the audio track starts at the EARLIEST picture, not the r
877
884
  const created = [];
878
885
  manager.viewerRequests.createOrGetSession = async (params) => {
879
886
  created.push(params);
880
- const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath, audioOnly: true });
881
- return { sessionId: VARIANT_ID, session: rendition };
887
+ // The output itself, which is what `createOrGetSession` answers, carrying
888
+ // the track it was asked for.
889
+ return fakeSession({
890
+ id: VARIANT_ID,
891
+ encodeHeight: 0,
892
+ dirPath,
893
+ audioOnly: true,
894
+ audioSourceTrackIndex: params.audioTrackIndex
895
+ });
882
896
  };
883
897
 
884
898
  await manager.renditions.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4", VIEWER);
@@ -916,8 +930,15 @@ test("a soundtrack begins where a viewer says they are, whenever they said it",
916
930
  const created = [];
917
931
  manager.viewerRequests.createOrGetSession = async (params) => {
918
932
  created.push(params);
919
- const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath, audioOnly: true });
920
- return { sessionId: VARIANT_ID, session: rendition };
933
+ // The output itself, which is what `createOrGetSession` answers, carrying
934
+ // the track it was asked for.
935
+ return fakeSession({
936
+ id: VARIANT_ID,
937
+ encodeHeight: 0,
938
+ dirPath,
939
+ audioOnly: true,
940
+ audioSourceTrackIndex: params.audioTrackIndex
941
+ });
921
942
  };
922
943
 
923
944
  await manager.renditions.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4", VIEWER);
@@ -1212,8 +1233,15 @@ test("a file opened at a position starts its sound THERE, not a look-ahead earli
1212
1233
  const created = [];
1213
1234
  manager.viewerRequests.createOrGetSession = async (params) => {
1214
1235
  created.push(params);
1215
- const rendition = fakeSession({ id: VARIANT_ID, encodeHeight: 0, dirPath, audioOnly: true });
1216
- return { sessionId: VARIANT_ID, session: rendition };
1236
+ // The output itself, which is what `createOrGetSession` answers, carrying
1237
+ // the track it was asked for.
1238
+ return fakeSession({
1239
+ id: VARIANT_ID,
1240
+ encodeHeight: 0,
1241
+ dirPath,
1242
+ audioOnly: true,
1243
+ audioSourceTrackIndex: params.audioTrackIndex
1244
+ });
1217
1245
  };
1218
1246
 
1219
1247
  await manager.renditions.resolveAudioRenditionFile(BASE_ID, 1, "segment-00010.mp4", VIEWER);
@@ -35,6 +35,21 @@ function writeSegment(dir, index, bytes = 16) {
35
35
 
36
36
  const KEY = "torrent:abc:fmt=fmp4:grid=kf@0:video-only:v=0/copy";
37
37
 
38
+ test("a closed but truncated non-final piece is not published or reused", (t) => {
39
+ const { store, root } = storeInATempRoot();
40
+ t.after(() => { store.dropAll("the check is over"); rmSync(root, { recursive: true, force: true }); });
41
+ const format = { ...fmp4Format, readMediaRanges: () => [{ start: 456, end: 456.084 }],
42
+ mediaCoverageEnd: (ranges) => ranges.at(-1).end };
43
+ const dir = store.directoryFor(KEY);
44
+ store.useFormat(KEY, format);
45
+ writeFileSync(path.join(dir, "making-0-00071.mp4"), Buffer.alloc(16));
46
+ assert.equal(store.publish(KEY, "making-0-00071.mp4", format, { endSeconds: 466 }), null);
47
+ writeSegment(dir, 71);
48
+ assert.ok(store.mediaRangesOf(KEY, 71));
49
+ assert.equal(store.mediaRangesOf(KEY, 71, { endSeconds: 466 }), undefined);
50
+ assert.equal(store.pathOf(KEY, 71), null);
51
+ });
52
+
38
53
  test("two viewers of one output are given the same directory", (t) => {
39
54
  const { store, root } = storeInATempRoot();
40
55
  t.after(() => {
@@ -28,6 +28,9 @@ function hostWithA1080pOutput(t) {
28
28
  manager.encodeRuns.planEncodersNow = () => {};
29
29
  manager.encodeRuns.planEncodersSoon = () => {};
30
30
  manager.getCachedMediaInfo = () => ({ durationSeconds: 400, width: 1920, height: 1080, fps: 24 });
31
+ // The soundtrack the plan listed, at the rate the file states: a copy of it
32
+ // has a figure, so it stays a copy.
33
+ manager.getCachedAudioTracks = () => [{ index: 0, fileIndex: 0, sourceTrackIndex: 0, bitrateKbps: 128 }];
31
34
  const spec = outputSpec({
32
35
  sourceKey: TORRENT,
33
36
  transcodeVideo: true,
@@ -0,0 +1,289 @@
1
+ /**
2
+ * @file The most a soundtrack's codec allows it to carry, how it is read from
3
+ * the containers, how a later reading is merged, and how the mode a track is
4
+ * produced in is chosen from it.
5
+ *
6
+ * Plain values and buffers built here: no process, no torrent, no file on disk.
7
+ */
8
+
9
+ import test from "node:test";
10
+ import assert from "node:assert/strict";
11
+ import {
12
+ codecParametersOf,
13
+ parseAudioSpecificConfig,
14
+ peakKbpsOf
15
+ } from "../services/media/tracks/audio-bound.js";
16
+ import { AudioTrack } from "../services/media/tracks/AudioTrack.js";
17
+ import { Mp4Container } from "../services/media/container/Mp4Container.js";
18
+ import { Container } from "../services/media/container/Container.js";
19
+ import { buildAudioInventory, mergeInventoryEntry } from "../services/media/audio-inventory.js";
20
+ import {
21
+ chooseSoundtrackMode,
22
+ LINK_VERDICT,
23
+ linkCouldCarry,
24
+ linkRefusalReason,
25
+ loadOf,
26
+ PEAK_CLASS,
27
+ SOUNDTRACK_MODE_CAUSE,
28
+ soundtrackLoadOf
29
+ } from "../services/encode/quality/link-budget.js";
30
+ import { AUDIO_TRANSCODE_KBPS } from "../services/encode/args.js";
31
+
32
+ // AudioSpecificConfig, ISO/IEC 14496-3 §1.6.2.1: objectType(5) frequencyIndex(4)
33
+ // channelConfiguration(4) then, for AAC-LC, frameLengthFlag(1) dependsOnCoreCoder(1)
34
+ // extensionFlag(1).
35
+ const LC_48K_STEREO = Buffer.from([0x11, 0x90]);
36
+ const LC_44K_STEREO = Buffer.from([0x12, 0x10]);
37
+ const LC_48K_SIX = Buffer.from([0x11, 0xb0]);
38
+ const LC_48K_STEREO_960 = Buffer.from([0x11, 0x94]);
39
+ // objectType 5 (SBR) signalled explicitly: HE-AAC, which has no confirmed bound here.
40
+ const HE_AAC = Buffer.from([0x2b, 0x92, 0x08, 0x00]);
41
+
42
+ const aacParams = (bytes) => codecParametersOf({ codec: "aac", codecPrivateB64: bytes.toString("base64") });
43
+
44
+ test("an AudioSpecificConfig gives its object type, frequency, channels and frame length", () => {
45
+ assert.deepEqual(parseAudioSpecificConfig(LC_48K_STEREO), { objectType: 2, sampleRate: 48000, channels: 2, frameLength: 1024 });
46
+ assert.deepEqual(parseAudioSpecificConfig(LC_44K_STEREO), { objectType: 2, sampleRate: 44100, channels: 2, frameLength: 1024 });
47
+ assert.equal(parseAudioSpecificConfig(LC_48K_STEREO_960).frameLength, 960);
48
+ assert.equal(parseAudioSpecificConfig(LC_48K_SIX).channels, 6);
49
+ assert.equal(parseAudioSpecificConfig(HE_AAC).objectType, 5);
50
+ assert.equal(parseAudioSpecificConfig(Buffer.from([0x11])), null, "too short to be a config");
51
+ });
52
+
53
+ test("AAC-LC with 1024-sample frames is bounded at 6144 bits per channel per frame", () => {
54
+ assert.equal(peakKbpsOf(aacParams(LC_48K_STEREO)), 576);
55
+ assert.equal(peakKbpsOf(aacParams(LC_44K_STEREO)), 529.2);
56
+ assert.equal(peakKbpsOf(aacParams(LC_48K_SIX)), 1728, "every channel the config states");
57
+ });
58
+
59
+ test("an AAC configuration no source confirms a bound for has none", () => {
60
+ assert.equal(peakKbpsOf(aacParams(LC_48K_STEREO_960)), null, "960-sample frames");
61
+ assert.equal(peakKbpsOf(aacParams(HE_AAC)), null, "explicit HE-AAC");
62
+ assert.equal(peakKbpsOf(codecParametersOf({ codec: "aac" })), null, "no config at all");
63
+ });
64
+
65
+ test("AC-3 is bounded by the largest syncframe of A/52 Table 5.18", () => {
66
+ assert.equal(peakKbpsOf(codecParametersOf({ codec: "ac3", samplingFrequency: 48000 })), 640);
67
+ assert.equal(peakKbpsOf(codecParametersOf({ codec: "ac3", samplingFrequency: 44100 })), 640.36875);
68
+ assert.equal(peakKbpsOf(codecParametersOf({ codec: "ac3" })), 640.36875, "with no frequency, the largest of the three");
69
+ });
70
+
71
+ test("codecs with no confirmed bound give none", () => {
72
+ for (const codec of ["opus", "mp3", "eac3", "dts", "flac", "vorbis"]) {
73
+ assert.equal(peakKbpsOf(codecParametersOf({ codec, channels: 2, samplingFrequency: 48000 })), null, codec);
74
+ }
75
+ });
76
+
77
+ test("a Matroska audio track takes its bound from its own CodecPrivate, not the container's channel count", () => {
78
+ const track = new AudioTrack({
79
+ trackNumber: 2,
80
+ declaredIndex: 0,
81
+ codecId: "A_AAC",
82
+ codecPrivateB64: LC_48K_STEREO.toString("base64"),
83
+ // What a Matroska Audio element may state for an HE-AAC stream's output,
84
+ // or simply get wrong; the config is what a frame is.
85
+ channels: 6,
86
+ samplingFrequency: 96000
87
+ });
88
+ assert.equal(track.peakKbps, 576);
89
+ assert.equal(track.codecParameters.channels, 2);
90
+ });
91
+
92
+ // ---- MP4: the AudioSpecificConfig inside `esds` ----
93
+
94
+ function box(type, payload) {
95
+ const header = Buffer.alloc(8);
96
+ header.writeUInt32BE(payload.length + 8, 0);
97
+ header.write(type, 4, "latin1");
98
+ return Buffer.concat([header, payload]);
99
+ }
100
+
101
+ function fullBox(type, payload) {
102
+ return box(type, Buffer.concat([Buffer.from([0, 0, 0, 0]), payload]));
103
+ }
104
+
105
+ function u32(...values) {
106
+ const buffer = Buffer.alloc(values.length * 4);
107
+ values.forEach((value, index) => buffer.writeUInt32BE(value, index * 4));
108
+ return buffer;
109
+ }
110
+
111
+ function descriptor(tag, payload) {
112
+ return Buffer.concat([Buffer.from([tag, payload.length]), payload]);
113
+ }
114
+
115
+ function mp4WithAac(config) {
116
+ const decoderSpecificInfo = descriptor(0x05, config);
117
+ const decoderConfig = descriptor(0x04, Buffer.concat([
118
+ Buffer.from([0x40, 0x15, 0, 0, 0]), u32(0, 0), decoderSpecificInfo
119
+ ]));
120
+ const esDescriptor = descriptor(0x03, Buffer.concat([Buffer.from([0, 1, 0]), decoderConfig]));
121
+ const esds = fullBox("esds", esDescriptor);
122
+ const entry = Buffer.alloc(28);
123
+ entry.writeUInt16BE(1, 6); // data_reference_index
124
+ entry.writeUInt16BE(2, 16); // channelcount
125
+ entry.writeUInt16BE(16, 18); // samplesize
126
+ entry.writeUInt32BE(48000 * 65536, 24); // samplerate, 16.16
127
+ const mp4a = box("mp4a", Buffer.concat([entry, esds]));
128
+ const stsd = fullBox("stsd", Buffer.concat([u32(1), mp4a]));
129
+ const trak = box("trak", Buffer.concat([
130
+ fullBox("tkhd", Buffer.concat([u32(0, 0, 1, 0, 0), Buffer.alloc(60)])),
131
+ box("mdia", Buffer.concat([
132
+ fullBox("mdhd", Buffer.concat([u32(0, 0, 48000, 48000), Buffer.from([0x15, 0xc7, 0, 0])])),
133
+ fullBox("hdlr", Buffer.concat([u32(0), Buffer.from("soun", "latin1"), u32(0, 0, 0)])),
134
+ box("minf", box("stbl", stsd))
135
+ ]))
136
+ ]));
137
+ // tkhd flag "enabled" so the track is offered.
138
+ const tkhd = trak.indexOf("tkhd", 0, "latin1");
139
+ trak[tkhd + 7] = 1;
140
+ const ftyp = box("ftyp", Buffer.from("isom\0\0\0\0isom", "latin1"));
141
+ return Buffer.concat([ftyp, box("moov", trak), box("mdat", Buffer.alloc(4))]);
142
+ }
143
+
144
+ test("an MP4 soundtrack's AudioSpecificConfig is read out of its esds", async () => {
145
+ const file = mp4WithAac(LC_48K_STEREO);
146
+ const container = new Mp4Container({
147
+ readRange: async (start, end) => file.subarray(start, Math.min(end, file.length - 1) + 1),
148
+ fileSize: file.length
149
+ });
150
+
151
+ const tracks = await container.readTracks();
152
+ const audio = tracks.find((track) => track.type === "audio");
153
+
154
+ assert.ok(audio, "the sound track is read");
155
+ assert.equal(audio.codecId, "mp4a");
156
+ assert.equal(audio.codecPrivateB64, LC_48K_STEREO.toString("base64"));
157
+ assert.equal(audio.channels, 2);
158
+ assert.equal(audio.samplingFrequency, 48000);
159
+ assert.equal(audio.peakKbps, 576);
160
+ });
161
+
162
+ // ---- The inventory, and what a later reading adds ----
163
+
164
+ test("the bound travels from the container through the merge into the inventory", () => {
165
+ const declared = new AudioTrack({
166
+ trackNumber: 2, declaredIndex: 0, codecId: "A_AAC", codecPrivateB64: LC_48K_STEREO.toString("base64"), language: "jpn"
167
+ });
168
+ const merged = Container.mergeAudioFlags([{ index: 0, language: "jpn", codec: "aac", bitrateKbps: 128 }], [declared]);
169
+ const [entry] = buildAudioInventory({ embedded: merged.tracks, videoFileIndex: 0, sidecars: [] });
170
+
171
+ assert.equal(entry.peakKbps, 576);
172
+ assert.equal(entry.bitrateKbps, 128, "the stated average stays beside it");
173
+ });
174
+
175
+ const entryWith = (codecParameters, extra = {}) => ({
176
+ index: 1,
177
+ fileIndex: 7,
178
+ sourceTrackIndex: 0,
179
+ kind: "sidecar",
180
+ language: "rus",
181
+ codecParameters,
182
+ peakKbps: peakKbpsOf(codecParameters),
183
+ ...extra
184
+ });
185
+
186
+ test("a later reading that says less keeps the bound already established", () => {
187
+ const known = entryWith(aacParams(LC_48K_STEREO));
188
+ const partial = entryWith({ codec: "aac", objectType: null, frameLength: null, channels: null, sampleRate: null }, { language: "" });
189
+
190
+ const merged = mergeInventoryEntry(known, partial);
191
+
192
+ assert.equal(merged.peakKbps, 576);
193
+ assert.equal(merged.language, "rus", "an empty field of the new reading does not erase the old value");
194
+ });
195
+
196
+ test("a later reading that contradicts the known configuration withdraws the bound", () => {
197
+ const known = entryWith(aacParams(LC_48K_STEREO));
198
+ const contradicting = entryWith({ codec: "aac", objectType: null, frameLength: null, channels: null, sampleRate: 44100 });
199
+
200
+ const merged = mergeInventoryEntry(known, contradicting);
201
+
202
+ assert.equal(merged.codecParameters, null);
203
+ assert.equal(merged.peakKbps, null);
204
+ });
205
+
206
+ test("a complete reading that contradicts the known one establishes neither, and the next complete one stands", () => {
207
+ const known = entryWith(aacParams(LC_48K_STEREO));
208
+ const other = entryWith(aacParams(LC_44K_STEREO));
209
+
210
+ const contradicted = mergeInventoryEntry(known, other);
211
+ assert.equal(contradicted.peakKbps, null, "two readings that disagree establish neither");
212
+
213
+ const settled = mergeInventoryEntry(contradicted, other);
214
+ assert.equal(settled.peakKbps, 529.2);
215
+ });
216
+
217
+ test("a complete reading that agrees replaces the configuration whole, and never moves the track", () => {
218
+ const known = entryWith({ codec: "aac", objectType: 2, frameLength: null, channels: 2, sampleRate: 48000 });
219
+ const complete = { ...entryWith(aacParams(LC_48K_STEREO)), index: 99, fileIndex: 99, sourceTrackIndex: 99, kind: "embedded" };
220
+
221
+ const merged = mergeInventoryEntry(known, complete);
222
+
223
+ assert.equal(merged.peakKbps, 576);
224
+ assert.deepEqual(
225
+ [merged.index, merged.fileIndex, merged.sourceTrackIndex, merged.kind],
226
+ [1, 7, 0, "sidecar"],
227
+ "the address a player uses stays"
228
+ );
229
+ });
230
+
231
+ // ---- What the link is asked, and how a track is produced ----
232
+
233
+ test("a copied soundtrack weighs its codec's bound, else its stated rate, else nothing", () => {
234
+ assert.deepEqual(soundtrackLoadOf({ peakKbps: 576, bitrateKbps: 128 }, false), { mbps: 0.576, peakClass: PEAK_CLASS.KNOWN });
235
+ assert.deepEqual(soundtrackLoadOf({ peakKbps: null, bitrateKbps: 128 }, false), { mbps: 0.128, peakClass: PEAK_CLASS.ESTIMATED });
236
+ assert.deepEqual(soundtrackLoadOf({ peakKbps: null, bitrateKbps: null }, false), { mbps: null, peakClass: PEAK_CLASS.UNKNOWN });
237
+ assert.deepEqual(soundtrackLoadOf(null, false), { mbps: null, peakClass: PEAK_CLASS.UNKNOWN });
238
+ });
239
+
240
+ test("a re-encoded soundtrack weighs the rate it is asked for, as an estimate — ffmpeg's -b:a is a target", () => {
241
+ assert.deepEqual(
242
+ soundtrackLoadOf({ peakKbps: 576, bitrateKbps: 640 }, true),
243
+ { mbps: AUDIO_TRANSCODE_KBPS / 1000, peakClass: PEAK_CLASS.ESTIMATED }
244
+ );
245
+ assert.equal(soundtrackLoadOf({ peakKbps: 576 }, null), null, "no sound, no part");
246
+ });
247
+
248
+ test("the field case: a dub with no stated rate on an 80 Mbit/s link is now re-encoded and fits", () => {
249
+ // 2026-10-01, Drifters 06: picture 3.186 Mbit/s copied, the `Rus Sound` .mka
250
+ // stating nothing. Copied, nothing can be compared; re-encoded, it can.
251
+ const dub = { peakKbps: null, bitrateKbps: null };
252
+ const picture = { mbps: 3.186, peakClass: PEAK_CLASS.ESTIMATED };
253
+
254
+ const copied = linkCouldCarry(80.57, loadOf(picture, soundtrackLoadOf(dub, false)));
255
+ assert.equal(copied.verdict, LINK_VERDICT.NO_SAFE_BOUND);
256
+
257
+ const mode = chooseSoundtrackMode({ entry: dub, browserPlays: true, transcodeAllowed: true });
258
+ assert.equal(mode.transcode, true);
259
+ assert.equal(mode.cause, SOUNDTRACK_MODE_CAUSE.NO_FIGURE);
260
+ const sent = linkCouldCarry(80.57, loadOf(picture, soundtrackLoadOf(dub, mode.transcode)));
261
+ assert.equal(sent.admitted, true);
262
+ assert.equal(sent.verdict, LINK_VERDICT.ESTIMATED_TO_FIT);
263
+ });
264
+
265
+ test("a track's mode follows from what the browser plays, whether it has a figure, and whether re-encoding is allowed", () => {
266
+ const stated = { bitrateKbps: 128 };
267
+ const bounded = { peakKbps: 576 };
268
+ const silent = {};
269
+ const mode = (entry, browserPlays, transcodeAllowed = true) =>
270
+ chooseSoundtrackMode({ entry, browserPlays, transcodeAllowed });
271
+
272
+ assert.deepEqual(mode(stated, true), { transcode: false, cause: SOUNDTRACK_MODE_CAUSE.COPY });
273
+ assert.deepEqual(mode(bounded, true), { transcode: false, cause: SOUNDTRACK_MODE_CAUSE.COPY });
274
+ assert.deepEqual(mode(silent, true), { transcode: true, cause: SOUNDTRACK_MODE_CAUSE.NO_FIGURE });
275
+ assert.deepEqual(mode(stated, false), { transcode: true, cause: SOUNDTRACK_MODE_CAUSE.UNPLAYABLE });
276
+ assert.deepEqual(mode(silent, true, false), { transcode: false, cause: SOUNDTRACK_MODE_CAUSE.COPY_WITHOUT_FIGURE });
277
+ assert.deepEqual(mode(stated, false, false), { transcode: null, cause: SOUNDTRACK_MODE_CAUSE.CANNOT_SERVE });
278
+ });
279
+
280
+ test("a refusal with no figure names the part that has none", () => {
281
+ const picture = { mbps: null, peakClass: PEAK_CLASS.UNKNOWN };
282
+ const sound = { mbps: null, peakClass: PEAK_CLASS.UNKNOWN };
283
+ const known = { mbps: 1, peakClass: PEAK_CLASS.KNOWN };
284
+
285
+ assert.match(linkRefusalReason(linkCouldCarry(50, loadOf(known, sound)), "too big"), /how much the soundtrack would send/);
286
+ assert.match(linkRefusalReason(linkCouldCarry(50, loadOf(picture, known)), "too big"), /how much the picture would send/);
287
+ assert.match(linkRefusalReason(linkCouldCarry(50, loadOf(picture, sound)), "too big"), /the picture or the soundtrack/);
288
+ assert.equal(linkRefusalReason(linkCouldCarry(0.5, loadOf(known, known)), "too big"), "too big");
289
+ });