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,14 @@ type Conn struct {
140
141
141
142
func (c * Conn ) init () error {
142
143
c .rtc .OnNegotiationNeeded (c .negotiate )
144
+ c .rtc .OnICEConnectionStateChange (func (is webrtc.ICEConnectionState ) {
145
+ c .opts .Logger .Debug (context .Background (), "ice connection updated" ,
146
+ slog .F ("state" , is ))
147
+ })
148
+ c .rtc .OnICEGatheringStateChange (func (is webrtc.ICEGathererState ) {
149
+ c .opts .Logger .Debug (context .Background (), "ice gathering state updated" ,
150
+ slog .F ("state" , is ))
151
+ })
143
152
c .rtc .OnICECandidate (func (iceCandidate * webrtc.ICECandidate ) {
144
153
if iceCandidate == nil {
145
154
return
@@ -169,8 +178,7 @@ func (c *Conn) init() error {
169
178
}
170
179
171
180
c .opts .Logger .Debug (context .Background (), "rtc connection updated" ,
172
- slog .F ("state" , pcs ),
173
- slog .F ("ice" , c .rtc .ICEConnectionState ()))
181
+ slog .F ("state" , pcs ))
174
182
175
183
switch pcs {
176
184
case webrtc .PeerConnectionStateDisconnected :
@@ -311,15 +319,22 @@ func (c *Conn) negotiate() {
311
319
c .pendingCandidatesMutex .Lock ()
312
320
defer c .pendingCandidatesMutex .Unlock ()
313
321
for _ , pendingCandidate := range c .pendingRemoteCandidates {
314
- c .opts .Logger .Debug (context .Background (), "flushing remote candidate" )
322
+ hash := sha256 .Sum224 ([]byte (pendingCandidate .Candidate ))
323
+ c .opts .Logger .Debug (context .Background (), "flushing buffered remote candidate" ,
324
+ slog .F ("hash" , hash ),
325
+ slog .F ("length" , len (pendingCandidate .Candidate )),
326
+ )
315
327
err := c .rtc .AddICECandidate (pendingCandidate )
316
328
if err != nil {
317
- _ = c .CloseWithError (xerrors .Errorf ("flush pending candidates : %w" , err ))
329
+ _ = c .CloseWithError (xerrors .Errorf ("flush pending remote candidate : %w" , err ))
318
330
return
319
331
}
320
332
}
333
+ c .opts .Logger .Debug (context .Background (), "flushed buffered remote candidates" ,
334
+ slog .F ("count" , len (c .pendingRemoteCandidates )),
335
+ )
321
336
c .pendingCandidatesFlushed = true
322
- c .opts . Logger . Debug ( context . Background (), "flushed remote candidates" )
337
+ c .pendingRemoteCandidates = make ([]webrtc. ICECandidateInit , 0 )
323
338
}
324
339
325
340
// LocalCandidate returns a channel that emits when a local candidate
@@ -332,12 +347,16 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
332
347
func (c * Conn ) AddRemoteCandidate (i webrtc.ICECandidateInit ) error {
333
348
c .pendingCandidatesMutex .Lock ()
334
349
defer c .pendingCandidatesMutex .Unlock ()
350
+ fields := []slog.Field {
351
+ slog .F ("hash" , sha256 .Sum224 ([]byte (i .Candidate ))),
352
+ slog .F ("length" , len (i .Candidate )),
353
+ }
335
354
if ! c .pendingCandidatesFlushed {
336
- c .opts .Logger .Debug (context .Background (), "adding remote candidate to buffer" )
355
+ c .opts .Logger .Debug (context .Background (), "bufferring remote candidate" , fields ... )
337
356
c .pendingRemoteCandidates = append (c .pendingRemoteCandidates , i )
338
357
return nil
339
358
}
340
- c .opts .Logger .Debug (context .Background (), "adding remote candidate" )
359
+ c .opts .Logger .Debug (context .Background (), "adding remote candidate" , fields ... )
341
360
return c .rtc .AddICECandidate (i )
342
361
}
343
362
0 commit comments