我们如何知道 webRTC 何时已完成收集 ICE 候选者

for*_*own 2 webrtc kurento

我正在使用Kurento Utils与 Kurento 媒体服务器(版本 5.x)进行 WebRTC 连接

在初始化期间的 kurento-utils-js 库内部,简化代码如下所示:

if (!this.pc) {
    this.pc = new RTCPeerConnection(server, options);
}

var ended = false;
pc.onicecandidate = function(e) {
    // candidate exists in e.candidate
    if (e.candidate) {
        ended = false;
        return;
    }

    if (ended) {
        return;
    }

    var offerSdp = pc.localDescription.sdp;
    console.log('ICE negotiation completed');

    self.onsdpoffer(offerSdp, self);

    ended = true;
};
Run Code Online (Sandbox Code Playgroud)

我的问题是,它似乎正在等待onicecandidate传递“null”值,这表示该过程已结束,从而能够继续创建 SDP 报价,但我在 WebRTC 规范中找不到这种行为?

我的下一个问题是,我们还能如何知道寻找ice候选人的过程已经结束?

我办公室的一台电脑无法访问该代码console.log('ICE negotiation completed');,因为未传递空值。

xdu*_*ine 5

您可以检查iceGatheringState属性(在chrome中运行):

var config = {'iceServers': [{ url: 'stun:stun.l.google.com:19302' }] };
var pc = new webkitRTCPeerConnection(config);
pc.onicecandidate = function(event) { 
    if (event && event.target && event.target.iceGatheringState === 'complete') {
        alert('done gathering candidates - got iceGatheringState complete');
    } else if (event && event.candidate == null) {
        alert('done gathering candidates - got null candidate');
    } else {
          console.log(event.target.iceGatheringState, event);   
    }
};

pc.createOffer(function(offer) {
    pc.setLocalDescription(offer);
}, function(err) {
    console.log(err);
}, {'mandatory': {'OfferToReceiveAudio': true}});

window.pc = pc;
Run Code Online (Sandbox Code Playgroud)