admin管理员组

文章数量:1289581

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;
  }
}

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;
  }
}
Share Improve this question edited Aug 23, 2016 at 3:03 Chiu asked Aug 23, 2016 at 2:45 ChiuChiu 3504 silver badges14 bronze badges 5
  • 1 it's setting a globally accessible variable called stream - you could call it fred - it doesn't matter - it's just for global access – Jaromanda X Commented Aug 23, 2016 at 2:50
  • Thanks!! How about the second question? Can you enlighten me for this one please? – Chiu Commented Aug 23, 2016 at 2:53
  • If I had answers I'd post answers - hence why I only posted a ment, as I only knew the simple half of your two questions :p – Jaromanda X Commented Aug 23, 2016 at 2:54
  • 1 The second question is for cross-browser support: Chrome uses the createObjectURL() function and Firefox can use just the steam by itself. Source: "Chrome uses the createObjectURL method whereas Firefox and Opera send the stream directly" – Samuel Méndez Commented Aug 23, 2016 at 6:06
  • 1 @SamuelMéndez Unfortunately, the code is wrong. Luckily the else clause never executes in practice. See my answer. – jib Commented Aug 25, 2016 at 4:21
Add a ment  | 

1 Answer 1

Reset to default 11

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 remend looking at the official WebRTC samples instead.

本文标签: