@@ -74,9 +74,10 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
74
74
dcOpenChannel : make (chan * webrtc.DataChannel ),
75
75
dcDisconnectChannel : make (chan struct {}),
76
76
dcFailedChannel : make (chan struct {}),
77
+ iceStableChannel : make (chan struct {}),
77
78
localCandidateChannel : make (chan webrtc.ICECandidateInit ),
78
- localSessionDescriptionChannel : make (chan webrtc.SessionDescription ),
79
- remoteSessionDescriptionChannel : make (chan webrtc.SessionDescription ),
79
+ localSessionDescriptionChannel : make (chan webrtc.SessionDescription , 1 ),
80
+ remoteSessionDescriptionChannel : make (chan webrtc.SessionDescription , 1 ),
80
81
}
81
82
if client {
82
83
// If we're the client, we want to flip the echo and
@@ -122,6 +123,7 @@ type Conn struct {
122
123
dcFailedListeners atomic.Uint32
123
124
dcClosedWaitGroup sync.WaitGroup
124
125
126
+ iceStableChannel chan struct {}
125
127
localCandidateChannel chan webrtc.ICECandidateInit
126
128
localSessionDescriptionChannel chan webrtc.SessionDescription
127
129
remoteSessionDescriptionChannel chan webrtc.SessionDescription
@@ -219,6 +221,13 @@ func (c *Conn) init() error {
219
221
c .rtc .OnSignalingStateChange (func (signalState webrtc.SignalingState ) {
220
222
c .opts .Logger .Debug (context .Background (), "signaling state updated" ,
221
223
slog .F ("state" , signalState ))
224
+
225
+ if signalState == webrtc .SignalingStateStable {
226
+ select {
227
+ case c .iceStableChannel <- struct {}{}:
228
+ default :
229
+ }
230
+ }
222
231
})
223
232
c .rtc .OnICECandidate (func (iceCandidate * webrtc.ICECandidate ) {
224
233
if iceCandidate == nil {
@@ -329,6 +338,15 @@ func (c *Conn) negotiate() {
329
338
}
330
339
c .opts .Logger .Debug (context .Background (), "sent answer" )
331
340
}
341
+
342
+ if c .rtc .SignalingState () != webrtc .SignalingStateStable {
343
+ c .opts .Logger .Debug (context .Background (), "waiting for stable signaling state..." )
344
+ select {
345
+ case <- c .closed :
346
+ return
347
+ case <- c .iceStableChannel :
348
+ }
349
+ }
332
350
}
333
351
334
352
// AddRemoteCandidate adds a remote candidate to the RTC connection.
0 commit comments