Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class WebRtc

  1. Object
  2. WebRtc

Real-time audio and video between two devices: the entry point of the package.

import com.codename1.webrtc.*;

Form form = new Form("Call", new BorderLayout());
if (WebRtc.isSupported()) {
    WebRtc.openUserMedia(true, true, true).ready(media -> {
        PeerConnection pc = WebRtc.createPeerConnection(
                new PeerConnectionConfig().addIceServer(IceServer.stun("stun:stun.example.com")),
                new PeerConnection.Listener() {
                    public void iceCandidate(PeerConnection connection, IceCandidate candidate) {
                        // send it to the other side over your own channel
                    }

                    public void stateChanged(PeerConnection connection, String state) {
                        form.setTitle(state);
                    }

                    public void remoteTrack(PeerConnection connection, MediaTrack track) {
                        if (track.isVideo()) {
                            form.add(BorderLayout.CENTER, new VideoView(track, false));
                            form.revalidate();
                        }
                    }
                });
        pc.addTrack(media.getAudio());
        pc.addTrack(media.getVideo());
        form.add(BorderLayout.SOUTH, new VideoView(media.getVideo(), true));
        form.revalidate();
    });
} else {
    form.add(BorderLayout.CENTER, new Label("No camera calls on this device"));
}
form.show();

The package carries media. It does not ring, place the call in the system call log, or wake a sleeping phone: that is com.codename1.call, which an app uses alongside this one. And it does not signal – see PeerConnection.

On Android this is Google’s WebRTC library and on iOS Apple-platform builds of the same library; both are added to an app only when it uses this package. In a browser it is the browser’s own WebRTC. The simulator connects calls without carrying media: tracks open, descriptions are exchanged and connections reach PeerConnection.CONNECTED, and the video views show a test pattern, so an app’s call flow can be developed and tested end to end on the desktop.

Methods

public static boolean isSupported()Whether this platform carries WebRTC media.
public static AsyncResource<LocalMedia> openUserMedia(boolean audio, boolean video, boolean frontCamera)Opens the camera and/or the microphone, asking the user’s permission where the platform asks for it.
public static PeerConnection createPeerConnection(PeerConnectionConfig config, PeerConnection.Listener listener)Creates a peer connection, which reports to listener on the EDT.
public static void setSpeakerphone(boolean on)Routes call audio to the loudspeaker, or with false back to the earpiece or headset.
public static void deliverDescription(int requestId, String type, String sdp, String error)A port’s answer to createDescription: the description, or a null sdp and the reason.
public static void deliverTracks(int requestId, int audio, int video, String error)A port’s answer to openUserMedia: the track handles, 0 for none, or both 0 and the reason.
public static void deliverResult(int requestId, String error)A port’s answer to setDescription: null for success, else the reason.
public static void iceCandidate(int peer, String sdpMid, int sdpMLineIndex, String candidate)A candidate a peer connection gathered.
public static void stateChanged(int peer, String state)A peer connection moved to state, one of the names on PeerConnection.
public static void remoteTrack(int peer, int track, boolean video)The other side’s track arrived on a peer connection.

Inherited methods

Method details

isSupported

public static boolean isSupported()
Whether this platform carries WebRTC media.

openUserMedia

public static AsyncResource<LocalMedia> openUserMedia(boolean audio, boolean video, boolean frontCamera)
Opens the camera and/or the microphone, asking the user’s permission where the platform asks for it. Fails when the user refuses or the device has neither.

Parameters

audio boolean
Not documented.
video boolean
Not documented.
frontCamera boolean
which camera to open first; see MediaTrack.switchCamera()

createPeerConnection

public static PeerConnection createPeerConnection(PeerConnectionConfig config, PeerConnection.Listener listener)
Creates a peer connection, which reports to listener on the EDT.

setSpeakerphone

public static void setSpeakerphone(boolean on)
Routes call audio to the loudspeaker, or with false back to the earpiece or headset.

deliverDescription

public static void deliverDescription(int requestId, String type, String sdp, String error)
A port’s answer to createDescription: the description, or a null sdp and the reason.

deliverTracks

public static void deliverTracks(int requestId, int audio, int video, String error)
A port’s answer to openUserMedia: the track handles, 0 for none, or both 0 and the reason.

deliverResult

public static void deliverResult(int requestId, String error)
A port’s answer to setDescription: null for success, else the reason.

iceCandidate

public static void iceCandidate(int peer, String sdpMid, int sdpMLineIndex, String candidate)
A candidate a peer connection gathered.

stateChanged

public static void stateChanged(int peer, String state)
A peer connection moved to state, one of the names on PeerConnection.

remoteTrack

public static void remoteTrack(int peer, int track, boolean video)
The other side’s track arrived on a peer connection.