Skip to content

Commit ebb94b8

Browse files
committed
Flush remote too
1 parent 18eac83 commit ebb94b8

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

peer/conn.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
7272
dcDisconnectChannel: make(chan struct{}),
7373
dcFailedChannel: make(chan struct{}),
7474
localCandidateChannel: make(chan webrtc.ICECandidateInit),
75-
pendingCandidates: make([]webrtc.ICECandidateInit, 0),
75+
pendingLocalCandidates: make([]webrtc.ICECandidateInit, 0),
76+
pendingRemoteCandidates: make([]webrtc.ICECandidateInit, 0),
7677
localSessionDescriptionChannel: make(chan webrtc.SessionDescription),
7778
remoteSessionDescriptionChannel: make(chan webrtc.SessionDescription),
7879
}
@@ -120,7 +121,8 @@ type Conn struct {
120121
localSessionDescriptionChannel chan webrtc.SessionDescription
121122
remoteSessionDescriptionChannel chan webrtc.SessionDescription
122123

123-
pendingCandidates []webrtc.ICECandidateInit
124+
pendingLocalCandidates []webrtc.ICECandidateInit
125+
pendingRemoteCandidates []webrtc.ICECandidateInit
124126
pendingCandidatesMutex sync.Mutex
125127
pendingCandidatesFlushed bool
126128

@@ -147,7 +149,7 @@ func (c *Conn) init() error {
147149

148150
if !c.pendingCandidatesFlushed {
149151
c.opts.Logger.Debug(context.Background(), "adding local candidate to buffer")
150-
c.pendingCandidates = append(c.pendingCandidates, iceCandidate.ToJSON())
152+
c.pendingLocalCandidates = append(c.pendingLocalCandidates, iceCandidate.ToJSON())
151153
return
152154
}
153155
c.opts.Logger.Debug(context.Background(), "adding local candidate")
@@ -291,7 +293,10 @@ func (c *Conn) negotiate() {
291293
if c.offerrer {
292294
// ICE candidates reset when an offer/answer is set for the first
293295
// time. If candidates flush before this point, a connection could fail.
294-
c.flushPendingCandidates()
296+
err = c.flushPendingCandidates()
297+
if err != nil {
298+
_ = c.CloseWithError(xerrors.Errorf("flush pending candidates: %w", err))
299+
}
295300
}
296301

297302
if !c.offerrer {
@@ -315,26 +320,39 @@ func (c *Conn) negotiate() {
315320
}
316321

317322
// Wait until the local description is set to flush candidates.
318-
c.flushPendingCandidates()
323+
err = c.flushPendingCandidates()
324+
if err != nil {
325+
_ = c.CloseWithError(xerrors.Errorf("flush pending candidates: %w", err))
326+
}
319327
}
320328
}
321329

322330
// flushPendingCandidates writes all local candidates to the candidate send channel.
323331
// The localCandidateChannel is expected to be serviced, otherwise this could block.
324-
func (c *Conn) flushPendingCandidates() {
332+
func (c *Conn) flushPendingCandidates() error {
325333
c.pendingCandidatesMutex.Lock()
326334
defer c.pendingCandidatesMutex.Unlock()
327-
for _, pendingCandidate := range c.pendingCandidates {
335+
for _, pendingCandidate := range c.pendingLocalCandidates {
328336
c.opts.Logger.Debug(context.Background(), "flushing local candidate")
329337
select {
330338
case <-c.closed:
331-
return
339+
return nil
332340
case c.localCandidateChannel <- pendingCandidate:
333341
}
334342
}
335-
c.pendingCandidates = make([]webrtc.ICECandidateInit, 0)
343+
344+
for _, pendingCandidate := range c.pendingRemoteCandidates {
345+
c.opts.Logger.Debug(context.Background(), "flushing remote candidate")
346+
err := c.rtc.AddICECandidate(pendingCandidate)
347+
if err != nil {
348+
return err
349+
}
350+
}
351+
352+
c.pendingLocalCandidates = make([]webrtc.ICECandidateInit, 0)
336353
c.pendingCandidatesFlushed = true
337354
c.opts.Logger.Debug(context.Background(), "flushed candidates")
355+
return nil
338356
}
339357

340358
// LocalCandidate returns a channel that emits when a local candidate
@@ -345,6 +363,13 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
345363

346364
// AddRemoteCandidate adds a remote candidate to the RTC connection.
347365
func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) error {
366+
c.pendingCandidatesMutex.Lock()
367+
defer c.pendingCandidatesMutex.Unlock()
368+
if !c.pendingCandidatesFlushed {
369+
c.opts.Logger.Debug(context.Background(), "adding remote candidate to buffer")
370+
c.pendingRemoteCandidates = append(c.pendingRemoteCandidates, i)
371+
return nil
372+
}
348373
c.opts.Logger.Debug(context.Background(), "adding remote candidate")
349374
return c.rtc.AddICECandidate(i)
350375
}

0 commit comments

Comments
 (0)