Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b69f269

Browse files
committed
Try removing buffered candidates
1 parent 1facda9 commit b69f269

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

peer/conn.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
7777
localCandidateChannel: make(chan webrtc.ICECandidateInit),
7878
localSessionDescriptionChannel: make(chan webrtc.SessionDescription),
7979
remoteSessionDescriptionChannel: make(chan webrtc.SessionDescription),
80-
pendingCandidatesToSend: make([]webrtc.ICECandidateInit, 0),
8180
}
8281
if client {
8382
// If we're the client, we want to flip the echo and
@@ -128,9 +127,7 @@ type Conn struct {
128127
remoteSessionDescriptionChannel chan webrtc.SessionDescription
129128

130129
negotiateMutex sync.Mutex
131-
132-
pendingCandidatesToSend []webrtc.ICECandidateInit
133-
pendingCandidatesToSendMutex sync.Mutex
130+
hasNegotiated bool
134131

135132
pingChannelID uint16
136133
pingEchoChannelID uint16
@@ -145,6 +142,9 @@ type Conn struct {
145142
}
146143

147144
func (c *Conn) init() error {
145+
// The negotiation needed callback can take a little bit to execute!
146+
c.negotiateMutex.Lock()
147+
148148
c.rtc.OnNegotiationNeeded(c.negotiate)
149149
c.rtc.OnICEConnectionStateChange(func(iceConnectionState webrtc.ICEConnectionState) {
150150
c.opts.Logger.Debug(context.Background(), "ice connection state updated",
@@ -227,16 +227,6 @@ func (c *Conn) init() error {
227227
// Run this in a goroutine so we don't block pion/webrtc
228228
// from continuing.
229229
go func() {
230-
c.pendingCandidatesToSendMutex.Lock()
231-
defer c.pendingCandidatesToSendMutex.Unlock()
232-
// If the remote description hasn't been set yet, we queue the send of these candidates.
233-
// It may work to send these immediately, but at the time of writing this package is
234-
// unstable, so better being safe than sorry.
235-
if c.rtc.RemoteDescription() == nil {
236-
c.pendingCandidatesToSend = append(c.pendingCandidatesToSend, iceCandidate.ToJSON())
237-
c.opts.Logger.Debug(context.Background(), "buffering local candidate")
238-
return
239-
}
240230
c.opts.Logger.Debug(context.Background(), "sending local candidate")
241231
select {
242232
case <-c.closed:
@@ -271,7 +261,10 @@ func (c *Conn) negotiate() {
271261
c.opts.Logger.Debug(context.Background(), "negotiating")
272262
// ICE candidates cannot be added until SessionDescriptions have been
273263
// exchanged between peers.
274-
c.negotiateMutex.Lock()
264+
if c.hasNegotiated {
265+
c.negotiateMutex.Lock()
266+
}
267+
c.hasNegotiated = true
275268
defer c.negotiateMutex.Unlock()
276269

277270
if c.offerrer {
@@ -336,24 +329,6 @@ func (c *Conn) negotiate() {
336329
}
337330
c.opts.Logger.Debug(context.Background(), "sent answer")
338331
}
339-
340-
// Flush bufferred candidates after both sides have been negotiated!
341-
go func() {
342-
c.pendingCandidatesToSendMutex.Lock()
343-
defer c.pendingCandidatesToSendMutex.Unlock()
344-
for _, pendingCandidate := range c.pendingCandidatesToSend {
345-
select {
346-
case <-c.closed:
347-
return
348-
case c.localCandidateChannel <- pendingCandidate:
349-
}
350-
c.opts.Logger.Debug(context.Background(), "flushed buffered local candidate")
351-
}
352-
c.opts.Logger.Debug(context.Background(), "flushed buffered local candidates",
353-
slog.F("count", len(c.pendingCandidatesToSend)),
354-
)
355-
c.pendingCandidatesToSend = make([]webrtc.ICECandidateInit, 0)
356-
}()
357332
}
358333

359334
// AddRemoteCandidate adds a remote candidate to the RTC connection.
@@ -366,12 +341,12 @@ func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) {
366341
go func() {
367342
c.negotiateMutex.Lock()
368343
defer c.negotiateMutex.Unlock()
369-
if c.isClosed() {
370-
return
371-
}
372344
c.opts.Logger.Debug(context.Background(), "accepting candidate", slog.F("length", len(i.Candidate)))
373345
err := c.rtc.AddICECandidate(i)
374346
if err != nil {
347+
if c.rtc.ConnectionState() == webrtc.PeerConnectionStateClosed {
348+
return
349+
}
375350
_ = c.CloseWithError(xerrors.Errorf("accept candidate: %w", err))
376351
}
377352
}()

0 commit comments

Comments
 (0)