summaryrefslogtreecommitdiffstats
path: root/dom/media/webaudio/test/test_decoderDelay.html
blob: d0fbfbed29e7e91dc8c796e19106b17a66200d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Test that decoder delay is handled</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <script class="testbody" type="text/javascript">
    SimpleTest.waitForExplicitFinish();
    const {AppConstants} =
      SpecialPowers.ChromeUtils.import("resource://gre/modules/AppConstants.jsm");

    var tests_half_a_second = [
      "half-a-second-1ch-44100-aac.mp4",
      "half-a-second-1ch-44100-flac.flac",
      "half-a-second-1ch-44100-libmp3lame.mp3",
      "half-a-second-1ch-44100-libopus.opus",
      "half-a-second-1ch-44100-libopus.webm",
      "half-a-second-1ch-44100-libvorbis.ogg",
      "half-a-second-1ch-44100.wav",
      "half-a-second-1ch-48000-aac.mp4",
      "half-a-second-1ch-48000-flac.flac",
      "half-a-second-1ch-48000-libmp3lame.mp3",
      "half-a-second-1ch-48000-libopus.opus",
      "half-a-second-1ch-48000-libopus.webm",
      "half-a-second-1ch-48000-libvorbis.ogg",
      "half-a-second-1ch-48000.wav",
      "half-a-second-2ch-44100-aac.mp4",
      "half-a-second-2ch-44100-flac.flac",
      "half-a-second-2ch-44100-libmp3lame.mp3",
      "half-a-second-2ch-44100-libopus.opus",
      "half-a-second-2ch-44100-libopus.webm",
      "half-a-second-2ch-44100-libvorbis.ogg",
      "half-a-second-2ch-44100.wav",
      "half-a-second-2ch-48000-aac.mp4",
      "half-a-second-2ch-48000-flac.flac",
      "half-a-second-2ch-48000-libmp3lame.mp3",
      "half-a-second-2ch-48000-libopus.opus",
      "half-a-second-2ch-48000-libopus.webm",
      "half-a-second-2ch-48000-libvorbis.ogg",
      "half-a-second-2ch-48000.wav",
    ];

    // Those files are almost exactly half a second, but don't have enough pre-roll/padding
    // information in the container, or the container isn't parsed properly, so
    // aren't trimmed appropriately.
    // vorbis webm, opus mp4, aac adts
    var tests_adts = [
      "half-a-second-1ch-44100-aac.aac",
      "half-a-second-1ch-44100-libopus.mp4",
      "half-a-second-1ch-44100-libvorbis.webm",
      "half-a-second-1ch-48000-aac.aac",
      "half-a-second-1ch-48000-libopus.mp4",
      "half-a-second-1ch-48000-libvorbis.webm",
      "half-a-second-2ch-44100-aac.aac",
      "half-a-second-2ch-44100-libopus.mp4",
      "half-a-second-2ch-44100-libvorbis.webm",
      "half-a-second-2ch-48000-aac.aac",
      "half-a-second-2ch-48000-libopus.mp4",
      "half-a-second-2ch-48000-libvorbis.webm",
    ];

    // Other files that have interesting characteristics.
    var tests_others = [
      {
        // Very short VBR file, 16 frames of audio at 44100. Padding spanning two
        // packets.
        "path": "sixteen-frames.mp3",
        "frameCount": 16,
        "samplerate": 44100,
        "fuzz": {}
      },
      {
        // This is incorrect (the duration should be 0.5s exactly)
        // This is tracked in https://github.com/mozilla/mp4parse-rust/issues/404
        "path":"half-a-second-1ch-44100-aac-afconvert.mp4",
        "frameCount": 22464,
        "samplerate": 44100,
        "fuzz": {
          "android": 2
        }
      }
    ];

    var all_tests = [tests_half_a_second, tests_adts, tests_others].flat();

    var count = 0;
    function checkDone() {
      if (++count == all_tests.length) {
        SimpleTest.finish();
      }
    }

    async function doit() {
      var context = new OfflineAudioContext(1, 128, 48000);
      tests_half_a_second.forEach(async testfile => {
        var response = await fetch(testfile);
        var buffer = await response.arrayBuffer();
        var decoded = await context.decodeAudioData(buffer);
        is(
          decoded.duration,
          0.5,
          "The file " + testfile + " is half a second."
        );
        // Value found empirically after looking at the files. The initial
        // amplitude should be 0 at phase 0 because those files are sine wave.
        // The compression is sometimes lossy and the first sample is not always
        // exactly 0.0.
        ok(
          Math.abs(decoded.getChannelData(0)[0]) <= 0.022,
          `The start point for ${testfile} is correct ${ decoded.getChannelData(0)[0] }`
        );
        checkDone();
      });
      tests_adts.forEach(async testfile => {
        var response = await fetch(testfile);
        var buffer = await response.arrayBuffer();
        var decoded = await context.decodeAudioData(buffer);
        // Value found empirically after looking at the files. ADTS containers
        // don't have encoder delay / padding info so we can't trim correctly.
        ok(
          Math.abs(decoded.duration - 0.5) < 0.02,
          `The ADTS file ${testfile} is about half a second (${decoded.duration}, error: ${Math.abs(decoded.duration-0.5)}).`
        );
        checkDone();
      });
      tests_others.forEach(async test => {
        // Get an context at a specific rate to avoid duration changes due to resampling.
        var contextAtRate = new OfflineAudioContext(1, 128, test.samplerate);
        var response = await fetch(test.path);
        var buffer = await response.arrayBuffer();
        var decoded = await contextAtRate.decodeAudioData(buffer);
        const fuzz = test.fuzz[AppConstants.platform] ?? 0;
        ok(Math.abs(decoded.length - test.frameCount) <= fuzz, `${test.path} is ${decoded.length} frames long`);
        checkDone();
      });
    }

    doit();
  </script>
</body>
</html>