@@ -77,7 +77,6 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
77
77
localCandidateChannel : make (chan webrtc.ICECandidateInit ),
78
78
localSessionDescriptionChannel : make (chan webrtc.SessionDescription ),
79
79
remoteSessionDescriptionChannel : make (chan webrtc.SessionDescription ),
80
- pendingCandidatesToSend : make ([]webrtc.ICECandidateInit , 0 ),
81
80
}
82
81
if client {
83
82
// If we're the client, we want to flip the echo and
@@ -128,9 +127,7 @@ type Conn struct {
128
127
remoteSessionDescriptionChannel chan webrtc.SessionDescription
129
128
130
129
negotiateMutex sync.Mutex
131
-
132
- pendingCandidatesToSend []webrtc.ICECandidateInit
133
- pendingCandidatesToSendMutex sync.Mutex
130
+ hasNegotiated bool
134
131
135
132
pingChannelID uint16
136
133
pingEchoChannelID uint16
@@ -145,6 +142,9 @@ type Conn struct {
145
142
}
146
143
147
144
func (c * Conn ) init () error {
145
+ // The negotiation needed callback can take a little bit to execute!
146
+ c .negotiateMutex .Lock ()
147
+
148
148
c .rtc .OnNegotiationNeeded (c .negotiate )
149
149
c .rtc .OnICEConnectionStateChange (func (iceConnectionState webrtc.ICEConnectionState ) {
150
150
c .opts .Logger .Debug (context .Background (), "ice connection state updated" ,
@@ -227,16 +227,6 @@ func (c *Conn) init() error {
227
227
// Run this in a goroutine so we don't block pion/webrtc
228
228
// from continuing.
229
229
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
- }
240
230
c .opts .Logger .Debug (context .Background (), "sending local candidate" )
241
231
select {
242
232
case <- c .closed :
@@ -271,7 +261,10 @@ func (c *Conn) negotiate() {
271
261
c .opts .Logger .Debug (context .Background (), "negotiating" )
272
262
// ICE candidates cannot be added until SessionDescriptions have been
273
263
// exchanged between peers.
274
- c .negotiateMutex .Lock ()
264
+ if c .hasNegotiated {
265
+ c .negotiateMutex .Lock ()
266
+ }
267
+ c .hasNegotiated = true
275
268
defer c .negotiateMutex .Unlock ()
276
269
277
270
if c .offerrer {
@@ -336,24 +329,6 @@ func (c *Conn) negotiate() {
336
329
}
337
330
c .opts .Logger .Debug (context .Background (), "sent answer" )
338
331
}
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
- }()
357
332
}
358
333
359
334
// AddRemoteCandidate adds a remote candidate to the RTC connection.
@@ -366,12 +341,12 @@ func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) {
366
341
go func () {
367
342
c .negotiateMutex .Lock ()
368
343
defer c .negotiateMutex .Unlock ()
369
- if c .isClosed () {
370
- return
371
- }
372
344
c .opts .Logger .Debug (context .Background (), "accepting candidate" , slog .F ("length" , len (i .Candidate )))
373
345
err := c .rtc .AddICECandidate (i )
374
346
if err != nil {
347
+ if c .rtc .ConnectionState () == webrtc .PeerConnectionStateClosed {
348
+ return
349
+ }
375
350
_ = c .CloseWithError (xerrors .Errorf ("accept candidate: %w" , err ))
376
351
}
377
352
}()
0 commit comments