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

content / test / data / media / peerconnection-pause-play.html [blame]

<html>
<body id="body">
    <h1>WebRTC Pause Play 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);

class TestRunner {
  constructor(runtimeSeconds, pausePlayIterationDelayMillis) {
    this.runtimeSeconds = runtimeSeconds;
    this.pausePlayIterationDelayMillis = pausePlayIterationDelayMillis;
    this.elements = [];
    this.peerConnections = [];
    this.iteration = 0;
    this.startTime;
  }

  addPeerConnection(elementType) {
    const element = document.createElement(elementType);
    element.autoplay = false;
    $('body').appendChild(element);
    let resolution;
    if (elementType === 'video') {
      resolution = {w: 300, h: 225};
    } else if (elementType === 'audio') {
      resolution = {w: -1, h: -1};  // -1 is interpreted as disabled
    } else {
      throw new Error('elementType must be one of "audio" or "video"');
    }
    this.elements.push(element);
    this.peerConnections.push(new PeerConnection(element, [resolution]));
  }

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

  pauseAndPlayLoop() {
    this.iteration++;
    this.elements.forEach((feed) => {
      if (Math.random() >= 0.5) {
        feed.play();
      } else {
        feed.pause();
      }
    });
    const status = this.getStatus();
    $('status').textContent = status
    if (status != 'ok-done') {
      return new Promise(resolve => setTimeout(resolve, this.pausePlayIterationDelayMillis))
        .then(() => this.pauseAndPlayLoop());
    } else {  // We're done. Pause all feeds.
      this.elements.forEach((feed) => {
        feed.pause();
      });
    }
  }

  getStatus() {
    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';
    } else {
      return `running, iteration: ${this.iteration}`;
    }
  }
}

let testRunner;

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

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

</script>
</html>