Overview
The ICETransport allows an application access to information about the ICE transport over which packets are sent and received.
This is part of the ORTC API. It is not meant to be used together with the basic WebRTC API.
Type Definition
type ICETransport struct {
// Contains internal state management for ICE transport
}
Constructor
NewICETransport
Creates a new ICETransport with the given ICEGatherer.
func NewICETransport(gatherer *ICEGatherer, loggerFactory logging.LoggerFactory) *ICETransport
The ICE gatherer to use for candidate gathering
loggerFactory
logging.LoggerFactory
required
Factory for creating loggers
Returns a new ICETransport instance
Example:
api := webrtc.NewAPI()
gatherer, _ := api.NewICEGatherer(webrtc.ICEGatherOptions{})
transport := webrtc.NewICETransport(gatherer, api.LoggerFactory)
Methods
Start
Starts incoming connectivity checks based on the configured role.
func (t *ICETransport) Start(
gatherer *ICEGatherer,
params ICEParameters,
role *ICERole,
) error
Optional ICE gatherer to use (can be nil to use the existing one)
Remote ICE parameters (username fragment and password)Show ICEParameters structure
ICE username fragment from remote peer
ICE password from remote peer
Whether remote peer is using ICE-lite
ICE role (controlling or controlled). If nil, defaults to controlled.
Returns nil on success, or an error if the transport cannot be started
Example:
role := webrtc.ICERoleControlling
err := transport.Start(nil, remoteParams, &role)
if err != nil {
panic(err)
}
Stop
Irreversibly stops the ICETransport.
func (t *ICETransport) Stop() error
Returns nil on success, or an error if stopping fails
Example:
err := transport.Stop()
if err != nil {
panic(err)
}
GracefulStop
Irreversibly stops the ICETransport and waits for goroutines to complete.
This is only safe to call outside of ICETransport callbacks or if in a callback, in its own goroutine.
func (t *ICETransport) GracefulStop() error
Returns nil on success, or an error if stopping fails
Example:
go func() {
err := transport.GracefulStop()
if err != nil {
log.Printf("Error stopping transport: %v", err)
}
}()
State
Returns the current ICE transport state.
func (t *ICETransport) State() ICETransportState
The current state (New, Checking, Connected, Completed, Failed, Disconnected, Closed)
Example:
state := transport.State()
fmt.Printf("Transport state: %s\n", state)
Role
Returns the current role of the ICE transport.
func (t *ICETransport) Role() ICERole
The current ICE role (Controlling or Controlled)
Example:
role := transport.Role()
fmt.Printf("ICE role: %s\n", role)
GetLocalParameters
Returns an ICEParameters object uniquely identifying the local peer.
func (t *ICETransport) GetLocalParameters() (ICEParameters, error)
Returns the local ICE parameters or an error
Example:
params, err := transport.GetLocalParameters()
if err != nil {
panic(err)
}
fmt.Printf("Local ICE params: %+v\n", params)
GetRemoteParameters
Returns an ICEParameters object uniquely identifying the remote peer.
func (t *ICETransport) GetRemoteParameters() (ICEParameters, error)
Returns the remote ICE parameters or an error
Example:
params, err := transport.GetRemoteParameters()
if err != nil {
panic(err)
}
fmt.Printf("Remote ICE params: %+v\n", params)
GetSelectedCandidatePair
Returns the selected candidate pair on which packets are sent.
func (t *ICETransport) GetSelectedCandidatePair() (*ICECandidatePair, error)
Returns the selected candidate pair or nil if no pair is selectedShow ICECandidatePair structure
Example:
pair, err := transport.GetSelectedCandidatePair()
if err != nil {
panic(err)
}
if pair != nil {
fmt.Printf("Selected pair - Local: %s, Remote: %s\n",
pair.Local.String(), pair.Remote.String())
}
GetSelectedCandidatePairStats
Returns statistics for the selected candidate pair.
func (t *ICETransport) GetSelectedCandidatePairStats() (ICECandidatePairStats, bool)
return
ICECandidatePairStats, bool
Returns stats and true if available, or empty stats and false if not available
Example:
stats, ok := transport.GetSelectedCandidatePairStats()
if ok {
fmt.Printf("Bytes sent: %d, Bytes received: %d\n",
stats.BytesSent, stats.BytesReceived)
}
AddRemoteCandidate
Adds a candidate associated with the remote ICETransport.
func (t *ICETransport) AddRemoteCandidate(remoteCandidate *ICECandidate) error
The remote ICE candidate to add (can be nil to signal end-of-candidates)
Returns nil on success, or an error if the candidate cannot be added
Example:
err := transport.AddRemoteCandidate(candidate)
if err != nil {
panic(err)
}
SetRemoteCandidates
Sets the sequence of candidates associated with the remote ICETransport.
func (t *ICETransport) SetRemoteCandidates(remoteCandidates []ICECandidate) error
Slice of remote ICE candidates to set
Returns nil on success, or an error if candidates cannot be set
Example:
err := transport.SetRemoteCandidates(remoteCandidates)
if err != nil {
panic(err)
}
OnConnectionStateChange
Sets a handler that fires when the ICE connection state changes.
func (t *ICETransport) OnConnectionStateChange(f func(ICETransportState))
f
func(ICETransportState)
required
Callback function that receives state change notifications
Example:
transport.OnConnectionStateChange(func(state webrtc.ICETransportState) {
fmt.Printf("ICE transport state changed to: %s\n", state)
})
OnSelectedCandidatePairChange
Sets a handler invoked when a new ICE candidate pair is selected.
func (t *ICETransport) OnSelectedCandidatePairChange(f func(*ICECandidatePair))
f
func(*ICECandidatePair)
required
Callback function that receives the selected candidate pair
Example:
transport.OnSelectedCandidatePairChange(func(pair *webrtc.ICECandidatePair) {
fmt.Printf("Selected candidate pair changed\n")
fmt.Printf(" Local: %s\n", pair.Local.String())
fmt.Printf(" Remote: %s\n", pair.Remote.String())
})
Stats
Reports the current statistics of the ICETransport.
func (t *ICETransport) Stats() TransportStats
Returns transport statistics including bytes sent and receivedShow TransportStats structure
Stats type (always StatsTypeTransport)
Example:
stats := transport.Stats()
fmt.Printf("Bytes sent: %d, received: %d\n",
stats.BytesSent, stats.BytesReceived)
States
The ICETransport can be in one of the following states:
- ICETransportStateNew: Initial state
- ICETransportStateChecking: Checking candidate pairs
- ICETransportStateConnected: Successfully connected
- ICETransportStateCompleted: Completed gathering and checks
- ICETransportStateFailed: Failed to establish connection
- ICETransportStateDisconnected: Temporarily disconnected
- ICETransportStateClosed: Transport has been closed
Usage Example
package main
import (
"fmt"
"github.com/pion/webrtc/v4"
)
func main() {
// Create API
api := webrtc.NewAPI()
// Create ICE gatherer
gatherer, err := api.NewICEGatherer(webrtc.ICEGatherOptions{
ICEServers: []webrtc.ICEServer{
{URLs: []string{"stun:stun.l.google.com:19302"}},
},
})
if err != nil {
panic(err)
}
// Create ICE transport
transport := webrtc.NewICETransport(gatherer, api.LoggerFactory)
// Set up event handlers
transport.OnConnectionStateChange(func(state webrtc.ICETransportState) {
fmt.Printf("ICE transport state: %s\n", state)
})
transport.OnSelectedCandidatePairChange(func(pair *webrtc.ICECandidatePair) {
fmt.Printf("Selected pair: %s <-> %s\n",
pair.Local.String(), pair.Remote.String())
})
// Gather candidates
if err = gatherer.Gather(); err != nil {
panic(err)
}
// Start transport with remote parameters
// (remoteParams would come from signaling)
role := webrtc.ICERoleControlling
err = transport.Start(nil, remoteParams, &role)
if err != nil {
panic(err)
}
// Add remote candidates as they arrive
// transport.AddRemoteCandidate(remoteCandidate)
// Get stats periodically
stats := transport.Stats()
fmt.Printf("Stats: %+v\n", stats)
// Clean up
defer transport.Stop()
}
See Also