Skip to content

Commit d21ae5e

Browse files
committed
Include ufrag in generated ICE candidates
Include ufrag extension in the ICE candidates generated by the ICE agent
1 parent 141df5a commit d21ae5e

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

agent.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ func (a *Agent) addCandidate(ctx context.Context, cand Candidate, candidateConn
799799
}
800800
}
801801

802+
a.setCandidateExtensions(cand)
802803
cand.start(a, candidateConn, a.startedCh)
803804

804805
set = append(set, cand)
@@ -818,6 +819,16 @@ func (a *Agent) addCandidate(ctx context.Context, cand Candidate, candidateConn
818819
})
819820
}
820821

822+
func (a *Agent) setCandidateExtensions(cand Candidate) {
823+
err := cand.AddExtension(CandidateExtension{
824+
Key: "ufrag",
825+
Value: a.localUfrag,
826+
})
827+
if err != nil {
828+
a.log.Errorf("Failed to add ufrag extension to candidate: %v", err)
829+
}
830+
}
831+
821832
// GetRemoteCandidates returns the remote candidates.
822833
func (a *Agent) GetRemoteCandidates() ([]Candidate, error) {
823834
var res []Candidate

agent_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,3 +2071,42 @@ func TestAgentGracefulCloseDeadlock(t *testing.T) {
20712071
closeNow.Done()
20722072
closed.Wait()
20732073
}
2074+
2075+
func TestSetCandidatesUfrag(t *testing.T) {
2076+
var config AgentConfig
2077+
2078+
agent, err := NewAgent(&config)
2079+
if err != nil {
2080+
t.Fatalf("Error constructing ice.Agent: %v", err)
2081+
}
2082+
defer func() {
2083+
require.NoError(t, agent.Close())
2084+
}()
2085+
2086+
dummyConn := &net.UDPConn{}
2087+
2088+
for i := 0; i < 5; i++ {
2089+
cfg := CandidateHostConfig{
2090+
Network: "udp",
2091+
Address: "192.168.0.2",
2092+
Port: 1000 + i,
2093+
Component: 1,
2094+
}
2095+
2096+
cand, errCand := NewCandidateHost(&cfg)
2097+
require.NoError(t, errCand)
2098+
2099+
err = agent.addCandidate(context.Background(), cand, dummyConn)
2100+
require.NoError(t, err)
2101+
}
2102+
2103+
actualCandidates, err := agent.GetLocalCandidates()
2104+
require.NoError(t, err)
2105+
2106+
for _, candidate := range actualCandidates {
2107+
ext, ok := candidate.GetExtension("ufrag")
2108+
2109+
require.True(t, ok)
2110+
require.Equal(t, agent.localUfrag, ext.Value)
2111+
}
2112+
}

0 commit comments

Comments
 (0)