Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 107eb96

Browse files
committed
Try ICE servers
1 parent 59751f4 commit 107eb96

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

peer/conn.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
7979
// This channel needs to be bufferred otherwise slow consumers
8080
// of this will cause a connection failure.
8181
localCandidateChannel: make(chan webrtc.ICECandidateInit, 16),
82+
pendingRemoteCandidates: make([]webrtc.ICECandidateInit, 0),
8283
localSessionDescriptionChannel: make(chan webrtc.SessionDescription, 1),
8384
remoteSessionDescriptionChannel: make(chan webrtc.SessionDescription, 1),
8485
}
@@ -128,6 +129,10 @@ type Conn struct {
128129
localSessionDescriptionChannel chan webrtc.SessionDescription
129130
remoteSessionDescriptionChannel chan webrtc.SessionDescription
130131

132+
pendingRemoteCandidates []webrtc.ICECandidateInit
133+
pendingCandidatesMutex sync.Mutex
134+
pendingCandidatesFlushed bool
135+
131136
pingChannelID uint16
132137
pingEchoChannelID uint16
133138

@@ -329,6 +334,28 @@ func (c *Conn) negotiate() {
329334
return
330335
}
331336

337+
// The RemoteDescription must be set before ICE candidates can be
338+
// added to a WebRTC connection.
339+
c.pendingCandidatesMutex.Lock()
340+
for _, pendingCandidate := range c.pendingRemoteCandidates {
341+
hash := sha256.Sum224([]byte(pendingCandidate.Candidate))
342+
c.opts.Logger.Debug(context.Background(), "flushing buffered remote candidate",
343+
slog.F("hash", hash),
344+
slog.F("length", len(pendingCandidate.Candidate)),
345+
)
346+
err := c.rtc.AddICECandidate(pendingCandidate)
347+
if err != nil {
348+
_ = c.CloseWithError(xerrors.Errorf("flush pending remote candidate: %w", err))
349+
return
350+
}
351+
}
352+
c.opts.Logger.Debug(context.Background(), "flushed buffered remote candidates",
353+
slog.F("count", len(c.pendingRemoteCandidates)),
354+
)
355+
c.pendingCandidatesFlushed = true
356+
c.pendingRemoteCandidates = make([]webrtc.ICECandidateInit, 0)
357+
c.pendingCandidatesMutex.Unlock()
358+
332359
if !c.offerrer {
333360
answer, err := c.rtc.CreateAnswer(&webrtc.AnswerOptions{})
334361
if err != nil {
@@ -362,10 +389,18 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
362389

363390
// AddRemoteCandidate adds a remote candidate to the RTC connection.
364391
func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) error {
365-
c.opts.Logger.Debug(context.Background(), "adding remote candidate",
392+
c.pendingCandidatesMutex.Lock()
393+
defer c.pendingCandidatesMutex.Unlock()
394+
fields := []slog.Field{
366395
slog.F("hash", c.hashCandidate(i)),
367396
slog.F("length", len(i.Candidate)),
368-
)
397+
}
398+
if !c.pendingCandidatesFlushed {
399+
c.opts.Logger.Debug(context.Background(), "bufferring remote candidate", fields...)
400+
c.pendingRemoteCandidates = append(c.pendingRemoteCandidates, i)
401+
return nil
402+
}
403+
c.opts.Logger.Debug(context.Background(), "adding remote candidate", fields...)
369404
return c.rtc.AddICECandidate(i)
370405
}
371406

peer/conn_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ func createPair(t *testing.T) (client *peer.Conn, server *peer.Conn, wan *vnet.R
271271
c1SettingEngine.SetVNet(c1Net)
272272
c1SettingEngine.SetPrflxAcceptanceMinWait(0)
273273
c1SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
274-
channel1, err := peer.Client([]webrtc.ICEServer{}, &peer.ConnOptions{
274+
channel1, err := peer.Client([]webrtc.ICEServer{{
275+
URLs: []string{"stun:stun.l.google.com:19302"},
276+
}}, &peer.ConnOptions{
275277
SettingEngine: c1SettingEngine,
276278
Logger: slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug),
277279
})
@@ -283,7 +285,9 @@ func createPair(t *testing.T) (client *peer.Conn, server *peer.Conn, wan *vnet.R
283285
c2SettingEngine.SetVNet(c2Net)
284286
c2SettingEngine.SetPrflxAcceptanceMinWait(0)
285287
c2SettingEngine.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
286-
channel2, err := peer.Server([]webrtc.ICEServer{}, &peer.ConnOptions{
288+
channel2, err := peer.Server([]webrtc.ICEServer{{
289+
URLs: []string{"stun:stun.l.google.com:19302"},
290+
}}, &peer.ConnOptions{
287291
SettingEngine: c2SettingEngine,
288292
Logger: slogtest.Make(t, nil).Named("server").Leveled(slog.LevelDebug),
289293
})

0 commit comments

Comments
 (0)