forestclown
forestclown

Reputation: 1602

How do we know when webRTC already finished collecting ICE candidates

I am using Kurento Utils for WebRTC connection with Kurento Media Server (ver 5.x)

Inside the kurento-utils-js library during init the simplify codes are shown below:

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;
};

My question is it seems like it is waiting until onicecandidate passing "null" value that signifies the process has ended and thus able to continue with creating SDP offer, but I couldn't find this behaviour in WebRTC specs?

My next question is, how else we can know the process of finding ice candidates has ended?

One of my PC in my office couldn't reached the code console.log('ICE negotiation completed'); as null value is not passed.

Upvotes: 2

Views: 3371

Answers (2)

xdumaine
xdumaine

Reputation: 10339

You could check the iceGatheringState property (run in 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;

Upvotes: 5

Dr. Alex Gouaillard
Dr. Alex Gouaillard

Reputation: 2128

http://www.w3.org/TR/webrtc/

4.3.1

" If the intent of the ICE Agent is to notify the script that:

[...]

  • The gathering process is done.

Set connection's ice gathering state to completed and let newCandidate be null."

So, you can either check for the ice gathering state against "completed" (in real life, this is not very reliable), or wait for a null candidate (super reliable).

Upvotes: 3

Related Questions