tor-browser

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

mediastreamaudiosourcenode-routing.html (4628B)


      1 <!DOCTYPE html>
      2 
      3 <html class="a">
      4  <head>
      5    <title>MediaStreamAudioSourceNode</title>
      6    <script src="/resources/testharness.js"></script>
      7    <script src="/resources/testharnessreport.js"></script>
      8  </head>
      9  <body class="a">
     10    <div id="log"></div>
     11    <script>
     12      function binIndexForFrequency(frequency, analyser) {
     13        return (
     14          1 +
     15          Math.round(
     16            (frequency * analyser.fftSize) / analyser.context.sampleRate
     17          )
     18        );
     19      }
     20 
     21      const t = async_test(
     22        "MediaStreamAudioSourceNode captures the right track."
     23      );
     24      t.step(function() {
     25          const ac = new AudioContext();
     26          // Test that the right track is captured. Set up a MediaStream that has two
     27          // tracks, one with a tone at 100Hz and one with a tone at 1000Hz.
     28          const dest0 = ac.createMediaStreamDestination();
     29          const dest1 = ac.createMediaStreamDestination();
     30          const osc0 = ac.createOscillator();
     31          const osc1 = ac.createOscillator();
     32          osc0.frequency.value = 100;
     33          osc1.frequency.value = 1000;
     34          osc0.connect(dest0);
     35          osc1.connect(dest1);
     36          osc0.start(0);
     37          osc1.start(0);
     38          const track0 = dest0.stream.getAudioTracks()[0];
     39          const track0id = track0.id;
     40          const track1 = dest1.stream.getAudioTracks()[0];
     41          const track1id = track1.id;
     42 
     43          let ids = [track0id, track1id];
     44          ids.sort();
     45          let targetFrequency;
     46          let otherFrequency;
     47          if (ids[0] == track0id) {
     48              targetFrequency = 100;
     49              otherFrequency = 1000;
     50          } else {
     51              targetFrequency = 1000;
     52              otherFrequency = 100;
     53          }
     54 
     55          let twoTrackMediaStream = new MediaStream();
     56          twoTrackMediaStream.addTrack(track0);
     57          twoTrackMediaStream.addTrack(track1);
     58 
     59          const twoTrackSource = ac.createMediaStreamSource(twoTrackMediaStream);
     60          const analyser = ac.createAnalyser();
     61          // Don't do smoothing so that the frequency data changes quickly
     62          analyser.smoothingTimeConstant = 0;
     63 
     64          twoTrackSource.connect(analyser);
     65 
     66          const indexToCheckForHighEnergy = binIndexForFrequency(
     67              targetFrequency,
     68              analyser
     69          );
     70          const indexToCheckForLowEnergy = binIndexForFrequency(
     71              otherFrequency,
     72              analyser
     73          );
     74          let frequencyData = new Float32Array(1024);
     75          let checkCount = 0;
     76          let numberOfRemovals = 0;
     77          let stopped = false;
     78          function analyse() {
     79              analyser.getFloatFrequencyData(frequencyData);
     80              // there should be high energy in the right bin, higher than 40dbfs because
     81              // it's supposed to be a sine wave at 0dbfs
     82              if (frequencyData[indexToCheckForHighEnergy] > -40 && !stopped) {
     83                  assert_true(true, "Correct track routed to the AudioContext.");
     84                  checkCount++;
     85              }
     86              if (stopped && frequencyData[indexToCheckForHighEnergy] < -40) {
     87                  assert_true(
     88                      true,
     89                      `After stopping the track, low energy is found in the
     90              same bin`
     91                  );
     92                  checkCount++;
     93              }
     94              if (checkCount > 5 && checkCount < 20) {
     95                  twoTrackMediaStream.getAudioTracks().forEach(track => {
     96                      if (track.id == ids[0]) {
     97                          numberOfRemovals++;
     98                          window.removedTrack = track;
     99                          twoTrackMediaStream.removeTrack(track);
    100                      }
    101                  });
    102                  assert_true(
    103                      numberOfRemovals == 1,
    104                      `The mediastreamtrack can only be
    105        removed once from the mediastream`
    106                  );
    107              } else if (checkCount >= 20 && checkCount < 30) {
    108                  window.removedTrack.stop();
    109                  stopped = true;
    110              } else if (checkCount >= 30) {
    111                  assert_true(
    112                      numberOfRemovals == 1,
    113                      `After removing the track from the
    114        mediastream, it's still routed to the graph.`
    115                  );
    116                  // After some time, consider that it worked.
    117                  t.done();
    118                  return;
    119              }
    120 
    121              t.step_timeout(analyse, 100);
    122          }
    123          t.step_timeout(analyse, 100);
    124      });
    125    </script>
    126  </body>
    127 </html>