admin管理员组

文章数量:1023885

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

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);
      }
    );
Share Improve this question asked May 24, 2018 at 7:29 ambach 66ambach 66 1133 silver badges11 bronze badges 1
  • 1 html5rocks./en/tutorials/webrtc/infrastructure explains what an ice candidate is (along with many other concepts needed to understand things) – Philipp Hancke Commented May 24, 2018 at 7:41
Add a ment  | 

1 Answer 1

Reset to default 7

When peer A has discovered an ICE candidate (a potential route which could be used to municate), 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 municate 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.

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

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);
      }
    );
Share Improve this question asked May 24, 2018 at 7:29 ambach 66ambach 66 1133 silver badges11 bronze badges 1
  • 1 html5rocks./en/tutorials/webrtc/infrastructure explains what an ice candidate is (along with many other concepts needed to understand things) – Philipp Hancke Commented May 24, 2018 at 7:41
Add a ment  | 

1 Answer 1

Reset to default 7

When peer A has discovered an ICE candidate (a potential route which could be used to municate), 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 municate 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.

本文标签: javascriptWebrtc adding ice candidate to remote peerStack Overflow