Overview
The ICEGatherer gathers local host, server reflexive and relay candidates, as well as enabling the retrieval of local Interactive Connectivity Establishment (ICE) parameters which can be exchanged in signaling.
This is part of the ORTC API. It is not meant to be used together with the basic WebRTC API.
Type Definition
type ICEGatherer struct {
// Contains internal state management for ICE gathering
}
Constructor
NewICEGatherer
Creates a new ICEGatherer with the specified options.
func ( api * API ) NewICEGatherer ( opts ICEGatherOptions ) ( * ICEGatherer , error )
Configuration options for the ICE gatherer Show ICEGatherOptions fields
List of ICE servers (STUN/TURN) to use for gathering
Policy for gathering candidates (all, relay, nohost)
Size of the prefetched ICE pool
Returns a new ICEGatherer instance or an error if creation fails
Example:
api := webrtc . NewAPI ()
gatherer , err := api . NewICEGatherer ( webrtc . ICEGatherOptions {
ICEServers : [] webrtc . ICEServer {
{
URLs : [] string { "stun:stun.l.google.com:19302" },
},
},
})
if err != nil {
panic ( err )
}
Methods
Gather
Starts gathering ICE candidates.
func ( g * ICEGatherer ) Gather () error
Returns nil on success, or an error if gathering cannot be started
Example:
err := gatherer . Gather ()
if err != nil {
panic ( err )
}
GetLocalParameters
Returns the ICE parameters of the ICEGatherer.
func ( g * ICEGatherer ) GetLocalParameters () ( ICEParameters , error )
Returns the local ICE parameters (username fragment, password) or an error Show ICEParameters structure
Whether ICE-lite mode is enabled
Example:
params , err := gatherer . GetLocalParameters ()
if err != nil {
panic ( err )
}
fmt . Printf ( "Username: %s , Password: %s \n " , params . UsernameFragment , params . Password )
GetLocalCandidates
Returns the sequence of valid local candidates associated with the ICEGatherer.
func ( g * ICEGatherer ) GetLocalCandidates () ([] ICECandidate , error )
Returns a slice of local ICE candidates or an error
Example:
candidates , err := gatherer . GetLocalCandidates ()
if err != nil {
panic ( err )
}
for _ , candidate := range candidates {
fmt . Printf ( "Candidate: %s \n " , candidate . String ())
}
State
Returns the current state of the ICE gatherer.
func ( g * ICEGatherer ) State () ICEGathererState
The current ICE gatherer state (New, Gathering, Complete, Closed)
Example:
state := gatherer . State ()
fmt . Printf ( "Gatherer state: %s \n " , state )
OnLocalCandidate
Sets an event handler which fires when a new local ICE candidate is available.
The handler will be called with a nil pointer when gathering is finished.
func ( g * ICEGatherer ) OnLocalCandidate ( f func ( * ICECandidate ))
f
func(*ICECandidate)
required
Callback function that receives ICE candidates. Called with nil when gathering completes.
Example:
gatherer . OnLocalCandidate ( func ( candidate * ICECandidate ) {
if candidate == nil {
fmt . Println ( "Gathering complete" )
return
}
fmt . Printf ( "New candidate: %s \n " , candidate . String ())
})
OnStateChange
Sets a handler that fires when the ICEGatherer state changes.
func ( g * ICEGatherer ) OnStateChange ( f func ( ICEGathererState ))
f
func(ICEGathererState)
required
Callback function that receives state change notifications
Example:
gatherer . OnStateChange ( func ( state webrtc . ICEGathererState ) {
fmt . Printf ( "Gatherer state changed to: %s \n " , state )
})
Close
Prunes all local candidates and closes the ports.
func ( g * ICEGatherer ) Close () error
Returns nil on success, or an error if closing fails
Example:
err := gatherer . Close ()
if err != nil {
panic ( err )
}
GracefulClose
Prunes all local candidates and closes the ports, waiting for goroutines to complete.
This is only safe to call outside of ICEGatherer callbacks or if in a callback, in its own goroutine.
func ( g * ICEGatherer ) GracefulClose () error
Returns nil on success, or an error if closing fails
Example:
go func () {
err := gatherer . GracefulClose ()
if err != nil {
log . Printf ( "Error closing gatherer: %v " , err )
}
}()
ICEAddressRewriteRule
Represents a rule for remapping candidate addresses.
type ICEAddressRewriteRule struct {
External [] string
Local string
Iface string
CIDR string
AsCandidateType ICECandidateType
Mode ICEAddressRewriteMode
Networks [] NetworkType
}
External IP addresses to use
Local IP address to match
Network interface name to match
CIDR notation to match local addresses
Type to use for rewritten candidates
Whether to replace or append candidates
Network types to apply this rule to
ICEAddressRewriteMode
Controls whether a rule replaces or appends candidates.
type ICEAddressRewriteMode byte
const (
ICEAddressRewriteModeUnspecified ICEAddressRewriteMode = iota
ICEAddressRewriteReplace
ICEAddressRewriteAppend
)
States
The ICEGatherer can be in one of the following states:
ICEGathererStateNew : Initial state
ICEGathererStateGathering : Currently gathering candidates
ICEGathererStateComplete : Gathering has completed
ICEGathererStateClosed : Gatherer has been closed
Usage Example
package main
import (
" fmt "
" github.com/pion/webrtc/v4 "
)
func main () {
// Create API
api := webrtc . NewAPI ()
// Create ICE gatherer with STUN server
gatherer , err := api . NewICEGatherer ( webrtc . ICEGatherOptions {
ICEServers : [] webrtc . ICEServer {
{
URLs : [] string { "stun:stun.l.google.com:19302" },
},
},
})
if err != nil {
panic ( err )
}
defer gatherer . Close ()
// Set up event handlers
gatherer . OnLocalCandidate ( func ( candidate * webrtc . ICECandidate ) {
if candidate == nil {
fmt . Println ( "Gathering complete" )
return
}
fmt . Printf ( "New candidate: %s \n " , candidate . String ())
})
gatherer . OnStateChange ( func ( state webrtc . ICEGathererState ) {
fmt . Printf ( "State changed: %s \n " , state )
})
// Start gathering
if err = gatherer . Gather (); err != nil {
panic ( err )
}
// Get local parameters
params , err := gatherer . GetLocalParameters ()
if err != nil {
panic ( err )
}
fmt . Printf ( "ICE Parameters - User: %s , Pass: %s \n " ,
params . UsernameFragment , params . Password )
// Wait for gathering to complete...
select {}
}
See Also