summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_peerConnection_callbacks.html
blob: ffd7a1b0c1e9fc5f43c87a2e4d55c3193215fc6f (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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  createHTML({
    title: "PeerConnection using callback functions",
    bug: "1119593",
    visible: true
  });

// This still aggressively uses promises, but it is testing that the callback functions
// are properly in place.

// wrapper that turns a callback-based function call into a promise
function pcall(o, f, beforeArg) {
  return new Promise((resolve, reject) => {
    var args = [resolve, reject];
    if (typeof beforeArg !== 'undefined') {
      args.unshift(beforeArg);
    }
    info('Calling ' + f.name);
    f.apply(o, args);
  });
}

var pc1 = new RTCPeerConnection();
var pc2 = new RTCPeerConnection();

var pc2_haveRemoteOffer = new Promise(resolve => {
  pc2.onsignalingstatechange =
    e => (e.target.signalingState == "have-remote-offer") && resolve();
});
var pc1_stable = new Promise(resolve => {
  pc1.onsignalingstatechange =
    e => (e.target.signalingState == "stable") && resolve();
});

pc1.onicecandidate = e => {
  pc2_haveRemoteOffer
    .then(() => !e.candidate || pcall(pc2, pc2.addIceCandidate, e.candidate))
    .catch(generateErrorCallback());
};
pc2.onicecandidate = e => {
  pc1_stable
    .then(() => !e.candidate || pcall(pc1, pc1.addIceCandidate, e.candidate))
    .catch(generateErrorCallback());
};

runNetworkTest(async function() {
  // Tests trigger warnings
  await SpecialPowers.pushPrefEnv({
    set: [['media.peerconnection.treat_warnings_as_errors', false]]
  });

  const v1 = createMediaElement('video', 'v1');
  const v2 = createMediaElement('video', 'v2');

  const delivered = new Promise(resolve => {
    pc2.onaddstream = e => {
      v2.srcObject = e.stream;
      resolve(e.stream);
    };
  });

  var canPlayThrough = new Promise(resolve => v2.canplaythrough = resolve);
  is(v2.currentTime, 0, "v2.currentTime is zero at outset");

  // not testing legacy gUM here
  await navigator.mediaDevices.getUserMedia({ video: true, audio: true })
    .then(stream => pc1.addStream(v1.srcObject = stream))
    .then(() => pcall(pc1, pc1.createOffer))
    .then(offer => pcall(pc1, pc1.setLocalDescription, offer))
    .then(() => pcall(pc2, pc2.setRemoteDescription, pc1.localDescription))
    .then(() => pcall(pc2, pc2.createAnswer))
    .then(answer => pcall(pc2, pc2.setLocalDescription, answer))
    .then(() => pcall(pc1, pc1.setRemoteDescription, pc2.localDescription))
    .then(() => delivered)
  //    .then(() => canPlayThrough)    // why doesn't this fire?
    .then(() => waitUntil(() => v2.currentTime > 0))
    .then(() => ok(v2.currentTime > 0, "v2.currentTime is moving (" + v2.currentTime + ")"))
    .then(() => ok(true, "Connected."))
    .then(() => { v1.pause(); v2.pause(); });
});
</script>
</pre>
</body>
</html>