summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/extensions/webgl-webcodecs-video-frame.html
blob: f0e413e2f6f474ccb6bea2e03b885130e0dff57b (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!--
Copyright (c) 2021 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="../../resources/js-test-style.css" />
  <script src="../../js/js-test-pre.js"></script>
  <script src="../../js/webgl-test-utils.js"></script>
  <script src="../../js/tests/out-of-bounds-test.js"></script>
  <script src="../../../../extensions/proposals/WEBGL_webcodecs_video_frame/webgl_webcodecs_video_frame.js"></script>
  <style>
    canvas {
      padding: 10px;
      background: gold;
    }

    button {
      background-color: #555555;
      border: none;
      color: white;
      padding: 15px 32px;
      width: 150px;
      text-align: center;
      display: block;
      font-size: 16px;
    }
  </style>
</head>

<body>
  <canvas id="src" width="640" height="480"></canvas>
  <canvas id="dst" width="640" height="480"></canvas>
  <p id="info"></p>
  <div id="description"></div>
  <div id="console"></div>
  <script>
    "use strict";
    description("Test of importing Videoframe from Webcodecs to Webgl");

    const kIsRunningTest = true;
    const kMaxFrame = 10;
    const kTestPixel = [255, 128, 0, 255];
    // Sum of pixel difference of R/G/B channel. Use to decide whether a
    // pixel is matched with another.
    const codec_string = "vp09.00.51.08.00";

    let wtu = WebGLTestUtils;
    let cnv = document.getElementById("src");
    let src_width = cnv.width;
    let src_height = cnv.height;
    let src_color = "rgba(" + kTestPixel[0].toString() + "," + kTestPixel[1].toString() + ","
      + kTestPixel[2].toString() + "," + kTestPixel[3].toString() + ")";
    let frame_counter = 0;
    let pixelCompareTolerance = 5;

    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
      }
      return false;
    }

    let th = parseInt(getQueryVariable('threshold'));
    if (!isNaN(th))
      pixelCompareTolerance = th;

    async function startDrawing() {
      let cnv = document.getElementById("src");
      var ctx = cnv.getContext('2d', { alpha: false });

      ctx.fillStyle = src_color;
      let drawOneFrame = function (time) {
        ctx.fillStyle = src_color;
        ctx.fillRect(0, 0, src_width, src_height);
        window.requestAnimationFrame(drawOneFrame);
      }
      window.requestAnimationFrame(drawOneFrame);
    }

    function captureAndEncode(processChunk) {
      let cnv = document.getElementById("src");
      let fps = 60;
      let pending_outputs = 0;
      let stream = cnv.captureStream(fps);
      let processor = new MediaStreamTrackProcessor(stream.getVideoTracks()[0]);

      const init = {
        output: (chunk) => {
          testPassed("Encode frame successfully.");
          pending_outputs--;
          processChunk(chunk);
        },
        error: (e) => {
          testFailed("Failed to encode frame.");
          finishTest();
          vtr.stop();
        }
      };

      const config = {
        codec: codec_string,
        width: cnv.width,
        height: cnv.height,
        bitrate: 10e6,
        framerate: fps,
      };

      let encoder = new VideoEncoder(init);
      encoder.configure(config);

      const frame_reader = processor.readable.getReader();
      frame_reader.read().then(function processFrame({done, value}) {
        if (done)
          return;

        if (pending_outputs > 30) {
          console.log("drop this frame");
          // Too many frames in flight, encoder is overwhelmed
          // let's drop this frame.
          value.close();
          frame_reader.read().then(processFrame);
          return;
        }

        if(frame_counter == kMaxFrame) {
          frame_reader.releaseLock();
          processor.readable.cancel();
          value.close();
          return;
        }

        frame_counter++;
        pending_outputs++;
        const insert_keyframe = (frame_counter % 150) == 0;
        encoder.encode(value, { keyFrame: insert_keyframe });

        frame_reader.read().then(processFrame);
      });
    }

    function startDecodingAndRendering(cnv, handleFrame) {
      const init = {
        output: handleFrame,
        error: (e) => {
          testFailed("Failed to decode frame.");
          finishTest();
        }
      };

      const config = {
        codec: codec_string,
        codedWidth: cnv.width,
        codedHeight: cnv.height,
        acceleration: "deny",

      };

      let decoder = new VideoDecoder(init);
      decoder.configure(config);
      return decoder;
    }

    function isFramePixelMatched(gl, th_per_pixel = pixelCompareTolerance) {
      WebGLTestUtils.checkCanvasRect(gl, 0, 0, src_width, src_width, kTestPixel, "should be orange", pixelCompareTolerance)
    }

    function main() {
      if (!("VideoEncoder" in window)) {
        testPassed("WebCodecs API is not supported.");
        finishTest();
        return;
      }
      let cnv = document.getElementById("dst");

      let webgl_webcodecs_test_context = {
        maxFrameTested: kMaxFrame,
        displayed_frame: 0,
        isFramePixelMatched: isFramePixelMatched,
        testFailed: testFailed,
        testPassed: testPassed,
        finishTest: finishTest
      };
      setTestMode(webgl_webcodecs_test_context);
      let handleFrame = requestWebGLVideoFrameHandler(cnv);
      if (handleFrame === null) {
        finishTest();
        return;
      }

      startDrawing();
      let decoder = startDecodingAndRendering(cnv, handleFrame);
      captureAndEncode((chunk) => {
        decoder.decode(chunk);
      });
    }

    document.body.onload = main;
  </script>

</body>

</html>