WebRTC - 是否可以拥有从源到接收器的浏览器代理视频?

时间:2016-05-04 03:59:29

标签: javascript html5 webrtc

例如:

  • 浏览器A正在播放视频
  • 浏览器B是个中间人
  • 浏览器C正在接收视频

是否有可能做到这样的事情,其中​​浏览器B充当A和C之间的中介?

其次,B是否可以从A 中查看广播的视频,将其转发到C?

1 个答案:

答案 0 :(得分:3)

当然这很好用。 (对Chrome使用https fiddle):



function Hop() {
  this.pc1 = new RTCPeerConnection();
  this.pc2 = new RTCPeerConnection();

  var add = (pc, can) => can && pc.addIceCandidate(can).catch(log);
  this.pc1.onicecandidate = e => add(this.pc2, e.candidate);
  this.pc2.onicecandidate = e => add(this.pc1, e.candidate);
  this.pc2.oniceconnectionstatechange = e => log(this.pc2.iceConnectionState);
};
Hop.prototype.send = function(stream) {
  this.pc1.addStream(stream);
  return Promise.all([
    new Promise(resolve => this.pc2.onaddstream = resolve),
    this.pc1.createOffer()
      .then(offer => this.pc1.setLocalDescription(offer))
      .then(() => this.pc2.setRemoteDescription(this.pc1.localDescription))
      .then(() => this.pc2.createAnswer())
      .then(answer => this.pc2.setLocalDescription(answer))
      .then(() => this.pc1.setRemoteDescription(this.pc2.localDescription))
  ])
  .then(results => results[0].stream);
};

var AtoB = new Hop(), BtoC = new Hop();

navigator.mediaDevices.getUserMedia({ video: true })
  .then(stream => AtoB.send(v1.srcObject = stream))
  .then(stream => BtoC.send(v2.srcObject = stream))
  .then(stream => v3.srcObject = stream)
  .catch(e => log(e));

var log = msg => div.innerHTML += msg + "<br>";
&#13;
<video id="v1" height="120" width="160" autoplay muted></video>
<video id="v2" height="120" width="160" autoplay></video>
<video id="v3" height="120" width="160" autoplay></video><br>
<div id="div"></div>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
&#13;
&#13;
&#13;

您可以根据需要创建任意数量的跃点。