docs.unity3d.com
    Show / Hide Table of Contents

    Class RTCPeerConnection

    Represents a WebRTC connection between the local peer and remote peer.

    Inheritance
    Object
    RTCPeerConnection
    Namespace: Unity.WebRTC
    Syntax
    public class RTCPeerConnection : IDisposable

    Constructors

    RTCPeerConnection()

    This constructor creates an instance of peer connection with a default configuration.

    Declaration
    public RTCPeerConnection()
    See Also
    RTCPeerConnection(ref RTCConfiguration)

    RTCPeerConnection(ref RTCConfiguration)

    This constructor creates an instance of peer connection with a configuration provided by user. An RTCConfiguration object providing options to configure the new connection.

    Declaration
    public RTCPeerConnection(ref RTCConfiguration configuration)
    Parameters
    Type Name Description
    RTCConfiguration configuration
    See Also
    RTCPeerConnection()

    Properties

    ConnectionState

    The readonly property of the RTCPeerConnection indicates the current state of the peer connection by returning one of the RTCPeerConnectionState enum.

    Declaration
    public RTCPeerConnectionState ConnectionState { get; }
    Property Value
    Type Description
    RTCPeerConnectionState
    Examples
    var peerConnection = new RTCPeerConnection(configuration);
    var connectionState = peerConnection.ConnectionState;
    See Also
    IceConnectionState

    CurrentLocalDescription

    Declaration
    public RTCSessionDescription CurrentLocalDescription { get; }
    Property Value
    Type Description
    RTCSessionDescription

    CurrentRemoteDescription

    Declaration
    public RTCSessionDescription CurrentRemoteDescription { get; }
    Property Value
    Type Description
    RTCSessionDescription

    GatheringState

    Declaration
    public RTCIceGatheringState GatheringState { get; }
    Property Value
    Type Description
    RTCIceGatheringState

    IceConnectionState

    The readonly property of the RTCPeerConnection indicates the current state of the peer connection by returning one of the RTCIceConnectionState enum.

    Declaration
    public RTCIceConnectionState IceConnectionState { get; }
    Property Value
    Type Description
    RTCIceConnectionState
    Examples
    var peerConnection = new RTCPeerConnection(configuration);
    var iceConnectionState = peerConnection.IceConnectionState;
    See Also
    ConnectionState

    LocalDescription

    Declaration
    public RTCSessionDescription LocalDescription { get; }
    Property Value
    Type Description
    RTCSessionDescription

    OnConnectionStateChange

    Declaration
    public DelegateOnConnectionStateChange OnConnectionStateChange { get; set; }
    Property Value
    Type Description
    DelegateOnConnectionStateChange

    OnDataChannel

    Declaration
    public DelegateOnDataChannel OnDataChannel { get; set; }
    Property Value
    Type Description
    DelegateOnDataChannel
    See Also
    RTCDataChannel

    OnIceCandidate

    Declaration
    public DelegateOnIceCandidate OnIceCandidate { get; set; }
    Property Value
    Type Description
    DelegateOnIceCandidate
    See Also
    RTCIceCandidate

    OnIceConnectionChange

    This property is delegate to be called when the IceConnectionState is changed.

    Declaration
    public DelegateOnIceConnectionChange OnIceConnectionChange { get; set; }
    Property Value
    Type Description
    DelegateOnIceConnectionChange

    A delegate containing IceConnectionState.

    Examples
    peerConnection.OnIceConnectionChange = iceConnectionState =>
    {
        ...
    };
    See Also
    IceConnectionState

    OnIceGatheringStateChange

    Declaration
    public DelegateOnIceGatheringStateChange OnIceGatheringStateChange { get; set; }
    Property Value
    Type Description
    DelegateOnIceGatheringStateChange
    See Also
    GatheringState

    OnNegotiationNeeded

    Declaration
    public DelegateOnNegotiationNeeded OnNegotiationNeeded { get; set; }
    Property Value
    Type Description
    DelegateOnNegotiationNeeded

    OnTrack

    Declaration
    public DelegateOnTrack OnTrack { get; set; }
    Property Value
    Type Description
    DelegateOnTrack
    See Also
    RTCTrackEvent

    PendingLocalDescription

    Declaration
    public RTCSessionDescription PendingLocalDescription { get; }
    Property Value
    Type Description
    RTCSessionDescription

    PendingRemoteDescription

    Declaration
    public RTCSessionDescription PendingRemoteDescription { get; }
    Property Value
    Type Description
    RTCSessionDescription

    RemoteDescription

    Declaration
    public RTCSessionDescription RemoteDescription { get; }
    Property Value
    Type Description
    RTCSessionDescription

    SignalingState

    The readonly property of the RTCPeerConnection indicates the current state of the peer connection by returning one of the RTCSignalingState enum.

    Declaration
    public RTCSignalingState SignalingState { get; }
    Property Value
    Type Description
    RTCSignalingState
    Examples
    var peerConnection = new RTCPeerConnection(configuration);
    var signalingState = peerConnection.SignalingState;
    See Also
    ConnectionState

    Methods

    AddIceCandidate(RTCIceCandidate)

    Declaration
    public bool AddIceCandidate(RTCIceCandidate candidate)
    Parameters
    Type Name Description
    RTCIceCandidate candidate
    Returns
    Type Description
    Boolean

    AddTrack(MediaStreamTrack, MediaStream)

    Declaration
    public RTCRtpSender AddTrack(MediaStreamTrack track, MediaStream stream = null)
    Parameters
    Type Name Description
    MediaStreamTrack track
    MediaStream stream
    Returns
    Type Description
    RTCRtpSender
    See Also
    RemoveTrack(RTCRtpSender)

    AddTransceiver(MediaStreamTrack, RTCRtpTransceiverInit)

    Declaration
    public RTCRtpTransceiver AddTransceiver(MediaStreamTrack track, RTCRtpTransceiverInit init = null)
    Parameters
    Type Name Description
    MediaStreamTrack track
    RTCRtpTransceiverInit init
    Returns
    Type Description
    RTCRtpTransceiver

    AddTransceiver(TrackKind, RTCRtpTransceiverInit)

    Declaration
    public RTCRtpTransceiver AddTransceiver(TrackKind kind, RTCRtpTransceiverInit init = null)
    Parameters
    Type Name Description
    TrackKind kind
    RTCRtpTransceiverInit init
    Returns
    Type Description
    RTCRtpTransceiver

    Close()

    Declaration
    public void Close()
    See Also
    Dispose()

    CreateAnswer()

    Declaration
    public RTCSessionDescriptionAsyncOperation CreateAnswer()
    Returns
    Type Description
    RTCSessionDescriptionAsyncOperation

    CreateAnswer(ref RTCOfferAnswerOptions)

    Create an SDP (Session Description Protocol) answer to start a new connection to a remote peer.

    Declaration
    public RTCSessionDescriptionAsyncOperation CreateAnswer(ref RTCOfferAnswerOptions options)
    Parameters
    Type Name Description
    RTCOfferAnswerOptions options
    Returns
    Type Description
    RTCSessionDescriptionAsyncOperation

    CreateDataChannel(String, RTCDataChannelInit)

    Creates a new data channel related the remote peer.

    Declaration
    public RTCDataChannel CreateDataChannel(string label, RTCDataChannelInit options = null)
    Parameters
    Type Name Description
    String label

    A string for the data channel. This string may be checked by Label.

    RTCDataChannelInit options

    A struct provides configuration options for the data channel.

    Returns
    Type Description
    RTCDataChannel

    A new data channel.

    CreateOffer()

    Declaration
    public RTCSessionDescriptionAsyncOperation CreateOffer()
    Returns
    Type Description
    RTCSessionDescriptionAsyncOperation

    CreateOffer(ref RTCOfferAnswerOptions)

    Create an SDP (Session Description Protocol) offer to start a new connection to a remote peer.

    Declaration
    public RTCSessionDescriptionAsyncOperation CreateOffer(ref RTCOfferAnswerOptions options)
    Parameters
    Type Name Description
    RTCOfferAnswerOptions options
    Returns
    Type Description
    RTCSessionDescriptionAsyncOperation
    See Also
    CreateAnswer(ref RTCOfferAnswerOptions)

    Dispose()

    Declaration
    public void Dispose()
    Implements
    IDisposable.Dispose()

    Finalize()

    Declaration
    protected void Finalize()

    GetConfiguration()

    Returns an object which indicates the current configuration of the RTCPeerConnection.

    Declaration
    public RTCConfiguration GetConfiguration()
    Returns
    Type Description
    RTCConfiguration

    An object describing the RTCPeerConnection's current configuration.

    Examples
    var configuration = myPeerConnection.GetConfiguration();
    if(configuration.urls.length == 0)
    {
        configuration.urls = new[] {"stun:stun.l.google.com:19302"};
    }
    myPeerConnection.SetConfiguration(configuration);
    See Also
    SetConfiguration(ref RTCConfiguration)

    GetReceivers()

    Returns array of objects each of which represents one RTP receiver.

    Declaration
    public IEnumerable<RTCRtpReceiver> GetReceivers()
    Returns
    Type Description
    IEnumerable<RTCRtpReceiver>

    Array of the senders

    Examples
    var senders = peerConnection.GetReceivers();
    See Also
    GetSenders()
    GetTransceivers()

    GetSenders()

    Returns array of objects each of which represents one RTP sender.

    Declaration
    public IEnumerable<RTCRtpSender> GetSenders()
    Returns
    Type Description
    IEnumerable<RTCRtpSender>

    Array of the receivers

    Examples
    var senders = peerConnection.GetSenders();
    See Also
    GetReceivers()
    GetTransceivers()

    GetStats()

    Returns an AsyncOperation which resolves with data providing statistics.

    Declaration
    public RTCStatsReportAsyncOperation GetStats()
    Returns
    Type Description
    RTCStatsReportAsyncOperation

    An AsyncOperation which resolves with an RTCStatsReport object providing connection statistics.

    Examples
    // Already instantiated peerConnection as RTCPeerConnection.
    var operation = peerConnection.GetStats();
    yield return operation;
    
    if (!operation.IsError)
    {
        var report = operation.Value;
        foreach (var stat in report.Stats.Values)
        {
            Debug.Log(stat.Type.ToString());
        }
    }
    See Also
    RTCStatsReport

    GetTransceivers()

    Returns array of objects each of which represents one RTP transceiver.

    Declaration
    public IEnumerable<RTCRtpTransceiver> GetTransceivers()
    Returns
    Type Description
    IEnumerable<RTCRtpTransceiver>

    Array of the transceivers

    Examples
    var transceivers = peerConnection.GetTransceivers();
    See Also
    GetSenders()
    GetReceivers()

    RemoveTrack(RTCRtpSender)

    Declaration
    public RTCErrorType RemoveTrack(RTCRtpSender sender)
    Parameters
    Type Name Description
    RTCRtpSender sender
    Returns
    Type Description
    RTCErrorType
    See Also
    AddTrack(MediaStreamTrack, MediaStream)

    RestartIce()

    Declaration
    public void RestartIce()

    SetConfiguration(ref RTCConfiguration)

    This method sets the current configuration of the RTCPeerConnection This lets you change the ICE servers used by the connection and which transport policies to use.

    Declaration
    public RTCErrorType SetConfiguration(ref RTCConfiguration configuration)
    Parameters
    Type Name Description
    RTCConfiguration configuration

    The changes are not additive; instead, the new values completely replace the existing ones.

    Returns
    Type Description
    RTCErrorType

    Error code.

    Examples
    var configuration = new RTCConfiguration
    {
        iceServers = new[]
        {
            new RTCIceServer
            {
                urls = new[] {"stun:stun.l.google.com:19302"},
                username = "",
                credential = "",
                credentialType = RTCIceCredentialType.Password
            }
        }
    };
    var error = myPeerConnection.SetConfiguration(ref configuration);
    if(error == RTCErrorType.None)
    {
        ...
    }
    See Also
    GetConfiguration()

    SetLocalDescription()

    Declaration
    public RTCSetSessionDescriptionAsyncOperation SetLocalDescription()
    Returns
    Type Description
    RTCSetSessionDescriptionAsyncOperation

    SetLocalDescription(ref RTCSessionDescription)

    This method changes the session description of the local connection to negotiate with other connections.

    Declaration
    public RTCSetSessionDescriptionAsyncOperation SetLocalDescription(ref RTCSessionDescription desc)
    Parameters
    Type Name Description
    RTCSessionDescription desc
    Returns
    Type Description
    RTCSetSessionDescriptionAsyncOperation

    An AsyncOperation which resolves with an RTCSessionDescription object providing a description of the session.

    Exceptions
    Type Condition
    ArgumentException

    Thrown when an argument has an invalid value. For example, when passed the sdp which is null or empty.

    RTCErrorException

    Thrown when an argument has an invalid value. For example, when passed the sdp which is not be able to parse.

    See Also
    LocalDescription

    SetRemoteDescription(ref RTCSessionDescription)

    This method changes the session description of the remote connection to negotiate with local connections.

    Declaration
    public RTCSetSessionDescriptionAsyncOperation SetRemoteDescription(ref RTCSessionDescription desc)
    Parameters
    Type Name Description
    RTCSessionDescription desc
    Returns
    Type Description
    RTCSetSessionDescriptionAsyncOperation

    An AsyncOperation which resolves with an RTCSessionDescription object providing a description of the session.

    Exceptions
    Type Condition
    ArgumentException

    Thrown when an argument has an invalid value. For example, when passed the sdp which is null or empty.

    RTCErrorException

    Thrown when an argument has an invalid value. For example, when passed the sdp which is not be able to parse.

    See Also
    RemoteDescription
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023