以下是我使用WebRTC进行视频聊天的代码。问题是我总是在remoteVideo元素中获取localStream(在两个对等体中),为什么会这样?
我没有管理员权限访问我要上传网站的主机,因此我应首先使用免费的STUN / TURN服务器。我不确定我的iceServers用法是否正确,问题是什么?
我真的很厌烦它!任何帮助表示赞赏:)
$(function () {
var localVideo = document.getElementById('localVideo');
var remoteVideo = document.getElementById('remoteVideo');
var iceServers = {
'iceServers': [
{ 'url': 'stun:stun4.l.google.com:19302' },
{ 'url': 'stun:stunserver.org' },
{ 'url': 'stun:stun.softjoys.com' },
{ 'url': 'stun:stun.voiparound.com' },
{ 'url': 'stun:stun.voipbuster.com' },
{ 'url': 'stun:stun.voipstunt.com' },
{ 'url': 'stun:stun.voxgratia.org' },
{ 'url': 'stun:stun.xten.com' },
{
'url': 'turn:numb.viagenie.ca',
'credential': 'muazkh',
'username': 'webrtc@live.com'
},
{
'url': 'turn:192.158.29.39:3478?transport=udp',
'credential': 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
'username': '28224511:1379330808'
},
{
'url': 'turn:192.158.29.39:3478?transport=tcp',
'credential': 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
'username': '28224511:1379330808'
}
]};
var localStream, pc1, pc2;
var sdpConstraints = {
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': true
}
};
getUserMedia({
audio: true,
video: true
}, gotLocalStream, function (error) {
alert('error: ' + error.name);
});
$('button').click(function () {
pc1 = new RTCPeerConnection(iceServers);
pc1.onicecandidate = function (event) {
if (event.candidate)
pc2.addIceCandidate(new RTCIceCandidate(event.candidate));
};
pc2 = new RTCPeerConnection(iceServers);
pc2.onicecandidate = function (event) {
if (event.candidate)
pc1.addIceCandidate(new RTCIceCandidate(event.candidate));
};
pc2.onaddstream = gotRemoteStream;
pc1.addStream(localStream);
pc1.createOffer(offer_success, offer_error);
});
function gotLocalStream(stream) {
attachMediaStream(localVideo, stream);
localStream = stream;
}
function gotRemoteStream(event) {
attachMediaStream(remoteVideo, event.stream);
}
function offer_success(desc) {
pc1.setLocalDescription(desc);
pc2.setRemoteDescription(desc);
//pc2.createAnswer(answer_success, answer_error, sdpConstraints);
pc2.createAnswer(answer_success, answer_error);
}
function offer_error(error) {
alert(error.toString());
}
function answer_success(desc) {
pc2.setLocalDescription(desc);
pc1.setRemoteDescription(desc);
}
function answer_error(error) {
alert('error: ' + error.toString());
}
});