我正在使用https://github.com/HenrikJoreteg/RTCPeerConnection,它简化了 WebRTC,但仍然无法正常工作。WebRTC 对等连接(offer、answer、ice)已建立,但 onaddstream 永远不会被调用。
(我打开 2 个具有相同 html 的浏览器选项卡,它们使用以下代码,我通过运行 start 函数来运行代码以获取视频,然后运行 peer 启动连接。)
谁能帮我指出这段代码中的错误?
var local_stream;
var localvid = document.getElementById('localvid');
var remotevid = document.getElementById('remotevid');
var servers= {iceServers: [{"url": "stun:stun.l.google.com:19302"}]};
var constraints = {optional: [{"DtlsSrtpKeyAgreement": true}]};
var peerConn = new PeerConnection(servers,constraints);
function start(){
navigator.getUserMedia ||
(navigator.getUserMedia = navigator.getUserMedia || navigator.mozGetUserMedia ||
navigator.webkitGetUserMedia || navigator.msGetUserMedia);
if (navigator.getUserMedia) {
navigator.getUserMedia({
video: true,
audio: false
}, onSuccess, function(){});
} else {
alert('getUserMedia is not supported in this browser.');
}
}
function onSuccess(stream){
localvid.src = window.webkitURL.createObjectURL(stream);
local_stream = stream;
localvid.autoplay = true;
}
function peer(){
peerConn.addStream(local_stream);
peerConn.offer({ mandatory: { OfferToReceiveAudio:false, OfferToReceiveVideo: true} },
function( err, offer){
if(!err){
console.log("Creating an offer...");
socket.emit('offer',offer);
}
}
);
}
peerConn.on('ice', function(candidate){
if(candidate){
console.log("Sending ice...");console.log(candidate);
socket.emit('ice',candidate);
}else { console.log("End of candidates.");}
});
peerConn.on('streamAdded',function(stream){
console.log("Adding stream..");
remotevid.src = window.webkitURL.createObjectURL(stream);
remotevid.autoplay = true;
});
socket.on('offer',function(offer){
peerConn.answer(offer,function (err,answer){
if(!err){
console.log("Creating the answer...");console.log(answer);
socket.emit('answer',answer);
}
});
});
socket.on('answer', function(answer){
console.log("Got the answer...");
peerConn.handleAnswer(answer);
});
socket.on('ice', function (candidate){
console.log("Processing ice...");
peerConn.processIce(candidate);
console.log(candidate);
});