我使用的是https://github.com/HenrikJoreteg/RTCPeerConnection,它简化了WebRTC,但仍无法使其正常运行。 WebRTC对等连接(提供,回答,冰)已建立,但onaddstream永远不会被呼叫。
(我使用相同的html打开2个浏览器选项卡,它使用以下代码,我通过运行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);
});
答案 0 :(得分:2)
行。我现在就开始工作了。
https://github.com/HenrikJoreteg/RTCPeerConnection
那里的文件错了......
没有'streamAdded'事件,但是'addStream'事件,里面的值不是流而是事件。所以,我需要做以下操作。
peerConn.on('addStream',function(e){
remotevid.src = window.webkitURL.createObjectURL(e.stream);
remotevid.autoplay =true;
});