Class PeerConnection

java.lang.Object
tel.schich.libdatachannel.PeerConnection
All Implemented Interfaces:
Closeable, AutoCloseable

public class PeerConnection extends Object implements Closeable
  • Field Details

  • Method Details

    • createPeer

      public static PeerConnection createPeer(PeerConnectionConfiguration config, Executor executor)
      Creates a Peer Connection. Remember to close() when done.
      Parameters:
      config - the peer configuration
      executor - the executor to run callbacks on
      Returns:
      the peer connection
    • createPeer

      public static PeerConnection createPeer(PeerConnectionConfiguration config, Executor executor, DtlsIdentity identity)
    • createPeer

      public static PeerConnection createPeer(PeerConnectionConfiguration config, Executor executor, @Nullable Path certificate, @Nullable Path key)
      Creates a peer using a paired PEM DTLS certificate/key, or the default identity if both null.
    • createPeer

      public static PeerConnection createPeer(PeerConnectionConfiguration config, Executor executor, @Nullable Path certificate, @Nullable Path key, @Nullable String keyPassword)
      Imports an endpoint identity using the upstream native certificate configuration.
    • nativeCreationAttempts

      public static long nativeCreationAttempts()
      Diagnostic count at native peer construction, including failed attempts.
    • createPeer

      public static PeerConnection createPeer(PeerConnectionConfiguration config)
    • dropTrackState

      public void dropTrackState(int trackHandle)
    • close

      public void close()
      If it is not already closed, the Peer Connection is implicitly closed before being deleted. After this function has been called, pc must not be used in a function call anymore. This function will block until all scheduled callbacks of pc return (except the one this function might be called in) and no other callback will be called for pc after it returns.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • closeAndAwait

      public boolean closeAndAwait(Duration timeout)
      Force-close and await native transport teardown before deleting the peer. Call only from an external owner thread, never a native callback. A timeout returns false and retains ownership so the caller can retry or fail closed.
    • closeAsync

      public CompletionStage<Void> closeAsync()
      Starts native closure without blocking a worker on teardown. Safe from event callbacks. Completion follows transport destruction and Java handle cleanup. A peer still being initialized remains owned by its incoming-request handler.
    • closeChannels

      public void closeChannels()
      Closes all Data Channels.
    • setLocalDescription

      public void setLocalDescription(@Nullable String type, String ufrag, String password)
      Installs the exact local ICE credentials before gathering starts.
    • setLocalDescription

      public void setLocalDescription(@Nullable String type)
      Initiates the handshake process.

      Following this call, the local description callback will be called with the local description, which must be sent to the remote peer by the user's method of choice.

      Note this call is implicit after setRemoteDescription(java.lang.String, tel.schich.libdatachannel.SessionDescriptionType) and createDataChannel(java.lang.String) if disableAutoNegotiation was not set on Peer Connection creation.

      Parameters:
      type - (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for autodetection.
    • localDescription

      public String localDescription()
      Retrieves the current local description in SDP format.
      Returns:
      the current local description
    • localDescriptionType

      public String localDescriptionType()
      Retrieves the current local description type as string. See setLocalDescription(java.lang.String, java.lang.String, java.lang.String).
      Returns:
      the current local description type
    • setRemoteDescription

      public void setRemoteDescription(String sdp, @Nullable SessionDescriptionType type)
      Sets the remote description received from the remote peer by the user's method of choice. The remote description may have candidates or not.

      If the remote description is an offer and disableAutoNegotiation was not set in rtcConfiguration, the library will automatically answer by calling rtcSetLocalDescription internally. Otherwise, the user must call it to answer the remote description.

      Parameters:
      sdp - the remote Session Description Protocol
      type - (optional): type of the description ("offer", "answer", "pranswer", or "rollback") or NULL for autodetection.
    • remoteDescription

      public String remoteDescription()
      Retrieves the current remote description in SDP format.
      Returns:
      the current remote description
    • remoteDescriptionType

      public @Nullable SessionDescriptionType remoteDescriptionType()
      Retrieves the current remote description type as string.
      Returns:
      the current remote description type
    • addRemoteCandidate

      public void addRemoteCandidate(String candidate)
      Adds a trickled remote candidate received from the remote peer by the user's method of choice. The Peer Connection must have a remote description set.
      Parameters:
      candidate - a null-terminated SDP string representing the candidate (with or without the "a=" prefix)
    • addRemoteCandidate

      public void addRemoteCandidate(String candidate, @Nullable String mediaId)
      Adds a trickled remote candidate received from the remote peer by the user's method of choice. The Peer Connection must have a remote description set.
      Parameters:
      candidate - a null-terminated SDP string representing the candidate (with or without the "a=" prefix)
      mediaId - (optional): a null-terminated string representing the mediaId of the candidate in the remote SDP description or NULL for autodetection
    • localAddress

      public InetSocketAddress localAddress()
      Retrieves the current local address, i.e. the network address of the currently selected local candidate. The address will have the format "IP_ADDRESS:PORT", where IP_ADDRESS may be either IPv4 or IPv6. The call might fail if the PeerConnection is not in state RTC_CONNECTED, and the address might change after connection.
      Returns:
      the local address
    • remoteAddress

      public InetSocketAddress remoteAddress()
      Retrieves the current remote address, i.e. the network address of the currently selected remote candidate. The address will have the format "IP_ADDRESS:PORT", where IP_ADDRESS may be either IPv4 or IPv6. The call may fail if the state is not RTC_CONNECTED, and the address might change after connection.
      Returns:
      the remote address
    • selectedCandidatePair

      public CandidatePair selectedCandidatePair()
      Retrieves the currently selected candidate pair. The call may fail if the state is not RTC_CONNECTED, and the selected candidate pair might change after connection.
      Returns:
      the selected candidate pair
    • maxDataChannelStream

      public int maxDataChannelStream()
      Retrieves the maximum stream ID a Data Channel may use. It is useful to create user-negotiated Data Channels with negotiated=true and manualStream=true. The maximum is negotiated during connection, therefore the final value after connection might be lower than before connection if the remote maximum is lower.
      Returns:
      maximum stream ID
    • remoteMaxMessageSize

      public int remoteMaxMessageSize()
      Retrieves the maximum message size for data channels on the peer connection as negotiated with the remote peer.
      Returns:
      the maximum message size
    • setAnswer

      public void setAnswer(String sdp)
    • createDataChannel

      public DataChannel createDataChannel(String label)
      Adds a Data Channel on a Peer Connection. The Peer Connection does not need to be connected, however, the Data Channel will be open only when the Peer Connection is connected.

      rtcDataChannel() is equivalent to rtcDataChannelEx() with settings set to ordered, reliable, non-negotiated, with automatic stream ID selection (all flags set to false), and protocol set to an empty string.

      Parameters:
      label - a user-defined UTF-8 string representing the Data Channel name
      Returns:
      the created data channel
    • createDataChannel

      public DataChannel createDataChannel(String label, DataChannelInitSettings init)
      Adds a Data Channel on a Peer Connection. The Peer Connection does not need to be connected, however, the Data Channel will be open only when the Peer Connection is connected.
      Parameters:
      label - a user-defined UTF-8 string representing the Data Channel name
      init - a structure of initialization settings
      Returns:
      the created data channel
    • addTrack

      public Track addTrack(String sdp)
    • addTrack

      public Track addTrack(TrackInit init)
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object