Class WebRtc

java.lang.Object
com.codename1.webrtc.WebRtc

public final class WebRtc extends Object

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.

  • 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
    • 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.