4
4
"bytes"
5
5
"context"
6
6
"crypto/rand"
7
+ "crypto/sha256"
7
8
"io"
8
9
"sync"
9
10
"time"
@@ -140,6 +141,26 @@ type Conn struct {
140
141
141
142
func (c * Conn ) init () error {
142
143
c .rtc .OnNegotiationNeeded (c .negotiate )
144
+ c .rtc .OnICEConnectionStateChange (func (iceConnectionState webrtc.ICEConnectionState ) {
145
+ c .closeMutex .Lock ()
146
+ defer c .closeMutex .Unlock ()
147
+ if c .isClosed () {
148
+ return
149
+ }
150
+
151
+ c .opts .Logger .Debug (context .Background (), "ice connection updated" ,
152
+ slog .F ("state" , iceConnectionState ))
153
+ })
154
+ c .rtc .OnICEGatheringStateChange (func (iceGatherState webrtc.ICEGathererState ) {
155
+ c .closeMutex .Lock ()
156
+ defer c .closeMutex .Unlock ()
157
+ if c .isClosed () {
158
+ return
159
+ }
160
+
161
+ c .opts .Logger .Debug (context .Background (), "ice gathering state updated" ,
162
+ slog .F ("state" , iceGatherState ))
163
+ })
143
164
c .rtc .OnICECandidate (func (iceCandidate * webrtc.ICECandidate ) {
144
165
if iceCandidate == nil {
145
166
return
@@ -169,8 +190,7 @@ func (c *Conn) init() error {
169
190
}
170
191
171
192
c .opts .Logger .Debug (context .Background (), "rtc connection updated" ,
172
- slog .F ("state" , pcs ),
173
- slog .F ("ice" , c .rtc .ICEConnectionState ()))
193
+ slog .F ("state" , pcs ))
174
194
175
195
switch pcs {
176
196
case webrtc .PeerConnectionStateDisconnected :
@@ -311,15 +331,22 @@ func (c *Conn) negotiate() {
311
331
c .pendingCandidatesMutex .Lock ()
312
332
defer c .pendingCandidatesMutex .Unlock ()
313
333
for _ , pendingCandidate := range c .pendingRemoteCandidates {
314
- c .opts .Logger .Debug (context .Background (), "flushing remote candidate" )
334
+ hash := sha256 .Sum224 ([]byte (pendingCandidate .Candidate ))
335
+ c .opts .Logger .Debug (context .Background (), "flushing buffered remote candidate" ,
336
+ slog .F ("hash" , hash ),
337
+ slog .F ("length" , len (pendingCandidate .Candidate )),
338
+ )
315
339
err := c .rtc .AddICECandidate (pendingCandidate )
316
340
if err != nil {
317
- _ = c .CloseWithError (xerrors .Errorf ("flush pending candidates : %w" , err ))
341
+ _ = c .CloseWithError (xerrors .Errorf ("flush pending remote candidate : %w" , err ))
318
342
return
319
343
}
320
344
}
345
+ c .opts .Logger .Debug (context .Background (), "flushed buffered remote candidates" ,
346
+ slog .F ("count" , len (c .pendingRemoteCandidates )),
347
+ )
321
348
c .pendingCandidatesFlushed = true
322
- c .opts . Logger . Debug ( context . Background (), "flushed remote candidates" )
349
+ c .pendingRemoteCandidates = make ([]webrtc. ICECandidateInit , 0 )
323
350
}
324
351
325
352
// LocalCandidate returns a channel that emits when a local candidate
@@ -332,12 +359,16 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
332
359
func (c * Conn ) AddRemoteCandidate (i webrtc.ICECandidateInit ) error {
333
360
c .pendingCandidatesMutex .Lock ()
334
361
defer c .pendingCandidatesMutex .Unlock ()
362
+ fields := []slog.Field {
363
+ slog .F ("hash" , sha256 .Sum224 ([]byte (i .Candidate ))),
364
+ slog .F ("length" , len (i .Candidate )),
365
+ }
335
366
if ! c .pendingCandidatesFlushed {
336
- c .opts .Logger .Debug (context .Background (), "adding remote candidate to buffer" )
367
+ c .opts .Logger .Debug (context .Background (), "bufferring remote candidate" , fields ... )
337
368
c .pendingRemoteCandidates = append (c .pendingRemoteCandidates , i )
338
369
return nil
339
370
}
340
- c .opts .Logger .Debug (context .Background (), "adding remote candidate" )
371
+ c .opts .Logger .Debug (context .Background (), "adding remote candidate" , fields ... )
341
372
return c .rtc .AddICECandidate (i )
342
373
}
343
374
0 commit comments