Getting WebRTC IceCandidates before createOffer or createAnswer - javascript

During establishing the video call ice candidates gathereing is started after the call to createOffer or createAnswer. I'd like to get all local ice candidates for the local peer before I call createOffer or createAnswer. This way I'll have all of them set in sdp description and there will be no need to send them separately to remote peer as they will go all together in sdp. Is it possible?
UPD:
All I want is to gather Ice candidates and keep them. After I want to create offers and asnwers but without of waiting for gathering of candidates will fininsh. Also How to add Ice candidates to description manually? And Is it possible to disabale Ice candidates gathering without recreating RTCPeerConnection?

Candidate gathering only starts when you call setLocalDescription.
If you want an SDP with all the candidates, wait for the onicecandidate event without a candidate and inspect the peerconnection's localDescription.sdp which contains all candidates gathered so far.

You can reduce the delay by setting icecandidatepoolsize - then the browser will try and pre-gather candidates before Offer/Answer.
see:
https://github.com/pipe/two/blob/master/index.html#L181

According to https://developer.mozilla.org/zh-CN/docs/Web/API/RTCPeerConnection/onicecandidate
When onicecandidate event in callback is null, issue that the peer connection has gather ice candidate complete. So at that time Create offer to other peer connection and the Offer's SDP info will contain 'a=candidate' attribute.
Reference: https://aggresss.github.io/webrtc-samples/src/content/peerconnection/pc1-mod/

Related

what triggers the webRTC API to connect to stun server

Don't know if this example is correct
The process, call new RTCPeerConnection() then createOffer() then setLocalDescription()
Then I wait for onicecandidate take what it gives and first send the offer and second the icecandidates through the signal server to the other peer
Then the other peer takes the received offer into setRemoteDescription(offer) then the received icecandidates into addIceCandidate(icecandidates) then calls createAnswer() this gives an answer to put in setLocalDescription(answer) this triggers onicecandidate take these icecandidates with the answer=offer and send them back to the other peer
The other peer takes the answer into setRemoteDescription(answer) then the received icecandidates into addIceCandidate(icecandidates)
I think in this example the connection will work when testing inside local network but what if it doesn't because its not a local network, at what step in this example will the API call the STUN server and what other functions do I need to call if it does call the STUN server?
I've found that one way to generate BIND requests to be sent to the STUN server right away is to set the iceCandidatePoolSize option in the configuration to be > 0.
config = {iceServers: [{urls:stun:stunserver.stunprotocol.org}], iceCandidatePoolSize: 1};
peerConnection = new RTCPeerConnection(config); // pretty much starts to resolve the DNS name and sends BIND requests right away.
Hope this helps.
Also: this link is chock-full of great suggestions to troubleshoot webrtc connections.
You need to specify a STUN server in the peer connection's configuration. E.g.:
pc = new RTCPeerConnection({iceServers: [{urls: "stun:stun.1.google.com:19302"}]});
There are no other methods to call, provided it works on a LAN already. You should see additional calls to onicecandidate from this, compared to before. That's it.
Note that a couple of the things you describe happen in parallel, but in short, what triggers the browser to connect to the STUN server is setLocalDescription. It causes the browser's built-in ICE agent to kick off its candidate gathering process for this connection, and STUN is part of that.

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

What is the proper way to reject a WebRTC offer?

In WebRTC, there seems to be a very well-defined order in which things happen.
Locally I use getUserMedia to get my local stream, and save the stream to a variable. I create an RTCPeerConnection object, which I name pc, and I add the local stream to it. I add an onaddstream event handler to pc, so that I can save the remote user's stream to a variable, and eventually set it as the src attribute of an HTML element like audio. I also set onicecandidate event handler on my pc to handle ice candidates.
At this point, there is an RTCPeerConnection, but no remote user "connected yet". This is where the "offer/answer" starts. Let's say I'm using websockets for signaling and I receive an offer, which is a message called 'offer' and containing an SDP object. How do I reject it and how should this be dealt with on both endpoints?
For instance, I could send a message 'reject' that would be relayed to the other user. My RTCPeerConnection still exists, and maybe I want to be able to receive other calls. As is, I don't have to do anything to my RTCPeerConnection, correct? Does the other user, who sent the offer, have to do anything? Does he have to close that particular RTCPeerConnection? I would think not, since all he did was create an SDP object, and then outside of WebRTC, through websockets, sent the object over to the other user. He did add the offer using setLocalDescription though. When the offer is rejected, does he need to do anything about this?
When I create an offer, and send it to the other user, if I never get an answer back, can I just send an offer to a third user and then if he sends an answer I'm connected with him?
I haven't found anything about the lifecycle of an RTCPeerConnection.
Proper (spec) way to reject media
The "proper" way to "reject" offered media in an answer hasn't been implemented in any browser yet:
pc.ontrack = e => e.transceiver.stop();
Basically, the WebRTC 1.0 spec has changed quite significantly in this area. In short, a transceiver is an object combining one sender and one receiver, each sending or receiving a single track.
transceiver.stop() lets you reject a single bidirectional m-line (negotiated media) in the signaled SDP media description. E.g. you can reject parts of an offer in your answer, without rejecting the whole thing.
Today
Today, the only way to reject individual m-lines is through mangling the SDP offer/answer manually.
But it sounds like you're not actually asking about that at all. Instead, it sounds like you're asking how to bail out of incomplete signaling and roll a peer connection back to "stable" state.
Rollback to stable state
The offer/answer negotiation cycle is a state machine. The state is pc.signalingState:
You asked if one side walks away from the negotiation, does either side need to do anything before they can re-purpose their connection object for a new attempt with the same or different peer. Well, it depends.
If you've only called createOffer then no rollback of state is necessary, since createOffer is not in the above diagram.
If you've called setLocalDescription however, then you're now in "have-local-offer" state, which means you do need to somehow get back to "stable" state before you can reuse the connection.
Your options are to either finish the negotiation, delete the connection, or rollback to stable state (currently only supported in Firefox, though it is in the spec):
let pc = new RTCPeerConnection();
pc.onnegotiationneeded = async e => {
try {
await pc.setLocalDescription(await pc.createOffer());
console.log(pc.signalingState); // have-local-offer
await pc.setLocalDescription({type: "rollback"});
console.log(pc.signalingState); // stable
} catch(e) {
console.log(e);
}
}
pc.createDataChannel("dummy");
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
Of course, you should also let the peer know, usually through out-of-band signaling.
Why this is typically not a problem.
In typical cases, you own the JavaScript on both ends, so this doesn't come up. In other words, the desire to have a connection usually precedes one being made.

