Class 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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic PeerConnectioncreatePeerConnection(PeerConnectionConfig config, PeerConnection.Listener listener) Creates a peer connection, which reports tolisteneron the EDT.static voiddeliverDescription(int requestId, String type, String sdp, String error) A port's answer tocreateDescription: the description, or a nullsdpand the reason.static voiddeliverResult(int requestId, String error) A port's answer tosetDescription: null for success, else the reason.static voiddeliverTracks(int requestId, int audio, int video, String error) A port's answer toopenUserMedia: the track handles, 0 for none, or both 0 and the reason.static voidiceCandidate(int peer, String sdpMid, int sdpMLineIndex, String candidate) A candidate a peer connection gathered.static booleanWhether this platform carries WebRTC media.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.static voidremoteTrack(int peer, int track, boolean video) The other side's track arrived on a peer connection.static voidsetSpeakerphone(boolean on) Routes call audio to the loudspeaker, or with false back to the earpiece or headset.static voidstateChanged(int peer, String state) A peer connection moved tostate, one of the names onPeerConnection.
-
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
frontCamera: which camera to open first; seeMediaTrack.switchCamera()
-
createPeerConnection
public static PeerConnection createPeerConnection(PeerConnectionConfig config, PeerConnection.Listener listener) Creates a peer connection, which reports tolisteneron 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
-
deliverTracks
A port's answer toopenUserMedia: the track handles, 0 for none, or both 0 and the reason. -
deliverResult
A port's answer tosetDescription: null for success, else the reason. -
iceCandidate
-
stateChanged
A peer connection moved tostate, one of the names onPeerConnection. -
remoteTrack
public static void remoteTrack(int peer, int track, boolean video) The other side's track arrived on a peer connection.
-