Skip to main content

Overview

The PeerConnection represents a WebRTC connection that establishes peer-to-peer communications with another PeerConnection instance in a browser, or to another endpoint implementing the required protocols.
Source: peerconnection.go:33-98

Type Definition

Constructors

NewPeerConnection

Creates a PeerConnection with the default codecs and interceptors.
Source: peerconnection.go:100-109
Configuration
Configuration for the new PeerConnection
*PeerConnection
The newly created PeerConnection
error
Error if creation fails, nil otherwise
If you wish to customize the set of available codecs and/or the set of active interceptors, create an API with a custom MediaEngine and/or interceptor.Registry, then call api.NewPeerConnection() instead.

Offer/Answer Methods

CreateOffer

Starts the PeerConnection and generates the local description.
Source: peerconnection.go:665-797
*OfferOptions
Optional offer options (e.g., ICERestart)
SessionDescription
The generated SDP offer
error
Error if offer creation fails

CreateAnswer

Starts the PeerConnection and generates the local description in response to an offer.
Source: peerconnection.go:893-965
*AnswerOptions
Optional answer options
SessionDescription
The generated SDP answer
error
Error if answer creation fails, or if no remote description is set

SetLocalDescription

Sets the SessionDescription of the local peer.
Source: peerconnection.go:1087-1146
SessionDescription
The local session description to set
error
Error if the description is invalid or connection is closed
According to JSEP 5.4, if desc.SDP is empty, the last created offer or answer will be used based on desc.Type.

SetRemoteDescription

Sets the SessionDescription of the remote peer.
Source: peerconnection.go:1160-1369
SessionDescription
The remote session description to set
error
Error if the description is invalid or connection is closed

Session Description Accessors

LocalDescription

Returns PendingLocalDescription if it is not null, otherwise returns CurrentLocalDescription.
Source: peerconnection.go:1148-1158
*SessionDescription
The current local description, or nil

RemoteDescription

Returns pendingRemoteDescription if it is not null, otherwise returns currentRemoteDescription.
Source: peerconnection.go:2056-2069
*SessionDescription
The current remote description, or nil

CurrentLocalDescription

Represents the local description that was successfully negotiated the last time the PeerConnection transitioned into the stable state.
Source: peerconnection.go:2606-2619

PendingLocalDescription

Represents a local description that is in the process of being negotiated. Returns nil if the PeerConnection is in the stable state.
Source: peerconnection.go:2621-2634

CurrentRemoteDescription

Represents the last remote description that was successfully negotiated.
Source: peerconnection.go:2636-2645

PendingRemoteDescription

Represents a remote description that is in the process of being negotiated.
Source: peerconnection.go:2647-2657

ICE Methods

AddICECandidate

Accepts an ICE candidate string and adds it to the existing set of candidates.
Source: peerconnection.go:2071-2116
ICECandidateInit
The ICE candidate to add
error
Error if no remote description is set or candidate is invalid

OnICECandidate

Sets an event handler which is invoked when a new ICE candidate is found.
Source: peerconnection.go:456-464
func(*ICECandidate)
Handler called with each new ICE candidate (nil when gathering is finished)
ICE candidate gathering only begins when SetLocalDescription or SetRemoteDescription is called. The handler will be called with a nil pointer when gathering is finished.

OnICEGatheringStateChange

Sets an event handler which is invoked when the ICE candidate gathering state has changed.
Source: peerconnection.go:466-480
func(ICEGatheringState)
Handler called when gathering state changes

OnICEConnectionStateChange

Sets an event handler which is called when an ICE connection state is changed.
Source: peerconnection.go:505-509
func(ICEConnectionState)
Handler called when ICE connection state changes

Media Methods

AddTrack

Adds a Track to the PeerConnection.
Source: peerconnection.go:2181-2219
TrackLocal
The local track to add
*RTPSender
The RTPSender for the added track
error
Error if the connection is closed or track cannot be added

RemoveTrack

Removes a Track from the PeerConnection.
Source: peerconnection.go:2221-2247
*RTPSender
The RTPSender to remove
error
Error if the connection is closed or sender not found

OnTrack

Sets an event handler which is called when remote track arrives from a remote peer.
Source: peerconnection.go:482-488
func(*TrackRemote, *RTPReceiver)
Handler called for each incoming remote track

AddTransceiverFromKind

Creates a new RTPTransceiver and adds it to the set of transceivers.
Source: peerconnection.go:2284-2329
RTPCodecType
The kind of media (Audio or Video)
...RTPTransceiverInit
Optional initialization parameters
*RTPTransceiver
The created transceiver
error
Error if creation fails

