8

I am developing the signaling system between two peers and have noticed that the RTCPeerConnection.onicecandidate event is not firing. I checked the iceGatheringState and it always returns as "new" meaning the peer connection has not began searching for ice candidates.

How do I initiate the gathering of ice candidate objects from the local machine to be sent out to a peer?

and

If I do not want to trickle candidates, how will I be able to send them over sdp once gathered?

This is my current code, I am able to successfully attain sdp data and capture them to be sent so ice and checking if the two clients are connected are the only issues.

var peerConn = new webkitRTCPeerConnection(
    {'iceServers':[{'url':'stun:stun.1.google.com:19302'}]}
);
var remoteConn = new webkitRTCPeerConnection(
    {'iceServers':[{'url':'stun:stun.1.google.com:19302'}]}
);

alert(peerConn.iceGatheringState);

///Event Handlers//
//will be called when each event occurs

//onicecandidate
//returns local ice candidates (when gathered) to be sent to peer
//peerConn.onicecandidate = onicecandidate;
peerConn.onicecandidate = function(iceEvent){ //not firing
    if(iceEvent.candidate === null){
        alert(peerConn.iceConnectionState);
        alert(iceEvent.candidate);

        //send to peer or put in with sdp data
    }
}
Pj Rigor
  • 391
  • 6
  • 17

1 Answers1

13

ICE gathering starts once you call setLocalDescription with the SDP you generated with createOffer or createAnswer.

If you don't want to use trickle ice, wait for the null candidate and then send the content of peerConn.localDescription.sdp -- which should include the candidates then.

Philipp Hancke
  • 15,855
  • 2
  • 23
  • 31
  • So in the non-trickle solution, the functions for sdp generation and local decription setting will be called only after the null candidate is recieved meaning that if I call them before, then the ice candidates will not be added? – Pj Rigor Jul 22 '16 at 18:45
  • no. You just don't signal the SDP before the null candidate. You must generate the SDP and call setLocalDescription to start gathering. Try [this fiddle](https://jsfiddle.net/z7w7kzxc/) – Philipp Hancke Jul 22 '16 at 19:20
  • 1
    I did not open any data channels or streams. That may be my problem, I'm trying to implement the fiddle I will see if it works out. – Pj Rigor Jul 22 '16 at 19:24
  • It works! the order in which I was doing the proccess was incorrect – Pj Rigor Jul 22 '16 at 21:53
  • 2
    @PjRigor can you show code, for some reason im setting localdescription and there is no gathering of ice on answer side, could be something to do with adding stream? – Muhammad Umer Oct 03 '17 at 17:11