Overview
ThePeerConnection is the central component in Pion WebRTC, representing a connection between two peers. It manages the entire lifecycle of a WebRTC session, from ICE candidate gathering to media transmission.
peerconnection.go
Creating a PeerConnection
Using Default Settings
Using Custom API
The source code shows howNewPeerConnection works:
peerconnection.go
Each PeerConnection gets a unique ID based on the current timestamp, useful for debugging and logging.
Signaling States
The PeerConnection goes through different signaling states during negotiation:1
Stable
No offer/answer exchange in progress. This is the initial state and the state after a successful negotiation.
2
Have Local Offer
Local peer has created an offer and called
SetLocalDescription().3
Have Remote Offer
Remote peer has sent an offer which was set via
SetRemoteDescription().4
Have Local Pranswer
Local peer has created a provisional answer.
5
Have Remote Pranswer
Remote peer has sent a provisional answer.
Monitoring Signaling State
peerconnection.go
peerconnection.go
Connection States
The overall connection state is derived from ICE and DTLS transport states:peerconnection.go
Connection State Handlers
Creating Offers and Answers
Creating an Offer
peerconnection.go
Creating an Answer
peerconnection.go
Setting Descriptions
Setting Local Description
peerconnection.go
Setting Remote Description
peerconnection.go
Adding Tracks
Receiving Tracks
peerconnection.go
peerconnection.go
Negotiation Needed
Pion automatically detects when renegotiation is needed:peerconnection.go
Configuration Updates
peerconnection.go
Most configuration fields cannot be changed after the PeerConnection is created. Only ICE servers can be safely updated.
Closing Connections
Next Steps
Signaling
Learn about SDP exchange and signaling protocols
ICE & Connectivity
Deep dive into ICE candidates and NAT traversal