Class PeerConnection
java.lang.Object
com.codename1.webrtc.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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceWhat a peer connection tells the app as the call proceeds. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringgetState()afterclose().static final StringgetState()while media flows.static final StringgetState()while a path is being tried.static final StringgetState()after the path was lost; it may come back by itself.static final StringgetState()when no path could be found or it was lost for good.static final StringgetState()before the two sides found each other. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddIceCandidate(IceCandidate candidate) Tries a path the other side found.voidaddTrack(MediaTrack track) Sends a local track to the other side.voidclose()Ends the connection.Describes this side in answer to the remote offer already set.Describes this side for the caller's half of the exchange.getState()setLocalDescription(SessionDescription description) setRemoteDescription(SessionDescription description)
-
Field Details
-
NEW
-
CONNECTING
-
CONNECTED
-
DISCONNECTED
getState()after the path was lost; it may come back by itself.- See Also:
-
FAILED
getState()when no path could be found or it was lost for good.- See Also:
-
CLOSED
-
-
Method Details
-
getState
-
addTrack
Sends a local track to the other side. Add every track before creating the offer or the answer. -
createOffer
Describes this side for the caller's half of the exchange. -
createAnswer
Describes this side in answer to the remote offer already set. -
setLocalDescription
-
setRemoteDescription
-
addIceCandidate
Tries a path the other side found. A candidate that arrives before the remote description is set is held by the port until it is. -
close
public void close()Ends the connection. Local tracks stay open: release them separately.
-