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
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240

content / test / data / gpu / vc / videos_mxn.html [blame]

<!--
Copyright 2021 The Chromium Authors
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<html>

<head>
  <title>MxN Video playbacks</title>
  <script>
    const _defaultRows = 7;
    const _defaultColumns = 7;
    const _totalVideoWidth = 1600;
    const _totalVideoHeight = 900;
    let codec = 'vp9';

    const parsedString = (function (names) {
      const pairs = {};
      for (let i = 0; i < names.length; ++i) {
        const keyValue = names[i].split('=', 2);
        if (keyValue.length === 1) {
          pairs[keyValue[0]] = '';
        } else {
          pairs[keyValue[0]] = decodeURIComponent(keyValue[1].replace(/\+/g, ' '));
        }
      }
      return pairs;
    })(window.location.search.substr(1).split('&'));

    function GetVideoSource(videoCount, index, codec) {
      switch (codec) {
        case 'vp8':
          if (videoCount <= 4) {
            return './teddy1_vp8_640x360_30fps.webm';
          } else {
            if (index < 4) {
              return './teddy3_vp8_320x180_30fps.webm';
            } else if (index < 16) {
              return './teddy2_vp8_320x180_15fps.webm';
            } else {
              return './teddy1_vp8_320x180_7fps.webm';
            }
          }
          break;

        case 'vp9':
        default:
          if (videoCount <= 4) {
            return './teddy1_vp9_640x360_30fps.webm';
          } else {
            if (index < 4) {
              return './teddy3_vp9_320x180_30fps.webm';
            } else if (index < 16) {
              return './teddy2_vp9_320x180_15fps.webm';
            } else {
              return './teddy1_vp9_320x180_7fps.webm';
            }
          }
          break;
      }
    }

    function startMxNVideos() {
      const container = document.getElementById('container');

      // Get the video row count and the column count from the string.
      // Example: videos_mxn.html?rows=9&columns=9
      let videoRows = parsedString['rows'];
      let videoColumns = parsedString['columns'];
      if (videoRows === undefined) {
        videoRows = _defaultRows;
      }
      if (videoColumns === undefined) {
        videoColumns = _defaultColumns;
      }

      // Get the video source option from the string.
      codecString = parsedString['codec'];
      if (codecString === 'vp8') {
        codec = 'vp8';
      } else if (codecString !== 'vp9' && codecString !== undefined) {
        console.warn('Unsupported video codec format! Switch to default VP9.');
      }

      // Limite the number of videos to 20x20.
      // These videos will not load when the number is too big.
      const maxColRow = Math.max(videoRows, videoColumns);
      if (maxColRow > 20) {
        const p = container.appendChild(document.createElement('p'));
        p.style.position = "absolute";
        p.style.top = 0;
        p.style.left = 0;
        p.style.width = _totalVideoWidth;
        p.style.height = _totalVideoHeight;
        p.innerHTML = "Cannot support videos more than 20 x 20!" + "<br />" +
          "Please change the number of rows/columns.";
        return;
      }

      // Calculate the video onscreen size
      const videoWidth = _totalVideoWidth / maxColRow;
      const videoHeight = _totalVideoHeight / maxColRow;
      const iconHeight = videoHeight / 8;

      // Create MxN videos.
      let animatedBar;
      let iconBottom;
      const videoCount = videoRows * videoColumns;
      for (let row = 0; row < videoRows; row++) {
        for (let column = 0; column < videoColumns; column++) {
          // Onscreen position.
          const videoTop = row * videoHeight;
          const videoLeft = column * videoWidth;

          // Video source.
          const i = row * videoColumns + column;
          const videoSrc = GetVideoSource(videoCount, i, codec);

          createOneVideo(videoTop, videoLeft, videoWidth, videoHeight,
            videoSrc, /*hasBorder=*/ i === 0);
          // Create an icon on top of each video.
          createOneIcon(videoTop, videoLeft, videoWidth, videoHeight, iconHeight);

          // For the voice animation on the last video.
          if (i === 0) {
            animatedBar = document.createElement('icon');
            iconBottom = videoTop + iconHeight * 2;
            createVoiceAnimationBar(animatedBar, videoTop, videoLeft,
              videoWidth, videoHeight, iconHeight);
          }
        }
      }

      // Create one small video at the upper right corner to similate the one
      // from the local camera (640x360).
      createLocalVideoAndIcon(videoWidth, videoHeight, codec);

      // Start the voice icon animation.
      const frameTime30Fps = 32;  // ms
      let lastTimestamp = performance.now();
      let barHeight = 0;

      // Voice bar animation at 30 FPS
      function voiceBarAnimation(timestamp) {
        const elapsed = timestamp - lastTimestamp;
        if (elapsed < frameTime30Fps) {
          window.requestAnimationFrame(voiceBarAnimation);
          return;
        }
        lastTimestamp = timestamp;

        const maxBarHeight = iconHeight - 4;
        barHeight = barHeight + (maxBarHeight / 10);
        if (barHeight > maxBarHeight)
          barHeight = maxBarHeight / 10;

        animatedBar.style.top = iconBottom - barHeight;
        animatedBar.style.height = barHeight;

        window.requestAnimationFrame(voiceBarAnimation);
      }

      window.requestAnimationFrame(voiceBarAnimation);
    }

    function createOneVideo(top, left, width, height, videoSrc, hasBorder) {
      const video = document.createElement('video');
      video.loop = true;
      video.autoplay = true;
      video.muted = true;
      video.src = videoSrc;
      video.width = width;
      video.height = height;
      video.style.position = "absolute";
      video.style.top = top;
      video.style.left = left;
      if (hasBorder) {
        const borderWidth = 3;
        video.style.borderWidth = borderWidth;
        video.width = width - borderWidth * 2;
        video.height = height - borderWidth * 2;
        video.style.borderStyle = "solid";
        video.style.borderColor = "rgba(90, 129, 193, 255)";
      }
      video.play();

      container.appendChild(video);
    }

    function createOneIcon(videoTop, videoLeft, videoWidth, videoHeight, iconHeight) {
      const icon = document.createElement('icon');

      icon.style.backgroundColor = "rgba(29, 110, 216, 255)";
      icon.style.position = "absolute";
      icon.style.width = iconHeight;
      icon.style.height = iconHeight;
      icon.style.top = videoTop + iconHeight;
      icon.style.left = videoLeft + videoWidth - iconHeight * 2;

      container.appendChild(icon);
    }

    function createLocalVideoAndIcon(videoWidth, videoHeight, codec) {
      const smallVideoWidth = videoWidth / 3;
      const smallVideoHeight = videoHeight / 3;
      const iconHeight = smallVideoHeight / 8;
      const videoSrc = GetVideoSource(1, 1, codec);

      createOneVideo(videoHeight - smallVideoHeight,
        _totalVideoWidth - smallVideoWidth, smallVideoWidth,
        smallVideoHeight, videoSrc, false);

      createOneIcon(videoHeight - smallVideoHeight, _totalVideoWidth -
        smallVideoWidth, smallVideoWidth, smallVideoHeight, iconHeight);
    }


    function createVoiceAnimationBar(bar, videoTop, videoLeft, videoWidth,
      videoHeight, iconHeight) {
      bar.style.backgroundColor = "white";
      bar.style.position = "absolute";
      bar.style.width = 5;
      bar.style.height = iconHeight - 4;
      bar.style.top = videoTop + iconHeight + 4;
      bar.style.left = videoLeft + videoWidth - iconHeight * 1.5 - 2;

      container.appendChild(bar);
    }


  </script>
</head>

<body>
  <div id="container" style="position:absolute; top:0px; left:0px">
  </div>
  <script>startMxNVideos();</script>
</body>

</html>