ICE Candidates and SDP Offer

When establishing a WebRTC connection between Kurento and a WebRTC client (e.g., a web browser), two key processes are involved:

  • ICE (Interactive Connectivity Establishment): This mechanism helps discover the best network paths (ICE candidates) for sending media between the peers.

  • SDP (Session Description Protocol): This protocol negotiates the media session details, including codecs, formats, and encryption.

The process of connecting a WebRTC client with a WebRtcEndpoint involves exchanging SDP offers/answers and ICE candidates to establish a peer-to-peer connection.

Example: WebRTC SDP Exchange in JavaScript

The following example shows how to exchange SDP offers/answers between a browser WebRTC client and a Kurento WebRtcEndpoint:

// WebRTC SDP Offer from the client (browser)
let webRtcClientSdpOffer = /* get the SDP offer from the browser */;

// Create a WebRtcEndpoint in Kurento
pipeline.create('WebRtcEndpoint', function(error, webRtcEndpoint) {
    if (error) return console.error(error);

    // Process the SDP offer from the client
    webRtcEndpoint.processOffer(webRtcClientSdpOffer, function(error, sdpAnswer) {
        if (error) return console.error(error);

        // Send the SDP answer back to the client
        console.log('SDP Answer:', sdpAnswer);
        // The client now sets the SDP answer on their WebRTC peer connection

        // Gather ICE candidates
        webRtcEndpoint.gatherCandidates(function(error) {
            if (error) return console.error(error);

            console.log('ICE candidate gathering started.');
        });
    });
});

// Receiving ICE candidates from the client
let candidate = /* get ICE candidate from client */;
webRtcEndpoint.addIceCandidate(candidate);

Example: WebRTC SDP Exchange in the Browser

The browser (or WebRTC peer) sends an SDP offer to Kurento, which processes the offer and returns an SDP answer. Afterward, both sides exchange ICE candidates to establish a media path.

let pc = new RTCPeerConnection(/* RTC configuration */);

pc.createOffer().then(offer => {
    // Send the SDP offer to Kurento
    sendOfferToKurento(offer);

    // Set local description
    pc.setLocalDescription(offer);
});

function onKurentoAnswer(sdpAnswer) {
    // Set the SDP answer received from Kurento
    let answer = new RTCSessionDescription({
        type: 'answer',
        sdp: sdpAnswer
    });

    pc.setRemoteDescription(answer);
}

pc.onicecandidate = function(event) {
    if (event.candidate) {
        // Send ICE candidate to Kurento
        sendCandidateToKurento(event.candidate);
    }
};