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

content / test / data / gpu / pixel_media_recorder_from_canvas_2d.html [blame]

<!DOCTYPE html>
<meta name="viewport" content="initial-scale=1">
<script src="pixel_video_from_canvas.js"></script>
<script>

async function main() {
  const canvas = document.getElementById('cnv');
  const video = document.getElementById('result');
  const ctx = canvas.getContext('2d');
  fourColorsFrame(ctx);
  video.width = canvas.width;
  video.height = canvas.height;
  let capturing = false;

  const stream = canvas.captureStream(60);
  const recorder = new MediaRecorder(stream);

  let time_base = 0;
  let data_chunks = [];
  const video_duration_ms = 300;

  const test_video_output = (e) => {
    if (checkFourColorsFrame(video)) {
      logOutput('Test completed');
      sendResult('SUCCESS');
    } else {
      logOutput('Test failed');
      sendResult('FAILED');
    }
  }

  recorder.error = (e) => {
    capturing = false;
    logOutput('Test failed with an error');
    logOutput(e);
    sendResult('FAILED');
  }

  recorder.ondataavailable = (e) => {
    data_chunks.push(e.data);
    if (time_base == 0) {
      time_base = e.timecode;
    } else if ((e.timecode - time_base > video_duration_ms) && capturing) {
      capturing = false;
      recorder.stop();
    }
  }

  recorder.onstop = (e) => {
    const blob = new Blob(data_chunks, { 'type': 'video/webm' });
    const videoURL = URL.createObjectURL(blob);
    video.src = videoURL;
    video.onended = test_video_output;
    video.play();
  }

  capturing = true;
  recorder.start(video_duration_ms);
  while(capturing) {
    await waitForNextFrame();
    fourColorsFrame(ctx);
  }
}
</script>
<body onload="main()">
<canvas id='cnv' width='128' height='128'></canvas>
<video id='result' muted></video>
</body>