@@ -79,7 +79,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
79
79
// This channel needs to be bufferred otherwise slow consumers
80
80
// of this will cause a connection failure.
81
81
localCandidateChannel : make (chan webrtc.ICECandidateInit , 16 ),
82
- pendingRemoteCandidates : make ([]webrtc.ICECandidateInit , 0 ),
82
+ pendingCandidates : make ([]webrtc.ICECandidateInit , 0 ),
83
83
localSessionDescriptionChannel : make (chan webrtc.SessionDescription , 1 ),
84
84
remoteSessionDescriptionChannel : make (chan webrtc.SessionDescription , 1 ),
85
85
}
@@ -129,8 +129,8 @@ type Conn struct {
129
129
localSessionDescriptionChannel chan webrtc.SessionDescription
130
130
remoteSessionDescriptionChannel chan webrtc.SessionDescription
131
131
132
- pendingRemoteCandidates []webrtc.ICECandidateInit
133
- pendingCandidatesMutex sync.Mutex
132
+ pendingCandidates []webrtc.ICECandidateInit
133
+ pendingCandidatesMutex sync.Mutex
134
134
135
135
pingChannelID uint16
136
136
pingEchoChannelID uint16
@@ -170,11 +170,19 @@ func (c *Conn) init() error {
170
170
if iceCandidate == nil {
171
171
return
172
172
}
173
+ c .pendingCandidatesMutex .Lock ()
174
+ defer c .pendingCandidatesMutex .Unlock ()
173
175
json := iceCandidate .ToJSON ()
174
- c . opts . Logger . Debug ( context . Background (), "writing candidate to channel" ,
176
+ fields := []slog. Field {
175
177
slog .F ("hash" , c .hashCandidate (json )),
176
178
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 ... )
178
186
select {
179
187
case <- c .closed :
180
188
break
@@ -352,22 +360,21 @@ func (c *Conn) negotiate() {
352
360
// The RemoteDescription must be set before ICE candidates can be
353
361
// added to a WebRTC connection.
354
362
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 )),
359
366
slog .F ("length" , len (pendingCandidate .Candidate )),
360
367
)
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 :
364
370
return
371
+ case c .localCandidateChannel <- pendingCandidate :
365
372
}
366
373
}
367
374
c .opts .Logger .Debug (context .Background (), "flushed buffered remote candidates" ,
368
- slog .F ("count" , len (c .pendingRemoteCandidates )),
375
+ slog .F ("count" , len (c .pendingCandidates )),
369
376
)
370
- c .pendingRemoteCandidates = make ([]webrtc.ICECandidateInit , 0 )
377
+ c .pendingCandidates = make ([]webrtc.ICECandidateInit , 0 )
371
378
c .pendingCandidatesMutex .Unlock ()
372
379
373
380
if ! c .offerrer {
@@ -403,18 +410,10 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
403
410
404
411
// AddRemoteCandidate adds a remote candidate to the RTC connection.
405
412
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" ,
409
414
slog .F ("hash" , c .hashCandidate (i )),
410
415
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
+ )
418
417
return c .rtc .AddICECandidate (i )
419
418
}
420
419
0 commit comments