DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (0c3bc698f640)

VCS Links

Line Code
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
<!doctype html>
<html>
<head>
    <title>MediaRecorder events and exceptions</title>
    <link rel="help" href="https://w3c.github.io/mediacapture-record/MediaRecorder.html#mediarecorder">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
</head>
<body>
<canvas id="canvas" width="320" height="320"></canvas>
<script>

  // This test exercises the MediaRecorder API event sequence:
  //   onStart -> onPause -> onResume -> onDataAvailable -> onStop
  // verifying the |state| and a few exceptions that are supposed to be thrown
  // when doing the wrong thing.

  function createVideoStream() {
      let canvas = document.getElementById("canvas");
      canvas.getContext('2d').lineTo(10, 10);
      return canvas.captureStream();
  }

  function drawSomethingOnCanvas() {
      let canvas = document.getElementById("canvas");
      // Drawing something on the canvas generates a frame in its captured stream.
      let context = canvas.getContext("2d");
      context.fillStyle = "red";
      context.fillRect(0, 0, 10, 10);
  }

  async_test(test => {

    recorderOnUnexpectedEvent = test.step_func(() => {
      assert_unreached('Unexpected event.');
    });

    recorderOnDataAvailable = test.step_func(event => {
      assert_equals(recorder.state, "inactive");
      assert_not_equals(event.data.size, 0, 'We should get a Blob with data');
    });

    recorderOnStop = test.step_func(function() {
      assert_equals(recorder.state, "inactive");
      recorder.onstop = recorderOnUnexpectedEvent;
      recorder.stop();
      assert_equals(recorder.state, "inactive", "stop() is idempotent");
      assert_throws("InvalidStateError", function() { recorder.pause() },
                    "recorder cannot be pause()ed in |inactive| state");
      assert_throws("InvalidStateError", function() { recorder.resume() },
                    "recorder cannot be resume()d in |inactive| state");
      assert_throws("InvalidStateError", function() { recorder.requestData() },
                    "cannot requestData() if recorder is in |inactive| state");
      test.done();
    });

    recorderOnResume = test.step_func(function() {
      assert_equals(recorder.state, "recording");
      recorder.onresume = recorderOnUnexpectedEvent;
      recorder.onstop = recorderOnStop;
      recorder.stop();
    });

    recorderOnPause = test.step_func(function() {
      assert_equals(recorder.state, "paused");
      recorder.onpause = recorderOnUnexpectedEvent;
      recorder.onresume = recorderOnResume;
      recorder.resume();
    });

    recorderOnStart = test.step_func(function() {
      assert_equals(recorder.state, "recording");
      recorder.onstart = recorderOnUnexpectedEvent;
      recorder.onpause = recorderOnPause;
      recorder.pause();
    });

    let stream = createVideoStream();
    assert_equals(stream.getAudioTracks().length, 0);
    assert_equals(stream.getVideoTracks().length, 1);
    assert_equals(stream.getVideoTracks()[0].readyState, 'live');

    assert_throws("NotSupportedError",
                  function() {
                    recorder = new MediaRecorder(
                      new MediaStream(), {mimeType : "video/invalid"});
                  },
                  "recorder should throw() with unsupported mimeType");
    let recorder = new MediaRecorder(new MediaStream());
    assert_equals(recorder.state, "inactive");

    recorder.stop();
    assert_equals(recorder.state, "inactive", "stop() is idempotent");
    assert_throws("InvalidStateError", function(){recorder.pause()},
                  "recorder cannot be pause()ed in |inactive| state");
    assert_throws("InvalidStateError", function(){recorder.resume()},
                  "recorder cannot be resume()d in |inactive| state");
    assert_throws("InvalidStateError", function(){recorder.requestData()},
                  "cannot requestData() if recorder is in |inactive| state");

    assert_throws("NotSupportedError",
                  function() {
                    recorder.start();
                  },
                  "recorder should throw() when starting with inactive stream");

    recorder.stream.addTrack(stream.getTracks()[0]);

    drawSomethingOnCanvas();

    recorder.onstop = recorderOnUnexpectedEvent;
    recorder.onpause = recorderOnUnexpectedEvent;
    recorder.onresume = recorderOnUnexpectedEvent;
    recorder.onerror = recorderOnUnexpectedEvent;
    recorder.ondataavailable = recorderOnDataAvailable;
    recorder.onstart = recorderOnStart;

    recorder.start();
    assert_equals(recorder.state, "recording");
  });

</script>