@@ -357,25 +357,9 @@ func (c *Conn) negotiate() {
357
357
return
358
358
}
359
359
360
- // The RemoteDescription must be set before ICE candidates can be
361
- // added to a WebRTC connection.
362
- c .pendingCandidatesMutex .Lock ()
363
- for _ , pendingCandidate := range c .pendingCandidates {
364
- c .opts .Logger .Debug (context .Background (), "sending buffered remote candidate" ,
365
- slog .F ("hash" , c .hashCandidate (pendingCandidate )),
366
- slog .F ("length" , len (pendingCandidate .Candidate )),
367
- )
368
- select {
369
- case <- c .closed :
370
- return
371
- case c .localCandidateChannel <- pendingCandidate :
372
- }
360
+ if c .offerrer {
361
+ c .flushPendingCandidates ()
373
362
}
374
- c .opts .Logger .Debug (context .Background (), "flushed buffered remote candidates" ,
375
- slog .F ("count" , len (c .pendingCandidates )),
376
- )
377
- c .pendingCandidates = make ([]webrtc.ICECandidateInit , 0 )
378
- c .pendingCandidatesMutex .Unlock ()
379
363
380
364
if ! c .offerrer {
381
365
answer , err := c .rtc .CreateAnswer (& webrtc.AnswerOptions {})
@@ -394,12 +378,35 @@ func (c *Conn) negotiate() {
394
378
_ = c .CloseWithError (xerrors .Errorf ("set local description: %w" , err ))
395
379
return
396
380
}
381
+
397
382
select {
398
383
case <- c .closed :
399
384
return
400
385
case c .localSessionDescriptionChannel <- answer :
401
386
}
387
+
388
+ c .flushPendingCandidates ()
389
+ }
390
+ }
391
+
392
+ func (c * Conn ) flushPendingCandidates () {
393
+ c .pendingCandidatesMutex .Lock ()
394
+ defer c .pendingCandidatesMutex .Unlock ()
395
+ for _ , pendingCandidate := range c .pendingCandidates {
396
+ c .opts .Logger .Debug (context .Background (), "sending buffered remote candidate" ,
397
+ slog .F ("hash" , c .hashCandidate (pendingCandidate )),
398
+ slog .F ("length" , len (pendingCandidate .Candidate )),
399
+ )
400
+ select {
401
+ case <- c .closed :
402
+ return
403
+ case c .localCandidateChannel <- pendingCandidate :
404
+ }
402
405
}
406
+ c .opts .Logger .Debug (context .Background (), "flushed buffered remote candidates" ,
407
+ slog .F ("count" , len (c .pendingCandidates )),
408
+ )
409
+ c .pendingCandidates = make ([]webrtc.ICECandidateInit , 0 )
403
410
}
404
411
405
412
// LocalCandidate returns a channel that emits when a local candidate
0 commit comments