Skip to content

Commit e87d2cb

Browse files
committed
Buffer local and remote messages
1 parent f58a980 commit e87d2cb

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

peer/conn.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ func newWithClientOrServer(servers []webrtc.ICEServer, client bool, opts *ConnOp
7474
dcOpenChannel: make(chan *webrtc.DataChannel),
7575
dcDisconnectChannel: make(chan struct{}),
7676
dcFailedChannel: make(chan struct{}),
77+
iceStableChannel: make(chan struct{}),
7778
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),
8081
}
8182
if client {
8283
// If we're the client, we want to flip the echo and
@@ -122,6 +123,7 @@ type Conn struct {
122123
dcFailedListeners atomic.Uint32
123124
dcClosedWaitGroup sync.WaitGroup
124125

126+
iceStableChannel chan struct{}
125127
localCandidateChannel chan webrtc.ICECandidateInit
126128
localSessionDescriptionChannel chan webrtc.SessionDescription
127129
remoteSessionDescriptionChannel chan webrtc.SessionDescription
@@ -219,6 +221,13 @@ func (c *Conn) init() error {
219221
c.rtc.OnSignalingStateChange(func(signalState webrtc.SignalingState) {
220222
c.opts.Logger.Debug(context.Background(), "signaling state updated",
221223
slog.F("state", signalState))
224+
225+
if signalState == webrtc.SignalingStateStable {
226+
select {
227+
case c.iceStableChannel <- struct{}{}:
228+
default:
229+
}
230+
}
222231
})
223232
c.rtc.OnICECandidate(func(iceCandidate *webrtc.ICECandidate) {
224233
if iceCandidate == nil {
@@ -329,6 +338,15 @@ func (c *Conn) negotiate() {
329338
}
330339
c.opts.Logger.Debug(context.Background(), "sent answer")
331340
}
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+
}
332350
}
333351

334352
// AddRemoteCandidate adds a remote candidate to the RTC connection.

0 commit comments

Comments
 (0)