我正在创建一对一的webrtc视频聊天室,这段代码不起作用,我想知道为什么
function hasUserMedia(){navigator.getUserMedia = navigator.getUserMedia ||航海家….
这是可怕的代码。它已经过时,有6个跟踪WebRTC API发展的问题。
的 TL; DR: 强> 它不起作用,因为您没有检查错误,并且您只测试了一个浏览器。
yourConnection = new RTCPeerConnection(configuration); theirConnection = new webkitRTCPeerConnection(configuration); // <-- wrong
webkit -names在Firefox或Edge中不起作用。多年来一直不需要这些。如果你切换到 navigator.mediaDevices.getUserMedia ,你可以完全跳过10行前缀错误的序言。
webkit
navigator.mediaDevices.getUserMedia
url
urls
这在技术上是错误的,但我怀疑大多数浏览器允许它:
iceServers: [{url: "stun:stun.1.google.com:19302"}] // <-- wrong
而是使用:
iceServers: [{urls: "stun:stun.1.google.com:19302"}]
...因为技术上可以在多个网址上访问ICE服务器。
这是错的:
navigator.getUserMedia({video: true, audio: true}, function(stream) { /* ... */ });
...因为a 第三个失败回调参数是必需的 。 Edge说 TypeError: Argument not optional 。
TypeError: Argument not optional
Chrome和Safari中的遗留错误允许这样做,但它不适用于Firefox或Edge。忽略错误会剥夺您学习为什么不起作用的原因。如果用户拒绝摄像头访问,你想知道。
所有现代浏览器都支持 诺言 API的版本 上 mediaDevices 。使用它代替:
mediaDevices
navigator.mediaDevices.getUserMedia({video: true, audio: true}) .then(stream => { /* use stream here */ }) .catch(error => console.log(error));
我有 之前回答过这个 ,但简而言之,这与上面的#2相似,但有一点扭曲。这是错的:
yourConnection.createOffer(function(offer) { /* ... */ });
你以为你在打电话给 旧的回调API ,但你不是。那些要求 的 二 强> 参数:
yourConnection.createOffer(successCallback, failureCallback /*, optionsObject */);
相反,你实际上是在调用同名的 现代承诺API ,因为函数是JS中的一个对象:
const promise = yourConnection.createOffer(optionsObject);
这是您的代码停止工作的地方。永远不会调用您的回调函数,而是将其解释为空选项对象。您忽略了返回的承诺。请改用promise API。
它已在Firefox和Chrome 71中删除了 警告 你收到)。这是错的:
theirVideo.src = URL.createObjectURL(stream);
而是使用这个:
theirVideo.srcObject = stream;
addStream() &安培; onaddstream 已经不在了 规范 ,仅适用于某些浏览器:
addStream()
onaddstream
yourConnection.addStream(stream); theirConnection.onaddstream = e => theirVideo.srcObject = e.stream;
相反,对等连接现在完全基于跟踪。请改用:
for (const track of stream.getTracks()) { yourConnection.addTrack(track, stream); } theirConnection.ontrack = e => theirVideo.srcObject = e.streams[0];
有关这些差异的更多信息,请参阅 我的博客 。
下列 应该适用于所有浏览器 :
const yourVideo = document.querySelector("#face_cam_vid"); const theirVideo = document.querySelector("#thevid"); (async () => { if (!("mediaDevices" in navigator) || !("RTCPeerConnection" in window)) { alert("Sorry, your browser does not support WebRTC."); return; } const stream = await navigator.mediaDevices.getUserMedia({video:true, audio:true}); yourVideo.srcObject = stream; const configuration = { iceServers: [{urls: "stun:stun.1.google.com:19302"}] }; const yours = new RTCPeerConnection(configuration); const theirs = new RTCPeerConnection(configuration); for (const track of stream.getTracks()) { yours.addTrack(track, stream); } theirs.ontrack = e => theirVideo.srcObject = e.streams[0]; yours.onicecandidate = e => theirs.addIceCandidate(e.candidate); theirs.onicecandidate = e => yours.addIceCandidate(e.candidate); const offer = await yours.createOffer(); await yours.setLocalDescription(offer); await theirs.setRemoteDescription(offer); const answer = await theirs.createAnswer(); await theirs.setLocalDescription(answer); await yours.setRemoteDescription(answer); })();