fcasanova
fcasanova

Reputation: 317

How to get ICE candidates to be added in sdp description

I'm developing a native android application that uses webrtc, more specifically a DataChannel, to send data to another person. It's working between two android phones, using ICE trickling for this case.

But right now I'm trying to establish a connection between the android phone and a browser (a Chrome instance in my computer, in this case).

The thing is that I'm using a library in js that doesn't handle the message with candidates received through the websocket, it's only working with the candidates that are already in the sdp of the offer/answer, if they come in another message, they are discarded (poor implementation, I know). I want to be able to handle this without getting rid of this library, and right now browser to browser it works just fine.

So here's my question: in android I was sending the candidates through websocket as soon as I got them in onIceCandidate( IceCandidate iceCandidate ). I changed it, I'm not sending them (commented that code) and I'm delaying the sending of the offer through websocket to happen only after onIceGatheringChange( PeerConnection.IceGatheringState iceGatheringState ) is returning COMPLETE. But even with this waiting, the sdp description does not contain anything about the ice-candidates, it's exactly the same as if no candidate was available yet... And there's a=ice-options:trickle renomination, which I don't know who is setting this option or if it can be changed.

Is the sdp description supposed to be changing after it's creation? I find that it remains the same no matter what, what is set in onCreateSuccess( SessionDescription sdp ).

I also tried another thing, to set iceCandidatePoolSize to 1 in order to get candidates before the offer creation, but it didn't change a thing.

Am I missing something? Does the sdp change only by waiting for candidates really? Or do I need to disable the trickle somehow? I wasn't able to find this info.

Thank you!!

Upvotes: 3

Views: 2474

Answers (2)

Vitaly Ivanov
Vitaly Ivanov

Reputation: 61

That's exactly the code you need: https://github.com/x186k/whip-whap-js/blob/main/whip-whap-js.js#L40

The order is: createOffer, setLocalDescription, wait till ICE gathering state is completed, grab RTCPeerConnection's localDescription - it will include all the ICE candidates. There's no need to do createOffer/setLocalDescription again

Upvotes: 2

Aiti
Aiti

Reputation: 21

Maybe a late reply, but I'll answer anyway. Recently faced the same problem. It is solved in this way: in createLocalPeerConnection, you need to add a method:

  @Override
        public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) {
            super.onIceGatheringChange(iceGatheringState);
            if(iceGatheringState == PeerConnection.IceGatheringState.COMPLETE) {
                createLoclOffer(sdpConstraints);
            }
        }

With a condition, you check the status of the collection of candidates and then call the sdp submit method.

Value:

new The peer connection was just created and hasn't done any networking yet.

gathering The ICE agent is in the process of gathering candidates for the connection.

complete The ICE agent has finished gathering candidates. If something happens that requires collecting new candidates, such as a new interface being added or the addition of a new ICE server, the state will revert to gathering to gather those candidates.

Please note that in order to collect ice candidates, an already formed sdp is required.

I found another way to solve the problem. If you wait for the COMPLETE status of the collection of ice candidates, then it takes a very long time. For this reason, you can set a time counter (2 seconds will be enough), after the time has elapsed, call the method with a suggestion.

 new CountDownTimer(2000, 1000) {

        
        public void onTick(long millisUntilFinished) {

        }
        
        public void onFinish() {
            createLoclOffer(sdpConstraints);
        }
    }
            .start();

Upvotes: 2

Related Questions