summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_peerConnection_maxFsConstraint.html
blob: 7f7431aa80b3978999f4457de1af67ec5444ab0f (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  createHTML({
    bug: "1393687",
    title: "Enforce max-fs constraint 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");
    return d;
  };

  var mungeSDP = (d, forceH264) => {
    if (forceH264) {
      removeAllButCodec(d, 126);
      d.sdp = d.sdp.replace(/a=fmtp:126 (.*);packetization-mode=1/, "a=fmtp:126 $1;packetization-mode=1;max-fs=100");
    } else {
      d.sdp = d.sdp.replace(/max-fs=\d+/, "max-fs=100");
    }
    return d;
  };

  const checkForH264Support = async () => {
    const pc = new RTCPeerConnection();
    const offer = await pc.createOffer({offerToReceiveVideo: true});
    return offer.sdp.match(/a=rtpmap:[1-9][0-9]* H264/);
  };

  let resolutionAlignment = 1;

  function testScale(codec) {
    var v1 = createMediaElement('video', 'v1');
    var v2 = createMediaElement('video', 'v2');

    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 max-fs with" + codec);

    pc1.onnegotiationneeded = e =>
      pc1.createOffer()
      .then(d => pc1.setLocalDescription(mungeSDP(d, codec == "H264")))
      .then(() => pc2.setRemoteDescription(pc1.localDescription))
      .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(mungeSDP(d, codec =="H264")))
      .then(() => pc1.setRemoteDescription(pc2.localDescription))
      .catch(generateErrorCallback());

    pc2.ontrack = e => {
      v2.srcObject = e.streams[0];
    };

    var stream;

    return navigator.mediaDevices.getUserMedia({ video: true })
      .then(s => {
        stream = s;
        v1.srcObject = stream;
        let track = stream.getVideoTracks()[0];
        let sender = pc1.addTrack(track, stream);
        is(v2.currentTime, 0, "v2.currentTime is zero at outset");
      })
      .then(() => wait(5000))
      .then(() => {
        if (v2.videoWidth == 0 && v2.videoHeight == 0) {
          info("Skipping test, insufficient time for video to start.");
        } else {
          const expectedWidth = 184 - 184 % resolutionAlignment;
          const expectedHeight = 138 - 138 % resolutionAlignment;
          is(v2.videoWidth, expectedWidth,
             `sink width should be ${expectedWidth} for ${codec}`);
          is(v2.videoHeight, expectedHeight,
             `sink height should be ${expectedHeight} for ${codec}`);
        }})
      .then(() => {
        stream.getTracks().forEach(track => track.stop());
        v1.srcObject = v2.srcObject = null;
      }).catch(generateErrorCallback());
  }

  runNetworkTest(async () => {
    await pushPrefs(['media.peerconnection.video.lock_scaling', true]);
    if (await checkForH264Support()) {
      if (navigator.userAgent.includes("Android")) {
        // Android only has a hw encoder for h264
        resolutionAlignment = 16;
      }
      await matchPlatformH264CodecPrefs();
      await testScale("H264");
    }

    // Disable h264 hardware support, to ensure it is not prioritized over VP8
    await pushPrefs(["media.webrtc.hw.h264.enabled", false]);
    await testScale("VP8");
  });
</script>
</pre>
</body>
</html>