115 uint32_t local_preference = 65535,
122 const std::string& base_address,
123 const std::string& server_address =
"");
183 uint32_t controlled_priority,
184 bool is_controlling);
204 IceServer(
const std::string& u,
const std::string& user,
const std::string& pass)
215 bool is_turn()
const {
return url.find(
"turn:") == 0 ||
url.find(
"turns:") == 0; }
220 bool parse_url(std::string& host, uint16_t& port)
const;
242 ice_servers.emplace_back(
"stun:" + host +
":" + std::to_string(port));
249 const std::string& username,
const std::string& password) {
250 ice_servers.emplace_back(
"turn:" + host +
":" + std::to_string(port),
354 const std::string& username,
const std::string& password);
510 uint16_t local_port_ = 0;
513 std::unique_ptr<StunClient> stun_client_;
514 std::unique_ptr<TurnClient> turn_client_;
517 mutable std::mutex mutex_;
522 std::vector<IceCandidate> local_candidates_;
523 std::vector<IceCandidate> remote_candidates_;
524 std::vector<IceCandidatePair> candidate_pairs_;
525 std::optional<IceCandidatePair> selected_pair_;
528 std::atomic<bool> gathering_ {
false};
529 std::atomic<bool> checking_ {
false};
530 bool remote_candidates_complete_ =
false;
533 std::thread gathering_thread_;
534 std::thread checking_thread_;
544 bool ensure_socket();
545 void gather_host_candidates();
546 void gather_srflx_candidates();
547 void gather_relay_candidates();
548 void gathering_complete();
549 void add_local_candidate(
const IceCandidate& candidate);
550 void form_candidate_pairs();
551 void perform_connectivity_checks();
552 bool perform_check(IceCandidatePair& pair);
553 void update_connection_state();
554 void select_best_pair();
ICE-lite Manager for NAT traversal.
void stop_checks()
Stop connectivity checks.
std::vector< IceCandidatePair > get_candidate_pairs() const
Get candidate pairs.
std::optional< std::pair< std::string, uint16_t > > get_public_address() const
Get our public address (from server-reflexive candidate)
void set_on_connection_state_changed(IceConnectionStateCallback callback)
void clear_ice_servers()
Clear all ICE servers.
IceGatheringState get_gathering_state() const
Get gathering state.
void set_on_gathering_state_changed(IceGatheringStateCallback callback)
IceManager & operator=(const IceManager &)=delete
IceManager(const IceConfig &config)
void close()
Close ICE manager and release resources.
void add_turn_server(const std::string &host, uint16_t port, const std::string &username, const std::string &password)
Add a TURN server with credentials.
bool is_gathering_complete() const
Check if gathering is complete.
void add_stun_server(const std::string &host, uint16_t port=STUN_DEFAULT_PORT)
Add a STUN server.
std::vector< IceCandidate > get_local_candidates() const
Get local candidates.
void restart()
Restart ICE (gather new candidates and start checks)
uint16_t get_local_port() const
Get local port.
void start_checks()
Start connectivity checks.
void add_remote_candidate(const IceCandidate &candidate)
Add a remote candidate.
IceManager(const IceManager &)=delete
std::vector< IceCandidate > get_remote_candidates() const
Get remote candidates.
void set_on_new_candidate(IceNewCandidateCallback callback)
std::optional< IceCandidatePair > get_selected_pair() const
Get the selected (best) candidate pair.
IceConnectionState get_connection_state() const
Get connection state.
void set_on_selected_pair(IceSelectedPairCallback callback)
bool gather_candidates()
Start gathering ICE candidates.
const IceConfig & config() const
Get current configuration.
void set_config(const IceConfig &config)
Set ICE configuration.
socket_t get_socket() const
Get the UDP socket used for ICE Can be used for data transfer after ICE completes.
void add_remote_candidates_from_sdp(const std::vector< std::string > &sdp_lines)
Add remote candidates from SDP.
void set_on_candidates_gathered(IceCandidatesCallback callback)
bool is_connected() const
Check if connected.
void end_of_remote_candidates()
Signal end of remote candidates (for trickle ICE)
constexpr uint32_t ICE_PRIORITY_RELAY
Default ICE candidate priority for relay candidates.
IceGatheringState
ICE gathering state.
@ Complete
Gathering complete.
std::function< void(IceGatheringState)> IceGatheringStateCallback
Callback when gathering state changes.
IceCandidateType
ICE candidate type.
@ PeerReflexive
Address discovered during connectivity checks.
@ Relay
TURN relay address.
@ ServerReflexive
Address discovered via STUN (public address)
@ Host
Local interface address.
constexpr uint32_t ICE_PRIORITY_HOST
Default ICE candidate priority for host candidates.
std::function< void(const IceCandidate &)> IceNewCandidateCallback
Callback when a new candidate is discovered (trickle ICE)
IceCandidatePairState
ICE candidate pair state.
@ InProgress
Check in progress.
@ Waiting
Waiting to be checked.
@ Succeeded
Check succeeded.
std::function< void(const std::vector< IceCandidate > &)> IceCandidatesCallback
Callback when candidates are gathered.
std::function< void(IceConnectionState)> IceConnectionStateCallback
Callback when connection state changes.
IceTransportProtocol
ICE candidate transport protocol.
std::function< void(const IceCandidatePair &)> IceSelectedPairCallback
Callback when ICE completes with selected pair.
constexpr uint32_t ICE_PRIORITY_SRFLX
Default ICE candidate priority for server-reflexive candidates
constexpr int ICE_CHECK_TIMEOUT_MS
Connectivity check timeout (ms)
constexpr int ICE_CHECK_MAX_RETRIES
Maximum connectivity check retries.
constexpr uint16_t STUN_DEFAULT_PORT
Default STUN port.
IceConnectionState
ICE connection state.
@ Closed
ICE agent closed.
@ Completed
ICE processing complete.
@ Checking
Performing connectivity checks.
@ Connected
At least one valid pair found.
@ Gathering
Gathering candidates.
@ Failed
ICE processing failed.
@ Disconnected
Connection lost.
#define INVALID_SOCKET_VALUE
int check_count
Number of checks performed.
std::string key() const
Get pair key for deduplication.
bool nominated
Nominated for use.
std::chrono::steady_clock::time_point last_check
static uint64_t compute_priority(uint32_t controlling_priority, uint32_t controlled_priority, bool is_controlling)
Compute pair priority (RFC 5245 Section 5.7.2)
uint64_t priority
Pair priority.
IceCandidatePairState state
uint16_t port
Port number.
static std::string generate_foundation(IceCandidateType type, const std::string &base_address, const std::string &server_address="")
Generate foundation string.
std::string address
IP address.
std::string type_string() const
Get candidate type as string.
uint32_t component_id
Component ID (typically 1 for RTP)
std::string address_string() const
Get address:port string.
std::string foundation
Unique identifier for candidate.
static uint32_t compute_priority(IceCandidateType type, uint32_t local_preference=65535, uint32_t component_id=1)
Compute candidate priority (RFC 5245 Section 4.1.2.1)
uint32_t priority
Candidate priority.
std::string to_sdp_attribute() const
Format candidate as SDP attribute string (a=candidate:...)
IceTransportProtocol transport
std::string related_address
Related address (for srflx/relay)
uint16_t related_port
Related port.
bool operator==(const IceCandidate &other) const
static std::optional< IceCandidate > from_sdp_attribute(const std::string &sdp)
Parse candidate from SDP attribute string.
bool gather_srflx_candidates
Gather server-reflexive candidates.
void add_turn_server(const std::string &host, uint16_t port, const std::string &username, const std::string &password)
Add a TURN server with credentials.
bool gather_host_candidates
Gather host candidates.
int check_max_retries
Max connectivity check retries.
std::string software
Software attribute.
int gathering_timeout_ms
Candidate gathering timeout.
void add_stun_server(const std::string &host, uint16_t port=STUN_DEFAULT_PORT)
Add a STUN server.
bool gather_relay_candidates
Gather relay candidates (requires TURN)
std::vector< IceServer > ice_servers
STUN/TURN servers.
int check_timeout_ms
Connectivity check timeout.
STUN/TURN server configuration.
std::string password
Password/credential (for TURN)
std::string url
Server URL (stun:host:port or turn:host:port)
bool is_stun() const
Check if this is a STUN server.
IceServer(const std::string &u, const std::string &user, const std::string &pass)
std::string username
Username (for TURN)
IceServer(const std::string &u)
bool parse_url(std::string &host, uint16_t &port) const
Parse host and port from URL.
bool is_turn() const
Check if this is a TURN server.
STUN (Session Traversal Utilities for NAT) Protocol Implementation.