Chiu Chiu - 3 months ago 16
Javascript Question

If we need to create a URL for stream, why can we set video.src to stream for WebRTC?

In a Google's WebRTC tutorial, it has the following example code.

I have two questions for it:


  1. Why do we need to set window.stream to stream? (What does
    "stream available to console" mean?)

  2. If we need to create a URL for stream, why can we set video.src
    to stream which should be a blob?



Thanks.

function successCallback(stream) {
window.stream = stream; // stream available to console
if (window.URL) {
video.src = window.URL.createObjectURL(stream);
} else {
video.src = stream;
}
}

jib jib
Answer

Old and wrong code. video.src = stream is wrong. It should be video.srcObject = stream. It just never runs because all browsers today support URL.

Instead, use srcObject whenever available (supported in both Chrome and Firefox) for better life time handling by browsers:

if (typeof video.srcObject == "object") {
  video.srcObject = stream;
} else {
  video.src = URL.createObjectURL(stream);
}

Or use adapter.js and don't worry about it:

video.srcObject = stream;

Oh, and window.stream is just some global variable. Likely used for debugging by the sample author.

2013 is old for WebRTC code. I recommend looking at the official WebRTC samples instead.