summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/tests/mochitests/test_peerConnection_multiple_captureStream_canvas_2d.html
blob: 9ad25e7852882099c1d8487471a090c6710ebe28 (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
115
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
  <script type="application/javascript" src="/tests/dom/canvas/test/captureStream_common.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
createHTML({
  bug: "1166832",
  title: "Canvas(2D)::Multiple CaptureStream as video-only input to peerconnection",
  visible: true
});

/**
 * Test to verify using multiple capture streams concurrently.
 */
runNetworkTest(async () => {
  // [TODO] re-enable HW decoder after bug 1526207 is fixed.
  if (navigator.userAgent.includes("Android")) {
    await pushPrefs(["media.navigator.mediadatadecoder_vpx_enabled", false]);
    await pushPrefs(["media.webrtc.hw.h264.enabled", false]);
  }

  var test = new PeerConnectionTest();
  var h = new CaptureStreamTestHelper2D(50, 50);

  var vremote1;
  var stream1;
  var canvas1 = h.createAndAppendElement('canvas', 'source_canvas1');

  var vremote2;
  var stream2;
  var canvas2 = h.createAndAppendElement('canvas', 'source_canvas2');

  const threshold = 128;

  test.setMediaConstraints([{video: true}, {video: true}], []);
  test.chain.replace("PC_LOCAL_GUM", [
    function PC_LOCAL_CANVAS_CAPTURESTREAM(test) {
      h.drawColor(canvas1, h.green);
      h.drawColor(canvas2, h.blue);
      stream1 = canvas1.captureStream(0); // fps = 0 to capture single frame
      test.pcLocal.attachLocalStream(stream1);
      stream2 = canvas2.captureStream(0); // fps = 0 to capture single frame
      test.pcLocal.attachLocalStream(stream2);
      var i = 0;
      return setInterval(function() {
        try {
          info("draw " + i ? "green" : "red/blue");
          h.drawColor(canvas1, i ? h.green : h.red);
          h.drawColor(canvas2, i ? h.green : h.blue);
          i = 1 - i;
          stream1.requestFrame();
          stream2.requestFrame();
        } catch (e) {
          // ignore; stream might have shut down, and we don't bother clearing
          // the setInterval.
        }
      }, 500);
    }
  ]);

  test.chain.append([
    function CHECK_REMOTE_VIDEO() {
      is(test.pcRemote.remoteMediaElements.length, 2, "pcRemote Should have 2 remote media elements");
      vremote1 = test.pcRemote.remoteMediaElements[0];
      vremote2 = test.pcRemote.remoteMediaElements[1];

      // since we don't know which remote video is created first, we don't know
      // which should be blue or red, but this will make sure that one is
      // green and one is blue
      return Promise.race([
               Promise.all([
                 h.pixelMustBecome(vremote1, h.red, {
                   threshold,
                   infoString: "pcRemote's remote1 should become red",
                 }),
                 h.pixelMustBecome(vremote2, h.blue, {
                   threshold,
                   infoString: "pcRemote's remote2 should become blue",
                 }),
               ]),
               Promise.all([
                 h.pixelMustBecome(vremote2, h.red, {
                   threshold,
                   infoString: "pcRemote's remote2 should become red",
                 }),
                 h.pixelMustBecome(vremote1, h.blue, {
                   threshold,
                   infoString: "pcRemote's remote1 should become blue",
                 }),
               ])
             ]);
    },
    function WAIT_FOR_REMOTE_BOTH_GREEN() {
      return Promise.all([
               h.pixelMustBecome(vremote1, h.green, {
                   threshold,
                   infoString: "pcRemote's remote1 should become green",
               }),
               h.pixelMustBecome(vremote2, h.green, {
                   threshold,
                   infoString: "pcRemote's remote2 should become green",
               }),
             ])
    },
  ]);
  await test.run();
});
</script>
</pre>
</body>
</html>