为什么ICE候选人在Webrtc视频通话时失败?

时间:2016-10-18 08:20:29

标签: javascript google-chrome browser webrtc mozilla

我用webRTC Technology写了一个简单的视频通话应用程序。这工作正常first 3 minutes,之后我遇到了一些"ICE candidate failing"错误。

这就是发生的事情:

步骤:

设置对等连接

  • 当窗口加载时,允许使用getUserMedia进行网络摄像头访问(访问网络摄像头 和麦克风)
  • 使用添加的远程流创建RTC对等连接 icecandidateAdded

启动对等连接

  • 创建优惠,发送优惠请求
  • addIceCandidate
  • 创建答案,发送答案回复

这是我的代码:

function hasUserMedia() {
    navigator.getUserMedia = navigator.getUserMedia ||
                         navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
    return !!navigator.getUserMedia;
};

function hasRTCPeerConnection(){
    window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
    window.RTCSessionDescription = window.RTCSessionDescription || window.webkitRTCSessionDescription || window.mozRTCSessionDescription;
    window.RTCIceCandidate = window.RTCIceCandidate || window.webkitRTCIceCandidate || window.mozRTCIceCandidate;

    return !!window.RTCPeerConnection;
};

//when window loading 
function startConnection() {

    if (hasUserMedia()){

        navigator.getUserMedia({ video: true, audio: true }, function(my_stream) {
        stream = my_stream;
        client_Video.src = window.URL.createObjectURL(stream);

        if(hasRTCPeerConnection()){
                console.log("RTC peer connection is ready");
                //set up peer connection
                setup_Peer_Connection(stream);
            }

    }
}

function setup_Peer_Connection(stream){

    var configuration = {"iceServers": [{ "url": "stun:stun.1.google.com:19302" }]};
    my_Connection = new RTCPeerConnection(configuration,{optional:[]});
    my_Connection.addStream(stream);
    my_Connection.onaddstream = remoteStreamAdded;
    my_Connection.onicecandidate = icecandidateAdded;
}

function remoteStreamAdded(ev){

    m_PeerVideo.src = window.URL.createObjectURL(ev.stream);
}

function icecandidateAdded(ev) {
    if (ev.candidate) {
            send({type: "candidate",candidate: ev.candidate});
        }
};  

并且用于启动对等连接:

var mediaConstraints = {
    optional: [],
    mandatory: {
        OfferToReceiveAudio: true,
        OfferToReceiveVideo: true
    }
};

function calluser(){
    //start video call with user
    Start_Peer_Connection();
}

function Start_Peer_Connection(){

    my_Connection.createOffer(function (offer) {
        console.log("offer started");
        send({type: "offer",offer: offer});
                my_Connection.setLocalDescription(offer, success2, failure_offer);
    }, failure_Start_Peer_Connection,mediaConstraints);
}


//getting message from server

connection.onmessage = function (message) {

    console.log("Got message", message.data);   

    var data = JSON.parse(message.data);

    switch(data.type) {

        case "offer":
            Client_Offer(data.offer, data.name);
        break;

        case "answer":
            Client_Answer(data.answer);
        break;

        case "candidate":
            Client_Candidate(data.candidate);
        break;

        case "leave":
            Client_Leave();
        break;

        default:
        break;
    }
};

function Client_Offer(offer, name) {

    my_Connection.setRemoteDescription(new RTCSessionDescription(offer));

        my_Connection.createAnswer(function (answer) {

            my_Connection.setLocalDescription(answer);

            send({type: "answer",answer: answer});

        },failure_Client_Offer); 

    }
}   

function Client_Answer(answer) {

    my_Connection.setRemoteDescription(new RTCSessionDescription(answer));
}

function Client_Candidate(candidate) {

    my_Connection.addIceCandidate(new RTCIceCandidate(candidate));
}

我正在使用Mozilla Firefox浏览器。我不知道为什么3分钟后这不起作用。连接很好。但为什么冰候选人在一段时间后失败?任何代码丢失?有什么建议 ?

0 个答案:

没有答案