gpt4 book ai didi

javascript - 为什么 WebRTC 本地流没有显示?

转载 作者:行者123 更新时间:2023-11-30 21:10:21 25 4
gpt4 key购买 nike

我想做的是让 p1 连接到 p2,p2 获取网络摄像头并将其流式传输到 p2。在同一页面上练习 webrtc。

但是 onaddstream 我确实得到了一个流,它有正确的 ID 和错误,但是当我将它分配给视频元素时没有任何反应。

但是,我确实从请求流的 p2 获得了有效流。如果我将视频设置为等于此流,那么它会显示网络摄像头视频。

这是代码

v = $0

pc1 = new RTCPeerConnection();
pc2 = new RTCPeerConnection();

pc1.onaddstream = (s) => {
v.src = URL.createObjectURL(s.stream);
window.s1 = s.stream;
};

pc1.createOffer({offerToReceiveVideo: 1})
.then((offer) => {
pc1.setLocalDescription(offer);
pc2.setRemoteDescription(offer)
})
.then(() => navigator.mediaDevices.getUserMedia({ video: true }))
.then((stream) => {
pc2.addStream(stream);
window.s2 = stream;
})
.then(() => pc2.createAnswer())
.then((answer) => {
pc2.setLocalDescription(answer);
pc1.setRemoteDescription(answer);
})
.catch((err)=>console.log(err));

最佳答案

您不是在向 ICE 候选人发出信号。添加:

pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate);

...它会起作用。

小提示:查看友好的网络控制台弃用警告:

⚠ onaddstream is deprecated! Use peerConnection.ontrack instead.

⚠ URL.createObjectURL(MediaStream) is deprecated and will be removed soon.

例如喜欢this .规范即将完成 evolving .

关于javascript - 为什么 WebRTC 本地流没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46214759/

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