Pardeep Beniwal
Pardeep Beniwal

Reputation: 63

How can I add candidate to peerConnection.addIceCandidate() while create a offer

I want to make voice calling with nodejs and webrtc.When I call to other user then getting error'ICE failed, see about:webrtc for more details'. The HTML just contains a button that calls offer().

I can confirm the offer and SessionDescriptions are transferring successfully from one client to the other. Please help me

 Client Side Javasrcipt: 
    navigator.getUserMedia({video:false, audio:true}, function(stream)                 {
document.getElementById("localvideo").mozSrcObject = stream;
document.getElementById("localvideo").play();
document.getElementById("localvideo").muted = true;

// var pc = new mozRTCPeerConnection();
var pc = new PeerConnection(iceServers, options);
pc.addStream(stream);

pc.onaddstream = function(obj) {
log("Got onaddstream of type " + obj.type);
document.getElementById("remotevideo").mozSrcObject = obj.stream;
document.getElementById("remotevideo").play();
document.getElementById("dialing").style.display = "none";
document.getElementById("hangup").style.display = "block";
};

pc.createOffer(function(offer) {
console.log("Created offer" + JSON.stringify(offer));
pc.setLocalDescription(offer, function() {
// Send offer to remote end.       
pc.iceCandidate = new RTCIceCandidate();      
console.log(pc.iceCandidate);
    peerc = pc;
    jQuery.post(
      "offer", {
        to: user,
        from: document.getElementById("user").innerHTML,
        offer: JSON.stringify(offer)
      },
      function() { console.log("Offer sent!"); }
    ).error(error);
  }, error);
}, error);

});

And my Server Side script-

        app.post("/offer", function(req, res) { 
          var channel = users[req.body.to];
           channel.write("event: offer\n");
          channel.write("data: " + JSON.stringify(req.body));
          channel.write("\n\n");
          res.send(200);
        });

Upvotes: 0

Views: 2283

Answers (3)

jib
jib

Reputation: 42500

You need to add the receiving end and handle ICE candidates. I see createOffer but not createAnswer, and the ICE failures are probably from not signaling ICE candidates across (a practice called Trickle ICE, which is required now).

It looks like you're using Firefox, so you should be able to run this local-loop demo (no signaling), which I still hope points out the missing pieces to close the loop. I use video because audio works terribly in local-loops for obvious reasons (feedback):

var pc1 = new mozRTCPeerConnection(), pc2 = new mozRTCPeerConnection();

pc1.onicecandidate = e => !e.candidate ||
    pc2.addIceCandidate(e.candidate).catch(failed);
pc2.onicecandidate = e => !e.candidate ||
    pc1.addIceCandidate(e.candidate).catch(failed);
pc2.onaddstream = e => v2.mozSrcObject = e.stream;

function start() {
  navigator.mediaDevices.getUserMedia({ video: true })
  .then(stream => pc1.addStream(v1.mozSrcObject = stream))
  .then(() => pc1.createOffer())
  .then(offer => pc1.setLocalDescription(offer))
  .then(() => pc2.setRemoteDescription(pc1.localDescription))
  .then(() => pc2.createAnswer())
  .then(answer => pc2.setLocalDescription(answer))
  .then(() => pc1.setRemoteDescription(pc2.localDescription))
  .then(() => log("Connected!"))
  .catch(failed);
}

var log = msg => div.innerHTML += "<p>" + msg + "</p>";
var failed = e => log(e +", line "+ e.lineNumber);
<video id="v1" height="120" width="160" autoplay></video>
<video id="v2" height="120" width="160" autoplay></video><br>
<button onclick="start()">Start!</button><div id="div"></div>

Upvotes: 1

JDT
JDT

Reputation: 109

Are you generating answer on the other end and sending it back to first party to process it?

Upvotes: 0

mido
mido

Reputation: 25054

I am not which is the case here, either you have not provided compelete app code or your app code for proper webrtc connection is incomplete,

For starters, as much as webrtc has simplified video chat, simply sending the offer sdp would not do the trick( I am assuming that you created answer sdp on the other side and sending it), you are going to have to exchange ICE candidates also. As peer's ice candidates are sort of like their calling card tells how they can be reached. So without excahnging them, they cannot communticate.

enter image description here

generally, the browser provides you the ice candidate in onIceCandidate event, this you send to your peer, which would add it as peerConnection.addIceCandidate(candidate), initially when I started out, this doc truly helped me in understanding the basics of WebRTC, you could give it a try.

Upvotes: 3

Related Questions