Skip to content

Commit a5bd7e4

Browse files
committed
Fix candidate accept ordering
1 parent 90d26f2 commit a5bd7e4

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

peer/conn.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +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),
82+
pendingCandidates: make([]webrtc.ICECandidateInit, 0),
8383
localSessionDescriptionChannel: make(chan webrtc.SessionDescription, 1),
8484
remoteSessionDescriptionChannel: make(chan webrtc.SessionDescription, 1),
8585
}
@@ -129,8 +129,8 @@ type Conn struct {
129129
localSessionDescriptionChannel chan webrtc.SessionDescription
130130
remoteSessionDescriptionChannel chan webrtc.SessionDescription
131131

132-
pendingRemoteCandidates []webrtc.ICECandidateInit
133-
pendingCandidatesMutex sync.Mutex
132+
pendingCandidates []webrtc.ICECandidateInit
133+
pendingCandidatesMutex sync.Mutex
134134

135135
pingChannelID uint16
136136
pingEchoChannelID uint16
@@ -170,11 +170,19 @@ func (c *Conn) init() error {
170170
if iceCandidate == nil {
171171
return
172172
}
173+
c.pendingCandidatesMutex.Lock()
174+
defer c.pendingCandidatesMutex.Unlock()
173175
json := iceCandidate.ToJSON()
174-
c.opts.Logger.Debug(context.Background(), "writing candidate to channel",
176+
fields := []slog.Field{
175177
slog.F("hash", c.hashCandidate(json)),
176178
slog.F("length", len(json.Candidate)),
177-
)
179+
}
180+
if c.rtc.RemoteDescription() == nil {
181+
c.pendingCandidates = append(c.pendingCandidates, json)
182+
c.opts.Logger.Debug(context.Background(), "buffering candidate", fields...)
183+
return
184+
}
185+
c.opts.Logger.Debug(context.Background(), "sending candidate directly", fields...)
178186
select {
179187
case <-c.closed:
180188
break
@@ -352,22 +360,21 @@ func (c *Conn) negotiate() {
352360
// The RemoteDescription must be set before ICE candidates can be
353361
// added to a WebRTC connection.
354362
c.pendingCandidatesMutex.Lock()
355-
for _, pendingCandidate := range c.pendingRemoteCandidates {
356-
hash := sha256.Sum224([]byte(pendingCandidate.Candidate))
357-
c.opts.Logger.Debug(context.Background(), "flushing buffered remote candidate",
358-
slog.F("hash", hash),
363+
for _, pendingCandidate := range c.pendingCandidates {
364+
c.opts.Logger.Debug(context.Background(), "sending buffered remote candidate",
365+
slog.F("hash", c.hashCandidate(pendingCandidate)),
359366
slog.F("length", len(pendingCandidate.Candidate)),
360367
)
361-
err := c.rtc.AddICECandidate(pendingCandidate)
362-
if err != nil {
363-
_ = c.CloseWithError(xerrors.Errorf("flush pending remote candidate: %w", err))
368+
select {
369+
case <-c.closed:
364370
return
371+
case c.localCandidateChannel <- pendingCandidate:
365372
}
366373
}
367374
c.opts.Logger.Debug(context.Background(), "flushed buffered remote candidates",
368-
slog.F("count", len(c.pendingRemoteCandidates)),
375+
slog.F("count", len(c.pendingCandidates)),
369376
)
370-
c.pendingRemoteCandidates = make([]webrtc.ICECandidateInit, 0)
377+
c.pendingCandidates = make([]webrtc.ICECandidateInit, 0)
371378
c.pendingCandidatesMutex.Unlock()
372379

373380
if !c.offerrer {
@@ -403,18 +410,10 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
403410

404411
// AddRemoteCandidate adds a remote candidate to the RTC connection.
405412
func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) error {
406-
c.pendingCandidatesMutex.Lock()
407-
defer c.pendingCandidatesMutex.Unlock()
408-
fields := []slog.Field{
413+
c.opts.Logger.Debug(context.Background(), "accepting candidate",
409414
slog.F("hash", c.hashCandidate(i)),
410415
slog.F("length", len(i.Candidate)),
411-
}
412-
if c.rtc.RemoteDescription() == nil {
413-
c.opts.Logger.Debug(context.Background(), "bufferring remote candidate", fields...)
414-
c.pendingRemoteCandidates = append(c.pendingRemoteCandidates, i)
415-
return nil
416-
}
417-
c.opts.Logger.Debug(context.Background(), "adding remote candidate", fields...)
416+
)
418417
return c.rtc.AddICECandidate(i)
419418
}
420419

0 commit comments

Comments
 (0)