@@ -42,7 +42,7 @@ type WorkerICE struct {
42
42
statusRecorder * Status
43
43
hasRelayOnLocally bool
44
44
45
- agent * ice. Agent
45
+ agent * icemaker. ThreadSafeAgent
46
46
agentDialerCancel context.CancelFunc
47
47
agentConnecting bool // while it is true, drop all incoming offers
48
48
lastSuccess time.Time // with this avoid the too frequent ICE agent recreation
@@ -121,6 +121,7 @@ func (w *WorkerICE) OnNewOffer(remoteOfferAnswer *OfferAnswer) {
121
121
if err := w .agent .Close (); err != nil {
122
122
w .log .Warnf ("failed to close ICE agent: %s" , err )
123
123
}
124
+ w .agent = nil
124
125
// todo consider to switch to Relay connection while establishing a new ICE connection
125
126
}
126
127
@@ -195,7 +196,7 @@ func (w *WorkerICE) Close() {
195
196
w .agent = nil
196
197
}
197
198
198
- func (w * WorkerICE ) reCreateAgent (dialerCancel context.CancelFunc , candidates []ice.CandidateType ) (* ice. Agent , error ) {
199
+ func (w * WorkerICE ) reCreateAgent (dialerCancel context.CancelFunc , candidates []ice.CandidateType ) (* icemaker. ThreadSafeAgent , error ) {
199
200
agent , err := icemaker .NewAgent (w .iFaceDiscover , w .config .ICEConfig , candidates , w .localUfrag , w .localPwd )
200
201
if err != nil {
201
202
return nil , fmt .Errorf ("create agent: %w" , err )
@@ -230,7 +231,7 @@ func (w *WorkerICE) SessionID() ICESessionID {
230
231
// will block until connection succeeded
231
232
// but it won't release if ICE Agent went into Disconnected or Failed state,
232
233
// so we have to cancel it with the provided context once agent detected a broken connection
233
- func (w * WorkerICE ) connect (ctx context.Context , agent * ice. Agent , remoteOfferAnswer * OfferAnswer ) {
234
+ func (w * WorkerICE ) connect (ctx context.Context , agent * icemaker. ThreadSafeAgent , remoteOfferAnswer * OfferAnswer ) {
234
235
w .log .Debugf ("gather candidates" )
235
236
if err := agent .GatherCandidates (); err != nil {
236
237
w .log .Warnf ("failed to gather candidates: %s" , err )
@@ -239,7 +240,7 @@ func (w *WorkerICE) connect(ctx context.Context, agent *ice.Agent, remoteOfferAn
239
240
}
240
241
241
242
w .log .Debugf ("turn agent dial" )
242
- remoteConn , err := w .turnAgentDial (ctx , remoteOfferAnswer )
243
+ remoteConn , err := w .turnAgentDial (ctx , agent , remoteOfferAnswer )
243
244
if err != nil {
244
245
w .log .Debugf ("failed to dial the remote peer: %s" , err )
245
246
w .closeAgent (agent , w .agentDialerCancel )
@@ -290,13 +291,14 @@ func (w *WorkerICE) connect(ctx context.Context, agent *ice.Agent, remoteOfferAn
290
291
w .conn .onICEConnectionIsReady (selectedPriority (pair ), ci )
291
292
}
292
293
293
- func (w * WorkerICE ) closeAgent (agent * ice. Agent , cancel context.CancelFunc ) {
294
+ func (w * WorkerICE ) closeAgent (agent * icemaker. ThreadSafeAgent , cancel context.CancelFunc ) {
294
295
cancel ()
295
296
if err := agent .Close (); err != nil {
296
297
w .log .Warnf ("failed to close ICE agent: %s" , err )
297
298
}
298
299
299
300
w .muxAgent .Lock ()
301
+ // todo review does it make sense to generate new session ID all the time when w.agent==agent
300
302
sessionID , err := NewICESessionID ()
301
303
if err != nil {
302
304
w .log .Errorf ("failed to create new session ID: %s" , err )
@@ -379,7 +381,7 @@ func (w *WorkerICE) onICESelectedCandidatePair(c1 ice.Candidate, c2 ice.Candidat
379
381
w .config .Key )
380
382
}
381
383
382
- func (w * WorkerICE ) onConnectionStateChange (agent * ice. Agent , dialerCancel context.CancelFunc ) func (ice.ConnectionState ) {
384
+ func (w * WorkerICE ) onConnectionStateChange (agent * icemaker. ThreadSafeAgent , dialerCancel context.CancelFunc ) func (ice.ConnectionState ) {
383
385
return func (state ice.ConnectionState ) {
384
386
w .log .Debugf ("ICE ConnectionState has changed to %s" , state .String ())
385
387
switch state {
@@ -412,12 +414,12 @@ func (w *WorkerICE) shouldSendExtraSrflxCandidate(candidate ice.Candidate) bool
412
414
return false
413
415
}
414
416
415
- func (w * WorkerICE ) turnAgentDial (ctx context.Context , remoteOfferAnswer * OfferAnswer ) (* ice.Conn , error ) {
417
+ func (w * WorkerICE ) turnAgentDial (ctx context.Context , agent * icemaker. ThreadSafeAgent , remoteOfferAnswer * OfferAnswer ) (* ice.Conn , error ) {
416
418
isControlling := w .config .LocalKey > w .config .Key
417
419
if isControlling {
418
- return w . agent .Dial (ctx , remoteOfferAnswer .IceCredentials .UFrag , remoteOfferAnswer .IceCredentials .Pwd )
420
+ return agent .Dial (ctx , remoteOfferAnswer .IceCredentials .UFrag , remoteOfferAnswer .IceCredentials .Pwd )
419
421
} else {
420
- return w . agent .Accept (ctx , remoteOfferAnswer .IceCredentials .UFrag , remoteOfferAnswer .IceCredentials .Pwd )
422
+ return agent .Accept (ctx , remoteOfferAnswer .IceCredentials .UFrag , remoteOfferAnswer .IceCredentials .Pwd )
421
423
}
422
424
}
423
425
0 commit comments