tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_mediaElementAudioSourceNodeCrossOrigin.html (2999B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <meta charset="utf-8">
      4 <head>
      5  <title>Test MediaStreamAudioSourceNode doesn't get data from cross-origin media resources</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      8 </head>
      9 <body>
     10 <pre id="test">
     11 <script class="testbody" type="text/javascript">
     12 SimpleTest.waitForExplicitFinish();
     13 
     14 // Turn off the authentication dialog blocking for this test.
     15 SpecialPowers.setIntPref("network.auth.subresource-http-auth-allow", 2)
     16 
     17 var tests = [
     18  // Not the same origin no CORS asked for, should have silence
     19  { url: "http://example.org:80/tests/dom/media/webaudio/test/small-shot.ogg",
     20    cors: null,
     21    expectSilence: true },
     22  // Same origin, should have sound
     23  { url: "small-shot.ogg",
     24    cors: null,
     25    expectSilence: false },
     26  // Cross-origin but we asked for CORS and the server answered with the right
     27  // header, should have
     28  { url: "http://example.org:80/tests/dom/media/webaudio/test/corsServer.sjs",
     29    cors: "anonymous",
     30    expectSilence: false }
     31 ];
     32 
     33 var testsRemaining = tests.length;
     34 
     35 tests.forEach(function(e) {
     36  e.ac = new AudioContext();
     37  var a = new Audio();
     38  if (e.cors) {
     39    a.crossOrigin = e.cors;
     40  }
     41  a.src = e.url;
     42  document.body.appendChild(a);
     43 
     44  a.onloadedmetadata = () => {
     45    // Wait for "loadedmetadata" before capturing since tracks are then known
     46    // directly. If we set up the capture before "loadedmetadata" we
     47    // (internally) have to wait an extra async jump for tracks to become known
     48    // to main thread, before setting up audio data forwarding to the node.
     49    // As that happens, the audio resource may have already ended on slow test
     50    // machines, causing failures.
     51    a.onloadedmetadata = null;
     52    var measn = e.ac.createMediaElementSource(a);
     53    var sp = e.ac.createScriptProcessor(2048, 1);
     54    sp.seenSound = false;
     55    sp.onaudioprocess = checkBufferSilent;
     56 
     57    measn.connect(sp);
     58    a.play();
     59  };
     60 
     61  function checkFinished(sp) {
     62    if (a.ended) {
     63      sp.onaudioprocess = null;
     64      var not = e.expectSilence ? "" : "not";
     65      is(e.expectSilence, !sp.seenSound,
     66         "Buffer is " + not + " silent as expected, for " +
     67         e.url + " (cors: " + e.cors + ")");
     68      if (--testsRemaining == 0) {
     69        SimpleTest.finish();
     70      }
     71    }
     72  }
     73 
     74  function checkBufferSilent(e) {
     75    var inputArrayBuffer = e.inputBuffer.getChannelData(0);
     76    var silent = true;
     77    for (var i = 0; i < inputArrayBuffer.length; i++) {
     78      if (inputArrayBuffer[i] != 0.0) {
     79        silent = false;
     80        break;
     81      }
     82    }
     83    // It is acceptable to find a full buffer of silence here, even if we expect
     84    // sound, because Gecko's looping on media elements is not seamless and we
     85    // can underrun. We are looking for at least one buffer of non-silent data.
     86    e.target.seenSound = !silent || e.target.seenSound;
     87    checkFinished(e.target);
     88    return silent;
     89  }
     90 });
     91 </script>
     92 </pre>
     93 </body>
     94 </html>