<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Stream API</title>
  <script>
    async function initiate() {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({video: true});
        const button = document.getElementById("button");
        button.addEventListener("click", function() { stopstream(stream); });
        const video = document.getElementById("video");
        video.srcObject = stream;
        video.play();
      } catch (error) {
        console.log("Error: " + error.name);
      }
    }
    function stopstream(stream) {
      const videoTracks = stream.getVideoTracks();
      const track = videoTracks[0];
      track.stop();
      alert("Stream Canceled");
    }
    window.addEventListener("DOMContentLoaded", initiate);
  </script>
</head>
<body>
  <section>
    <video id="video"></video>
  </section>
  <nav>
    <button type="button" id="button">Turn Off</button>
  </nav>
</body>
</html>