What is an ICE Agent?
TheAgent type (defined in agent.go:40) orchestrates the entire ICE process:
Creating an Agent
Using Options (Recommended)
Using Config (Deprecated)
The
AgentConfig approach is deprecated. Use NewAgentWithOptions() for new code.Agent Lifecycle
The ICE agent progresses through several phases during its lifetime:1. Creation and Initialization
2. Credential Exchange
3. Candidate Gathering
The gathering state transitions from
GatheringStateNew → GatheringStateGathering → GatheringStateComplete.4. Connectivity Establishment
5. Connection Usage
6. Cleanup
Connection States
The agent’s connection state (defined inice.go:10-34) tracks the ICE negotiation progress:
State Transitions
State changes are triggered by:- New → Checking: When
Dial()orAccept()is called - Checking → Connected: When a candidate pair succeeds
- Connected → Disconnected: After
disconnectedTimeoutwithout traffic - Disconnected → Failed: After
failedTimeoutfrom disconnected state - Connected → Connected: Can switch selected pairs during renomination
- Any → Closed: When
Close()is called
agent.go:731-747:
Controlling vs Controlled Roles
ICE agents take on one of two roles during connection establishment:Controlling Agent
- Initiates nomination of candidate pairs
- Sends binding requests with
USE-CANDIDATEattribute - Typically the peer that initiated the session (caller)
- Set by calling
Dial()
Controlled Agent
- Responds to nomination from controlling agent
- Accepts nominated pairs
- Typically the peer that received the session (callee)
- Set by calling
Accept()
Role Conflicts
If both agents claim the same role, ICE resolves the conflict using the tie-breaker value (randomly generated 64-bit number). The agent with the higher tie-breaker value keeps its role; the other switches. Implementation inagent.go:1452-1481:
Agent Configuration Options
Pion ICE provides extensive configuration through options:Network Configuration
Candidate Configuration
Timeout Configuration
Configured inagent_config.go:16-59:
Understanding timeout values
Understanding timeout values
Disconnected Timeout (default 5s):
- How long without traffic before transitioning to
ConnectionStateDisconnected - Set to 0 to never go to disconnected
- How long in disconnected state before transitioning to
ConnectionStateFailed - Set to 0 to never go to failed
- How often to send STUN binding requests to maintain the connection
- Set to 0 to disable keepalives (not recommended)
- How often to perform connectivity checks while in checking state
Advanced Configuration
Restarting an Agent
You can restart an agent to perform ICE restart (generates new credentials):agent.go:1694-1754, restarting:
- Cancels ongoing gathering
- Clears all candidates
- Resets checklist and selected pair
- Generates new credentials if not provided
- Returns to
ConnectionStateChecking(if not new)
Event Handlers
OnCandidate
OnConnectionStateChange
OnSelectedCandidatePairChange
Best Practices
Always set event handlers before calling
GatherCandidates() to ensure you don’t miss any candidates.When to use ICE Lite
When to use ICE Lite
ICE Lite is suitable when:
- Your application runs on a server with a public IP address
- You want to reduce server complexity
- The remote peer will be a full ICE agent
- You don’t need relay candidates
Related Topics
- ICE Protocol - Overview of the ICE protocol
- Candidates - Understanding candidate types
- Connectivity - Connection establishment details