tor-browser

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

get_user_media2.html (2542B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head><meta charset="UTF-8"></head>
      4 <body>
      5 <div id="message"></div>
      6 <script>
      7 // Specifies whether we are using fake streams to run this automation
      8 var useFakeStreams = true;
      9 try {
     10  var audioDevice = SpecialPowers.getCharPref("media.audio_loopback_dev");
     11  var videoDevice = SpecialPowers.getCharPref("media.video_loopback_dev");
     12  dump("TEST DEVICES: Using media devices:\n");
     13  dump("audio: " + audioDevice + "\nvideo: " + videoDevice + "\n");
     14  useFakeStreams = false;
     15 } catch (e) {
     16  dump("TEST DEVICES: No test devices found (in media.{audio,video}_loopback_dev, using fake streams.\n");
     17  useFakeStreams = true;
     18 }
     19 
     20 function message(m) {
     21  document.getElementById("message").innerHTML += `${m}<br>`;
     22  top.postMessage(m, "*");
     23 }
     24 
     25 var gStreams = [];
     26 var gVideoEvents = [];
     27 var gAudioEvents = [];
     28 
     29 async function requestDevice(aAudio, aVideo, aShare, aBadDevice = false) {
     30  const opts = {video: aVideo, audio: aAudio};
     31  if (aShare) {
     32    opts.video = { mediaSource: aShare };
     33  }
     34  if (useFakeStreams) {
     35    opts.fake = true;
     36  }
     37 
     38  if (aVideo && aBadDevice) {
     39    opts.video = {
     40      deviceId: "bad device",
     41    };
     42    opts.fake = true;
     43  }
     44 
     45  if (aAudio && aBadDevice) {
     46    opts.audio = {
     47      deviceId: "bad device",
     48    };
     49    opts.fake = true;
     50  }
     51 
     52  try {
     53    const stream = await navigator.mediaDevices.getUserMedia(opts)
     54    gStreams.push(stream);
     55 
     56    const videoTrack = stream.getVideoTracks()[0];
     57    if (videoTrack) {
     58      for (const name of ["mute", "unmute", "ended"]) {
     59        videoTrack.addEventListener(name, () => gVideoEvents.push(name));
     60      }
     61    }
     62 
     63    const audioTrack = stream.getAudioTracks()[0];
     64    if (audioTrack) {
     65      for (const name of ["mute", "unmute", "ended"]) {
     66        audioTrack.addEventListener(name, () => gAudioEvents.push(name));
     67      }
     68    }
     69    message("ok");
     70  } catch (err) {
     71    message("error: " + err);
     72  }
     73 }
     74 message("pending");
     75 
     76 function stopTracks(aKind) {
     77  for (let stream of gStreams) {
     78    for (let track of stream.getTracks()) {
     79      if (track.kind == aKind) {
     80        track.stop();
     81        stream.removeTrack(track);
     82      }
     83    }
     84  }
     85  gStreams = gStreams.filter(s => !!s.getTracks().length);
     86  if (aKind == "video") {
     87    gVideoEvents = [];
     88  } else if (aKind == "audio") {
     89    gAudioEvents = [];
     90  }
     91 }
     92 
     93 function closeStream() {
     94  for (let stream of gStreams) {
     95    for (let track of stream.getTracks()) {
     96      track.stop();
     97    }
     98  }
     99  gStreams = [];
    100  gVideoEvents = [];
    101  gAudioEvents = [];
    102  message("closed");
    103 }
    104 </script>
    105 </body>
    106 </html>