tor-browser

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

test_peerConnection_callbacks.html (2859B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <script type="application/javascript" src="pc.js"></script>
      5 </head>
      6 <body>
      7 <pre id="test">
      8 <script type="application/javascript">
      9  createHTML({
     10    title: "PeerConnection using callback functions",
     11    bug: "1119593",
     12    visible: true
     13  });
     14 
     15 // This still aggressively uses promises, but it is testing that the callback functions
     16 // are properly in place.
     17 
     18 // wrapper that turns a callback-based function call into a promise
     19 function pcall(o, f, beforeArg) {
     20  return new Promise((resolve, reject) => {
     21    var args = [resolve, reject];
     22    if (typeof beforeArg !== 'undefined') {
     23      args.unshift(beforeArg);
     24    }
     25    info('Calling ' + f.name);
     26    f.apply(o, args);
     27  });
     28 }
     29 
     30 var pc1 = new RTCPeerConnection();
     31 var pc2 = new RTCPeerConnection();
     32 
     33 var pc2_haveRemoteOffer = new Promise(resolve => {
     34  pc2.onsignalingstatechange =
     35    e => (e.target.signalingState == "have-remote-offer") && resolve();
     36 });
     37 var pc1_stable = new Promise(resolve => {
     38  pc1.onsignalingstatechange =
     39    e => (e.target.signalingState == "stable") && resolve();
     40 });
     41 
     42 pc1.onicecandidate = e => {
     43  pc2_haveRemoteOffer
     44    .then(() => !e.candidate || pcall(pc2, pc2.addIceCandidate, e.candidate))
     45    .catch(generateErrorCallback());
     46 };
     47 pc2.onicecandidate = e => {
     48  pc1_stable
     49    .then(() => !e.candidate || pcall(pc1, pc1.addIceCandidate, e.candidate))
     50    .catch(generateErrorCallback());
     51 };
     52 
     53 runNetworkTest(async function() {
     54  // Tests trigger warnings
     55  await SpecialPowers.pushPrefEnv({
     56    set: [['media.peerconnection.treat_warnings_as_errors', false]]
     57  });
     58 
     59  const v1 = createMediaElement('video', 'v1');
     60  const v2 = createMediaElement('video', 'v2');
     61 
     62  const delivered = new Promise(resolve => {
     63    pc2.onaddstream = e => {
     64      v2.srcObject = e.stream;
     65      resolve(e.stream);
     66    };
     67  });
     68 
     69  var canPlayThrough = new Promise(resolve => v2.canplaythrough = resolve);
     70  is(v2.currentTime, 0, "v2.currentTime is zero at outset");
     71 
     72  // not testing legacy gUM here
     73  await navigator.mediaDevices.getUserMedia({ video: true, audio: true })
     74    .then(stream => pc1.addStream(v1.srcObject = stream))
     75    .then(() => pcall(pc1, pc1.createOffer))
     76    .then(offer => pcall(pc1, pc1.setLocalDescription, offer))
     77    .then(() => pcall(pc2, pc2.setRemoteDescription, pc1.localDescription))
     78    .then(() => pcall(pc2, pc2.createAnswer))
     79    .then(answer => pcall(pc2, pc2.setLocalDescription, answer))
     80    .then(() => pcall(pc1, pc1.setRemoteDescription, pc2.localDescription))
     81    .then(() => delivered)
     82  //    .then(() => canPlayThrough)    // why doesn't this fire?
     83    .then(() => waitUntil(() => v2.currentTime > 0))
     84    .then(() => ok(v2.currentTime > 0, "v2.currentTime is moving (" + v2.currentTime + ")"))
     85    .then(() => ok(true, "Connected."))
     86    .then(() => { v1.pause(); v2.pause(); });
     87 });
     88 </script>
     89 </pre>
     90 </body>
     91 </html>