Skip to content

Commit 5021b95

Browse files
committed
Use flushed bool instead of checking remote
1 parent 48078d2 commit 5021b95

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

peer/conn.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ type Conn struct {
120120
localSessionDescriptionChannel chan webrtc.SessionDescription
121121
remoteSessionDescriptionChannel chan webrtc.SessionDescription
122122

123-
pendingCandidates []webrtc.ICECandidateInit
124-
pendingCandidatesMutex sync.Mutex
123+
pendingCandidates []webrtc.ICECandidateInit
124+
pendingCandidatesMutex sync.Mutex
125+
pendingCandidatesFlushed bool
125126

126127
pingChannelID uint16
127128
pingEchoChannelID uint16
@@ -144,7 +145,7 @@ func (c *Conn) init() error {
144145
c.pendingCandidatesMutex.Lock()
145146
defer c.pendingCandidatesMutex.Unlock()
146147

147-
if c.rtc.RemoteDescription() == nil {
148+
if !c.pendingCandidatesFlushed {
148149
c.opts.Logger.Debug(context.Background(), "adding local candidate to buffer")
149150
c.pendingCandidates = append(c.pendingCandidates, iceCandidate.ToJSON())
150151
return
@@ -280,8 +281,6 @@ func (c *Conn) negotiate() {
280281
case remoteDescription = <-c.remoteSessionDescriptionChannel:
281282
}
282283

283-
// Must lock new candidates from being sent while the description is being flushed.
284-
c.pendingCandidatesMutex.Lock()
285284
err := c.rtc.SetRemoteDescription(remoteDescription)
286285
if err != nil {
287286
c.pendingCandidatesMutex.Unlock()
@@ -294,13 +293,8 @@ func (c *Conn) negotiate() {
294293
// time. If candidates flush before this point, a connection could fail.
295294
c.flushPendingCandidates()
296295
}
297-
c.pendingCandidatesMutex.Unlock()
298296

299297
if !c.offerrer {
300-
// Lock new candidates from processing until we set the local description.
301-
c.pendingCandidatesMutex.Lock()
302-
defer c.pendingCandidatesMutex.Unlock()
303-
304298
answer, err := c.rtc.CreateAnswer(&webrtc.AnswerOptions{})
305299
if err != nil {
306300
_ = c.CloseWithError(xerrors.Errorf("create answer: %w", err))
@@ -328,15 +322,18 @@ func (c *Conn) negotiate() {
328322
// flushPendingCandidates writes all local candidates to the candidate send channel.
329323
// The localCandidateChannel is expected to be serviced, otherwise this could block.
330324
func (c *Conn) flushPendingCandidates() {
331-
for _, localCandidate := range c.pendingCandidates {
325+
c.pendingCandidatesMutex.Lock()
326+
defer c.pendingCandidatesMutex.Unlock()
327+
for _, pendingCandidate := range c.pendingCandidates {
332328
c.opts.Logger.Debug(context.Background(), "flushing local candidate")
333329
select {
334330
case <-c.closed:
335331
return
336-
case c.localCandidateChannel <- localCandidate:
332+
case c.localCandidateChannel <- pendingCandidate:
337333
}
338334
}
339335
c.pendingCandidates = make([]webrtc.ICECandidateInit, 0)
336+
c.pendingCandidatesFlushed = true
340337
c.opts.Logger.Debug(context.Background(), "flushed candidates")
341338
}
342339

0 commit comments

Comments
 (0)