@@ -73,6 +73,7 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
73
73
dcFailedChannel : make (chan struct {}),
74
74
localCandidateChannel : make (chan webrtc.ICECandidateInit ),
75
75
localSessionDescriptionChannel : make (chan webrtc.SessionDescription , 1 ),
76
+ negotiated : make (chan struct {}),
76
77
remoteSessionDescriptionChannel : make (chan webrtc.SessionDescription , 1 ),
77
78
settingEngine : opts .SettingEngine ,
78
79
}
@@ -124,8 +125,7 @@ type Conn struct {
124
125
localSessionDescriptionChannel chan webrtc.SessionDescription
125
126
remoteSessionDescriptionChannel chan webrtc.SessionDescription
126
127
127
- negotiateMutex sync.Mutex
128
- hasNegotiated bool
128
+ negotiated chan struct {}
129
129
130
130
loggerValue atomic.Value
131
131
settingEngine webrtc.SettingEngine
@@ -152,9 +152,6 @@ func (c *Conn) logger() slog.Logger {
152
152
}
153
153
154
154
func (c * Conn ) init () error {
155
- // The negotiation needed callback can take a little bit to execute!
156
- c .negotiateMutex .Lock ()
157
-
158
155
c .rtc .OnNegotiationNeeded (c .negotiate )
159
156
c .rtc .OnICEConnectionStateChange (func (iceConnectionState webrtc.ICEConnectionState ) {
160
157
c .closedICEMutex .Lock ()
@@ -290,11 +287,13 @@ func (c *Conn) negotiate() {
290
287
c .logger ().Debug (context .Background (), "negotiating" )
291
288
// ICE candidates cannot be added until SessionDescriptions have been
292
289
// exchanged between peers.
293
- if c .hasNegotiated {
294
- c .negotiateMutex .Lock ()
295
- }
296
- c .hasNegotiated = true
297
- defer c .negotiateMutex .Unlock ()
290
+ defer func () {
291
+ select {
292
+ case <- c .negotiated :
293
+ default :
294
+ close (c .negotiated )
295
+ }
296
+ }()
298
297
299
298
if c .offerer {
300
299
offer , err := c .rtc .CreateOffer (& webrtc.OfferOptions {})
@@ -368,8 +367,10 @@ func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) {
368
367
// This must occur in a goroutine to allow the SessionDescriptions
369
368
// to be exchanged first.
370
369
go func () {
371
- c .negotiateMutex .Lock ()
372
- defer c .negotiateMutex .Unlock ()
370
+ select {
371
+ case <- c .closed :
372
+ case <- c .negotiated :
373
+ }
373
374
if c .isClosed () {
374
375
return
375
376
}
0 commit comments