chromium/chrome/test/data/webrtc/webrtc_getdisplaymedia_test.html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
  <title>WebRTC Automated Test</title>
  <script type="text/javascript" src="test_functions.js"></script>
  <script type="text/javascript" src="video_detector.js"></script>
  <script>
  'use strict';
  let controller;
  let settings;
  let video_track;
  let audio_track;
  let stored_media_stream;
  let video;

  function handleSuccess(stream) {
    settings = stream.getVideoTracks()[0].getSettings();
    video = document.querySelector('video');
    video.srcObject = stream;
    video_track = stream.getVideoTracks()[0];
    if (stream.getAudioTracks().length > 0) {
      audio_track = stream.getAudioTracks()[0];
    }
    video.play();
    return logAndReturn("capture-success");
  }

  function compareErrors(expectedError) {
    return function(error) {
      console.log('getUserMedia error: ' + error.name, error);
      return logAndReturn(error.toString() == expectedError
          ? "expected-error"
          : "unexpected-error");
    }
  }

  function handleError(error) {
    console.log('getUserMedia error: ' + error.name, error);
    return logAndReturn("capture-failure");
  }

  function runGetDisplayMedia(constraints, frame, expectedError) {
    if (frame == "allowedFrame" || frame == "disallowedFrame") {
      return new Promise(resolve => {
        window.addEventListener('message', resolve, {once: true});
        document.getElementById(frame).contentWindow.postMessage(constraints);
      }).then((event) => event.data);
    }

    controller = new CaptureController;
    return navigator.mediaDevices.getDisplayMedia({...constraints, controller}).
      then(handleSuccess).catch(expectedError ? compareErrors(expectedError)
                                              : handleError);
  }

  function resizeVideoForHiDpiCapture(width, height) {
    video.style.width = width + 'px';
    video.style.height = height + 'px';
    return logAndReturn("success");
  }

  function hasAudioTrack() {
    return logAndReturn(`${!!audio_track}`);
  }

  function getVideoTrackType() {
    if (!video_track) {
      return logAndReturn("error-no-video-track");
    }
    return logAndReturn(video_track.constructor.name);
  }

  function getVideoCloneTrackType() {
    if (!video_track) {
      return logAndReturn("error-no-video-track-to-clone");
    }
    return logAndReturn(video_track.clone().constructor.name);
  }

  function getAudioTrackType() {
    if (!audio_track) {
      return logAndReturn("error-no-audio-track");
    }
    return logAndReturn(audio_track.constructor.name);
  }

  function getDisplaySurfaceSetting() {
    return logAndReturn(settings.displaySurface);
  }

  function getLogicalSurfaceSetting() {
    return logAndReturn(settings.logicalSurface.toString());
  }

  function getCursorSetting() {
    return logAndReturn(settings.cursor);
  }

  function getWidthSetting() {
    return logAndReturn(settings.width.toString());
  }

  function getFrameRateSetting() {
    return logAndReturn(settings.frameRate.toString());
  }

  function getDevicePixelRatio() {
    return logAndReturn(window.devicePixelRatio.toString());
  }

  function getVideoWidth() {
    return logAndReturn(video.videoWidth.toString());
  }

  function getVideoHeight() {
    return logAndReturn(video.videoHeight.toString());
  }

  function waitVideoMuted() {
    if (video_track.muted) {
      return logAndReturn("muted");
    }
    return new Promise(resolve => {
      video_track.onmute = function(evt) {
        return resolve(logAndReturn("muted"));
      };
    });
  }

  function waitVideoUnmuted() {
    if (!video_track.muted) {
      return logAndReturn("unmuted");
    }
    return new Promise(resolve => {
      video_track.onunmute = function(evt) {
        return resolve(logAndReturn("unmuted"));
      };
    });
  }

  function waitVideoEnded() {
    if (video_track.readyState == "ended") {
      return logAndReturn("ended");
    }
    return new Promise(resolve => {
      video_track.onended = function(evt) {
        return resolve(logAndReturn("ended"));
      };
    });
  }

  function stopAllTracks() {
    if (audio_track) {
      audio_track.stop();
    }
    if (video_track) {
      video_track.stop();
    }
    return logAndReturn("stopped");
  }

  async function sendWheel(action) {
    await controller.sendWheel(action);
    return logAndReturn("send-wheel-resolved");
  }

  async function setZoomLevel(zoomLevel) {
    try {
      await controller.setZoomLevel(zoomLevel);
    } catch (e) {
      return logAndReturn("set-zoom-level-error");
    }
    return logAndReturn("set-zoom-level-resolved");
  }

  function getZoomLevel() {
    return controller.getZoomLevel();
  }
</script>
</head>
<body>
  <table border="0">
    <tr>
      <td>getdisplayMedia() output</td>
    </tr>
    <tr>
      <td>
        <video id="local-view" autoplay></video>
      </td>
    </tr>
  </table>
  <br/>
    <iframe id="allowedFrame" src="webrtc_getdisplaymedia_embedded_document.html"></iframe>
    <iframe id="disallowedFrame" src="webrtc_getdisplaymedia_embedded_document.html" allow="display-capture 'none'"></iframe>
</body>
</html>