TCP Candidate Types
There are three types of TCP candidates, defined by theTCPType enum:
tcptype.go
Active TCP Candidates
Active TCP candidates initiate outbound connections to remote passive candidates. TheactiveTCPConn type handles connection establishment:
The ICE agent automatically ignores remote candidates with
TCPTypeActive since they don’t accept inbound connections.Passive TCP Candidates
Passive TCP candidates listen for incoming connections. They are the server-side counterpart to active candidates:agent.go
Simultaneous-Open (SO) Candidates
SO candidates can both initiate and accept connections, useful for NAT traversal scenarios where both peers attempt to connect simultaneously:TCP Packet Framing
TCP is a stream-oriented protocol, so Pion ICE implements packet framing according to RFC 4571. Each packet is prefixed with a 2-byte length header:readStreamingPacket and writeStreamingPacket functions:
tcp_mux.go
TCP Multiplexing
ThetcpPacketConn type manages multiple TCP connections to different remote addresses, providing a packet-oriented interface over stream-based TCP:
tcp_packet_conn.go
Adding Connections
Connections are added dynamically as peers connect:The TCP packet connection supports optional write buffering via
bufferedConn to optimize small writes.Configuration Parameters
Active TCP Connection Lifecycle
TheactiveTCPConn type manages outbound TCP connections with buffered I/O:
active_tcp.go
Connection Establishment
When to Use TCP vs UDP
Use TCP Candidates When:
- UDP is blocked: Corporate firewalls or restrictive networks
- Reliable delivery needed: Application requires in-order, reliable delivery at the transport layer
- Firewall traversal: TCP is more likely to traverse certain enterprise firewalls
- Port restrictions: Only TCP ports are available
Use UDP Candidates When:
- Low latency is critical: UDP has less overhead and no head-of-line blocking
- Packet loss is acceptable: Real-time media can handle some loss
- Network supports it: Most internet connections allow UDP
- Best performance: UDP is preferred for WebRTC and real-time applications
Configuration Example
Disabling Active TCP
If you want to prevent the agent from establishing outbound TCP connections:With
DisableActiveTCP: true, your agent will only gather passive and simultaneous-open TCP candidates, not active ones.SDP Representation
TCP candidates are represented in SDP with thetcptype extension:
Reference
- Active TCP:
active_tcp.go:18- activeTCPConn implementation - TCP Packet Conn:
tcp_packet_conn.go:81- tcpPacketConn multiplexer - TCP Types:
tcptype.go:10- TCPType enum definition - Streaming Packets:
tcp_mux.go:436- readStreamingPacket and writeStreamingPacket