Skip to content

Commit b6003c7

Browse files
committed
Fix send ordering
1 parent 6528cdf commit b6003c7

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

peer/conn.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,25 +357,9 @@ func (c *Conn) negotiate() {
357357
return
358358
}
359359

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()
373362
}
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()
379363

380364
if !c.offerrer {
381365
answer, err := c.rtc.CreateAnswer(&webrtc.AnswerOptions{})
@@ -394,12 +378,35 @@ func (c *Conn) negotiate() {
394378
_ = c.CloseWithError(xerrors.Errorf("set local description: %w", err))
395379
return
396380
}
381+
397382
select {
398383
case <-c.closed:
399384
return
400385
case c.localSessionDescriptionChannel <- answer:
401386
}
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+
}
402405
}
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)
403410
}
404411

405412
// LocalCandidate returns a channel that emits when a local candidate

0 commit comments

Comments
 (0)