WebRTC-是否可以将浏览器代理视频从源发送到接收器

WebRTC - is it possible to have a browser proxy video from a source to a receiver?

本文关键字:接收器 视频 代理 是否 浏览器 WebRTC-      更新时间:2023-09-26

例如:

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

在浏览器B充当A和C之间的中介的情况下,有可能这样做吗?

第二,B是否可以同时观看A的广播视频并将其转发给C?

当然可以。(对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>";
<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>

您可以创建任意数量的啤酒花。