4
4
"bytes"
5
5
"context"
6
6
"crypto/rand"
7
+ "crypto/sha256"
7
8
"io"
8
9
"sync"
9
10
"time"
@@ -140,15 +141,43 @@ 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
+ // Close must be locked here otherwise log output can appear
146
+ // after the connection has been closed.
147
+ c .closeMutex .Lock ()
148
+ defer c .closeMutex .Unlock ()
149
+ if c .isClosed () {
150
+ return
151
+ }
152
+
153
+ c .opts .Logger .Debug (context .Background (), "ice connection state updated" ,
154
+ slog .F ("state" , iceConnectionState ))
155
+ })
156
+ c .rtc .OnICEGatheringStateChange (func (iceGatherState webrtc.ICEGathererState ) {
157
+ // Close can't be locked here, because this is triggered
158
+ // when close is called. It doesn't appear this get's
159
+ // executed after close though, so it shouldn't cause
160
+ // problems.
161
+ if c .isClosed () {
162
+ return
163
+ }
164
+
165
+ c .opts .Logger .Debug (context .Background (), "ice gathering state updated" ,
166
+ slog .F ("state" , iceGatherState ))
167
+ })
143
168
c .rtc .OnICECandidate (func (iceCandidate * webrtc.ICECandidate ) {
144
169
if iceCandidate == nil {
145
170
return
146
171
}
147
- c .opts .Logger .Debug (context .Background (), "adding local candidate" )
172
+ json := iceCandidate .ToJSON ()
173
+ c .opts .Logger .Debug (context .Background (), "writing candidate to channel" ,
174
+ slog .F ("hash" , sha256 .Sum224 ([]byte (json .Candidate ))),
175
+ slog .F ("length" , len (json .Candidate )),
176
+ )
148
177
select {
149
178
case <- c .closed :
150
179
break
151
- case c .localCandidateChannel <- iceCandidate . ToJSON () :
180
+ case c .localCandidateChannel <- json :
152
181
}
153
182
})
154
183
c .rtc .OnDataChannel (func (dc * webrtc.DataChannel ) {
@@ -169,8 +198,7 @@ func (c *Conn) init() error {
169
198
}
170
199
171
200
c .opts .Logger .Debug (context .Background (), "rtc connection updated" ,
172
- slog .F ("state" , pcs ),
173
- slog .F ("ice" , c .rtc .ICEConnectionState ()))
201
+ slog .F ("state" , pcs ))
174
202
175
203
switch pcs {
176
204
case webrtc .PeerConnectionStateDisconnected :
@@ -311,15 +339,22 @@ func (c *Conn) negotiate() {
311
339
c .pendingCandidatesMutex .Lock ()
312
340
defer c .pendingCandidatesMutex .Unlock ()
313
341
for _ , pendingCandidate := range c .pendingRemoteCandidates {
314
- c .opts .Logger .Debug (context .Background (), "flushing remote candidate" )
342
+ hash := sha256 .Sum224 ([]byte (pendingCandidate .Candidate ))
343
+ c .opts .Logger .Debug (context .Background (), "flushing buffered remote candidate" ,
344
+ slog .F ("hash" , hash ),
345
+ slog .F ("length" , len (pendingCandidate .Candidate )),
346
+ )
315
347
err := c .rtc .AddICECandidate (pendingCandidate )
316
348
if err != nil {
317
- _ = c .CloseWithError (xerrors .Errorf ("flush pending candidates : %w" , err ))
349
+ _ = c .CloseWithError (xerrors .Errorf ("flush pending remote candidate : %w" , err ))
318
350
return
319
351
}
320
352
}
353
+ c .opts .Logger .Debug (context .Background (), "flushed buffered remote candidates" ,
354
+ slog .F ("count" , len (c .pendingRemoteCandidates )),
355
+ )
321
356
c .pendingCandidatesFlushed = true
322
- c .opts . Logger . Debug ( context . Background (), "flushed remote candidates" )
357
+ c .pendingRemoteCandidates = make ([]webrtc. ICECandidateInit , 0 )
323
358
}
324
359
325
360
// LocalCandidate returns a channel that emits when a local candidate
@@ -332,12 +367,16 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
332
367
func (c * Conn ) AddRemoteCandidate (i webrtc.ICECandidateInit ) error {
333
368
c .pendingCandidatesMutex .Lock ()
334
369
defer c .pendingCandidatesMutex .Unlock ()
370
+ fields := []slog.Field {
371
+ slog .F ("hash" , sha256 .Sum224 ([]byte (i .Candidate ))),
372
+ slog .F ("length" , len (i .Candidate )),
373
+ }
335
374
if ! c .pendingCandidatesFlushed {
336
- c .opts .Logger .Debug (context .Background (), "adding remote candidate to buffer" )
375
+ c .opts .Logger .Debug (context .Background (), "bufferring remote candidate" , fields ... )
337
376
c .pendingRemoteCandidates = append (c .pendingRemoteCandidates , i )
338
377
return nil
339
378
}
340
- c .opts .Logger .Debug (context .Background (), "adding remote candidate" )
379
+ c .opts .Logger .Debug (context .Background (), "adding remote candidate" , fields ... )
341
380
return c .rtc .AddICECandidate (i )
342
381
}
343
382
0 commit comments