gpt4 book ai didi

javascript - WebRTC - 是否可以让浏览器代理视频从源到接收者?

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:23 25 4
gpt4 key购买 nike

例如:

  • 浏览器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>

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

关于javascript - WebRTC - 是否可以让浏览器代理视频从源到接收者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37018168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com