Simulcast playground (Firefox supported)

by Andreas Pehrson

HTML

<!--
BSD 3-Clause License

Copyright (c) 2024, WebRTC

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<table>
  <tr>
    <td>
      <div class="VideoPlayer">
        <video id="videoSource" autoplay muted></video>
        <div class="VideoPlayerControls">
          <button onclick="javascript:videoFullscreen(this)" title="Fullscreen">⛶</button>
        </div>
      </div>
    </td>
    <td>
      <div class="VideoPlayer" style="display: none">
        <video id="videoMerged" autoplay></video>
        <div class="VideoPlayerControls">
         ...

CSS

body {
  font-family: monospace;
  line-height: 2em;
}

.encodingBox {
  display: inline-block;
  border: 1px solid black;
  margin: 5px 5px 5px 5px;
  padding: 5px 5px 5px 5px;
  max-width: 90%;
}

video {
  max-width: 600px;
}

button, input, select {
  font-family: inherit;
}

summary {
  font-weight: bold;
}

.VideoPlayer {
  position: relative;
}

.VideoPlayer button {
  color: darkgray;
  border-width: medium;
  border-color: white;
  border-style: hidden;
  background-color: transparent;
  text-shadow:
    1px 1px 1px #000,
    -1px 1px 1px #000,
    -1px -1px 0 #000,
    1px -1px 0 #000;
  font-size: 1.8em;
  line-height: 1.5em;
  vertical-align: middle;
  filter: drop-shadow(2px 4px 6px black)
}


.VideoPlayer button:hover {
  color: white;
}

.RecordButton {
  font-size: 1em !important;
}
.RecordButton:hover {
  filter: brightness(1.2) drop-shadow(2px 4px 6px black);
}

.VideoPlayerControls {
  position:absolute;
  z-index: 1;
  bottom: 0px;
  right: 0px;
  margin-right:5px;
}

#recordingList {
  margin-left: 10px;
}

.blink {
  animation: blink 1s linear infinite;
}

@keyframes blink {
  25% {
    opacity: 0.5;
  }
  50% {
    opacity: 0;
  }
  75% {
    opacity: 0.5;
  }
}

JavaScript

const constraints = {
  WHQVGA: {
    video: {
      width: 240,
      height: 135,
    },
  },
  WQVGA: {
    video: {
      width: 320,
      height: 180,
    },
  },
  QVGA: {
    video: {
      width: 320,
      height: 240,
    },
  },
  WHVGA: {
    video: {
      width: 480,
      height: 270,
    },
  },
  HVGA: {
    video: {
      width: 480,
      height: 360,
    },
  },
  WVGA: {
    video: {
      width: 640,
      height: 360,
    },
  },
  VGA: {
    video: {
      width: 640,
      height: 480,
    },
  },
  HD: {
    video: {
      width: 1280,
      height: 720,
    },
  },
  FullHD: {
    video: {
      width: 1920,
      height: 1080,
    },
  },
  "4K": {
    video: {
      width: 4096,
      height: 2160,
    },
  },
  "8K": {
    video: {
      width: 7680,
      height: 4320,
    },
  },
  screen: {
    video: true,
  },
  "screen-FullHD": {
    video: { width: { max: 1920 } },
  },
  "screen-HD": {
    video: { width: { max: 1280 } },
  },
  "screen-conference": {
    video: true,
  },
  "screen-conference-FullHD": {
    video: { width: { max: 1920 } },
  },
  "screen-conference-HD": {
    video: { width: { max: 1280 } },
  },
}

let pc1
let pc2
let stream
let transceiver
let sender
let asender
let lastBitrateMesurementTime
let lastBytesSent
let lastBytesReceived
let streamResolution
let bookmarkDevice
let prevReport = null
const intervals = []
let videoSourceInterval = null
const recorders = {}
const recordings = []

const stopPeerConnection = () => {
  if (pc1) {
    pc1.close()
  }
  if (pc2) {
    videoMerged.srcObject = null
    pc2.close()
  }

  const layers = document.querySelectorAll("div[data-layer]")
  for (const layer of layers) {
    layer.querySelector("#videoPreview").style.display = "none"
  }

  videoMerged.closest(".VideoPlayer").style.display =
    splitLayer.value == "merged" ? "inline" : "none"

 ...