Android WebRTC onIceCandidate never get called

557 views Asked by At

There are already bunch of questions on this issue but they all are related to Javascript. I am trying in Android and my onIceCandidate() is never get called when I call setLocalDescription.

private PeerConnection initPeerConnection(boolean isLocal) {

    MediaConstraints sdpConstraints = new MediaConstraints();
    sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveAudio", "true"));
    sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("offerToReceiveVideo", "true"));

    List<PeerConnection.IceServer> servers = new ArrayList<>();

    return peerConnectionFactory.createPeerConnection(servers, new CustomPeerConnectionObserver() {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            if (isLocal)
                onIceCandidateReceived(localPeerConnection, iceCandidate);
            else
                onIceCandidateReceived(remotePeerConnection, iceCandidate);
        }

        @Override
        public void onAddStream(MediaStream mediaStream) {
            super.onAddStream(mediaStream);
            if (!isLocal)
                gotRemoteStream(mediaStream);
        }
    });
}

Creating Connections:

    localPeerConnection = initPeerConnection(true);
    remotePeerConnection = initPeerConnection(false);

Create offer. Now it should trigger the onIceCandidate():

localPeerConnection.createOffer(new SimpleSdpObserver() {
        @Override
        public void onCreateSuccess(SessionDescription sessionDescription) {
            Log.e("SDP", sessionDescription.description + sessionDescription.type.toString());
            localPeerConnection.setLocalDescription(new SimpleSdpObserver(), sessionDescription);
            sendOffer(sessionDescription);
        }
    }, sdpMediaConstraints);

But unfortunately it doesn't trigger it. What is wrong? What needs to be correct?

NOTE: Any answer specific to Android JavaScript will be much appreciated.

1

There are 1 answers

0
Paul Oliva On

You're close. Here is a snippet from my Android code:

private void createAndSendSubscriberOffer(boolean iceRestart) {
    llConnectionListener.onWebRTCOfferStart();
    peerConnection.createOffer(getSubscriberSDPObserver(), getSubscriberMediaConstraints(iceRestart));
}

@NotNull
private SimpleSdpObserver getSubscriberSDPObserver() {
    return new SimpleSdpObserver() {
        @Override
        public void onCreateSuccess(final SessionDescription sessionDescription) {
            peerConnection.setLocalDescription(new SimpleSdpObserver() {
                @Override
                public void onSetSuccess() {
                    Log.i(TAG, "Local description set.");
                    sendOfferToSocket(sessionDescription);
                }

                @Override
                public void onSetFailure(String s) {
                    Log.i(TAG, s);
                }
            }, sessionDescription);
        }
    };
}

The only thing I think you're missing is that you should use onSetSuccess to send the offer.