Class StreamPeerConnection

  • All Implemented Interfaces:
    org.webrtc.PeerConnection.Observer

    
    public final class StreamPeerConnection
     implements PeerConnection.Observer
                        

    Wrapper around the WebRTC connection that contains tracks.

    • Constructor Detail

      • StreamPeerConnection

        StreamPeerConnection(CoroutineScope coroutineScope, StreamPeerType type, MediaConstraints mediaConstraints, Function1<MediaStream, Unit> onStreamAdded, Function2<StreamPeerConnection, StreamPeerType, Unit> onNegotiationNeeded, Function2<IceCandidate, StreamPeerType, Unit> onIceCandidate, Integer maxBitRate)
        Parameters:
        coroutineScope - The scope used to listen to stats events.
        type - The internal type of the PeerConnection.
        mediaConstraints - Constraints used for the connections.
        onStreamAdded - Handler when a new MediaStream gets added.
        onNegotiationNeeded - Handler when there's a new negotiation.
        onIceCandidate - Handler whenever we receive IceCandidates.
    • Method Detail

      • getConnection

         final PeerConnection getConnection()

        The wrapped connection for all the WebRTC communication.

      • getVideoTransceiver

         final RtpTransceiver getVideoTransceiver()

        Transceiver used to send video in different resolutions.

      • getAudioTransceiver

         final RtpTransceiver getAudioTransceiver()

        Transceiver used to send audio.

      • initialize

         final Unit initialize(PeerConnection peerConnection)

        Initialize a StreamPeerConnection using a WebRTC PeerConnection.

        Parameters:
        peerConnection - The connection that holds audio and video tracks.
      • createOffer

         final Result<SessionDescription> createOffer()

        Used to create an offer whenever there's a negotiation that we need to process on the publisher side.

        Returns:

        Result wrapper of the SessionDescription for the publisher.

      • createAnswer

         final Result<SessionDescription> createAnswer()

        Used to create an answer whenever there's a subscriber offer.

        Returns:

        Result wrapper of the SessionDescription for the subscriber.

      • setRemoteDescription

         final Result<Unit> setRemoteDescription(SessionDescription sessionDescription)

        Used to set up the SDP on underlying connections and to add pendingIceCandidates to the connection for listening.

        Parameters:
        sessionDescription - That contains the remote SDP.
        Returns:

        An empty Result, if the operation has been successful or not.

      • setLocalDescription

         final Result<Unit> setLocalDescription(SessionDescription sessionDescription)

        Sets the local description for a connection either for the subscriber or publisher based on the flow.

        Parameters:
        sessionDescription - That contains the subscriber or publisher SDP.
        Returns:

        An empty Result, if the operation has been successful or not.

      • addIceCandidate

         final Result<Unit> addIceCandidate(IceCandidate iceCandidate)

        Adds an IceCandidate to the underlying connection if it's already been set up, or stores it for later consumption.

        Parameters:
        iceCandidate - To process and add to the connection.
        Returns:

        An empty Result, if the operation has been successful or not.

      • addAudioTransceiver

         final Unit addAudioTransceiver(MediaStreamTrack track, List<String> streamIds)

        Adds a local MediaStreamTrack with audio to a given connection, with its streamIds. The audio is then sent through a transceiver.

        Parameters:
        track - The track that contains audio.
        streamIds - The IDs that represent the stream tracks.
      • addVideoTransceiver

         final Unit addVideoTransceiver(MediaStreamTrack track, List<String> streamIds, Boolean isScreenShare)

        Adds a local MediaStreamTrack with video to a given connection, with its streamIds. The video is then sent in a few different resolutions using simulcast.

        Parameters:
        track - The track that contains video.
        streamIds - The IDs that represent the stream tracks.
      • onIceCandidate

         Unit onIceCandidate(IceCandidate candidate)

        Triggered whenever there's a new RtcIceCandidate for the call. Used to update our tracks and subscriptions.

        Parameters:
        candidate - The new candidate.
      • onAddStream

         Unit onAddStream(MediaStream stream)

        Triggered whenever there's a new MediaStream that was added to the connection.

        Parameters:
        stream - The stream that contains audio or video.
      • onAddTrack

         Unit onAddTrack(RtpReceiver receiver, Array<out MediaStream> mediaStreams)

        Triggered whenever there's a new MediaStream or MediaStreamTrack that's been added to the call. It contains all audio and video tracks for a given session.

        Parameters:
        receiver - The receiver of tracks.
        mediaStreams - The streams that were added containing their appropriate tracks.
      • onRenegotiationNeeded

         Unit onRenegotiationNeeded()

        Triggered whenever there's a new negotiation needed for the active PeerConnection.

      • onRemoveStream

         Unit onRemoveStream(MediaStream stream)

        Triggered whenever a MediaStream was removed.

        Parameters:
        stream - The stream that was removed from the connection.
      • onConnectionChange

         Unit onConnectionChange(PeerConnection.PeerConnectionState newState)

        Triggered when the connection state changes. Used to start and stop the stats observing.

        Parameters:
        newState - The new state of the PeerConnection.
      • onRemoveTrack

         Unit onRemoveTrack(RtpReceiver receiver)

        Domain - PeerConnection and PeerConnection.Observer related callbacks.