Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class PeerConnection
- Object
- PeerConnection
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())));
});
Nested types
interface PeerConnection.Listener | What a peer connection tells the app as the call proceeds. |
Fields
public static final String NEW = "new" | getState() before the two sides found each other. |
public static final String CONNECTING = "connecting" | getState() while a path is being tried. |
public static final String CONNECTED = "connected" | getState() while media flows. |
public static final String DISCONNECTED = "disconnected" | getState() after the path was lost; it may come back by itself. |
public static final String FAILED = "failed" | getState() when no path could be found or it was lost for good. |
public static final String CLOSED = "closed" | getState() after close(). |
Methods
public String getState() | |
public void addTrack(MediaTrack track) | Sends a local track to the other side. |
public AsyncResource<SessionDescription> createOffer() | Describes this side for the caller’s half of the exchange. |
public AsyncResource<SessionDescription> createAnswer() | Describes this side in answer to the remote offer already set. |
public AsyncResource<Boolean> setLocalDescription(SessionDescription description) | |
public AsyncResource<Boolean> setRemoteDescription(SessionDescription description) | |
public void addIceCandidate(IceCandidate candidate) | Tries a path the other side found. |
public void restartIce() | Gathers new paths on the next offer, for a connection whose path was lost – the device changed network, say, and the addresses the two sides agreed on no longer reach it. |
public void close() | Ends the connection. |
Inherited methods
Field details
NEW
public static final String NEW = "new"getState() before the two sides found each other.CONNECTING
public static final String CONNECTING = "connecting"getState() while a path is being tried.CONNECTED
public static final String CONNECTED = "connected"getState() while media flows.DISCONNECTED
public static final String DISCONNECTED = "disconnected"getState() after the path was lost; it may come back by itself.
A phone moving from Wi-Fi to mobile data lands here, and the path it
had is usually gone for good: see restartIce().FAILED
public static final String FAILED = "failed"getState() when no path could be found or it was lost for good.CLOSED
public static final String CLOSED = "closed"getState() after close().Method details
getState
public String getState()addTrack
public void addTrack(MediaTrack track)createOffer
public AsyncResource<SessionDescription> createOffer()createAnswer
public AsyncResource<SessionDescription> createAnswer()setLocalDescription
public AsyncResource<Boolean> setLocalDescription(SessionDescription description)setRemoteDescription
public AsyncResource<Boolean> setRemoteDescription(SessionDescription description)addIceCandidate
public void addIceCandidate(IceCandidate candidate)restartIce
public void restartIce()Gathers new paths on the next offer, for a connection whose path was lost – the device changed network, say, and the addresses the two sides agreed on no longer reach it.
Nothing changes until the offer is exchanged: call this, then
createOffer(), and send the offer over the app’s signalling as at
the start of the call. The other side answers it with
setRemoteDescription(SessionDescription) and createAnswer() and
sends that back; tracks, views and the call keep going, and the
connection reports CONNECTED again once a new path is found.
Only one side restarts – by convention the one that made the first
offer – or both offer at once and neither offer can be answered.
DISCONNECTED is the cue to show “reconnecting”, and to restart if
it does not recover within a second or two; FAILED is the cue to
restart at once.
close
public void close()