gpt4 book ai didi

webrtc - 为什么WebRTC远程视频源是URL.createObjectURL生成的

转载 作者:行者123 更新时间:2023-12-02 04:27:42 25 4
gpt4 key购买 nike

this document ,它使用 URL.createObjectURL设置视频源。 (这是接听电话的代码)。

var offer = getOfferFromFriend();
navigator.getUserMedia({video: true}, function(stream) {
pc.onaddstream = e => video.src = URL.createObjectURL(e.stream);
pc.addStream(stream);

pc.setRemoteDescription(new RTCSessionDescription(offer), function() {
pc.createAnswer(function(answer) {
pc.setLocalDescription(answer, function() {
// send the answer to a server to be forwarded back to the caller (you)
}, error);
}, error);
}, error);
});

我希望 video.src 是检索远程视频的地址。因此,它应该由连接的另一端(发起调用的任何人)固定并给出。但是 URL.createObjectURL的值是在应答者一侧生成的,它的事件取决于函数被调用的时间。如何使用它来获取远程视频流?

编辑: URL.createObjectURL的结果看起来像 Blob : http://some.site.com/xxxx-the-token-xxxx .有了这个字符串,视频组件如何知道在哪里加载远程流?是否有 {url:stream} 的哈希图存储在某处?如果是这样,视频组件如何访问hashmap?

流对象确实存储了一个 token 字符串,您可以通过 stream.toURL 获得该字符串。 .但与 URL.createObjectURL的结果不同. URL.createObjectURL的值取决于时间。如果你连续调用它两次,你会得到不同的值。

最佳答案

URL.createObjectURL(stream)是一个黑客。停止使用它。正在努力中remove it .

使用 video.srcObject = stream 直接代替。它是标准的且实现良好。

本地资源的这种分配从一开始就不应该是 URL,并且是理解 WebRTC 如何工作的红鲱鱼。

WebRTC 是一种传输 API,直接将数据从一个对等方发送到另一个对等方。不涉及内容 URL。远程stream您来自 onaddstream是本地对象接收方,是传输的直播结果,准备播放。

您阅读的文档已经过时且过时。谢谢指出,我会改正的。它还有其他问题:您应该立即调用 setRemoteDescription,而不是等待接收者共享他们的相机,否则会错过传入的候选人。代替您显示的代码,请执行以下操作:

pc.onaddstream = e => video.srcObject = e.stream;

function getOfferFromFriend(offer) {
return pc.setRemoteDescription(new RTCSessionDescription(offer))
.then(() => navigator.getUserMedia({video: true}))
.then(stream => {
pc.addStream(stream);
return pc.createAnswer();
})
.then(answer => pc.setLocalDescription(answer))
.then(() => {
// send the answer to a server to be forwarded back to the caller (you)
})
.catch(error);
}

它使用 srcObject , 避免了已弃用的回调 API,并且不会导致间歇性 ICE 故障。

关于webrtc - 为什么WebRTC远程视频源是URL.createObjectURL生成的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41798731/

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