ambach 66
ambach 66

Reputation: 113

Webrtc adding ice candidate to remote peer

Below is the sample webrtc peer to peer connection code from google webrtc tutorilal. this link. I couldn't understand properly ,how addIceCandidate() add its Ice candidate to its remote peer using onIceCandidate(). what does event.candidate means here. A clear explanation would be appreciated

function onIceCandidate(pc, event) {   //pc1.onicecandidate
  if (event.candidate) {
    getOtherPc(pc).addIceCandidate(
      new RTCIceCandidate(event.candidate)
    ).then(
      function() {
        onAddIceCandidateSuccess(pc);
      },
      function(err) {
        onAddIceCandidateError(pc, err);
      }
    );

Upvotes: 2

Views: 2912

Answers (1)

deceze
deceze

Reputation: 522005

When peer A has discovered an ICE candidate (a potential route which could be used to communicate), it needs to send this ICE candidate to peer B (and vice versa). Peer B then adds that ICE candidate to its connection. Both peers exchange ICE candidates this way until they have found the optimal route that both are able to use to communicate with each other directly.

In that simple sample, peer A and B seem to be in the same machine, so the (dummy) getOtherPc function can get a handle of "the other peer" and you can directly use its addIceCandidate method. In practice however you will have to send that ICE candidate using a signalling server; some other way in which the peer can exchange the information across a network. Typically that signalling server will use a websocket connection via which information can be relayed in near-realtime.

Upvotes: 7

Related Questions