1

I have a MediaStream object that points to a live stream. It is stored in a variable called vs. This is my console:

> vs
< MediaStream {id: "MEDIASTREAMID", active: true, ...}

> JSON.stringify({Livestream:vs})
< "{"Livestream":{}}"

How come a MediaStream object is {} in JSON?

Edit:

Here is an example:

var canvas = document.querySelector("canvas");
var context = canvas.getContext("2d");
const video = document.querySelector('#myVidPlayer');
var w, h;
canvas.style.display = "none";
function snapshot(){
    context.fillRect(0, 0, w, h);
    context.drawImage(video, 0, 0, w, h);
    canvas.style.display = "block";
}
var vs;
window.navigator.mediaDevices.getUserMedia({ video: true, audio: true })
    .then(function(stream){
        video.srcObject = stream;
        vs=stream;
        console.log(JSON.stringify({Livestream:stream}));
        video.onloadedmetadata = (e) => {
            video.play();
            w = video.videoWidth;
            h = video.videoHeight
            canvas.width = w;
            canvas.height = h;
        };
    }).catch(function(e){
        alert('You have to enable the microphone and the camera');
    });
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>Livestream</title>
    </head>
    <body style="margin:0">
        <video id="myVidPlayer" style="width:100%;height:100%" muted autoplay></video>
        <button onclick="snapshot()" >Snapshot</button>
        <div class="mycanvas">
            <h2>Captured snapshot</h2>
            <p>Right click and save the image</p>
            <canvas></canvas>
        </div>
    </body>
</html>
Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34

0 Answers0