WebRTC sequence of events

Is it mandatory for the WebRTC sequence of events be allowed to execute in the order depicted in this diagram?
WebRTC Flow
The reason I ask is that my one on one WebRTC chat performs flawlessly, however when a third participant enters the video conference, frequently I find one of the RTCPeerConnection shows iceConnectionState: "failed".
I do find that the remote stream is received before I process the Answer and before ICE Candicates are received.
How the following sequence be strictly enforced:
1) Send offer from caller.
2) Receive offer on callee.
3) Process offer on callee, generate answer and send to caller.
4) Receive and process offer on caller.
5) Send ICE candidates from caller to callee.
6) Process ICE candidates on callee.
7) Send ICE candidates from callee to caller.
The debug console shows that ICE candidates start getting generated as soon as the localmedia stream is added to the RTCPeerConnection. Should they be sent over immediately or wait till the sequence allows for sending?
May be too late but for the records:
Yes, the RTCPeerConnection is based on sequence. You can refer here for good explanation. I too struggled to control the flow and errors didn't mean much.
Based on my successful research/implementation (Chrome v74), to enforce sequence do (you can also see any sample online or here):
Send CreateOffer from Caller (signalling part; custom transport to the other users); enable PeerConnection.onnegotiationneeded => CreateOffer() only for Caller
Callee can be ready with video/audio streams added it to RTCPeerConnection on its side, then accepts offer and sends answer to Caller (signalling part)
Caller gets the answer and adds the video/audio streams to RTCPeerConnection conditionally when PeerConnection.connectionState == "new", the tricky parts: condition connectionState == "new" ensures it does not add again as offer / answer might be exchanged many times with state being connecting etc. Another is, if you add video/audio streams before this step, it will be hard to control the ice sdp flowing and raising errors at wrong states (relates to comment from Benjamin Trent, here) (signalling part)
since offer, answer is exchanged, now many ice events flows are exchanged (signalling part)
Connection should be established (assuming STUN configured and if peer to peer not possible, TURN required)
I hope I have highlighted few points missing online when you get hands on into this.

WebRTC: How to add stream after offer and answer?

I am working on webRTC video calling. I got datachannel successfully implemented. Now I would like to add video stream to the same peer connection.
I have read that stream should be added before answer and offer. Is there a way to add stream after answer or offer?
In case I have added stream before offer or answer, how could I stop streaming and start it again when needed?
Could there be any issues in maintaining so many streams?
To add stream after creating complete signalling, the Peer connection should renegotiate with stream.
pc1.addstream(stream)
Then once again create offer and send it to other Peer.
Remote peer will add stream and send answer SDP.
To stop streams:
stream.stop();
pc1.removeStream(stream);
In my experience, what Konga Raju advised didn't work. I couldn't send an "updated offer" and have the video streaming actually happen.
I found that this sequence of events works for my case, in which I wish to stream video from peer 1 to peer 2.
set up some way for the peers to exchange messages. (The variance in how people accomplish this is what makes different WebRTC code samples so incommensurable, sadly.)
On each side, set up handlers for the important signalling events. (Some folks have reported that you need to create these handlers at special times, but I haven't found that to be the case.
) There are 3 basic events:
an ice candidate sent from the other side ==> call addIceCandidate with it
an offer message ==> SetRemoteDescription & make an answer & send it
an answer message ===> SetRemoteDescription
On each side, create the peerconnection object with the event handlers we care about: onicecandidate, onremovestream, onaddstream, etc.
ice candidate pops out of the peerconnection object ===> send it to other side
When both peers are present and all the handlers are in place, peer 1 gets a trigger message of some kind to start video capture (the getUserMedia call)
Once getUserMedia succeeds, we have a stream. Call addStream on the peer connection object.
Then peer 1 makes an offer
Due to the handlers we set up earlier, peer 2 sends an answer
Concurrently with this (and rather opaquely), the peer connection object starts producing ice candidates. They get sent back and forth between the two peers and handled (steps 2 & 3 above)
Streaming starts by itself, opaquely, as a result of 2 conditions:
offer/answer exchange
ice candidates received, exchanged, and handled
I haven't found a way to add video after step 9. When I want to change something, I go back to step 3.
MediaStream should be added to peerconnection first only then exchange of offer, answer ,candidates should be done. If the onAddStream() is called ,that mean you are receiving the remote video.

Categories

Resources