AddTransceiverFromTrack

Creates a new RTPTransceiver (SendRecv or SendOnly) and adds it to the set of transceivers.
Source: peerconnection.go:2331-2355
TrackLocal
The local track for the transceiver
...RTPTransceiverInit
Optional initialization parameters
*RTPTransceiver
The created transceiver
error
Error if creation fails

GetSenders

Returns the RTPSender that are currently attached to this PeerConnection.
Source: peerconnection.go:2145-2157
[]*RTPSender
Array of RTPSenders

GetReceivers

Returns the RTPReceivers that are currently attached to this PeerConnection.
Source: peerconnection.go:2159-2171
[]*RTPReceiver
Array of RTPReceivers

GetTransceivers

Returns the RTPTransceivers that are currently attached to this PeerConnection.
Source: peerconnection.go:2173-2179
[]*RTPTransceiver
Array of RTPTransceivers

Data Channel Methods

CreateDataChannel

Creates a new DataChannel object with the given label and optional DataChannelInit.
Source: peerconnection.go:2357-2442
string
Label for the data channel
*DataChannelInit
Optional configuration for the data channel
*DataChannel
The created data channel
error
Error if creation fails or connection is closed

OnDataChannel

Sets an event handler which is invoked when a data channel message arrives from a remote peer.
Source: peerconnection.go:298-304
func(*DataChannel)
Handler called for each incoming data channel

State Accessors

SignalingState

Returns the signaling state of the PeerConnection instance.
Source: peerconnection.go:2668-2672
SignalingState
Current signaling state

ICEGatheringState

Returns the ICE gathering state of the PeerConnection instance.
Source: peerconnection.go:2674-2689
ICEGatheringState
Current ICE gathering state

ICEConnectionState

Returns the ICE connection state of the PeerConnection instance.
Source: peerconnection.go:2135-2143
ICEConnectionState
Current ICE connection state

ConnectionState

Returns the connection state of the PeerConnection instance.
Source: peerconnection.go:2691-2699
PeerConnectionState
Current peer connection state

CanTrickleICECandidates

Reports whether the remote endpoint indicated support for receiving trickled ICE candidates.
Source: peerconnection.go:2659-2666
ICETrickleCapability
Trickle ICE capability (Unknown, Supported, or Unsupported)

Event Handlers

OnSignalingStateChange

Sets an event handler which is invoked when the peer connection’s signaling state changes.
Source: peerconnection.go:279-285
func(SignalingState)
Handler called when signaling state changes

OnConnectionStateChange

Sets an event handler which is called when the PeerConnectionState has changed.
Source: peerconnection.go:519-523
func(PeerConnectionState)
Handler called when connection state changes

OnNegotiationNeeded

Sets an event handler which is invoked when a change has occurred which requires session negotiation.
Source: peerconnection.go:306-310
func()
Handler called when negotiation is needed

Configuration Methods

GetConfiguration

Returns a Configuration object representing the current configuration of this PeerConnection.
Source: peerconnection.go:632-639
Configuration
Copy of the current configuration
The returned object is a copy and direct mutation on it will not take effect until SetConfiguration has been called.

SetConfiguration

Updates the configuration of this PeerConnection object.
Source: peerconnection.go:533-630
Configuration
New configuration to apply
error
Error if configuration is invalid or cannot be modified
Some configuration properties cannot be modified after initial creation (e.g., Certificates, BundlePolicy, RTCPMuxPolicy).

Statistics

GetStats

Returns data providing statistics about the overall connection.
Source: peerconnection.go:2701-2765
StatsReport
Statistics report for the connection

Lifecycle Methods

Close

Ends the PeerConnection.
Source: peerconnection.go:2461-2464
error
Any errors encountered during closure (may be nil)

GracefulClose

Ends the PeerConnection and waits for any goroutines it started to complete.
Source: peerconnection.go:2466-2471
error
Any errors encountered during closure (may be nil)
This is only safe to call outside of PeerConnection callbacks or if in a callback, in its own goroutine.

RTCP Methods

WriteRTCP

Sends a user provided RTCP packet to the connected peer.
Source: peerconnection.go:2449-2455
[]rtcp.Packet
RTCP packets to send
error
Error if sending fails
If no peer is connected the packet is discarded. It also runs any configured interceptors.

Additional Methods

SCTP

Returns the SCTPTransport for this PeerConnection.
Source: peerconnection.go:3101-3108
*SCTPTransport
The SCTP transport, or nil if SCTP has not been negotiated

ID

Returns the unique identifier for this PeerConnection.
Source: peerconnection.go:641-646
string
Unique identifier string

See Also