summaryrefslogtreecommitdiffstats
path: root/dom/media/tests/mochitest/test_peerConnection_scaleResolution.html
blob: ec83780b837412eb30389ae3592a751e6aac3597 (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
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript;version=1.8">
  createHTML({
    bug: "1244913",
    title: "Scale resolution down on a PeerConnection",
    visible: true
  });

  var mustRejectWith = (msg, reason, f) =>
    f().then(() => ok(false, msg),
             e => is(e.name, reason, msg));

  var removeAllButCodec = (d, codec) =>
    (d.sdp = d.sdp.replace(/m=video (\w) UDP\/TLS\/RTP\/SAVPF \w.*\r\n/,
                           "m=video $1 UDP/TLS/RTP/SAVPF " + codec + "\r\n"), d);

  function testScale(codec) {
    var pc1 = new RTCPeerConnection();
    var pc2 = new RTCPeerConnection();

    var add = (pc, can, failed) => can && pc.addIceCandidate(can).catch(failed);
    pc1.onicecandidate = e => add(pc2, e.candidate, generateErrorCallback());
    pc2.onicecandidate = e => add(pc1, e.candidate, generateErrorCallback());

    info("testing scaling with " + codec);

    pc1.onnegotiationneeded = e =>
      pc1.createOffer()
      .then(d => pc1.setLocalDescription(codec == "VP8" ? d : removeAllButCodec(d, 126)))
      .then(() => pc2.setRemoteDescription(pc1.localDescription))
      .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
      .then(() => pc1.setRemoteDescription(pc2.localDescription))
      .catch(generateErrorCallback());

    return navigator.mediaDevices.getUserMedia({ video: true })
    .then(stream => {
      var v1 = createMediaElement('video', 'v1');
      var v2 = createMediaElement('video', 'v2');

      is(v2.currentTime, 0, "v2.currentTime is zero at outset");

      v1.srcObject = stream;
      var sender = pc1.addTrack(stream.getVideoTracks()[0], stream);

      return mustRejectWith("Invalid scaleResolutionDownBy must reject", "RangeError",
                            () => sender.setParameters({ encodings:
                                                       [{ scaleResolutionDownBy: 0.5 } ] }))
      .then(() => sender.setParameters({ encodings: [{ maxBitrate: 60000,
                                                       scaleResolutionDownBy: 2 }] }))
      .then(() => new Promise(resolve => pc2.ontrack = e => resolve(e)))
      .then(e => v2.srcObject = e.streams[0])
      .then(() => new Promise(resolve => v2.onloadedmetadata = resolve))
      .then(() => waitUntil(() => v2.currentTime > 0 && v2.srcObject.currentTime > 0))
      .then(() => ok(v2.currentTime > 0, "v2.currentTime is moving (" + v2.currentTime + ")"))
      .then(() => wait(3000)) // TODO: Bug 1248154
      .then(() => {
        ok(v1.videoWidth > 0, "source width is positive");
        ok(v1.videoHeight > 0, "source height is positive");
        if (v2.videoWidth == 640 && v2.videoHeight == 480) { // TODO: Bug 1248154
          info("Skipping test due to Bug 1248154");
        } else {
          is(v2.videoWidth, v1.videoWidth / 2, "sink is half the width of source");
          is(v2.videoHeight, v1.videoHeight / 2, "sink is half the height of source");
        }
      })
      .then(() => {
        stream.getTracks().forEach(track => track.stop());
        v1.srcObject = v2.srcObject = null;
      })
    })
    .catch(generateErrorCallback());
  }

  runNetworkTest(() => testScale("VP8").then(() => testScale("H264"))
                 .then(networkTestFinished));
</script>
</pre>
</body>
</html>