chromium/content/test/data/media/peerconnection-resolution-switch.html

<html>
<body id="body">
    <h1>WebRTC Resolution Switch Test</h1>
    <p>Status: <span id="status">not-started</span></p>
</body>
<script type="text/javascript" src="webrtc_test_utilities.js"></script>
<script type="text/javascript" src="peerconnection-multiple-streams.js"></script>
<script type="text/javascript">
const $ = document.getElementById.bind(document);

// Available resolutions to switch between. These are 4:3 resolutions chosen
// since they have significant distance between them and are quite common. E.g.
// they can be selected for youtube videos. We also avoid higher resolutions
// since they consume a lot of resources.
const RESOLUTIONS = [
  {w:320, h:240},
  {w:480, h:360},
  {w:640, h:480},
  {w:1280, h:720},
];

class TestRunner {
  constructor(runtimeSeconds, switchResolutionDelayMillis) {
    this.runtimeSeconds = runtimeSeconds;
    this.switchResolutionDelayMillis = switchResolutionDelayMillis;
    this.videoElements = [];
    this.peerConnections = [];
    this.numConnections = 0;
    this.iteration = 0;
    this.startTime = 0;  // Initialized to dummy value.
    this.status = this.getStatusInternal_();
  }

  addPeerConnection() {
    const videoElement = document.createElement('video');
    videoElement.autoplay = true;
    $('body').appendChild(videoElement);
    this.videoElements.push(videoElement);
    this.peerConnections.push(new PeerConnection(videoElement, RESOLUTIONS));
  }

  runTest() {
    const promises = this.peerConnections.map((conn) => conn.start());
    return Promise.all(promises)
        .then(() => {
          this.startTime = Date.now();
          return this.switchResolutionLoop();
        })
        .then(logSuccess);
  }

  switchResolutionLoop() {
    this.iteration++;
    this.status = this.getStatusInternal_();
    $('status').textContent = this.status;
    if (this.status === 'ok-done') {
      return;
    }
    return Promise.all(this.peerConnections.map((pc) => pc.switchToRandomStream()))
        .then(
            () => setTimeout(
                () => this.switchResolutionLoop(),
                this.switchResolutionDelayMillis));
  }

  getStatus() {
    return this.status;
  }

  getStatusInternal_() {
    if (this.iteration == 0) {
      return 'not-started';
    }
    this.peerConnections.forEach((conn) => conn.verifyState());
    const timeSpent = Date.now() - this.startTime;
    if (timeSpent >= this.runtimeSeconds * 1000) {
      return 'ok-done';
    }
    return `running, iteration: ${this.iteration}`;
  }
}

function startTest(
    runtimeSeconds, numPeerConnections, switchResolutionDelayMillis) {
  testRunner = new TestRunner(runtimeSeconds, switchResolutionDelayMillis);
  for (let i = 0; i < numPeerConnections; i++) {
    testRunner.addPeerConnection();
  }
  return testRunner.runTest();
}

function getStatus() {
  return testRunner ? testRunner.getStatus() : 'not-initialized';
}

</script>
</html>