Class PeerConnection

java.lang.Object
com.codename1.webrtc.PeerConnection

public final class PeerConnection extends Object

One end of a call: the media this device sends, what it receives, and the network path between the two.

The sequence is the standard one. The caller adds its tracks, creates an offer, sets it as its local description and sends it; the callee sets it as its remote description, adds its own tracks, creates an answer, sets that locally and sends it back; the caller sets it as remote. Meanwhile both sides send each other every candidate their listener is given. Carrying the descriptions and candidates is the app's job -- over its own server, a WebSocket, anything -- and nothing here sends them.

import com.codename1.io.Log;
import com.codename1.webrtc.*;

WebRtc.openUserMedia(true, false, false).ready(media -> {
    PeerConnection pc = WebRtc.createPeerConnection(new PeerConnectionConfig(),
            new PeerConnection.Listener() {
                public void iceCandidate(PeerConnection connection, IceCandidate candidate) {
                    Log.p("send to the other side: " + candidate.getCandidate());
                }

                public void stateChanged(PeerConnection connection, String state) {
                    Log.p("state " + state);
                }

                public void remoteTrack(PeerConnection connection, MediaTrack track) {
                }
            });
    pc.addTrack(media.getAudio());
    pc.createOffer().ready(offer -> pc.setLocalDescription(offer)
            .ready(done -> Log.p("send to the other side: " + offer.getSdp())));
});