summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/performance/webgl_webcodecs_video_frame/index.html
blob: 97eb95a174830c5cfd2219b3af033fb3feb91ebf (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
<!-- This was created from the demo of webcodecs-blogpost-demo.glitch.me, and added the webgl support for test only purpose. -->

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta http-equiv="origin-trial"
    content="AoSY6Q4OOuuZVXTqOwJlfk4n77EL0esumtLRHj+9V97JoFfLq4AKsWlza8A8HmxTx1PU7SSzpjWK6F62bwWLPQYAAAB3eyJvcmlnaW4iOiJodHRwczovL3dlYmNvZGVjcy1ibG9ncG9zdC1kZW1vLmdsaXRjaC5tZTo0NDMiLCJmZWF0dXJlIjoiV2ViQ29kZWNzIiwiZXhwaXJ5IjoxNjA1NDc0OTQ4LCJpc1N1YmRvbWFpbiI6dHJ1ZX0=">
  <title>WebCodecs API demo: Encoding and Decoding</title>
  <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>
  <script src="../../../../extensions/proposals/WEBGL_webcodecs_video_frame/webgl_webcodecs_video_frame.js"></script>
</head>

<body>
  <canvas id="src" width="640" height="480"></canvas>
  <button onclick="playPause()">Pause</button>
  <canvas id="dst" width="640" height="480"></canvas>
  <script>
    let codec_string = "vp8";
    let keep_going = true;

    function playPause() {
      keep_going = !keep_going;
      let btn = document.querySelector("button");
      if (keep_going) {
        btn.innerText = "Pause";
      } else {
        btn.innerText = "Play";
      }
    }

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

      ctx.fillStyle = "white";
      let width = cnv.width;
      let height = cnv.height;
      let cx = width / 2;
      let cy = height / 2;
      let r = Math.min(width, height) / 5;
      let drawOneFrame = function (time) {
        let angle = Math.PI * 2 * (time / 5000);
        let scale = 1 + 0.3 * Math.sin(Math.PI * 2 * (time / 7000));
        ctx.save();
        ctx.fillRect(0, 0, width, height);

        ctx.translate(cx, cy);
        ctx.rotate(angle);
        ctx.scale(scale, scale);

        ctx.font = '30px Verdana';
        ctx.fillStyle = 'black';
        const text = "😊📹📷Hello WEBGL_webcodecs_video_frame🎥🎞️😊";
        const size = ctx.measureText(text).width;
        ctx.fillText(text, -size / 2, 0);
        ctx.restore();
        window.requestAnimationFrame(drawOneFrame);
      }
      window.requestAnimationFrame(drawOneFrame);
    }

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

      const init = {
        output: (chunk) => {
          pending_outputs--;
          processChunk(chunk);
        },
        error: (e) => {
          console.log(e.message);
          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) {
          // Too many frames in flight, encoder is overwhelmed
          // let's drop this frame.
          value.close();
          frame_reader.read().then(processFrame);
          return;
        }

        if(!keep_going) {
          value.close();
          frame_reader.read().then(processFrame);
          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) => {
          console.log(e.message);
        }
      };

      const config = {
        codec: codec_string,
        codedWidth: cnv.width,
        codedHeight: cnv.height
      };

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


    function main() {
      if (!("VideoEncoder" in window)) {
        document.body.innerHTML = "<h1>WebCodecs API is not supported.</h1>";
        return;
      }

      let cnv = document.getElementById("dst");
      let handleFrame = requestWebGLVideoFrameHandler(cnv);
      if (handleFrame === null) {
        document.body.innerHTML =
          "<h1>Unable to request WebGL to render video frames.</h1>";
        return;
      }

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

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

</body>

</html>